Skip to content
KALANBLON
Show contents

Chapter 2 of 6 · 20 min

Commit messages and code reviews

By the end of this chapter, you'll be able to

  • Write a commit message following the Conventional Commits convention
  • Check a message's format automatically with a regular expression
  • Write precise, justified and kind review comments
  • Receive a review and respond to it without taking it personally

A useful commit message

A commit message has two parts: a short title line, then, after a blank line, an optional body that explains why. The what is already visible in the changed code; the why disappears if nobody writes it down.

fix: round the total to the nearest franc

The total showed 1,499.9999 F CFA because of floating-point numbers.
We now compute in whole cents, then round when displaying.
Fixes ticket #87.

Many teams follow the Conventional Commits convention for the title line:

type(optional scope): description in the imperative, no final full stop
TypeWhen to use it
feata new feature
fixa bug fix
docsdocumentation only
refactorrestructuring code without changing its behaviour
testadding or fixing tests
choretooling, dependencies, configuration

A ! after the type (feat!:) signals a breaking change: code that used the old version must be adapted. This convention makes the history readable at a glance, and tools use it to generate release notes automatically.

Checking the format automatically

Teams check the format with a tool (commitlint, for example) hooked into Git or into the pull request checks. The principle fits in a regular expression, a pattern describing the expected shape of a text:

import re

TYPES = "feat|fix|docs|refactor|test|chore"
PATTERN = re.compile(rf"^({TYPES})(\([a-z0-9-]+\))?!?: \S.{{0,70}}$")

def check(message):
    title = message.split("\n")[0]
    if not PATTERN.match(title):
        return "rejected: the expected shape is \"type(scope): description\""
    if title.endswith("."):
        return "rejected: no full stop at the end of the title"
    lines = message.split("\n")
    if len(lines) > 1 and lines[1] != "":
        return "rejected: a blank line must separate the title from the body"
    return "accepted"

for m in [
    "feat(catalogue): add search by title",
    "fix: round the total to the nearest franc\n\nFixes ticket #87.",
    "feat!: remove the old courses API",
    "change",
    "Fix: fix the bug",
    "docs: update the guide.",
    "fix: fix the calculation\nForgot the blank line",
]:
    print(f"{check(m):<62} ← {m.splitlines()[0]}")
accepted                                                       ← feat(catalogue): add search by title
accepted                                                       ← fix: round the total to the nearest franc
accepted                                                       ← feat!: remove the old courses API
rejected: the expected shape is "type(scope): description"     ← change
rejected: the expected shape is "type(scope): description"     ← Fix: fix the bug
rejected: no full stop at the end of the title                 ← docs: update the guide.
rejected: a blank line must separate the title from the body   ← fix: fix the calculation

Let's take the pattern apart: ^ and $ require the whole title to match; (feat|fix|…) accepts one of the types; (\([a-z0-9-]+\))? makes the scope optional; !? allows the exclamation mark; \S.{0,70} requires a description that starts with a visible character and limits its length. In an f-string, the pattern's braces are doubled ({{0,70}}) so they are not taken for a variable.

The code review

A code review is the reading of a pull request by another person before the merge. It serves three purposes: finding mistakes, keeping the code understandable by the whole team, and sharing knowledge (the reviewer learns the code, the author learns tips).

Once the blocking points are settled, the reviewer approves.

Step 5 of 5
Text description of the animation
  1. First read the purpose of the change: you cannot judge code without knowing what it should do.
  2. Tests pass; for an interface, try the page yourself.
  3. Read the code and comment: questions, blocking problems, optional suggestions.
  4. The author fixes or explains their choice; the discussion stays about the code.
  5. Once the blocking points are settled, the reviewer approves.

Comments that help

A good comment is precise (it points to a line), justified (it says why), and says whether it is blocking or not. Many teams use prefixes: "blocking:", "question:", "suggestion:", "nit:".

✗ "This is wrong."
✓ "blocking: if cart is empty, total / len(cart) raises ZeroDivisionError.
    We could return 0 in that case, and add a test."

✗ "Why on earth did you do that??"
✓ "question: you chose a list rather than a dictionary for the prices?
    I'm asking because looking up by code will be slow with 10,000 products."

✗ "I would have written it differently."
✓ "suggestion (non-blocking): sum(p.price for p in cart) avoids the intermediate variable."

Comment on the code, never the person: "this function is hard to follow" rather than "you code in a confusing way". And don't forget to point out what is done well: it helps as much as criticism.

Receiving a review

A remark about your code is not a remark about you. Reply to every comment: fix it ("fixed in the next commit"), or explain your choice calmly. If an exchange goes round in circles after two back-and-forths, suggest a five-minute call: it is often faster and friendlier.

Mini-lab: complete the checkerPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: rewriting messages

Here are four real messages from a repository. Rewrite each one following Conventional Commits, guessing the context from the description in brackets, then check them with the checker.

  1. "stuff" (adding an "Export to CSV" button on the sales page)
  2. "bug" (the delivery date was shown in US format)
  3. "update readme" (adding the installation steps)
  4. "cleanup" (an 80-line function split into three, with no change in behaviour)
Hint 1
Start by choosing the type: new feature, fix, documentation or restructuring?
Hint 2
A scope such as (sales) or (delivery) helps locate the change.
Show the solution and common mistakes
import re

PATTERN = re.compile(r"^(feat|fix|docs|refactor|test|chore)(\([a-z0-9-]+\))?!?: \S.{0,70}$")

messages = [
    "feat(sales): add CSV export to the sales page",
    "fix(delivery): show the date as day/month/year",
    "docs: add the installation steps to the README",
    "refactor(orders): split the order calculation into three functions",
]
for m in messages:
    print("✓" if PATTERN.match(m) else "✗", m)
✓ feat(sales): add CSV export to the sales page
✓ fix(delivery): show the date as day/month/year
✓ docs: add the installation steps to the README
✓ refactor(orders): split the order calculation into three functions

Here the pattern is not an f-string, so the braces {0,70} are written as is.

Common mistakes:

  • Describing the how instead of the what: "change a for loop" teaches nothing; "fix the total of empty carts" says what changes for the user.
  • Using fix for a cleanup: with no bug fixed, it is a refactor. The type is used to sort the history and the release notes.
  • Writing a 120-character title: the details go in the body, after a blank line.

Summary

  1. 1A commit message has a short title and, after a blank line, a body explaining why.
  2. 2Conventional Commits: type(scope): description, with a ! for a breaking change.
  3. 3A regular expression is enough to check the format of titles automatically.
  4. 4A review comment is precise, justified, says whether it is blocking, and targets the code, not the person.
  5. 5An approval without reading is worse than no review: ask for small pull requests.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.