December 15, 2006

On Variable Naming in Code

night

Much is made of the pain of typing long variable names (or function names or class names), but code is usually read more often than it's typed. And when I'm reading code (even if it's my own), I'm grateful for names that err on the side of descriptiveness.

This means:

The exceptions are common language idioms which any programmer could reasonably be expected to know (ie: cons in Lisp, def in Python, x as an index into array loops). These should be abbreviated as their meaning is more intrinsic and independent of the name.

In Technology and Software

Posted by Josh Staiger at 08:37 AM

Comments

Srry bout tht.

Posted by: Bill Higgins December 15, 2006 09:49 AM | Permanent link

You mention common language idioms, but do you think those idioms should be limited to finite set found in the language's standard libraries? Or should you also take into account the ideology behind the idiom?

For Python, it seems like abbreviation and concatenation are so much a part of the builtin names that I began to feel icky for not aligning my own naming conventions: delattr, execfile, iter, len, repr, and so on.

What about conventions in popular user code, such as the postfix "_dammit" or "Dammit" (BeautifulSoup, BitTorrent, etc)? 'xDammit' is usually synonomous for "ensureThatX".

Posted by: Kurtiss Hare December 15, 2006 04:53 PM | Permanent link

Idioms don't have to be limited to the language standard. x as an array index isn't part of any language standard, but I still like it.

An idiom can gain mind share through a popular framework or codebase, but this is more rare. I wasn't aware of the "dammit" convention until you mentioned it :)

I'd be careful in adopting a language standard's abbreviation technique for your own code. There's no problem with Python abbreviating "len" abbreviation because every Pythoner uses len and knows what it means. The functions you define aren't as likely to be etched into people's consciousness.

Posted by: Josh Staiger December 16, 2006 02:00 PM | Permanent link