Chapter 6. Code Formatting, Linting, and Type Checking
This chapter covers tools you can use to help you with some of the more tedious aspects of writing code. Code formatting, linting, and type checking tools analyze your code to check for mistakes or areas for improvement. Code formatting tools concentrate on how your code looks, while linting and type checking tools ensure that your code functions correctly.
You might be wondering why the formatting of your code gets so much attention. Why does it matter what your code looks like? Why do people spend their precious time setting standards for the number of spaces around a +
sign? It’s because consistent, standardized formatting makes your code much easier to read. And, as discussed in Chapter 1, if your code is more readable it’s much more likely to get reused. Formatting tools mean that you don’t have to spend time updating the aesthetics of your code manually.
Linting and type checking tools help ensure that your code is robust. When you run Python code, syntax errors will crash your code straight away wherever they are in the script, but if you have another mistake (for example, a misspelled variable name) the error will not show up until this line. If your script takes a long time to run, this is frustrating. Linters and associated tools can help you find some of these mistakes before you run your code.
My key message in this chapter is that you should use tools rather than checking your code manually. The details of your ...
Get Software Engineering for Data Scientists now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.