Another great tip that saved me from tons of refactoring: "The wrong abstraction is worse code duplication". Meaning sometimes duplication of code is better than trying to create the wrong architecture (as long as you mark it in your quick and dirty list).
There's lots of materials on how to refactor code (improving its structure without changing functionality), including blogs, books and video lectures.
- Line should not be too long (120 max)
- Function should fit into 1 page in the view port of screen, even when your console and debugger take 1/3 bottom part of the screen.
- Variable name should be pronounceable and longer than 3 letters (to prevent name like `i`, `x`, `s`)
- File should not be too long (1000 lines max).
- Return/Exit/Throw as early as possible.
- Comment:
- If an `if` take more than 2 condition, it's worth commenting.
- If a funciton is longer than 10 lines, it's worth commenting.
- All file's worth commenting.
- If you ask yourself "should I add comment", add comment.Robert Martin, Clean Code.
Most people that have read the book (and it is a classic so many have) will swear by it. I most certainly do.