Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Tue, 09 Jan 2007

The end of my one-typo coding rule (updated)

Long ago, two bugs made a profound impression on me.

One bug was in a Unix kernel function that did something-or-other with inodes (integer indexes into the file system). The function header looked like this:

int
foo(i, ii, iii) { /* this was before types in arg list definitions */
  ...
}

The original programmer had somewhere used an i instead of an ii. Difficult to credit, eh?

The other bug was because a programmer used strchr instead of strrchr. Internal evidence suggested this was a typo—mistyping "rr" as "r"—rather than intentionally-but-incorrectly searching for the first match instead of the last. (That's the difference between the two functions.)

From these experiences I devised a rule:

Don't create two identifiers one typo apart.

That rule made me an outcast. Other programmers laughed at me because I didn't use i and j when looping over arrays, and because I would use parameters like art and other_art instead of art and art2 (or the much more in-group-ey art0 and art1). But I'd gotten used to the cool kids ostentatiously avoiding me in high school, so I persisted.1

However, today I'm working in Java with Intellij IDEA. Just now, I realized one constant needed to be two:

A_NEW_CONFIG_REQUIRES_WITHIN_LAYER_COORDINATION, A_NEW_CONFIG_REQUIRES_BETWEEN_LAYER_COORDINATION,

When using them, I don't type those full names. Instead, I type A, _, N, ^Space. IDEA then gives me a choice between those two constants. I can pick the first by typing Return or the second by typing DownArrow, Return.

Many more identifiers are now one typo apart. Rats.


1 They also laughed at me for writing C conditionals like this:

if (CONST == variable) ...

instead of

if (variable == CONST) ...

(For the non-C programmers among you, the second form is one typo away from variable = CONST, which is legal C but is often not what you want. The same typo in the first form is a syntax error.)

That was back when I was working on Common Lisp for the Gould PowerNode, and one C programmer looking at my code said, "Is that some kind of weird Lisp thing?" I don't think I ever convinced anyone to do it the safer but awkward way. That was not the first time I noticed that aesthetics trumps cost-benefit analysis.

(In case you're wondering, I'm not changing my two constant names. I'm thinking that it'll be too hard to come up with memorable, meaningful names that are not a typo apart, so I'm abandoning my rule. Maybe a bad idea, but I don't claim to be any less swayed by aesthetics and personal preference than anyone else is.)


UPDATE: I just spent ten minutes tracking down a problem caused by using ...WITHIN... instead of ...BETWEEN.... Even when I knew that was the problem, I got the correction wrong. "Between" and "within" are just too vague. So I've changed the constants to APPS_SHOULD_COORDINATE_AROUND_A_NEW_CONFIG and LAYERS_SHOULD_NOTICE_A_NEW_CONFIG. Besides having more visible differences, the reason I think they're is less error-prone is because the format is "who should care... about what..." rather than "what happened... and who should care...". That means that, when using IDEA's identifier completion, I'm explicitly typing "who should care" and picking the "about what" from a list. That's better because I'm less likely to confuse what just happened than where the announcement is going.

## Posted at 09:03 in category /coding [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.

 

Syndication

 

Agile Testing Directions
Introduction
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects
Postscript

Permalink to this list

 

Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list

 

Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI

 

Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."

 

Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich

 

Where to Find Me


Software Practice Advancement

 

Archives
All of 2006
All of 2005
All of 2004
All of 2003

 

Join!

Agile Alliance Logo