Ask HN: Would it be educational to map error messages to common mistakes?

32 points by melvinroest ↗ HN
This question is just a musing, a "shower thought" if you will.

I have a JS/Python/Java background (side projects can be much lower level though such as C++ or x86-64 ;-) ). I'm currently learning Go from Learn Go With Tests [1]. And I sometimes make silly mistakes such as testAdder instead of TestAdder (lowercase t vs capital T). In that example I got back "testing: warning: no tests to run". I had to use a search engine to find out why. I don't find it a big deal, but I am simply wondering if there is a more efficient way to deal with this problem of understanding "cryptic" error messages when you're starting out in a new language.

So, might it be useful to have a site for programmers starting out in a new language that could give potential causes when a particular error message is displayed?

[1] https://quii.gitbook.io/learn-go-with-tests/go-fundamentals/...

22 comments

[ 4.9 ms ] story [ 63.5 ms ] thread
Some people believe the best place to handle this problem is the compiler or runtime themselves by generating non-cryptic error messages. I believe Raku takes this approach.
It certainly does. (Or rather Rakudo the compiler for Raku does.)

There are some for people coming from Perl.

    say $^V;

    ===SORRY!=== Error while compiling:
    Unsupported use of $^V variable; in Raku please use
    $*RAKU.version or $*RAKU.compiler.version
    ------> say $^V⏏<EOL>
Some for simple typos.

    my $morning = DateTime.now.truncated-to('day').later( :9hours );
    say $moroning;

    ===SORRY!=== Error while compiling:
    Variable '$moroning' is not declared.  Did you mean '$morning'?
    ------> say ⏏$moroning;


There are some errors that it can't find until it runs your code.

    say $var.length;

    No such method 'length' for invocant of type '...'.  Did you mean
    any of these: 'elems', 'chars', 'codes'?
(I omitted the type because it doesn't really matter.)

The reason Raku doesn't use `length` is because of the common Perl error of calling `length` on an array.

In Perl `length` is a string operation. It would turn your array into a number representing the number of elements, and get the count of characters in that. So it would give you a number representing the order of magnitude of an arrays length.

In order to catch this error, and to reduce the number of times it happens, `chars`, `codes`, and `elems` were chosen instead. (Functions in Raku are intended to do a single operation on a single type. If the argument isn't that type it often coerces into that type. `elems` for example is a list operation, so something like `Str.elems()` always returns `1`.)

Most exceptions are in https://github.com/rakudo/rakudo/blob/master/src/core.c/Exce...

I've seen a number of things that relate to this:

1. a printer troubleshooting site where the only question is "What brand/model of printer do you have?" It suggests the most common problem, and most often it's the rignt answer.

2. Sorbet is a static type checker for Ruby that will make suggestions for typo's similar to your example. It also has an autocorrect option, but use with care because sometimes it will choose the closest but completely unrelated name.

3. Error messages that are/have clickable links, which take you to a site that has entries from people who got this error and what they found caused and if known fixed it.

Woah. #3 just blew my mind. What if we had a package for every language that linked the error to the top stack overflow thread or equivalent.
I wanted to make this for myself in my terminal.

It would read error output, search in a database I have, and then output how I fixed that error last time I received it.

I am not quite sure how to intercept and inject terminal output though

I prefer we had generic search engines for the raw textual error messages. This reduces the fragility and vendor lock-in (like SO).

Which is exactly the current situatuon.

I prefer having both. Perhaps there's some opt-in/opt-out flag that allows you to not make clickable links. But having a database with most common errors sorted by programming experience in that particular language might be quite useful.

I wouldn't be surprised if it would allow a programmer -- being a beginner in a new language -- quickly find his/her mistake most of the time.

Basically this is the concept of stackoverflow. People (hopefully) post their error messages and then others answer.

That being said if it could so easily be answered, then one might wonder why it the computer doesn't fix it for you? I would suggest that there is often sufficient ambiguity about what the programmer desired that the compiler (or other tools) cannot just correct it.

There’s been several dissertations on this topic and a lot of research tools. Compiler errors are [slowly] getting better.
Could you link one or two to give us a hook?
The trade off is that sometimes only the highly technical answer can give insight, and the more experienced the developer the more useful the technical answer is.

That being said, gcc and clang have started to put in suggestion-like responses. Answers don’t have to be obscure, especially not for obscurity’s sake.

But all is not lost. You (or somebody) could write a utility that read the compiler output through a pipe and gave more verbose messages

Inform7 has very educational error messages:

(Each time Go or Replay is clicked, Inform tries to translate the source text into a working story, and updates this report.)

Problem. The sentence 'This is a book' appears to say two things are the same - I am reading 'This' and 'book' as two different things, and therefore it makes no sense to say that one is the other: it would be like saying that 'John is Paul'. It would be all right if the second thing were the name of a kind, perhaps with properties: for instance 'Abbey Road is a lighted room' says that something called Abbey Road exists and that it is a 'room', which is a kind I know about, combined with a property called 'lighted' which I also know about.

Because of this problem, the source could not be translated into a working game. (Correct the source text to remove the difficulty and click on Go once again.)

http://inform7.com/

powershell does this with the suggestions component. The easiest way to trigger it is to try to execute something in the current working directory without the ./
I don't know about other compilers, but in D, the compiler can suggest simple typos, so you'd get something like "Undefined identifier testAdder, did you mean TestAdder?".

I think such lists are very useful however. Back when I used C++, Parashift C++ FAQ https://www.parashift.com/c++-faq/ was very helpful for me. It helps with trickier parts of C++, for example the "most vexing parse" which doesn't have obvious error messages.

Ruby does similar:

Run:

class Foobar end

f = Foobaz.new

Output:

NameError (uninitialized constant Foobaz) Did you mean? Foobar

D also dispenses advice with some errors. I've mostly ran into these when accidentally doing C-style type declarations. e.g. when you try to compile

    int foo[3];
The D compiler will tell you

    Error: instead of C-style syntax, use D-style `int[3] foo`
Which makes D positively a delight to learn if you know C already.
Yes, there are some errors designed for people coming over from C. Similarly, (void)somePointer gives you an error saying "instead of C-style cast, use cast(void)somePointer instead".