Friday, July 16, 2021

Meaningful Naming in programming

Our goal as authors is to make our code as easy as possible to understand. We want our code to be a quick skim, not an intense study. The 'Clean Code' book by uncle Bob lays out the following steps to write meaningful names when coding:

  1. Use Intention-revealing names
  2. Avoid disinformation
  3. Make meaningful distinctions
  4. Use pronounceable names
  5. Use searchable names
  6. Avoid encodings
  7. Avoid mental mapping
  8. Class names
  9. Method names
  10. Don't be cute
  11. Pick one word per concept
  12. Don't pun
  13. Use solution domain names
  14. Use problem domain names
  15. Add meaningful context
  16. Don't add gratuitous context

1. Use Intention-Revealing Names

Choosing names that reveal intent can make it much easier to understand and change code.

The problem with the above code isn't its simplicity but its implicity.

2. Avoid Disinformation

Programmers must avoid leaving false clues that obscure the meaning of the code. For example, do not refer to a group accounts as accountList unless it's actually a list. Also, Beware using names which vary in small ways.

3. Make Meaningful Distinctions

Do not write code solely to satisfy the compiler or interpreter, this creates problems. It is not sufficient to add noise words although the compiler is satisfied. If names must be different, then they should also mean something different.

4. Use Pronounceable Names

If you can't pronounce names you can't discuss it without sounding silly.

5. Use Searchable Names

Avoid using single letters for names. For example, using the letter e which is the most used letter in the English language won't let the IDE offer any help to you when you are looking for it. Also, one might easily grep for a constant rather than searching for a number.

6. Avoid Encodings

Programmers have enough encodings to deal with without adding more to their burden.

7. Avoid Mental Mapping

Use problem domain terms or solution domain terms. Readers shouldn't have to translate your names into other names they already know.

8. Class Names

Class names and objects should be nouns.

9. Method Names

Method names should have verbs.

10. Don't be Cute

Say what you mean, mean what you say. Don't say culture-dependent jokes.

11. Pick One Word per Concept

Pick one word for an abstract concept and stick with it. For instance it's confusing to have fetch, retrieve, and get as equivalent methods of different classes.

12. Don't Pun

Avoid using the same word for two purposes.

13. Use Solution Domain Names

The people who are going to read your code are programmers. So go ahead and use computer science terms.

14. Use Problem Domain Names

When there is no a computer science term use the name from the problem domain. At least the programmer who maintains your code can ask a domain expert what it means.

15. Add Meaningful Context

Enclose your names in well named classes, functions, or namespaces. When all fails then prefixing the name may be necessary as a last resort.

16. Don't Add Gratuitous Context

Add no more context to name than necessary. Shorter names are better than longer ones so long as they are clear.

 

 

 

No comments:

Post a Comment