Why Good Commit Message Matters?

Time is money. Time is expensive. Time to market is twice as expensive. Time of a high-skilled developer is as expensive as you could imagine. That’s why a good commit message matters.

“Context! Context! My kingdom for context!”

If you’ve been involved in a lot of different projects and worked with tons of source codebases during your career, you probably read commit messages that were a total mess.

Here is a log fragment from a real codebase:

What were the changes? Why were they formatted so badly without keeping one style in the same codebase? Who knows… It all happened a long time ago and in a galaxy far, far away…

Let’s compare the fragment above with the recent commits to the Spring framework on Github:

Which would you rather read? Which commits look concise and consistent?

A well-crafted commit message is the best way to convey the context of a change to other teammates. A source diff will tell what has changed, but only the commit message can tell why.

Peter Hutterer pointed it out:

  • Any software project is a collaborative one. It includes at least two developers, the first developer that starts working on a project and the same developer but a few weeks or months later when the train of thought has long left the station. The second one has to reestablish the context of a particular piece of code each time a new bug occurs or a new feature needs to be implemented.
  • Re-establishing the context of a piece of code is wasteful. But since we can’t avoid it completely, our efforts should go into making it as short as possible. Commit messages can do exactly that. As a result, a good commit message indicates whether a developer is a good collaborator.

How a well-crafted commit message looks like

There’s no strict definition of an ideal commit, but in our company, we try to follow the known best practices. Some of them I’ll share below.

Three questions

According to Peter Hutterer, a good commit message should answer the three questions:

  • Why is it necessary? It may fix a bug, add a feature, improve performance, reliability, stability, or just be a change for the sake of correctness.
  • How does it address the issue? For short obvious patches, this part can be omitted, but in other cases, it should be a high-level description of what the approach was.
  • What effects does the patch have? (In addition to the obvious ones, this may include benchmarks, side effects, etc.)

These three questions establish the context for the actual code changes, allow reviewers and others to look at the diff, and check if the chosen approach was correct.

One logical change

In general, a good commit should contain exactly one logical change. It may include adding a new feature, fixing a specific bug, code refactoring, etc. Don’t mix different changes from different areas in the same commit, omit bulk code changes in one commit as much as possible. It’s better to split them into several observable commits based on the functionality provided. It will greatly help reviewers understand the changes.

Seven rules

Chris Beams summarized the best practices of the great commit messages into the seven rules:

Keep in mind: This has all been said before.

  1. Separate subject from body with a blank line.
  2. Limit the subject line to 50 characters.
  3. Capitalize the subject line.
  4. Do NOT end the subject line with a period.
  5. Use the imperative mood in the subject line.
  6. Wrap the body at 72 characters.
  7. Use the body to explain what and why vs. how.

For example:

The rules look quite simple but bring a lot. They put everybody on the same page, allow to keep the product’s consistency, properly track the changes, and restore the rationale easily, without asking the author. Ironically, that author could be you several months earlier. It really doesn’t matter how long ago the changes were made and whether the changes’ owner still works in the company or on the same project.

Summarizing all the above, use the best practices and take care of your changes. Always remember, you are not the only one involved, so a good commit message really matters.

Happy coding!

Add a comment