This blog entry mentions bad variables names; among them is the always confusing $data. My favorite quote from Andy Lester’s post is:
‘Of course it’s data! That’s what variables contain! That’s all they ever can contain. It’s like you’re packing up your belongings to move to a new house, and on the side of the box you write, in big black marker, “matter.”‘
Are you bored by lame variable and class name? If so, give us some examples? Or, write a haiku on creative class naming. How you use this forum is entirely up to you.
Couldn’t agree more, but I do like the idea of labelling all of my moving boxes with the label “matter”. I’d like to add my two cents when it comes to ill-monikered Java. For example, I never want to see the following code again. It is just too “generic” for my taste:
Collection collection = getStuff();
Iterator iterator = collection.iterator();
while( iterator.hasNext() ) {
Integer integer = (Integer) iterator.next();
String string = integer.toString();
System.out.println( "String: " + string );
}
Notice a “pattern”? Every variable is a type name. There is another variation on this code poem–adding a nice little label at the end of a descriptive name. Variables such as firstNameString or lastNameString are just as offensive as the previous example. Code is language; nouns, verbs, adjectives, pronouns are all identified by context, not by some distracting suffix. You should know what a variable *is* by the context it appears in. Write concise code, and read it like prose. And, along those lines, make sure that the code is somewhat interesting to read…
Avoid Interminable Boredom: Use Creative Class Names
If something is important, use an interesting word. In my travails, I’ve found many programmers uncreative wordsmiths, prefering mundane object names such as ProductManager, SecurityFilter, and LogicTester to active object names like Vanguard, Bouncer, or Adjudicator. Spice it up; personify your applications, or you’ll never be able to relate to them. Programmers work with words every day, but few keep a good dictionary and thesaurus on the desk. Which sounds better?
- Each request is processed by the SecurityFilter to ensure that no resource is accessed by an unauthenticated user. OR
- Each request faces the Bouncer which checks for a valid ID.
Sentence #2 is something people can relate to, but reading an entire document written in the style of #1 would drive me mad. Being forced to read 50 pages of robot-esque mumbo-jumbo is a punishment similar to Dante’s 5th circle of Hell—”The Angry” and “The Sullen”. It should be no surprise that in #1 is the norm for technical writing. My theory: organizations that don’t encourage creativity get what the ask for—drudgery.
You may not think that your company’s fancy e-commerce system can afford to have an object named Bouncer, but I urge you to reconsider. Bouncer communicates the idea better that AuthenticationEnforcementObject or ServletSecurityFilter. You would like to convey ideas to “management”, right? Would you rather sit in a room with your fancy boss sptting out a string of long but very serious object names, watching everyone slowly lose interest in what you have to say., or would you rather use colorful nouns. I’ll take colorful nouns over seriousness any day.
- Q: So how does our system enforce security?
- A: Every request has to pass to get through our
Bouncer on the way in. If the request
doesn’t have a valid ID, the bouncer rejects the
request.
If the request appears to be malicious (i.e. a
port scan ), the Bouncer will call the
Manager and make a note in the log.
Our Bouncer is regularly trained to
recognize funny business.
I’ll stay awake for this meeting, and I guarantee that people will have an easier time visualizing something colorful and concrete. Get creative, sitting in the flourescent aura of your drab office all day doesn’t give you a license to be boring.
POSTSCRIPT
Please don’t mistake creativity for dot-com zaniness. There was a recent article in BusinessWeek on a company named IDEO, and it reminded me of the psuedo-creativity that was chic during the roaring-90s. Evidently IDEO has invented a whole new vocabulary, and inventing new vocabulary should be a warning sign that the dot-com bubble may burst yet again. My favorite new word from IDEO is Bodystorming—a creative form of role playing involved in product design. I’m not sure about you, but I don’t think I ever want to do anything called “bodystorming” at work, it just doesn’t sound appropriate. To each his own.



Names
I'm probably betraying the confidence of former colleagues, but once upon a time, I came across this code (in Java):
BufferedOutputStream buffy = new BufferedOutputStream(...);
Aww, how cute. The theme continued a few lines later with a variable named "angel". Incidents like this make me disagree with your encouragement of creative variable names. In fact, when I read that section, I was never quite sure if you were being sarcastic or not, since that advice seemed so out-of-line with your preceding advice.
Names
I take it your former colleague was a fan of Buffy the Vampire Slayer. There is no sin in making it interesting, but I do think that "cute" code is a mortal sin. I'm not encouraging buffoonery or lightness, but "active naming". I've seen many times where a set of programmers are so bored with coding on a particular system that they start to resort to offense and explicit in-code comments as a form of communication.
What I talk about is more applicable to Class naming than variable naming. People often tend to forget that they can use real-world analogies; instead, they get bogged down in "pattern-speak". I'm only trying to encourage people to use bolder language.
I would prefer the cute name to something like this:
BufferedOutputStream outputStreamToHoldTheData =
new BufferedOutputStream(...);
I am happy for you that these are former colleagues.
Names
I do want to make it clear that I continue to have tremendous respect for these former colleagues, their taste in televison notwithstanding. (-: I certainly did not mean to disparage anyone.
To paraphrase Will Rogers, I've never met a programmer I didn't like.
Names
There are some examples out there when it comes to creative class naming that can't help but make me smile.
An absolute beaut of an example was the 'StinkyException' that one of the best software engineers I've had the fortune to work with named her 'quick noddy test exception'.
This exception was hidden away until it was raised in front of a customer to some general embarrassment for the poor engineer in charge of the demo.
Where would software be without a sense of humor!