Static Analysis Tools

 Introduction

This week I learned how to incorporate the usage of static analysis tools in my development process. These tools are an easy way to make sure my code is correctly formatted and free of any common or easy to miss errors. The project I have been developing ( my CLI DeadOrNot ) was written in python so I decided on using the following two tools: 

Black (Code Formatter for Python) 

Black is a python code formatter that cleans and up and makes the formatting of your code consistent. When I ran it on my two python source code files, it shortened some of the lines I had that were too long, fixed the spacing issues I had (missing spaces after an operator, leading spaces at the end of a line etc) which overall resulted in my code looking more readable. 

To set up Black, I used the python installer pip in my terminal using the command pip install black (note: Black requires python 3.6+ to work). Once Black was installed I ran it on my projects directory using the command black ../deadOrNot. This formatted both my python source code files. 

Flake8 (Linter for Python) 

Flake8 is a python linter which is a tool that allows you to catch style differences as well as simple mistakes that may not stop compilation but could lead to hard to spot bugs down the line. Using the linter on my code, I had to first configure it to ignore a few errors it would be looking for, namely the line length it opposed was shorter than the one used by Black. Once I had done that I was able to spot the mistakes in my code as they were shown to me in my terminal. I had used Flake8 before so I had been coding with what errors it would be looking for in mind so when I ran it I did not find many errors, I just needed to remove a few variables that were unused that had not removed yet. 

Setting up Flake8 was similar to setting up Black and you first need to install it using the command pip install Flake. Once Flake8 is installed you run a command similar to what was done with black. But this time you may want to you use the --ignore flag to ignore any error codes you want to ignore. The list can me found here

Setting up Black and Flake8 in VsCode

Having flake8 and Black configured to work with vscode was surprisingly straightforward. I just had to add the two extensions to visual studio code and then create settings.json file inside a .vscode folder in my projects directory. Inside the settings.json file I had to a few lines that set python code to use flake8 and Black as the defaults and I also enabled a feature that would run both flake8 and Black on python source code whenever it was saved. Finally, as I mentioned before, for this project I needed to have flake8 configured to ignore a few errors and I was able to also add that to the settings.json file. 

Setting up a Pre-commit Git Hook  

Lastly, I added a pre-commit git hook to my repo. This guide was really helpful in allowing me to do this as it provided a step by step method on how I would be able to do this for my python project. To summarize I needed to install pre-commit using pip install pre-commit and then setup a config file named .pre-commit-config.yaml with how I wanted flake8 and black to be used. Next I needed to run the command pre-commit install which would set up the git hook scripts I would need.

Once I finished I made my last commit to the branch I was working and then squashed al the previous commits I had been working on and merged to my master branch. 

Overall, I found this excerise to be very informative and I learned a lot about using tools that will really help make keeping my code simple. 

Comments

Popular posts from this blog

Adding Features and Pull Request

Planning for Release 0.4

Release 0.4 Progress - Organizing the code