simont: A picture of me in 2016 (Default)
simont ([personal profile] simont) wrote2008-11-08 04:13 pm

A modest proposal

When you compile a source file and get multiple compile errors, there are two conflicting principles governing the order in which you go through the source file fixing them.

On the one hand, you want to fix the errors from the top downwards, because of the possibility that some errors are cascades from others: fixing earlier errors may cause later ones to turn into non-errors or shed light on what the problem really was.

On the other hand, if you fix an early error in a way that changes the number of lines in the source file, then you have to mentally adjust all the line numbers in the subsequent error messages. Do this multiple times and you're looking at keeping a running track of the cumulative change to the file's line count as you edit, which you didn't really want to have to hold in your head at the same time as the more important state regarding the problems you're fixing.

The solution is simple. Line numbers in source files should be indexed backwards from the end, with line 0 being the part of the file after the final newline, line 1 the line before that, and so on. If compilers reported errors using those line numbers, and programmers' text editors displayed the same line numbers, then there would no longer be a conflict: you'd fix compile errors from the top of the file downwards, each error message would have a correct line number when you reached it, and you'd only see cascade errors after fixing the primary error that caused them.

[identity profile] feanelwa.livejournal.com 2008-11-08 04:18 pm (UTC)(link)
That's a good idea!
ext_8103: (Default)

[identity profile] ewx.livejournal.com 2008-11-08 05:07 pm (UTC)(link)
Or have the compiler/editor integration able to cope with the line number changes...
ext_8103: (Default)

[identity profile] ewx.livejournal.com 2008-11-08 05:38 pm (UTC)(link)
The relevant integration here is the editor being able to parse error messages to find the source file and line, something that's indispensable even if it doesn't perform cleverness regarding changes to the source file.
ext_8103: (Default)

[identity profile] ewx.livejournal.com 2008-11-08 05:46 pm (UTC)(link)
That's a trivial variant of getting the output of any other command into your editor - also something that's indispensable for other reasons.

[identity profile] gareth-rees.livejournal.com 2008-11-08 06:06 pm (UTC)(link)
there's at least an implicit requirement to have the compiler's error messages show up in the editor in the first place

In Emacs, you type M-x compile RET and then enter your command (typically make).

translating Windows to Unix path separators and prepending the right top-level source directory

Emacs does this automatically. And it can handle cygwin paths too using the cygwin-mount library.

(I don't want to sound like an Emacs zealot so I'll stop now...)
pm215: (Default)

[personal profile] pm215 2008-11-08 06:30 pm (UTC)(link)
Of course M-x compile doesn't work quite so well when you're running your compile on one machine and the editor on another...

[identity profile] gareth-rees.livejournal.com 2008-11-08 06:39 pm (UTC)(link)
In that case, wouldn't you type something like M-x compile RET ssh user@host make RET? (Or whatever mechanism you use to run a command remotely.) You probably also need to organize some form of path translation in this case: that's what file-name-handler-alist is for.
pm215: (Default)

[personal profile] pm215 2008-11-08 06:47 pm (UTC)(link)
The approved mechanism in this case is to run a command saying "I want an interactive session on a random machine, give me an xterm" and then do everything in that xterm or things spawned from it. sshing into the machines directly is frowned upon. I suppose one could do compiles as a batch job but that feels distressingly 1970s.

(I'm sure something is possible, it just needs more tuits than I've come up with so far.)
pm215: (Default)

[personal profile] pm215 2008-11-09 01:49 pm (UTC)(link)
The sshd idea sounds neat but also sounds like it makes it even easier to accidentally leave interactive sessions running over the weekend ;-)

[identity profile] gareth-rees.livejournal.com 2008-11-08 05:25 pm (UTC)(link)
you have to mentally adjust all the line numbers in the subsequent error messages

Or you could use a development environment which handles this book-keeping for you. This need not involve any compiler/editor integration at all. For example, Emacs has a "compilation-mode" for reading the output of compilers; for each error message in the compiler's output it creates a marker (a kind of bookmark) for the original position of the error in the file. This marker stays with the text even if you add or delete lines above or below it, so you just run the command ‘next-error’ to go to the next error without needing to do any mental adjustment. I'm sure other programmer editors do something similar.

[identity profile] gareth-rees.livejournal.com 2008-11-08 05:59 pm (UTC)(link)
You might want to have a look. Emacs went through a bit of a doldrums in the early 2000s as it took six years to bring out version 21 *. But now it seems back on track, and if you used to be a fan, but haven't used it since version 20 I think you may be pleasantly surprised with version 22.

* Native windowing system integration was pretty tough, but I think the big problem was Unicode. Emacs was a victim of being a first mover here: in the 1990s it had adopted its own system (MULE) for integration of multilingual character sets, so when Unicode turned out to be the standard way to achieve this, MULE had to be backed out while the rather complex interface remained backwards compatible to avoid breaking a vast installed codebase.
ext_78: A picture of a plush animal. It looks a bit like a cross between a duck and a platypus. (Default)

