Navigation by Compile Error

Where is the mistake in this code:

The problem is that x is declared as an integer, but used as if it were a string. But my question was, where is the mistake? Is the mistake that x is declared as an integer, or is it a mistake that x is used as a string? A compiler would probably chose to highlight line 2 as the problem (possibly as a warning in this case).

Compile errors are usually conflicts between two different assertions in the code. “Undefined field”? – it’s a conflict between using a field and not declaring it. You can either solve it by adding the field, if you intended it to be there, or by not using it, perhaps if you intended to use a different field.

The problem is that compilers almost always only error on one side of the conflict. This is sensible in some ways, but inconvenient in other ways.

I have a habit of using compile errors as a means of searching through code. If I need an extra parameter in the function I’m working on, I simply add it. The compiler will automatically “find” all the places where the function is called with one less value than it should be, and takes me to those places to modify the code. In other words, I’m relying on the compiler to tell me where I need to change the code.

And there’s the problem with only getting an error on one side of a conflict: it only takes you to where you want to go half the time. If I want to add an argument to a function I’m calling, the error will be on the code I’ve just written rather than on the function declaration I need to add the parameter to. Likewise if I use an undefined field or function of an object, it would be nice if the error took me to the class declaration so I can change the class.

Yes, there are ambiguities in deciding where exactly the other side of the conflict is. Is it the base class? Which overload of the function do we go to? Where would the variable be declared? But I think a simple approach would be for the compiler to present a few of the top candidates and list them along side the error. The programmer could click to go to the code location, and perhaps the IDE would even perform the obvious action at the same time, like adding in the parameter or field at that location.

Would you find this useful?

Leave a Reply