Eight must have pre-commit hooks for #python code
Recently there was an article on LinkedIn about how debugger
statement was
left in the production code (front end javascript) and thus user could not move
forward.
People commented about the code review.
But why invest manual efforts when such things can be automated ? See #4 in the list below.
pre-commit
hook is an under appreciated (dare I say, unknown) feature of git
As the name suggests, set of commands/scripts can be executed before git commit
If any of these fail (exit code other than 0
) the code is not committed.
Some of these are just checks, but some automatically fix/change your code for the better. How cool is that.
Here are the eight precommit hooks you must use:
- Automatically remove trailing spaces
- Exactly 1 new-line at the end of file
- Check YAML (prevent bad config getting into production)
- debug statements are only for local testing, but get rid of them before committing to git
- reorder imports alphabetically (and group them)
- Automatically use latest idiomatic python. (pyupgrade)
- automatically fix style errors to make code look consistent
- catch type mismatches during development, leading to fewer runtime errors
While some of these may seem like they do not add value (consistent looking code) others have tremendous value (detect debugger statements, type mismatches and automatically using latest idiomatic python)
While these are specific to python, similar ones are available for other
languages as well (git
is programming language neutral). e.g. there is husky
for node/javascript.
Read the original article here