Open ermalgashi opened 2 years ago
Very good question.
Linting and formatting are one of those things that I think you should get very familiar with early on in your career, because they'll just save you so much trouble. A lot of developers in the industry completely ignore formatters and linters, and they are constantly having to deal with minor style changes and tiny fixes in code review that they could have caught way earlier and automatically using these tools.
The other major advantage of these tools is that it makes your code look and feel the same everywhere, which is a huge boon when you're adding new developers, hiring, etc. When all your code looks and feels the same, it's much easier to read and get familiar and learn your code base, which will help junior and senior devs alike.
For linting, the major tools are flake8, autopep8, and pylint.
For formatting, I can't really recommend anything other than black. It's very opinionated, so you don't have to make any decisions about it, and it'll "just work". It'll also keep your git diff
entries clean, which will be helpful in the long run.
So after we initialize the project, we want to initialize these tools (one of the linters, and black) in the codebase, commit the configuration files, and run these formatters. After that, I'll show you pre-commit
, a way to automagically run these tools whenever you run git commit
so you don't ever have to run them manually.
Also, once you have them set up, we can set up your code editor to do highlighting and auto-formatting, so you can see the recommendations in your editor before you even save the file.
So to get this out of the way, the linters such as flake8 are suggestion and shows when you are not using a variable or you are having extra white space. The fixer such as black, helps us format or style checker the code to look more standard and to be more readable in the sense that formats the code in the pep8 style guide.
So in short flake8 calls you out on format problems, and black formats automatically.
What I don't know right now is how to initialize the project with the tools.
You got it.
We'll do the initialization for this stuff after we set up the virtual environments, because they'll need to be installed in there. But basically you just install the tool, create a config file depending on what the docs say, and that's it! then you can just run the flake8 command on the terminal, or we'll hook it into pre-commit
, and/or hook it up to your editor and it'll be automatic. Should be no problem, but we'll tackle that after virtual environment stuff.
So configuring flake8 and code formating with black, now what is left to do is install pre-commit
and configure, or maybe we take the route take the step to configure our code formating and styling directly in the editor?
Adding pre-commit
,
Our process was not as simple, the guide was at https://bit.ly/3GADNAI was pretty detailed but rushing to get it done I forgot to execute the fourth step. The pre-commit install command, so after some more research and a youtube video I saw that command being executed and had confirmed it with the guide I was back on track. So now before committing any changes based on our configuration files such as .pre-commit-config.yaml have the Git-Hook pre-commit that checks for any changes and another style formatting.
How do we start code formating and linting to use industry standards, do we use Flake8Black, or would we use something different?!