In the life of every programmer, there comes a moment when it’s not all about making the code work 😉 , but also its transparency. Many books have been written about the so-called clean code. Below, I have prepared a quick cheat sheet to help you get started:

1. Significant Names:

  • Names are showing the intention
  • Avoid disinformation
  • Names easy to pronounce
  • Easy to find names

2. Functions:

• As short as possible (max 20 lines)
• Preferably without or with one argument
• One level of Abstraction
• Try {} and catch {} blocks in a separate function

3. Comments:

  • As little as possible (self-commenting code)
  • Don’t leave commented block of code
  • Allowed comments:
      • A comment with the type of license
      • TODO
      • Warning comments

4. Formatting:

• Small classes are better than large ones
• In classes – on the top place general methods and as you move down place methods that are more detailed
• Vertical spacing between code segments
• Called function immediately under calling function
• Spaces around operators
• Horizontal notches separating the blocks of code

5. Objects and data structures:

• Don’t add blind setters and getters
• Follow the law of Demeter
• Don’t do this: a.getB().getC().getD().getZ()…..
• Avoid hybrids (objects with a bit of data structure)

6. Error handling:

• Exceptions instead of return codes
• Don’t return NULLs
• Don’t pass NULLs
• Create error messages with information about the type of failure and what was to be done and sending them in an exception

9. Classes:

  • Keep classes small
  • Maintain high consistency
  • Few instance variables
  • Classes organization (from the top)
    • Public final statics
    • Public static variables
    • Public instance variables
    • Private methods

LEAVE A REPLY

Please enter your comment!
Please enter your name here