Clean Code Book – top 5 tips

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”

  • http://sportsbooksforbabies.com/ Ken Ashe

    That’s some good advice.

  • Klaitos

    Good to know, thank.

  • Dogcatcher

    Limiting functions to 5 lines is simply ridiculous.  Functions without arguments even more ridiculous. A function with an argument is not a function, it is a method.   

    I could excuse this lack of knowledge in a hobbyist, but you seem to be purporting to be a developer.

    • http://vitomd.com/blog vitomd

      These are my notes from the book. It’s just a guide.  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.” I am not following all his tips, but some are interesting , you can found more articles from the same author (Robert C. Martin)
      here: http://www.informit.com/authors/bio.aspx?a=361a5e70-f1e2-432b-9928-b30b4742ae80