GIVE ME MY STATIC ANALYSIS!

In which I lament my own utter incompetence, but also switch back from VS Code to PHPStorm in a flash and cry over programming languages

I’ve been spoiled. Yes. I admit it. Early in my coding career I got to use SPARK Ada and the SPARK Examiner and it left its mark.

Ada, for those that don’t know, is a curious programming language. Pascal-like, clunky, verbose, typed. But I was brought up on Pascal so I got on with it quite nicely.

SPARK Ada is a subset of Ada that removes, well, all the dangerous stuff. It’s a subset that lets you prove properties about your code, such as whether or not you it will go beyond the bounds of an array, or whether you have loop invariants. And all before you even run it through a compiler.

This checking is called “static analysis”.  Remember that – you’ll need it later!

The rules of SPARK make coding in it even more verbose than with standard Ada. It’s strongly typed so all type conversions are explicit. Arrays have fixed types and bounds. All variables and data structures are of a known size.

BUT…it gloriously eliminated huge swathes of potential run-time error conditions without even needing to write tests.

Web development

Ada, as far as I know, isn’t used on the web (someone will prove me wrong, I’m sure). I mostly write PHP and JavaScript which, if you do things wrong, have a habit of making it up as you go along.

I’ve used various text editors and IDE’s in my web development career.  Notepad++, Aptana Studio, Sublime Text, and more recently PHPStorm and VS Code.  VS Code recently replaced PHPStorm as the main tool I use for coding small projects.

I have a number of PHP extensions installed in VS Code, including Intelliphense, and I somehow thought that the editor and extensions would be clever enough to at least let me know if a variable I was using was undeclared or something.

But…well…here’s some code I wrote today in VS Code:

Code snipper in VS code with a subtle error

Spot the deliberate error. It’s subtle. It’s on the second line.

This code actually worked with a human-initiated “integration test” on the front end. This is because of a WordPress quirk whereby, further down the stack, get_post( null ) actually returns the current post, and it just so happened that the only place I called this method was in some code inside a loop where there was a current post, and the current post’s ID was always the same as the $related_to_id.

Anyway – I blame my text editor for this. How is an error of this sort not reported by my text editor, or by PHP, or anywhere?!  This is exactly the sort of thing that a computer is really good at detecting, but a human is not (unless you are Tonya – THANK YOU!)

And so I discovered that I kinda take static analysis for granted, and miss it when it’s not there.

After some research, it seems that setting up useful static analysis in VS Code is non-trivial (again, correct me if I’m wrong). But PHPStorm has some of this baked right into it’s core.

It’s not SPARK Ada-level static analysis. But this is my computer doing what it’s best at: automating stuff that humans are bad at.

Code snippet in PHPStorm with the same error, but with the error underlined!

And so I’ve gone running back to it.