The engineering team in our organization was inspired by Robert C Martin’s work on Clean Code and have decided to come up with their own concise version of best coding practice and guidelines.
Precision
- The number of branching points in a function
- Methods should have precisely one responsibility only
- Create methods with a maximum of 5 branching points, Eg. minimize nested switch and if..else statements
- Method names should be self describing
Interaction
- The number of parameters of a function
- Create functions with a a maximum of 4 parameters
- Create parameter objects to pass multiple arguments to a function
Encapsulation / Encapsulate Conditionals
- Complicated chaining of conditional logic
- Extract into a separate functions
- Use a configuration instead when the logic is repeatable and complex
Concision
- The number of statements in a function
- Create functions with a maximum of 20 lines of statement
- … but favor readability over conciseness
Exception Handling
- Refactor empty and illegal catches
- Refactor catches which are used for normal control flow
Duplication
- The amount of code that has been duplicated
- Reduce duplication
- Follow the DRY principles
- Refactor existing code to increase reusability with caution
General Guidelines
KISS – Keep It Simple Silly
Keep your code and logic simple, clear and easy to understand for other engineers. Break complex conditions into smaller methods.
DRY – Don’t Repeat Yourself
– Avoid repeating and duplicated logic and code across the code base. Extract into a method to be used in these instances if necessary.
YAGNI – You Aren’t Going to Need It
– Avoid implementing features that are not required yet. Refactor in the future again if new features are required.
TDD – Test Driven Development
– Write tests when starting a new function or class to increase code coverage because this reduces the technical debt
– Write tests when fixing a bug to prevent future reoccurrence
Boy Scout Rule
– When working on a piece of code, leave it cleaner than before if necessary so that the next developer will have a better coding experience
Clean code from Amazon
https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
More on engineering best practices
https://www.kianyang.co/git-commit-and-github-best-practices/