Skip to main content

Comments: A deodorant to mask code smells

·246 words·2 mins
Programming
Alejandro Duarte
Author
Alejandro Duarte
Alejandro Duarte is a Software Engineer, published author, and award winner. He currently works for MariaDB plc as a Developer Relations Engineer. Starting his coding journey at 13 with BASIC on a rudimentary black screen, Alejandro quickly transitioned to C, C++, and Java during his academic years at the National University of Colombia. Relocating first to the UK and then to Finland, Alejandro deepened his involvement in the open-source community. He’s a recognized figure in Java circles, credited with articles and videos amassing millions of views, and presentations at international events.
Funny T-shirt for Developers

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() {
  connectToDatabase();
  createDefaultConfiguration();
  loadConfiguration();
}

connectToDatabase() {
  ... Code to connect to the database ...
}

createDefaultConfiguration() {
  ... Code to create the default configuration ...
}

loadConfiguration() {
  ... Code to load the configuration ...
}

You might argue “this extract to method thing will lead to classes with too many methods, making them difficult to understand”. Well, use the Extract Class refactoring if you reach that point. Remember: The object oriented programs that live best and longest are those with short methods.

Enjoyed this post? I can help your team implement similar solutions—contact me to learn more.

Related

Lightning fast commenting (in and out)
·123 words·1 min
Programming
I have used this two or three times in my life but it’s one of those simple (and useful?
Empty lines and semantics in source code
·537 words·3 mins
Programming
I remember a couple of years ago, while working with some developers, one of them seemed to be irritated by seeing empty lines in source code.
Book Give-away: Win a free copy of the book "Vaadin 7 UI Design By Example" - Just by commenting!
·246 words·2 mins
Vaadin News
And the winners are: Iskael Diaz Marquez Camilo Gonzalez Packt Publishing has two copies of Vaadin 7 UI Design By Example to be given away to two lucky winners.