– What is static program analysis ?

Static program analysis allows the gathering of informations about the execution behaviour of your code without actually executing it. It is the opposite of dynamic program analysis (like debugging) which required the code to be executed.

– Ok! But why should I use this in practice ?

  1. To save time by suppressing the save/execute cycles induced by syntax errors (missing “;”, function or variable not initialized, typos, …). Correcting these errors at the debugging step (dynamic analysis) requires that you write, save, (re)compile or (re)execute your program multiple times.
  2. To gain in performance by identifying libraries/variables/functions that are not initialized properly or not use at all. A must, for self-taught programmers or for those who do not have a lot of coding experience.
  3. To gain in readability by following some well established stylistic rules developed by a group of programmers. These rules are a standard to adopt. Quite helpful to learn a new language!

– Ok, I’m interested, how does it work?

It depends on your programming preferences. Do you write your code in a text editor or in an integrated development environment (IDE)?

Static analysis tools are already integrated in most of the IDE. Or language specific plugins are available for you to install (for example, PyDev for python in Eclipse). Those tools will directly highlight hence identified errors in your code (it works like a spell-checker).

For those of you who prefer simpler text editor (like me!), small programs (namely lint or linter) can be added to your favorite editor
(emacs, vim, sublime text, …). Here, your errors are also highlighted in your code. A quick look at your program is all is needed to spot them and correct them quickly.

Errors are in red, warnings in yellow

Errors are in red (Undifined name ‘Tot’) and the warnings are in yellow (imported but unused).

If there is no linter available for your text editor, you can install one and execute it in the console. A list of errors and warnings will appear in the console; it is less convenient but better than nothing (faster and better than classic debugging).

flake8

If you’re not sure which solution to choose, I recommend that you try Sublime Text with the package sublimeLinter. It’s really easy to use and very efficient. It also has a package manager which facilitates the installation of the linter.
Get yourself started with Sublime Text here.