I’ve always been a fan of commenting code. To me, it’s a no brainer, the sensible thing to do, and a fundamental part of building software that’s easy to understand and maintain. Not everyone agrees though. Some argue that comments are unnecessary and simply shine a light on code that isn’t as expressive as it should be. In this post I’ll try to counter those arguments and tell you why I think quality comments are an important part of building software that easier to understand and maintain.
Helping Others Understand Your Code
As developers, we spend a considerable amount of time reading and trying to understand other peoples code. At times this can be a difficult and frustrating task. Most of us have been in a situation where we’re neck-deep in legacy code with no documentation, no unit tests and not a comment in sight. Chances are the guy who wrote it isn’t around anymore and we’re left to our own devices to figure out WTF is going on. Not much fun, is it?
These experiences should remind us that as developers we have a responsibility to go beyond implementing functional requirements and write code that’s easy to read, understand and maintain. A code base that’s easy to understand, makes life easier for your future self and those coming behind you. It allows developers with varying levels of technical ability and domain knowledge to jump into your code and become productive with the least amount of pain.
Comments – Just One Piece of the Puzzle
When it comes to crafting code that’s easy to understand and maintain there are a number of important pieces in the puzzle. Based on my own experience I’d order these as follows.
- good structure – classes with a single responsibility, functions that do one thing
- descriptive naming – classes, functions and variables should have clear, descriptive names
- unit test coverage – unit tests describe how a component should behave. As well as testing expected behaviour they act as a form of component specification
- comments – comments provide important information that can’t easily be expressed by the code.
You might be surprised that comments are at the bottom of the list. That’s not to play down the importance of comments, but rather to highlight the importance of the other factors. Emphasis should always be placed on writing well structured, expressive code that’s easy to understand. Think of comments as the icing on the cake, that exist to provide the reader with information that can’t easily be expressed by the code itself.
Think about Your Audience
Over the lifetime of a system, developers with varying levels of technical ability, experience and domain knowledge will work on the code. At one end of the spectrum, you may have very experienced tech lead/architect level guys, while at the other end you may have graduate developers fresh out of college. The point is, you don’t know exactly who’ll be working on your code in the future or what level of expertise they’ll have. With this in mind, you should make your comments accessible and useful to as broad a range of readers as possible. If you’re an experienced tech lead with strong domain knowledge, you shouldn’t comment your code assuming that the guy coming behind you will know as much as you do. Instead, write your comments with less experienced developers in mind.
You could argue that this runs the risk of comments that are too obvious, especially for more experienced developers. While this is a risk it’s important to remember that when we describe a comment as obvious we’re being subjective. Something that’s obvious to a tech lead with 15 years experience may be anything but obvious to a junior developer.
What and Why
There are essentially 2 things you’ll want other developers to grasp when they’re looking at your code. What the code is doing and why it’s doing it. Let’s take a closer look.
Comments that explain What
Well written code in many cases will be self-documenting. Your aim should be to make your code as expressive as possible, therefore reducing the need for comments that describe what the code does. However, there are times when describing what the code does is useful.
- When the business logic is complex you should use comments to describe your intent. Yes, you still want to make the code as self-descriptive as possible but comments add value in this instance by describing what you’re trying to achieve. As we all know, what the code does and what it’s meant to do is not always the same thing
- Some code simply isn’t expressive. Take regular expressions as an example. Yes, you can give a regex a descriptive name but it might not help the reader understand the pattern that’s being resolved. In this instance, comments that describe the pattern and provide sample matches are really useful. Complex SQL is another example of code that doesn’t tend to be very expressive. Some simple comments can help guide the reader by describing the intent of the statement.
- Domain concepts can be difficult to describe. It sounds straight forward, but naming things well can be very tricky, especially in a complex domain. I’ve spent quite a bit of time working in life assurance and pensions. I sometimes find it difficult to encapsulate the purpose of a class, function or variable in a simple name. In these situations, I’ll come up with the most descriptive name possible and then add a comment to describe in plain English, what the class, method or variable represents in business terms.
Comments that explain Why
Comments that explain why your code does something are very important because this information can’t be derived from the code. Below are a few examples of when it’s important to explain why you’ve chosen to do something.
- Design Decisions – Comments that explain why you’ve made design decisions are important, especially if you’ve implemented some kind of pattern that you expect other developers to leverage in the future. A guiding hand can help point people in the right direction by showing them the most appropriate way to extend existing functionality. Making it clear why you’ve done something also reduces the chances of someone refactoring your code in the future and inadvertently breaking a pattern you’ve put in place.
- Workarounds – we sometimes have to work around problems caused by external factors we have little or no control over. Perhaps data received from an external system needs to be tweaked in certain instances before it can be processed. Workarounds like this, while not ideal are a reality of enterprise development, especially where legacy systems are involved. Comments help by explaining the root of the problem and why we need to work around it. Comments provide other developers with context and reduce the chances of someone removing the workaround because they don’t understand it.
- Edge cases – We sometimes write logic that has to handle peculiar edge cases. These scenarios aren’t usually intuitive and are therefore an excellent candidate for comments. Logic that handles quirky scenarios often seems more complicated than it needs to be. Comments are a good way to explain the edge cases and why you’ve implemented the solution you have.
Conversational Comments
I try to keep my comments informal and conversational. Imagine one of your teammates is sat beside you and you’re explaining how some part of your code works. You’d explain in simple terms, making it as easy as possible for your teammate to understand. I think comments should read the same way by using simple language to get the point across. Remember, comments are there to provide information that the code can’t, so make them as easy to digest as you can.
Quality Over Quantity
Like most things in life, there should be an emphasis on quality over quantity. Comments should be used thoughtfully, to tell the reader something that can’t be easily expressed by the code. Well structured, expressive code with a sprinkling of useful comments is much better than poorly written code with a ton of comments explaining what each and every line does.
Avoid superfluous comments that echo something clearly described by the code. Comments, like code, evolve over time and need to be maintained. Avoiding superfluous comments reduces the maintenance overhead and helps the reader focus on the comments that are important.
Avoid Comment Rot
Comment rot is when the code is updated but the accompanying comments are not. This leads to comments that are out of date and often contradictory to the code they’re supposed to describe. Comment rot is often cited as a major downside to commenting code but I think this argument is weak. While I agree that comment rot is a bad thing and something to be avoided, it’s not a good enough reason to stop commenting your code. The best way to avoid comment rot is obvious…..update comments when you update the code. Like most good practice this requires a combination of developer discipline and governance in the form of code reviews.
Wrapping Up
Comments aren’t a substitute for well structured, expressive code but they can help by providing information that isn’t easily expressed by the code itself. Code that’s easier to read, understand and maintain, ultimately translates to greater productivity with more features being shipped, and I’ll bet that’s what keeps your boss happy.
The post Be Nice – Comment your Code appeared first on briansdevblog.
Top comments (1)
Nice article, I think I agree with everything in it.
I still find it quite challenging to, as a developer, implement good maintainable code and also create a good maintainable documentation.
I often find myself asking this questions :
In the cases mentioned above, I am not talking about a simple method documentation, but a code/solution design decision.
Add to that, in most of cases, we also need to manage due time constraints which is usually a great "motivation" to not document and start coding the next task.