I am reading the Clean Code: A Handbook of Agile Software Craftsmanship book by Robert C. Martin. I will write short summaries of the book chapters as I go through them to serve as notes for me and anyone else.
Chapter 1: Clean Code
The book is about good programming, about how to turn bad code into good code. Code can be bad for several reasons:
- You’re coding in a rush
- Your boss is pushing you to finish soon
- You don’t have time to do a good job
- You want to get a quick fix out and tell yourself you’ll fix it later. Later equals never.
Clean code should be elegant, efficient, readable, simple, well-written, and without duplications.
Chapter 2: Meaningful names
- Use intention-revealing names. Good variable, function, and class names should reveal what it does, why it exists, and how it is used. If a variable name requires a comment, it does not reveal its intent. Instead of naming a variable like this
int d; // Elapsed time in days
It is better to useint elapsedTimeInDays;
- Avoid words whose meanings vary from the intended meaning, e.g. calling a grouping of Accounts
AccountList
when it does not contain aList
because the wordList
has special meaning to programmers. - Use pronounceable names
- Names should be meaningful and searchable.
- Single-letter names should only be used as local variables inside short methods. The length of the name should correspond to the size of its scope.
- Shorter names are generally better than longer ones, so long as they are clear.
- Don’t pun or use inside jokes when naming things.
- Use names that reflect the system domain, the context, and the problems that must be solved.
- Avoid acronyms and confusing names which may bring anyone reading your code to confusing or wrong conclusions
Class Names
- Classes and objects should have noun phrase names like
Customer
,WikiPage
,Account
,AddressParser
. - Avoid names like
Manager
,Processor
,Data
,Info
in the class name. - A class name should not be a verb.
Method names
- Method names should use verbs or verb phrase names, such as
save
,postPayment
,deletePage
- Accessors should be named for their value and prefixed with
get
orset
- pick one word per concept and stick with it, e.g. retrieve, get, and fetch all mean the same thing. Choose one, and stick with it.