|
| 1 | +.. title: Error Message Style Guides of Various Languages |
| 2 | +.. slug: error-message-style-guides |
| 3 | +.. date: 2021-12-05 14:00:00 UTC |
| 4 | +.. tags: |
| 5 | +.. category: |
| 6 | +.. link: |
| 7 | +.. description: |
| 8 | +.. type: rest |
| 9 | +.. author: Carl Friedrich Bolz-Tereick |
| 10 | + |
| 11 | +================================================ |
| 12 | +Error Message Style Guides of Various Languages |
| 13 | +================================================ |
| 14 | + |
| 15 | +PyPy has been trying to produce good `SyntaxErrors`_ and `other`_ `errors`_ for |
| 16 | +a long time. CPython has also made an enormous push to `improve its |
| 17 | +SyntaxErrors in the last few releases`_. These improvements are great, but the process |
| 18 | +feels somewhat arbitrary sometimes. To see what other languages are doing, I |
| 19 | +`asked people on Twitter`_ whether they know of error message style guides for |
| 20 | +other programming languages. |
| 21 | + |
| 22 | +.. _`SyntaxErrors`: https://www.pypy.org/posts/2018/04/improving-syntaxerror-in-pypy-5733639208090522433.html |
| 23 | +.. _`other`: https://twitter.com/cfbolz/status/783313503230844929/photo/1 |
| 24 | +.. _`errors`: https://twitter.com/pypyproject/status/999930324481081344 |
| 25 | +.. _`improve its SyntaxErrors in the last releases`: https://docs.python.org/3/whatsnew/3.10.html#better-error-messages |
| 26 | +.. _`asked people on Twitter`: https://twitter.com/cfbolz/status/1466033151315173384 |
| 27 | + |
| 28 | +Wonderfully, people answered me with lots of helpful links (`full list`_ at the |
| 29 | +end of the post), thank you everybody! All those sources are very interesting |
| 30 | +and contain many great points, I recommend reading them directly! In this |
| 31 | +post, I'll try to summarize some common themes or topics that I thought were |
| 32 | +particularly interesting. |
| 33 | + |
| 34 | +Language Use |
| 35 | +------------ |
| 36 | + |
| 37 | +Almost all guides stress the need for plain and simple English, as well as |
| 38 | +conciseness and clarity [Flix, Racket, Rust, Flow]. Flow suggests to put coding |
| 39 | +effort into making the grammar correct, for example in the case of plurals or |
| 40 | +to distinguish between "a" and "an". |
| 41 | + |
| 42 | +The suggested tone should be friendly and neutral, the messages should not |
| 43 | +blame the Programmer [Flow]. Rust and Flix suggest to not use the term |
| 44 | +'illegal' and use something like 'invalid' instead. |
| 45 | + |
| 46 | +Flow suggests to avoid "compiler speak". For example terms like 'token' and |
| 47 | +'identifier' should be avoided and terms that are more familiar to programmers |
| 48 | +be used (eg "name" is better). The Racket guide goes further and has a list of |
| 49 | +allowed technical terms and some prohibited terms. |
| 50 | + |
| 51 | +Structure |
| 52 | +--------- |
| 53 | + |
| 54 | +Several guides (such as Flix and Flow) point out a 80/20 rule: 80% of the times an error message is |
| 55 | +read, the developer knows that message well and knows exactly what to do. For |
| 56 | +this use case it's important that the message is short. On the other hand, 20% |
| 57 | +of the times this same message will have to be understood by a developer who |
| 58 | +has never seen it before and is confused, and so the message needs to contain |
| 59 | +enough information |
| 60 | +to allow them to find out what is going on. So the error message needs to strike |
| 61 | +a balance between brevity and clarity. |
| 62 | + |
| 63 | +The Racket guide proposes to use the following general structure for errors: |
| 64 | +'State the constraint that was violated ("expected a"), followed by what was |
| 65 | +found instead.' |
| 66 | + |
| 67 | +The Rust guides says to avoid "Did you mean?" and questions in general, and |
| 68 | +wants the compiler to instead be explicit about why something was suggested. The |
| 69 | +example the Rust guide gives is: 'Compare "did you mean: Foo" vs. "there is a |
| 70 | +struct with a similar name: Foo".' Racket goes further and forbids |
| 71 | +suggestions altogether because "Students will follow well‐meaning‐but‐wrong |
| 72 | +advice uncritically, if only because they have no reason to doubt the |
| 73 | +authoritative voice of the tool." |
| 74 | + |
| 75 | +Formatting and Source Positions |
| 76 | +------------------------------- |
| 77 | + |
| 78 | +The Rust guide suggests to put all identifiers into backticks (like in |
| 79 | +Markdown), Flow formats the error messages using full Markdown. |
| 80 | + |
| 81 | +The Clang, Flow and Rust guides point out the importance of using precise |
| 82 | +source code spans to point to errors, which is especially important if the |
| 83 | +compiler information is used in the context of an IDE to show a red squiggly |
| 84 | +underline or some other highlighting. The spans should be as small as possible to point out the source of |
| 85 | +the error [Flow]. |
| 86 | + |
| 87 | +Conclusion |
| 88 | +---------- |
| 89 | + |
| 90 | +I am quite impressed how advanced and well-thought out the approaches are. I wonder whether it would makes sense for |
| 91 | +Python to adopt a (probably minimal, to get started) subset of these ideas as guidelines for its own errors. |
| 92 | + |
| 93 | +.. _`full list`: |
| 94 | + |
| 95 | +Sources |
| 96 | +------- |
| 97 | + |
| 98 | +* Rust: https://rustc-dev-guide.rust-lang.org/diagnostics.html |
| 99 | + |
| 100 | +* Clang: https://clang.llvm.org/diagnostics.html |
| 101 | + |
| 102 | +* Flix: https://flix.dev/principles/ |
| 103 | + |
| 104 | +* Racket: https://cs.brown.edu/~kfisler/Misc/error-msg-guidelines-racket-studlangs.pdf |
| 105 | + |
| 106 | +* More about the research that lead to the Racket guidelines (including the referenced papers): https://twitter.com/ShriramKMurthi/status/1451688982761381892 |
| 107 | + |
| 108 | +* Flow: https://calebmer.com/2019/07/01/writing-good-compiler-error-messages.html |
| 109 | + |
| 110 | +* Elm: https://elm-lang.org/news/compiler-errors-for-humans |
| 111 | + |
| 112 | +* Elm's error message catalog: https://github.com/elm/error-message-catalog |
| 113 | + |
| 114 | +* Reason: https://reasonml.github.io/blog/2017/08/25/way-nicer-error-messages.html |
0 commit comments