Clean Code Book – Chapter 2: Meaningful Names

Last modified date

Comment: 1

This chapter focusses on the best practices for naming things when writing code.

Use intention revealing names

Rather than giving ambiguous names to variables such as int x = 4 and having arbitrary numbers in the code such as if (x == 4), these names should all be changed to more explicit.

By having ambiguous names, it gives the codes implicitly, meaning to understand the code you will need some underlying knowledge such as what is x? and what is the significance of the number 4?

Source: pages 18-19 in the book shows a good example of this.

Avoid Disinformation

Using a variable name of hp is a poor variable name because it is a name of a Unix platform, even if you think that it is a good name for the hypotenuse. It is disinformative as it can have another meaning.

Another example would be to call something a carList when it is not actually a List.

Also avoid using variable names that vary in small ways, long names that have a subtle difference can be easily mistaken for one another.

Source: pages 19-20 in the book.

Make Meaningful Distinctions

How will anyone reading your code be able to tell the difference between the objects Product, ProductInfo and ProductData? There is no meaningful distinction between those objects so a programmer reading and using your code would have no idea which one to use.

Source: pages 20-21 in the book.

Use Pronounceable Names

Don’t use acronyms or shorthand forms for variable names. They are hard to pronounce and hard to understand. And you look silly saying them out-loud.

Use Searchable Names

Searching for single digit/character variables is troublesome as it will returns results in file names and anywhere else that digit or character is used.

Longer names are always easier to search for, they make the code longer, but that is a compromise.

Interfaces and Implementations

If you have a ShapeFactory for the creation of shapes, a concrete class implementing the Interface could be called IShapeFactory. This is undesirable and a better way of naming a concrete class would be to call is ShapeFactoryImp, encoding the implementation not the interface.

Class Names

Class and object name should be nouns or noun phrases.

Class and object names should not be verbs.

Method Names

Method names should be verbs or verb phrases.

The clean code book can be found here on Amazon.

Read the summary of the Clean Code Chapter 3 here.


1 Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.