Reading Code Over the Telephone

Reading Code Over the Telephone

Rate This
  • Comments 25

In my youth I once attended a lecture given by Brian Kernighan on the subject of code quality, which was very influential on my attitudes towards writing legible code. One of the things that Kernighan recommended was to endeavour write code that was so clear that it could be easily read over the phone and understood. Most people find code much easier to comprehend when read than when heard; if you can make it clear enough to be understood when heard, it's probably pretty clear code.

I was reminded of this when I got the following question from a colleague by email:

Subject: Stupid C# 3.0 lambda expression question

How does one read the => operator?

First off, I told my colleague that there are no stupid questions, only stupid people. No stupid people work here, so don't stress about it. This is a perfectly sensible question.

As far as I know, we do not have an "official" line on how to read this operator over the phone. In the absense of any other context, I personally would say c=>c+1 as "see goes to see plus one". Some variations that I've heard:

For a projection, (Customer c)=>c.Name: "customer see becomes see dot name"

For a predicate, (Customer c)=>c.Age > 21: "customer see such that see dot age is greater than twenty-one"

An unfortunate conflation is that the => operator looks a lot like ⇒, the "implies" operator in mathematics. Since => does not have the same semantics as ⇒, it is probably a bad idea to read => as "implies". (x⇒y would have the semantics of !x|y in C#.)

Incidentally, it is a little known fact that VB6 and VBScript implemented the ⇒ operator with the Imp keyword and the ⇔ operator with the Eqv keyword. They disappeared in VB.NET. Where did they go? It is a mystery!

  • I usualy translate

    {ParameterList} => {Lambda Body}

    for

    given a {ParameterList}, {Lambda Body}

    with nothing in the middle but a comma.

  • For over a decade (and then some) I've written code in VB and I never found a use for Imp or Eqv even when I tried to invent one.  Perhaps thats the reason they're not in VB.NET ;)

  • Your method would only work with a C programmer who was on the *exact* same wavelength as you.  Even many C programmers would be clueless as to what you are say.   So like your method abnd logic = FAIL.  

    What if you were reading the same code to a secretary, or your Dilbert managemetn boss, or a high school teenager taking a programming class, or your mother learning to program?  They would have no clue what you were talking about.

    Reading over the telephone, you need to spell it out exactly as its keyboard character names....   below is how I would do it in English, other languages of course would vary to their approapriate keyboard symbol names:

    sea <pause> equals sign <pause> greater than sign <pause> sea <pause> plus sign <pause> one

    I stress the inclusion of pauses of silence, to give the person on the other side to process the symbol in their brain, locate it on the keyboard, and press it, and mainly, to deliminate each individual symbol from the previous and the next, for many symbols are expressed not as a single word (ie, asterix) but as a string of words (ie, greater than symbol)

  • So if you were reading a recipe to someone over the phone, you wouldn't say "add two tablespoons of baking powder", you'd say "add two T-B-S-P-S of baking powder"?

    My point, and Kernighan's point, which I seem to not have made very clear, was not about actually solving the problem of reading code over the phone to someone who is not fluent in the language. No one actually has to do that!

    Rather, it was intended to be a thought experiment that makes you think about whether the logic and structure of the code is sufficiently clear that it could be read aloud and understood WITHOUT having to write it down on the other end.

    So, for example, were I to read the code

    for (int i = 0; i < 10 ; ++i) Console.WriteLine(i);

    I'd read that aloud as "for eye from zero to nine, console dot writeline eye". That gets across the meaning of the code, which, after all, is what we're attempting to communicate. And then when reading it aloud, one might notice that

    foreach(int i in Range(0, 10)) Console.WriteLine(i);

    would ALSO be read aloud the same way. Which is good, because these two blocks of code express the same idea.

    It is precisely those expressions which cannot be read aloud and understood without spelling them out character by character and writing them down on the other end which are sufficiently hard to understand to warrant at least considering rewriting them to be more understandable.  

  • Just another "silly" question :-) how would you read Python code indentation over the phone ?

  • @Stephan:

    I suppose you could read it how it lexes. So "if a is greater than 5, then foo of a, dedent. bar of a". Though what you say doesn't have to be a literal transcription of the syntax -- I think I'd probably say "end if" or similar...

    As for (Customer c) => (c.Age > 5), I think I'd say "lambda customer see to see dot age greater than five". I might not say the "lambda" if it's clear what I mean without it.

  • For a predicate, (Customer c)=>c.Age > 21: "customer see such that see dot age is greater than twenty-one"

    I would say "customer c where c dot age is greater than twenty-one"

  • So how well does the "c" read over the phone? What does see mean? Vision? Sea? Si (Spanish)? Yes dot name? The Atlantic ocean dot name? Or is it the Gulf of Mexico we're talking about?

    What is it with lambda expressions (and linq) that dictate that we should start writing unreadable code?

    Do you use variable names like "c" and "s" elsewhere?

    Especially with linq, when I look a query with all those single character variables I have to unnecessarily use extra brain cycles to map what those meaningless single char variables mean. I should be able to understand what every part of a lambda expression is doing without having to go back and map what those variables refer to.

    With intellisense and Resharper there's just no excuse to be writing code like that.

  • Rather than place the links to the most recent C# team content directly in Community Convergence, I have

  • 30 years ago my brother, Mark, worked on a new HP desktop calculator called the 9825.  it had a new feature and a new button with a symbol much like "=>"; he named it the "gazinta" key.  (as in, "goes in to").  Why not create a new name for this feature and call it "gazinta"?

    +tom

Page 2 of 2 (25 items) 12