Come On, Lint A Little: Cleaning Up Your Code With Linters

Jacob Crabb
Towards Data Science
3 min readOct 30, 2019

--

You know what they say, “It’s what’s on the inside that counts.” While this may be true, you still have to find a way to express what’s inside so that people can understand it. You might have the most functional piece of code you’ve ever written. It might be elegant, thorough, and foolproof. But it still looks like this:

def buy_me_dinner():
"lala"
print("yummy dinner")
def attack():
"oh"
print("I love you" to)
from fileinput import input

suspects = input()[9:].split(',')
weapons = input()[8:].split(',')
rooms = input()[6:].split(',')
clues = input()
for line in input():
clue = line.split(' ')
if clue[1] != 'the':
suspects.remove(" ".join(clue[1:]))


else:
thing = " ".join(clue[2:])
if thing in weapons:
weapons.remove(thing)


else:
rooms.remove(thing)
print(suspects[0])
print("in the " + rooms[0])
print("with the " + weapons[0])

This is where a linter will come in handy. A linter is a tool that analyzes code to flag style or programming errors. The linter I’m using today is pycodestyle, and it’s based on Python’s Pep-8 style guide. Linter’s will give you a list of the errors found so that you can fix them one by one, and hopefully learn how to write code that betters adheres to your chosen style guide in the future. There are other linters like Black, which reformat your code automatically, but you don’t learn anything that way, so when starting out with linters it’s better to use something verbose.

Let’s see what happens when we lint the above code with pycodestyle. Once you have it installed, simply go to your terminal and type

pycodestyle <name of file to lint>

And you’ll get back something like this:

messy.py:4:1: E302 expected 2 blank lines, found 0
messy.py:4:14: W291 trailing whitespace
messy.py:13:1: E303 too many blank lines (6)
messy.py:13:1: E402 module level import not at top of file
messy.py:14:1: W293 blank line contains whitespace
messy.py:15:6: E111 indentation is not a multiple of four
messy.py:15:6: E113 unexpected indentation
messy.py:16:2: E901 IndentationError: unindent does not match any outer indentation level

So the 1st character in line 4 is having an issue with the number of blank lines, then the 14th character has trailing whitespace after, and so on. What does the code look like after we’ve taken its recommendations? Funnily enough, sometimes fixing one error can reveal another error. But eventually we end up with something like this:

Just by looking at it you can tell something about the way Pep-8 says to format code. There should be 2 blank spaces after importing libraries, 2 blank spaces after declaring a function, and a new line at the end of a file. These linters aren’t perfect though, and sometimes you will find style errors that they just don’t pick up on. Also, pycodestyle doesn’t pick up on functionality errors. But I can check this code with mypy for certain types of errors. mypy is used in the same way.

mypy <name of file to check>

And it will check for errors. For the above code, it returned

messy.py:12: error: invalid syntax

Which is telling me about the syntax error in line 11 inside of the print statement. Using simple tools like these can make a huge difference in the way your code looks, and reduce frustration when debugging. I’ve heard of managers who won’t even look at code that hasn’t been linted, so it’s a good habit to get into, and aside from that it’s a great way to learn how your code should look, and what areas you make mistakes in most often.

Happy coding adventures!

--

--

Just a data engineer trying to be something more than he was yesterday. I like puns, helping people, and learning new things.