News Archive
PhpRiot Newsletter
Your Email Address:

More information

aY You are not aoMr. Xa

Note: This article was originally published at Planet PHP on 22 September 2011.
Planet PHP

I keep coming across code like this (lifted, verbatim, from an OSS project I've been working with recently):

$a = $num * $coeff + $_SANITIZED_GET['val'];

What, in the name of all that is good and just, is aoa?a Or, for that matter, what are aonuma and aocoeff?a

Computer manufacturers, OS vendors, and programming tool developers have worked for decades to give us the ability to create identifiers of arbitrary length. Personally, I'd much rather see someReallyLongNameThatLooksALittleRidiculous than a function or variable name that won't tell me what it does at first sight.

The same goes for data. A ao1a and ao0a in a database don't tell you as much as aoYesa or aoNo,a which are just as localizable, indexable, and searchable. Enumerations, as far as I'm concerned, should be verbose unless there are very compelling space or performance requirements that say otherwise.

I know that there are many people who disagree with this approach, but their arguments tend to fall in the aothat's the way I've been taught to do ita camp. The reality is, a numeric enumeration is just as arbitrary as a string-based one, but much harder for the human eye to make sense of-and we all know that, sooner or later, all data is looked at by a human eye. There are some circumstances in which performance and space constraints come into play, but otherwise the development tools at our disposal are mature enough to handle either type as easily as the other.

Besides, using numbers to identify non-numeric value is a domain violation. It used to be a necessity twenty years ago, but the vast majority of us work in environment that are well past that point. You wouldn't want to be called ao492829,a right1?

So, if you are writing a book or teaching a course on programming, please teach your readers and students to write descriptive code. They'll be better for it, and we'll be grateful that you did.

  1. Yes, I know. You probably have some kind of social security number assigned to you by your government. But that's not your name-it's an index (that is, a numeric value) in a table that *contains* your name. Big difference. a