2017-10-06 03:13:00 UTC
I am currently reading Clean code by Uncle Bob Martin. Every time I read a new book by a computer science veteran, I feel I know so little. Regarding naming entities in our code, he says
Our goal as author (programmer of a code block) is to make our code as easy as possible to understand. It should be like paperback model not an academic model
1. Shorter names are generally better than longer ones, so long as they are clear
2. Use Intention revealing names
2.1. the name of a variable, function or class should answer all the big questions: why it exists, what it does and how it is used.
2.2. If a name requires a comment, it does not reveal its intent
3. Avoid disinformation
4. Make meaningful distinctions
5. Use pronounceable names
6. Use searchable names
6.1. Single letter should only be used for local variables inside short methods or for loop variables for small scope
6.2.The length of a name should correspond to the size of its scope
6.3 if a variable or a constant might be seen or used in multiple places in a body of code, it is imperative to give it search friendly name.
7. Avoid encoding except for interfaces
8. Avoid member prefixes
9. Avoid mental mapping
10. For class names: Use noun or noun phrase, avoid verbs and words like Manager, Processor, Data or Info
11. For methods names: Should have verb or verb phrase names: save, deletePage etc
12. Accessors, mutators and predicates should be named for their value and prefixed with get, set and is
13. For constructors overloading, use static factory methods: eg: Instead of Complex fulcrumPoint = new Complex(23.0), use Complex fulcrumPoint = Complex.FromRealnumber(23.0)
14. Don't use cute names: choose clarity over entertainment value, say what you mean, mean what you say
15. Pick one word for one abstract concept and stick with it: Use same word for similar methods across different classes in a codebase. e.g: Don't use fetch, retrieve and get as equivalent methods of different classes, don't use controller, manager and a driver in the same code base.
16. Avoid Puns: Avoid using the same word for two purposes: If you are using add indicating a method for adding or concatenting in your code base, don't use add for adding to a list. Use append or insert as a name
17. Use solution domain names: for eg. use AccountVisitor for resembing Vistor pattern (use computer science names for entities that behave like that)
18. Use Problem domain names: use names from problem domain if programmer-eese name aren't suitable
19. Add Meaningful Context: place names in context of your reader by enclosing them in well named class, functions, namespaces.
20. Don't Add gratuitous Context: Add no more context than necessary
Owned & Maintained by Saurav Prakash
If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)