Don’t comment (unless you must)

I have not written for a while, but I had this topic on my mind for a while.

It has been on my mind for a while because we (my current workplace) have some mix of new and old code and there are a lot of comments in the old code.

Back in the day, developers were told to add comments, and this something that is changing for the past few years, but our old code was written a while ago. However we do have some ‘older’ guys that still currently add meaningless comments.

Now some comments are helpful but the majority is not needed and it’s just cluttering the code.

I will give some examples (this is real code):

// Check for previously open doors. 
CheckForPreviouslyOpenDoors();

// Post the door opened metered event.
PostDoorOpenMeteredEvent(logicalId, doorName);

You can probably see why those comment are useless. They have zero value.

The reason for not adding any value is because the methods under the comments say it all.

The way to write code in my mind is to keep it simple. Give meaningful names to everything; for properties, for variables and of course to methods.

Now this should always be applied but sometimes we do write complex code within a method (that we gave it a good name of course). In those cases I would add short comments.

Your code should be easy to understand and to follow. Giving meaningful names will help people understand without adding useless comments.