Skip to content

Commit a254639

Browse files
committed
a draft blog post about error message style guides
1 parent 5118609 commit a254639

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
.. title: Error Message Style Guides of Various Languages
2+
.. slug: error-message-style-guides
3+
.. date: 2021-12-10 16: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 a
16+
long time, and thankfully CPython has made an enormous push to `improve its
17+
errors in the last 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+
.. raw:: html
23+
24+
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Is there any programming language that has a style guide for its error messages? <a href="https://twitter.com/ShriramKMurthi?ref_src=twsrc%5Etfw">@ShriramKMurthi</a>, do I remember correctly that Pyret has one?</p>&mdash; Carl Friedrich Bolz-Tereick (@cfbolz) <a href="https://twitter.com/cfbolz/status/1466033151315173384?ref_src=twsrc%5Etfw">December 1, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
25+
26+
Wonderfully, people answered me with lots of helpful links (full list at the
27+
end of the post), thank you everybody! All those sources are very interesting
28+
and contain many great points, I recommend reading them directly! In the
29+
following I'm, going to try to summarize some common themes or topics that I found
30+
particularly interesting.
31+
32+
Several guides point out a 80/20 rule: 80% of the time an error message is
33+
read, the developers knows that message well and knows exactly what to do. For
34+
this use case it's important that the message is short. On the other end of the
35+
spectrum, 20% of the time a developer gets a message that they have never seen
36+
before and are confused and so the message needs to contain enough information
37+
to allow them to find out what is going on [Flix, Flow].
38+
39+
Almost all guides stress the need for plain and simple English, as well as
40+
conciseness and clarity [Flix, Racket, Rust, Flow]. Flow suggests to put coding
41+
effort into making the grammar correct, for example in the case of plurals or
42+
to distinguish between "a" and "an".
43+
44+
The suggested tone should be friendly and neutral, the messages should not
45+
blame the Programmer [Flow]. Rust and Flix suggest to not use the term
46+
'illegal' and use something like 'invalid' instead.
47+
48+
Flow suggests to avoid "compiler speak". For example terms like 'token' and
49+
'identifier' should be avoided and terms that are more familiar to programmers
50+
be used (eg "name" is better). The Racket guide goes further and has a list of
51+
allowed technical terms and some prohibited terms.
52+
53+
The Racket guide proposes to use the following general structure for errors:
54+
'State the constraint that was violated ("expected a"), followed by what was
55+
found instead.'
56+
57+
The Rust guides says to avoid "Did you mean?" and questions in general, and
58+
wants the compiler to instead be explicit why something was suggested. The
59+
example the Rust guide gives is: '"Compare "did you mean: Foo" vs. "there is a
60+
struct with a similar name: Foo"."'. Racket goes further and forbids
61+
suggestions altogether because "Students will follow well‐meaning‐but‐wrong
62+
advice uncritically, if only because they have no reason to doubt the
63+
authoritative voice of the tool."
64+
65+
The Rust guide suggests to put all identifiers into backticks, the Flow goes
66+
further and uses Markdown for the error message.
67+
68+
The Clang, Flow and Rust guides point out the importance of using precise
69+
source code spans to point to errors, which is especially important if the
70+
compiler information is used in the context of an IDE to show a red squiggly
71+
underline. The spans should be as small as possible to point out the source of
72+
the error [Flow].
73+
74+
I don't really have a conclusion! I wonder whether it would makes sense for
75+
Python to adopt a (probably minimal, to get started) subset of some of these
76+
ideas as guidelines for its own errors.
77+
78+
79+
Sources:
80+
81+
* Rust: https://rustc-dev-guide.rust-lang.org/diagnostics.html
82+
83+
* Clang: https://clang.llvm.org/diagnostics.html
84+
85+
* Flix: https://flix.dev/principles/
86+
87+
* Racket: https://cs.brown.edu/~kfisler/Misc/error-msg-guidelines-racket-studlangs.pdf
88+
89+
* More about the research that lead to the Racket guidelines (including the referenced papers): https://twitter.com/ShriramKMurthi/status/1451688982761381892
90+
91+
* Flow: https://calebmer.com/2019/07/01/writing-good-compiler-error-messages.html
92+
93+
* Elm: https://elm-lang.org/news/compiler-errors-for-humans
94+
95+
* Elm's error message catalog: https://github.com/elm/error-message-catalog
96+
97+
* Reason: https://reasonml.github.io/blog/2017/08/25/way-nicer-error-messages.html
98+
99+
100+

0 commit comments

Comments
 (0)