Obey your rules #cleancode

What are your rules? What makes your code good and readable? What you leave to the ones after you is quite important depending on the app you are writing.

I have a few simple rules I try to follow, to keep the code quite clean and understandable:

  • 10 loc per method
  • Only 1 statement (if, loop..) per method
  • 2 parameters in a method
  • Max 5 methods in a class, after that it gets messy and harder to get a quick overview. It also increases the risk of handling to much in a single class
  • Always program against an abstraction, never an implementation

There are some nice benefits that comes out of this. It is easier to structure the entire code base when things aren’t entangled and does lots of things. Naming is so much simpler. Even if you don’t do TDD or write your tests, your code (almost) automatically becomes testable. If something sneaks up from behind and attacks you, just sit back, check the code, write a couple of tests which proves the scenario and you shall see it will surface if you can’t spot it right away.

Heaps of other benefits there are too. Can you figure them out?

I’d love to hear what you constrain yourself with to not get carried away with large pieces of code at the end of the day! Tell me on twitter already.

Further reading