Scroll Top

“Hooks” to prevent low-quality code

What prevents pushing low-quality code into a shared repository? Is there a way to filter, clean, or completely block certain things from entering the codebase?

Say hello to Pre-Commit Hooks! A “hook” is a process applied when certain conditions are met, and “pre-commit” means exactly that: applied before a Git commit is accepted.
These pre-commit hooks (PCHs) generally come in two flavours: modifiers and blockers. Here are some that apply to a Python codebase.

Modifiers include the library black, which automatically reformats committed work to an agreed-upon style. On the other hand, blockers include mypy, that makes sure all type-annotations are logically consistent. If the check fails, then the commit won’t be accepted.

These checks provide a valuable automatic filter of code quality. For a while now in Henesis we have been using PCHs to ensure the software we develop satisfies a minimum standard and to reduce inconsistency in formatting (important in a language like Python that is pretty lenient). It also reduces the burden during code-review, since everybody’s code now has the same style.

As a result our collaboration is streamlined and productivity greatly improved.

We’re happy to share the pre-commit-config.yaml we’ve built up using this technology. Feel free to copy and paste it!

 pre-commit-config.yaml »