CLEAN CODEA Summary
What is Clean Code?
Easy for others to change
Easy for others to read
Focused on a particular ‘thing’
Completely covered by tests
Why is Clean Code Important?
Easy to Read, Easy to Change
The ratio of time spent reading vs. writing is well over 10:1.
“You know you are working on clean code when each routine you read turns out to be pretty much what you expected.” – Ward Cunningham
Causes of Poor Code Quality
Inexperience
Lack of time
Laziness
Broken Window Theory
Boy Scout Rule
Leave the campground cleaner than you found it.
Naming Guidelines Names should reveal intent
You may change them repeatedly
Avoid misleading names
If you can’t pronounce it, don’t use it
Longer names > Shorter names
Avoid prefixes & type encoding
Nouns vs Verbs
• Nouns• Representing
things in the real world
Classes/
Objects
• Verbs• An action being
taken by the object
Methods
Private Constructors & Static Methods
Consider using static factory methods to call parameterized constructors
Student.CreateStudentFirstNameLastName(“Jason”, “Rosado”);
vs
new Student(“Jason”, “Rosado”);
Rules of Thumb Keep methods as short as possible (< 20
lines) If statements & loops should only
contain method calls
Single Responsibility Principle
Be one thing, do one thing
Comments Comments describe things that our code
cannot
int temp = 0; // temperature in celsius
int temperatureInCelsius= 0;
Use more descriptive names instead of comments
Bad Comments - Continued
Useless or Redundant Comments
Incorrect
Comments
Good Comments
Warnings of bad things if code is deleted
Future things that need to be added