- Comments should say what the code does, not how it does it.
- Name your routines so that they are self-evident. The same goes for parameters.
That way, comments' necessity is reduced.
- Keep the number of parameters small. Try not to go over four. Tree to four is optimal.
- Try not to repeat the code.
- Avoid long routines of code.
- Avoid complex conditional statements with large blocks of code. Reduce the complexity.
** Refactoring (https://youtu.be/1KeJc6V4Jjk?t=127)
- Extract methods.
Change long routines to few/many small ones (ca. 20 lines of code).
- Extract conditional and loop blocks of code as separate routines.
- Move related concepts closer together.
Physically put similar parts/ideas (code that represent those) closer together.
- Reuse same parts of the code extracted earlier.
- Separate concerns/Modularity.
Make each module (class/routine/...) do only one thing.
Don't bloat/clog the code. Goal is to achieve that one goal:)
- Work in small steps.
Small changes are less prone to errors.
- When you code,try to refactor your code after every small change. Try to clean it up a bit if you can.
Remember: "Our job as programmers is to solve problems, not to write code":)
(source: https://youtu.be/1KeJc6V4Jjk?t=716)
##References:
5 Ways to Improve Your Code, https://www.youtube.com/watch?v=1KeJc6V4Jjk,
Refactoring Legacy Code: STEP BY STEP
Bertrand Meyer:Object-Oriented Software Construction: