Inspired by Clean Code by Robert C Martin, trying to summarize coding best practices. Starting with Naming and Functions best practices in this post.
Naming
- Names should encode the intent, for example, studentBirthYear.
- Use Good distinction: Do not use list1, list2, etc
- Use Pronounceable name: dobmmyy vs dateOfBirthInMonthAndYear
- use searchable names: int i, j, when you will try to search you will find a lot of them in code
- Do not add type: phoneString, name String, name and phone should be sufficient
- Avoid unclear prefixes: m_name vs manager_name
- nouns for names and verbs for functions: employee for class and paySalary for function
- Use Consistent concept: controller vs manager
- Don’t use the same name twice to mean 2 different things. paymentInfo at one place returns bank details and another place user payment
- Use Domain specific names
- Avoid too long or too short names: Long is fine if it conveys better information, but not too long that makes it difficult to pronounce
Functions
- Write Small functions, functions larger than 20 lines should be avoided
- Make sure the function does only one thing
- Use minimum arguments: max 2, if the function takes too many arguments, it is doing too much
- DRY, Do not Repeat yourself: IF you are doing the same thing in multiple functions, move it to commonplace
- Don’t use flag element, parameters of the Boolean type as a parameter already clearly state that it does more than one thing.