Code linters & formatters:
What are they and should you use them? (I think so)
4 min read
Save brainpower! Use coding tools.
I recently installed Black, an opinionated code formatter for Python, and took interest in how it changed my code. Then I noticed how Prettier, my other code formatter, alters my other code. It added things I forgot, like semi-colons. I remembered how many frustrating hours I had spent chasing down indentation errors (for Python) or missing semi-colons (for several other languages). It made me wonder why it isn't normal to get people using tools early on to take care of these details?
The tools I use now save me time. They save a lot of thinking too. We have a limited amount of mental resources, "The problem is that excessive focus exhausts the focus circuits in your brain. It can drain your energy and make you lose self-control". It's good to conserve our mental abilities for the most vital tasks. If you're learning to code your best use of focus is learning the concepts that will help you solve problems in the future. I think avoiding getting bogged down in certain details early on can be helpful.
What are code linters and formatters?
Linters get their name from an old tool called "lint" developed by Stephen C. Johnson in 1978 while he worked at Bell Labs. It was used to analyze C source code. I know because I read it here (By the way, if you want to nerd out on some CS history check out Bell Labs Early Contributions to Computer Science). Linters do things like check for syntax errors, make suggestions for adhering to code standards, security checks, and point out other potential problems. Not all linters do the same thing. I didn't know what linters did but I initially tried them out cause I saw them suggested many times. Sometimes when I couldn't get something working I eventually checked the messages from the linter and would google the messages I saw there. This often helped me figure out what was wrong. The downside is that they "report the problems, but do not fix them".
Which is where code formatters can help. These opinionated tools your code to conform to a coding style and save you from common errors. Another benefit is you don't have to think about your coding style. Here's a quote from Prettier's website, "What usually happens once people are using Prettier is that they realize that they actually spend a lot of time and mental energy formatting their code". That's time and energy that could be spent learning and solving problems.
Implementing both of these tools can help. Linters highlight potential issues while a formatter can make some changes saving you time and mental energy. Linters can also flag up code quality issues that might cause bugs but a code formatter won't fix for you.
But which ones should you use?
Developers can be very opinionated about their tools. If you already have a toolset, I'm not trying to change your mind. If you don't, I am suggesting these can help you. There is still the problem of the cognitive load of setting up these tools (which run right back into the problem I'm hoping to minimize). BUT I believe in the long term you'll benefit. If you would like some help exploring and setting up these tools I am happy to help. Feel free to hit me up on Twitter.
In the meantime, I'll share some of the things I'm using (the ones I remember right now anyways). I'm thinking of doing some content related to setting this up from scratch. If that would be helpful, let me know.
What I currently use
Here is some of what I have set up currently. Prettier, which I have set up in Visual Studio Code. It can support several things except for Python which I also use (or at least try to?). For that I have Black set up. I also recently set up tabnine marketed as an AI assistant for code completion. I do like it, though occasionally I accidentally put something in I didn't mean to when I don't pay enough attention to the suggestions.
There are many options, what I use may not be best for you. If you work with a certain language do a little research and find out what the most common tools are. They likely have wide adoption for good reason. By no means am I trying to suggest that using these tools will make learning easy! But I am starting to think they could help you focus on the important concepts. This doesn't take care of silly typos, my nemesis, but that's a problem for another day. But it does remind me, I should go throw this in Grammarly before I think about posting it...