Some month ago I read the book: Clean Code, I highly recommend it. Here are the best tips that I found.
- Boy-scout Law: Leave the campground cleaner than you found it. The cleanup doesn’t have to be something big. If you have to modify something, try to improve the readability of nearby code
- Functions:
- Must be short (5 lines)
- Better without arguments or less than 2
- Should do only one thing, do it well, and have no side effects
- Variables
- Better with long and descriptive names
- Be consistent. Don’t use get and retrieve for the same
- Use constant. Like MAX_NUMBER_LINES instead of just 10
- Comments
- It’s better to write a descriptive function than a comment
- Outdated comments confuse
- Instead of comment an ugly function, try to refactor it, so it express clearly what it does
- Classes
- Small classes (200 lines)
- Line width < 120
edit: These are my notes from the book “Clean Code” ,Robert C. Martin. I know that some tips are polemic so I wanted to share it to know what others think about it. You can read clean code articles by Robert C Martin
edit2: I want to share more info about the “less that 2 arguments” and 5 line functions.
The author says: “The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification — and then shouldn’t be used anyway.”
A really good stackoverflow ‘s thread about the “5 lines functions”