What’s in a useful commit message?

(Originally published on dev.to)

When making a commit, Git prompts you to enter a message to store with the commit. Commit messages are messages for someone looking at the change in the future. They can help them in making sense of the changes, but Git can’t enforce that you write a useful one – how can it? Instead, it is up to you to write something useful for them. So, what can you write to in a commit message to make it useful?

No matter who is looking at the commit message, it helps to describe what is in the commit. This is most likely the first thing anyone will want to know about a commit – what is this commit about? The git commit documentation recommends this being the first thing in commit message:

Though not required, it’s a good idea to begin the commit message with a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git.

Visually, the format looks something like this:

commit title (less than 50 characters recommended)

more thorough description

Log viewers like gitk display their title in the list of commits.

Gitk with list

A descriptive title describing what is in the commit will make the logs much more easier to work with.

If you’re finding it difficult to write a descriptive title for the commit, perhaps have a look what is in the change and see whether the commit is trying to do more than one thing. Ideally, the commit should be doing just one thing.

When looking back to a previous change, a very common question that is asked is why did we make that change? When trying to build changes on top of previous changes or trying to fix an issue in code, knowing why the changes were made in the first place can help avoiding introducing more problems later. For contributions to other projects, it helps to convince maintainers that they should be included. For your future self, it can help to serve as a reminder of why you did what you did at the time. An explanation of the why indicates that the changes were thought out and wasn’t just done on a whim.

Sometimes, the why is implied in the title. For example, “fix spelling errors” would likely be self explanatory. When its not, this is something that should be written in the commit description. Describing the why could also include answering:

  • If the change fixes something, what was wrong in the first place? Were there any lessons learnt?
  • If it is an new feature, what does it do?
  • If it is an optimisation or improvement, how does it improve things? Are you also able to quantify the improvement?

When Git prompts your for a commit message it may be tempting to just fill this in with something to fulfil this step, but to do so would be squandering an opportunity to capture valuable information about it. Writing one is about spending the time and effort now to help someone in the future – it can be immensely helpful in understanding your change and its purpose. Someone may thank you for it one day!

Grouping DataPoints in Junit 4.12

Theories is one of my most used features of JUnit. It is very useful for writing tests for behaviour with a wide range of parameters. JUnit 4.12 introduces named data points. This is useful grouping the data points in the tests. For example, you can use it to group formats for date and time separately like this:

@DataPoint("date")
public static final String SEPARATED_DATE = "yyyy-mm-dd";

@DataPoint("date")
public static final String UNSEPARATED_DATE = "yyyymmdd";

@DataPoint("time")
public static final String SEPARATED_TIME = "hh:mm:ss";

@DataPoint("time")
public static final String UNSEPARATED_TIME = "hhmmss";

@Theory
public void test(@FromDataPoints("date") String dateFormat, 
  @FromDataPoints("time") String timeFormat) {

  ...
}

The runner will pass the values SEPARATED_DATE and UNSEPARATED_DATE as the dateFormat parameter and SEPARATED_TIME and UNSEPARATED_TIME as the timeFormat parameter. No more needing to wrap the parameters in some kind of object or having to test the parameters inside the tests! Furthermore, the runner will also automatically generate all possible values for enumerations and boolean values, so you no longer need to define them as data points.

Mocking Java classes in Groovy vs Mockito

While experimenting with using an ODF parser in Groovy, I was attempting to write unit tests in Groovy and mocking objects from the ODF parser library was required in my tests. Prior to this, most of experience in writing unit test was in Java and mocking using Mockito. Since I had been working in Groovy, I decided to see what support Groovy provides for mocking and how it compares to Mockito (the documentation for mocking in Groovy is available on the Groovy Mocks page).

Read more of this post

[Vim] Diff and merge using Vim

After performing a system update on my Linux machine, the package manager placed a copy of an updated configuration file on the system – it couldn’t overwrite them as I had previously modified it. Since I was also using SSH to remote in the Linux machine, I didn’t want to merge the changes using a GUI like Meld. Fortunately, Vim supports diffing and runs within the terminal!

To view the diff between files, you can use vimdiff.

vimdiff {first file} {second file}

Read more of this post

[Junit] Including the parameters in the names of parameterized tests

Junit 4.11 has added the ability to include the name of the parameters in a Parameterized test. This is done by providing a String to the name argument of the @Parameters annotation. Here is an example of a test that uses this feature:

@RunWith(Parameterized.class)
public class AdditionTest 
{
    @Parameters(name="{0} + {1}")
    public static Collection<Object[]> parameters() {
        Object[][] data = new Object[][] {{1, 2, 3}, {0, 3, 3}, {-1, 1, 0}};
        return Arrays.asList(data);
    }
    
    private final int first;
    
    private final int second;
    
    private final int expected;
    
    public AdditionTest(int first, int second, int expected) {
        this.first = first;
        this.second = second;
        this.expected = expected;
    }
    
    @Test
    public void testAddition() {
        Assert.assertThat(first + second, CoreMatchers.is(expected));
    }
}

The arguments are substitued into the numeric arguments of message. In the above example, the tests would be named “testAddition[1 + 2]“, “testAddition[0 + 3]” and “testAddition[-1 + 1]“. Using this feature, you would be able to tell what the parameters were without having to refer back to your unit test code.

Prior to 4.11 the tests would have been named “testAddition[0]“, “testAddition[1]” and “testAddition[2]“, where the 0, 1 and 2 correspond to the index in the Collection that was provided as the parameters – you have to refer back to your unit test code to figure out what the parameters were. If you do NOT specify the name in 4.11, the tests will also be named in this fasion.

[Bash] Reading from Standard Input

To read from standard input in a Bash shell, use read. The following script reads content from stanard input and outputs the content:

#!/bin/sh

# Uncomment this to use comma as a separator.
# IFS=","

read first second third
echo first=\"${first}\" second=\"${second}\" third=\"${third}\"

Read more of this post

[Sed] Editing files in place

Sed provides the -i or --in-place option for saving changes made to the file. For example, you can replace all tabs with a space with the following:

sed 's/\t/ /g' -i file.txt

Read more of this post

Create your own website, free on WordPress.com.Create your own website.
Start free