Clean Code and single character variable names

Andrew Wilkins andrew.wilkins at canonical.com
Mon Aug 26 23:26:56 UTC 2013


Still waiting for the book to arrive. I'll admit to using single-letter
names in my personal projects, and I'd say that has been largely influenced
by the other Go code that I have worked with.

Traditionally I've also avoided abbreviated names (preferring context over
ctx, transaction over tx/txn). I'm not 100% convinced that this is
necessary, though, in these cases where there's a *conventional* and
obvious abbreviation. I mean, ctx is pretty obvious. "c" not so much
(though I use "c" a *lot* in llgo to shorten "compiler"; not great, but at
least I'm pretty consistent about it, so it's a project-specific
convention).

My perspective is: don't make people think about what a variable is; make
it obvious. Where it makes sense, name a thing so its purpose for existence
is clear (I nearly said raison d'être, but that might make people go to a
translator... ;))

Minimising cognitive overhead is more important than saving a few
keystrokes.

Cheers,
Andrew


On Tue, Aug 27, 2013 at 9:13 AM, Tim Penhey <tim.penhey at canonical.com>wrote:

> Hi all,
>
> If you haven't yet, please get a copy of Clean Code [1] as we will be
> starting reviews of it soon.
>
> I have found a reference in that book that pretty clearly describes my
> dislike for single character variable names.  I type that section here.
>
>
> Avoid Mental Mapping [2]
>
> Readers shouldn't have to mentally translate your names into other names
> they already know. This problem generally arises from a choice to use
> neither problem domain terms nor solution domain terms.
>
> This is a problem with single-letter variable names. Certainly a loop
> counter may be named i or j or k (although never l!) if its scope is
> very small and no other names can conflict with it. This is because
> those single-letter names for loop counters are traditional. However, in
> most other contexts a single-letter name is a poor choice; it's just a
> place holder that the reader must mentally map to the actual concept.
> There can be no worse reason for using the name c than because a and b
> were already taken.
>
> In general programmers are pretty smart people. Smart people sometimes
> like to show off their smarts by demonstrating their mental juggling
> abilities. After all, if you can reliably remember that r is the
> lower-cased version of the url with the host and scheme removed, then
> you must clearly be very smart.
>
> One difference between a smart programmer and a professional programmer
> is that the professional understands that clarity is king. Professionals
> use their powers for good and write code that others can understand.
>
>
>
> Sometimes I feel that Go developers are trying to show off how smart
> they are by preferring single character variable names.  This has been
> beaten (most often verbally) out of most other professional programming
> groups I have worked with.
>
> Saying that "using single character variables is idiomatic Go" just
> makes me sad, and says to me that "idiomatic Go" is all about being
> smart and not professional.
>
>
> Tim
>
> [1]
>
> http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
>
> [2] page 25, Clean Code
>
> --
> Juju-dev mailing list
> Juju-dev at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/juju-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/juju-dev/attachments/20130827/e3893b59/attachment.html>


More information about the Juju-dev mailing list