5

I am working on a compilers assignment in OCaml, and the following is an example of the pre-written code in said assignment:

(* Build a CFG and collection of global variable definitions from a stream *)
let cfg_of_stream (code:stream) : Ll.cfg * (Ll.gid * Ll.gdecl) list  =
    let gs, einsns, insns, term_opt, blks = List.fold_left
      (fun (gs, einsns, insns, term_opt, blks) e ->
        match e with
        | L l ->
           begin match term_opt with
           | None ->
              if (List.length insns) = 0 then (gs, einsns, [], None, blks)
              else failwith @@ Printf.sprintf "build_cfg: block labeled %s has\
                                               no terminator" l
           | Some term ->
              (gs, einsns, [], None, (l, {insns; term})::blks)
           end
        | T t  -> (gs, einsns, [], Some (Llutil.Parsing.gensym "tmn", t), blks)
        | I (uid,insn)  -> (gs, einsns, (uid,insn)::insns, term_opt, blks)
        | G (gid,gdecl) ->  ((gid,gdecl)::gs, einsns, insns, term_opt, blks)
        | E (uid,i) -> (gs, (uid, i)::einsns, insns, term_opt, blks)
      ) ([], [], [], None, []) code
    in
    match term_opt with
    | None -> failwith "build_cfg: entry block has no terminator"
    | Some term ->
       let insns = einsns @ insns in
       ({insns; term}, blks), gs

I find that the liberal use of extra-short identifiers (L l, gs, insns, etc.) makes this code very unreadable. Even the (fairly) short word blocks is contracted to blks.

Were I to write the same thing in C#, for instance, I would have no problems having 10+ character variable names, clearly spelt out every time.

Is this something peculiar to functional-first languages, and is this... normal? F# is almost the same language, but .NET namespaces and their contents are very long, so I see a bit of a disconnect here.

For the record, this phenomenon appears to be part of the OCaml standard library as well: consider hd for head and tl for tail.

7
  • 2
    Naming conventions depend on the people writing the code and the reason they're writing it -- there's generally an enormous difference between a programming instructor writing 20 lines of code for their students to use as a brief learning exercise compared with code which an employer or client is paying for, and which needs to consider long-term maintainability. (Programming instructors are often not likely to be bothered about presenting code which would fit the latter, they usually want to make their point as succinctly as possible) Commented Oct 15, 2022 at 7:22
  • 5
    There is a bit of a culture in functional programming that values a terse, mathematical style, often with very short or even single-letter variable names. Point-free style in Haskell even gets rid of many intermediate variables. I don't think that's a good culture, and you shouldn't necessarily imitate this naming. And sometimes, people use abbreviations because their editor doesn't autocomplete. I would have also written that function more clearly either recursively or imperatively with a loop. I think that the fold operation obscures the data flow here.
    – amon
    Commented Oct 15, 2022 at 8:07
  • @BenCottrell, thanks for the answer. Maybe I should have prefixed with the fact that this assignment is not trivial, and contains several thousand lines; in that context, even small functions are hard to parse.
    – SRSR333
    Commented Oct 15, 2022 at 9:37
  • 3
    @JörgWMittag I disagree with your premise. Most programming languages use English keywords, and as such, English competency is almost a pre-requisite to program. Your example is contrived, and I can provide several counterexamples of standard libraries that are verbose, and whose function names make their actions clear (with some exceptions). This point makes even less sense when one notices OCaml was implemented by French developers in INRIA: clearly they have adhered to some FP norm.
    – SRSR333
    Commented Oct 15, 2022 at 11:14
  • 1
    There are a lot of things in programming that are "normal" or common - doesn't mean they are good. The terse mathematical style makes sense when it's clear from the immediate context what the one letter thing is, or when the naming comes from a very common convention (e.g. x:xs for "single x, a list of x-es"). The amount of it should also vary by subject matter (domain) and intended readership/audience (e.g. general programming community if writing a generic library, vs a more domain-specific audience). Not everything has to be inline - naming functions allows for simple abstraction/modeling Commented Oct 15, 2022 at 21:50

2 Answers 2

5

Yes and no. Functional programming is often more abstract, so it's not unusual to have names like x and xs because there isn't really a better name at that level of abstraction.

However, there is also better and worse code depending on domain. Academic code in particular tends to not be written with maintainability in mind, because it's usually done in short snippets that doesn't need maintaining. In addition to the terse names in this example, the level of nesting and cramming everything into a 5-tuple would not typically pass a pull request in production code.

7

The code probably makes reasonable sense, together with a glossary of abbreviations and a textbook on the subject, but clearly it cannot stand on its own.

I wouldn't ask what is normal for a particular language, I would ask what is normal for a particular context. Descriptive names are more valued in business programming than they are amongst academics, and different languages are themselves more common to each.

I'm not quite clear what explains that difference in culture. Perhaps it is because industrial programmers tend to encounter a wider variety of subjects and complex automations (which makes it difficult to keep the finest details of all things solely in your head for years on end), and it is more common for code to pass between two industrial programmers without the writer delivering a detailed course on the subject to the reader beforehand.

By contrast, academics and educators only deal with code in the context of delivering a course of study, or in the context of executing a study, where there will be verbal interactions between the people, there will be various written and graphical paraphernalia produced and made available, and there will be fewer subjects covered but investigated in more depth, and that perhaps allows a less thoughtful approach towards naming in the code itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.