Showing posts from December, 2013

Comments: A deodorant to mask code smells

Don't get me wrong. Comments are useful and not all of them have the olfactory purpose of the famous analogy I'm using in this article's title. So, what's so wrong about comments that programmers are willing to even dress a shirt about this odorous matter? Let's say we have this fragrant method:

 nastyMethod() {
  // connect to database
  ... Code to connect to the database ...

  // create default configuration
  ... Code to create the default configuration ...

  // load configuration
  ... Code to load the configuration ...

  ... and more, and more, and even more of this...

The problem here is that the comments are saying what the code is doing. Comments should say WHY the code is doing something, not WHAT the code is doing. Moreover, this typically leads to the Long Method anti-pattern putting in risk two basic OO design principles: Interface Segregation Principle and Single Responsibility Principle to say the less.

Make your code self explanatory:

 betterMethod() {