## Wednesday, July 03, 2013

### Write good commit messages

Over the years I've become more and more verbose in commit messages. For example, here's a recent commit message for something I'm working on at CloudFlare (I've obscured some details). This is actually a one line change to a Makefile but gives a good example of what I'm aiming for.
commit 6769d6679019623a6749783ea285043d9449d009
Author: John Graham-Cumming
Date:   Mon Jul 1 13:04:05 2013 -0700

Sort the output of $(wildcard) as it is unsorted in GNU Make 3.82+ The Makefile was relying on the output of$(wildcard) to be sorted. This is
important because the XXXXXXXXXXXX rules have files that are numbered and
must be handled in order. The XXXXXXX relies on this order to build the rules
in the correct order (and set the order attributes in the JSON files). This
worked with GNU Make 3.81

In GNU Make 3.82 the code that globs has been changed to add the GLOB_NOSORT
option and so the output of $(wildcard) is no longer ordered and the build would break. For example, make clean-out && make would fail because the XXXXXXXXXXXXXXXX (which is used for the XXXXX action) which appears in XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX would not have been parsed before the XXXXX action was used in some other XXXXX file. That would generate a fatal error. The solution is simple: wrap the$(wildcard) in $(sort). The actual change uses a$(foreach) loop because it's necessary to keep the directories in the
order specified in the Makefile and the files within the directory are sorted
by the $(sort$(wildcard ...)). The directory order is important because
XXXXXXXXXXXX must be processed before the other rule directories because it
contains XXXXXXXXXXXXXXXXXXXXXXXXXXX which sets the XXXXXXXXXX thresholds.

The first line gives a brief summary of the commit. But the rest explains in detail why this change was made (a change in GNU Make 3.82 in this case), why the change in GNU Make 3.82 caused a problem, verification of what actually changed that caused the problem, how to reproduce the problem and finally a note about the specific implementation. The final note is there so that someone looking at the commit later can understand what I was thinking and assumptions that went into the change.

I've come to like long commit messages for a number of reasons.

Firstly, I tend to forget why I changed things. And I certainly forget detailed reasoning about a change.

Secondly, I'm not working alone. Other people are looking at my code (and its changes) and need to be able to understand how the code evolves.

And these long commit messages overcome the problem of code comments that get out of date. Because the commit message is tied to a specific diff (and hence state of the code) it never gets out of date.

There's another interesting effect. These log messages take just a minute or two to write, but they force me to write clearly what I've been doing. Sometimes this causes me to stop and go "Oh wait, I've forgotten X". Some part of writing down a description of what I'm doing (for someone else to read) makes my brain apply different neurons to the task.

Here's another example from a different project:
commit 86db749caf52b20c682b3230d2488dad08b7b7fe
Author: John Graham-Cumming
Date:   Mon Jul 1 10:14:49 2013 -0700

Handle SIGABRT and force a panic

It can be useful to crash XXXXXXX via a signal to get a stack trace of every
running goroutine. To make this reliable have added handling of SIGABRT.

If you do,

kill -ABRT

A panic is generated with message "panic: SIGABRT called" followed by
a stack trace of every goroutine.


If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: The Geek Atlas. Signed copies of The Geek Atlas are available.

<$BlogCommentBody$>

<$BlogCommentDateTime$> <$BlogCommentDeleteIcon$>

<$BlogBacklinkControl$> <$BlogBacklinkTitle$> <$BlogBacklinkDeleteIcon$>
<$BlogBacklinkSnippet$>