Shruti Turner.

How high are your standards?

Pythonpre-commitFlake8PEP8Tutorial

Why do we have standards?

No matter what position you're in, if you're writing code you can make life easier for yourself and others and write code which is high quality. What even is high quality code? Good question, the intricacies of good code can mean something different to different people and even in different contexts, but some things are standard. In fact, there are code quality standards out there which we should be following. I'll be the first to own up to the fact that I know that following these standards doesn't always come naturally to me and I know I'm not alone.

For one, though I like my code to be high quality, I am in fact a human being too and therefore have a lazy streak in me. Sometimes you just get so stuck into the code you're writing, and code quality just gets forgotten about. After whatever you're writing has been written, there is a chance to reconfigure the working code to adhere to standards. Let's be honest about it though, it's not generally a priority for many once the code is working as it's tedious and comes into our "tech debt" which just goes back into the backlog. Does it really matter if a few variable names don't stick to convention or line are more than the stated characters?

Well, yes and no. Let's start with the no, it's quicker....no it doesn't matter because the code is working. But that's not the whole story, life is not that simple. Standards exist for a reason. They're there to help you out, as much as you think future you will understand and remember everything present you thinks...you're wrong. That's not to mention all the people you're collaborating with as well - they can't read your mind. What if you've decided a convention for something in your code, but they decide a different one when they add theirs? It just ends up being a mess. Clean code, which follows set conventions, is easier to read and makes working together much more efficient.

If you're working in Python, we should be following PEP8 standards for our code. If you've ever taken a look, the standards are long and not particularly exhilarating to read. Fear not, there's a way to help (and even make you) improve your code without reading the standards...

This comes in the handy form of the Flake8 tool, a PyPi library someone has written to check your code against the PEP8 standards. Depending on where you write your code, there are different ways you can use this...

Option 1 - pre-commit.

The pre-commit library provides hooks to help with checking your code quality before you commit it (don't forget you'll need to download it!). You can write a pre-commit-config.yaml file which runs when you try to commit your code. You can choose how you want to set up your file, which dictates the checks that are run. For now, take a look at how you'd check your python standards via Flake8. You need the repo where the information is stored, the version and the hook id (both of which should be found in the repo). If your code meets the standards, great it'll commit as desired, but if not you'll get a log of the issues along with the line of code identifying where the issue is.

Shows the code required in the pre-commit-config.yaml file to check Flake8 standards.
pre-commit yaml for Flake8

You can even run your pre-commit checks against your code before you try and commit too if you like. That way you can check your code as you go along, you just have to remember to do it!

To run pre-commit on all files:

pre-commit run --all-files

To run pre-commit on specific files:

pre-commit run --files <file_path>

Option 2 - IDE extension.

How well this works very much depends on where you choose to write your code. Personally, I use VScode for various reasons I won't go into here. I find VScode has a helpful selection of extensions to make life easier, one of which is the Flake8 extension. With this installed your code will get underlined if it doesn't meet the standards. At first it really irritated me because my functioning code would be covered with squiggly lines, but over time this has decreased. I now rarely get those lines because I've learned through practice what I should be done, which I find much more effective that reading up what I should do and hoping I remember it. It's a great learning tool and means that you don't have to change your code after it's written, but can do it as you go along so it feels like less of a chore.

Personally, I like to use both the pre-commit library and the VScode extension. I like to be able to see the errors I'm making along the way so I can fix them as I go along and learn as I write, but also we have many more pre-commit checks that it's helpful to have the Flake8 one in there just in case I've missed anything.

There are plenty of other tools out there to check your code quality, integrating with different IDEs and more. The world is your oyster, check out what works for you and let me know! I'm always open to new tools and ideas!

Share Now



More Stories

Cover Image for Tickets, Please?
Ways of WorkingTicketsAgileScrumKanban

Tickets are the building blocks that make up a team’s work, without clearly defined blocks it’s difficult to work efficiently and effectively as a team, catching gaps and avoiding duplication of work.