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