The goal of static code analysis is to have a positive expected return.
- Using static code analysis tools requires effort (or other cost).
- Dealing with bugs also costs effort (or other cost).
- Finding and fixing bugs before they become a problem typically requires less effort (or other cost).
Thus, using these tools is worth it if the cost of using them is outweighed by the expected reduction in cost from bugs that slip through:
cost of doing QA + expected cost fixing bugs early < expected cost of dealing with bug later
where the expected cost is likelihood × impact.
Tools such as Pylint have low cost, mostly just creating a config file that disables warnings that you are not interested in. However, Pylint has rarely helped me find immediate problems.
Tools such as Mypy have considerable cost because it can only work if you add useful type annotations throughout your code. And as you've experienced, these type annotations don't always harmonize well with more dynamic programming techniques such as attrs
. However, type checking has helped me find many problems up front.
It is not so easy though, because these tools have second-order effects.
- For example, Pylint will complain if you have a function with lots of local variables. That is not a bug. But it indicates complexity, and bugs love to hide within complexity. Pushing you towards simpler code has value.
- Mypy is probably even more useful, since it encourages you to add type annotations throughout your code. This can make developers much more productive, in particular by enabling extremely useful IDE features such as precise go-to-definition or Intellisense-style type-based autocomplete. Being forced to think about types can also help design clearer interfaces, for example by clarifying whether a function can take only int or float arguments or both.
Thus, these tools don't just have value by pointing you towards bugs, they also have value by encouraging a certain programming style and helping you to think about the design of the software more consciously.
In this context, it's worth pointing out that very useful libraries like attrs
are not only useful, they also make the use of other tools more difficult. By the same logic with which Mypy might be not worth it, attrs might be not worth it either.
In practice, it is not possible to figure out precisely whether using a particular tool is worth it for a given project. It is not realistically possible to provide a useful estimate of expected costs of dealing with a bug now or later (though empirical evidence suggests dealing with bugs early is substantially cheaper). A lot of this comes down to your particular subjective preferences.
- It is perfectly fine to decide that using these tools is not worth it, especially if you are dealing with a legacy code base that would produce a large amount of false positives. The same effort might be better invested in other QA measures, such as code reviews or better tests.
- It is also perfectly fine to decide that these tools are worth it, even if it requires substantial changes to your code.
A lot of this depends on the context of the software – a Jupyter notebook with an explorative data science session will have completely different quality requirements from a business-critical financial system. There is no one-size-fits-all approach.