Clean Code
Clean Code
Clean Code
RAJESH KUMAR
1
WHAT IS CLEAN CODE
2
MEANINGFUL NAMES
int i;
What is i?
int elapsedTime;
Now we know it is an integer indicating elapsed time.
But what time? Seconds, years what?
int elapsedTimeInDays
Better?
int elapsedTimeInDaysSinceRestart
3
MEANINGFUL NAMES
controllerForEfficientHandlingOfStrings
controllerForEfficientStorageOfStrings
Frightfully similar
int a = l;
if ( O == 0 ){
a = O;
}
Not very clear?
4
MEANINGFUL NAMES
5
NO MAGIC NUMBERS
6
NO MAGIC NUMBERS CONTD
7
METHODS
9
FUNCTIONS CONTD
10
WORDS OF WISDOM
Every function, and every block within a function, should have one entry
and one exit.
Means that there should only be one return statement in a function,
no break or continue statements in a loop,
and never, ever, any goto statements.
Dijkstra
11
COMMENTS
12
COMMENTS
13
FORMATTING
14
ABSTRACTION
15
ERROR HANDLING
16
TESTS
F.I.R.S.T. principle
Tests must be uniquely named
Given-When-Then
One assert per test
Making a private function protected to test
Generally, not required if the functions are small
17
CLASSES
18
SYSTEM
19
SYSTEM
20
QUESTIONS?
21