[identity profile] pne.livejournal.com 2008-11-08 08:16 pm (UTC)(link)
Jed's syntax highlighting has got noticeably worse

That must have taken some special effort.

Do you really mean, worse in absolute? Or merely "not as good as what other programs offer these days"?

If the former, I wonder what made people take something that works and make it worse.

[identity profile] geekette8.livejournal.com 2008-11-08 09:42 pm (UTC)(link)
Yep, even Visual Studio manages this. Something like Alt-F4 (or just F4 maybe, I can't remember) moves to the next error.

[identity profile] samholloway.livejournal.com 2008-11-10 05:41 pm (UTC)(link)
VS seems to track the changes very well. If you click on an error message, you nearly always get taken to the correct line, even after having made substantial changes elsewhere in the file. That way, the line numbers become quite irrelevant. VS only gets really confused if you actually delete the line that the error itself was on.
andrewducker: (Default)

[personal profile] andrewducker 2008-11-25 12:49 pm (UTC)(link)
F8. Alt-F4 is "close window" and F4 is "Properties".

And yeah, it's very nice.

And if it starts to lose where the errors are then I just hit compile again - even with reasonably large systems it's only 30 seconds to do a build.

[identity profile] reenigne.livejournal.com 2008-11-08 08:21 pm (UTC)(link)
Another way might be for an editor to have two sets of line numbers - the normal set plus a set which is "the line this text was on the last time it was loaded or renumbered". Then you can fix the errors in whatever order you like, and only the editors need to change. Navigating could get confusing if fixing early errors entailed moving lots of code around, but that's a problem with negative numbering too.

The old DOS assembler A86 had a rather different way of solving this problem - it injected its error messages right into the source files! It actually worked surprisingly well, but it would probably cause too much confusion these days.
gerald_duck: (quack)

[personal profile] gerald_duck 2008-11-09 06:25 pm (UTC)(link)
Agreed about editors having a mode where line numbers don't change until you hit the magic button. That would be potentially very useful, while probably not too much trouble to implement.

[identity profile] cartesiandaemon.livejournal.com 2008-11-09 01:13 am (UTC)(link)
Now I find myself using "a modest proposal" in some kind of meta-sarcastic way, for things that ought to actually be modest, but turn out to be a giant hurdle... :)

[identity profile] cartesiandaemon.livejournal.com 2008-11-09 12:27 pm (UTC)(link)
:) Yeah, I can't remember when I actually looked up the original, I know osmosis had filled the phrase in (complete with nuance) earlier on :)

Although when I use it in mostly the original sense, it actually tends to be something like how I read this post -- a nominal solution, which sounds completely ridiculous, and you describe as a "modest proposal" to make it clear you're not yet advocating it, yet you can't help but wonder if maybe it _would_ be sensible after all.

[identity profile] cartesiandaemon.livejournal.com 2008-11-10 01:17 pm (UTC)(link)
LOL. Yeah. I certainly enjoy living on that edge -- I like to measure the success of a joke by the length of time before someone laughs, assuming they eventually do :)

But I have to admit I also use irony in situations like this as a defense; if I come up with a wacky idea, I'll phrase it as a "modest proposal" in order to raise it, but not feel the need to justify it. Which is useful, if it invites some objective consideration of the problem and potential solution, but is also questionable, in that it would be more honest to simply say "I had this idea, is there any merit, or is it _just_ silly?" :)
gerald_duck: (ascii)

[personal profile] gerald_duck 2008-11-09 03:54 pm (UTC)(link)
Unfortunately, I tend to fix the lexically later errors first, because I can't be bothered with scrollback. Only if something non-trivial is broken do I go back and start from the top.
gerald_duck: (duck and computer)

[personal profile] gerald_duck 2008-11-09 06:23 pm (UTC)(link)
…though I now realise the lexically earlier errors could be printed last as a further convenience. It would involve the compiler in some tedious cacheing of error messages, but it's going to have to do that anyway, because it doesn't know the line numbers for any of the errors until it's finished. The alternative would be to cache the input file or make two passes, but hopefully the volume of errors emitted is less than the size of the source file. (Until you screw up entertainingly with C++, at any rate.)

[identity profile] naath.livejournal.com 2008-11-10 01:48 pm (UTC)(link)
What, you don't recompile (or attempt to) every time you fix an error? (this being my standard approach to "all the errors will have moved now and half of them were probably cascades from that one and I can't be bothered to look at them all")

[identity profile] cartesiandaemon.livejournal.com 2008-11-11 02:39 pm (UTC)(link)
an undeclared hippo here, a wombat of the wrong type there, an elephant passed to a function that expected a gibbon elsewhere

LOL. I love your metaphor. It sounds like line-numbers are the least of the problems with your errors, though :)