[Haskell-cafe] Readable Haskell

Ben Franksen ben.franksen at online.de
Sun Sep 20 10:34:13 UTC 2020


Am 19.09.20 um 18:22 schrieb Oliver Charles:
> For naming, I follow Chris Done's suggestion:
> https://chrisdone.com/posts/german-naming-convention/

FWIW, I tend to disagree with the position presented in this blog, at
least in the generality in which it is stated. Yes, long names can be
useful to enhance readability, but this is mostly the case when naming
highly specific things.

In Haskell, core algorithms often implement some algebraic theory. It is
not by accident that mathematical formulae, including applications in
the natural sciences, almost exclusively use single letters or very
short names. Understanding such formulae at a glance requires
familiarity with the notational conventions used, but once you have
achieved that familiarity, the short names actually /improve/
readability, assuming the naming convention is applied in a consistent
manner.

It is also no accident that formulae typically make up only a part of
the complete text. A complex formula needs to be accompanied by precise
definitions and explanations. Whenever you write code for which it is
not immediately obvious why it was done in exactly this way, add a
comment that explains what is going on!

To illustrate my point, here is some code that implements a highly
non-trivial algebra I wrote some time ago:

https://hub.darcs.net/darcs/darcs-screened/browse/src/Darcs/Patch/V3/Core.hs#176

I think using longer names in this code would be awkward and
distracting. Also note that the naming convention is documented:

https://hub.darcs.net/darcs/darcs-screened/browse/src/Darcs/Patch/V3/Core.hs#131

I even introduced operator symbols +| and -| as synonyms for some
Data.Set operations in order to make the formulae more concise (and
thus, IMHO, easier to read).

It goes without saying that you cannot read non-trivial code like this
as you read english prose. There is simply no way to understand it
without having at least a rough idea of the underlying theoretical
foundations.

Cheers
Ben



More information about the Haskell-Cafe mailing list