<div dir="ltr">Hi!<div><br></div><div>I found myself exploring new parts of the GHC code base the last few weeks (exciting!), which again reminded me of my biggest frustration when working on GHC: the lack of per-function/type (Haddock) comments.</div>

<div><br></div><div>GHC code is sometimes commented with "notes", which are great but tend to (1) mostly cover the exceptional cases and (2) talk about the implementation of a function, not how a caller might use it or why.</div>

<div><br></div><div>Lack of documentation, in GHC and other software projects, usually has (at least) two causes:</div><div><ul><li>Programmers comment code they think is "complex enough to warrant a comment". The problem is that the author is usually a poor judge of what's complex enough, because he/she is too familiar with the code and tends to under-document code when following this principle.</li>

<li>Documenting is boring and tends to have little benefit the person writing to documentation. Given lack of incentives we tend to document less than we ought to.<br></li></ul></div><div>I've only seen one successful way to combat the lack of documentation that stems from the above: have the project's style guide mandate that top-level functions and types (or at least those that are exported) have documentation. This works well at Google.</div>

<div><br></div><div>Anecdote: we have one code base inside Google that was until recently exempt from this rule and documentation is almost completely absent in that code base, even though hundreds of engineers work on and need to understand it every day. This breeds institutional knowledge problems i.e. if the author of a core piece of code leaves, lots of knowledge is lost.</div>

<div><br></div><div><b>Proposal: </b>I propose that we require that new top-level functions and types have Haddock comments, even if they start out as a single, humble sentence.</div><div><br></div><div>I've found that putting even that one sentence (1) helps new users and (2) establishes a place for improvements to be made. There's a strong "broken window" effect to lack of comments, in that lack of comments breeds more lack of comments as developers follow established practices.</div>

<div><br></div><div>We should add this requirement to the style guide. Having it as a written down policy tends to prevent having to re-hash the whole argument about documentation over and over again. This has also helped us a lot at Google, because programmers can spend endless amount of time arguing about comments, placement of curly braces, etc. and having a written policy helps cut down on that.</div>

<div><br></div><div>To give an idea of how to write good comments, here are two examples of undocumented code I ran into in GHC and how better comments would have helped.<br></div><div><br></div><div><b>First example</b></div>

<div>In compiler/nativeGen/X86/Instr.hs there's a (local) function called mkRUR, which is a helper function use when computing instruction register usage.</div><div><br></div><div>The first question that I asked upon seeing uses of that function was "what does RUR stand for?" Given the context the function is in, I guessed it stands for read-update-read, because R is used to mean "read" in the enclosing function and "updating" is related to "reading" so that must be what U stands for. It turns out that it stands for RegUsageReadonly. Here's a comment that would have captured, in a single sentence, what this function is for:</div>

<div><br></div><div><font face="courier new, monospace">    -- | Create register usage info for instruction that only</font></div><div><font face="courier new, monospace">    -- reads registers.</font></div><div><font face="courier new, monospace">    mkRUR src = src' `seq` RU src' []</font><br>

</div><div><font face="courier new, monospace">        where src' = filter (interesting platform) src<br></font></div><div><br></div><div>That already a big improvement. A note about the register filtering, which means that not all registers you pass to the function will be recorded as being read in the end, could also be useful.</div>

<div><br></div><div>Aside: providing a type signature, which would have made it clear that the return type is RU, might also have helped in this particular case.</div><div><br></div><div><b>Second example</b></div><div>In the same file there a function called x86_regUsageOfInstr. It's the function that encloses the local function mkRUR above.</div>

<div><br></div><div>I could figure out that this function has something to do with register usage, of the instruction passed as an argument, and that register usage is important for the register allocator. However, trying to understand in more detail what that meant was more of challenge than it needed to be. First, a comment more clearly explaining what computing register usage means in practice would be helpful:</div>

<div><br></div><font face="courier new, monospace">    -- | Returns which registers are read and written by this </font><div><font face="courier new, monospace">    -- instruction, as a (read, written) pair. This info is used</font></div>

<div><font face="courier new, monospace">    -- by the register allocator.<br></font><div><font face="courier new, monospace">    x86_regUsageOfInstr :: Platform -> Instr -> RegUsage</font></div><div><br></div><div>

The reason mentioning that the return value is essentially a (read, written) pair is helpful is because the body of the function a big case statement full of lines like this one:</div></div><div><br></div><div><font face="courier new, monospace">    GCMP _ src1 src2 -> mkRUR [src1,src2]<br>

</font></div><div><font face="courier new, monospace">    ...</font></div><div><font face="courier new, monospace">    FDIV _ src  dst  -> usageRM src dst</font><br></div><div><br></div><div>It's not immediately clear that all the various helper functions used here just end up computing a pair of the above form. A top-level comment lets you understand what's going on without understanding exactly what all these helper functions are doing.<br>

</div><div><br></div><div>Thoughts?</div><div><br></div><div>-- Johan</div><div><br></div></div>