Most programmers are familiar with the quote, "There are only two hard things in Computer Science: cache invalidation and naming things." I'm curious about your personal experiences with the latter.
- What are some examples of really bad names you've encountered in code?
- What about the alternative -- any really good names that have stuck with you?
- What frameworks/libraries do it well or do it poorly?
- Has a really poor name ever come back to haunt you (i.e. caused you hours of extra debugging or misled you?)
I know we all have strategies for naming things, but I'm really curious about your personal examples and stories.
Top comments (20)
I am annoyed when variable names seem correct at first glance, but turn out to be not exactly what the name says. I was working on a project, where the was a variable called
n_items
, and you would assume, that it stored the number of items. However, it was calculated like this:where the array of item_ids goes from 0 to the number of items minus one. So actually,
n_items
was the number of items minus one. Super confusing, super dangerous. I encountered that discrepancy, because I saw these kinds of loops, further below:Yuck. Thanks for sharing :)
To comment on each point:
Broadcast
which is such a good word, it's remarkable it didn't come to us sooner. But it's so easy to say that in hindsight.User
asCustomer
because I thought it would encourage a more focused sales-oriented mindset. At that initial base stage it seemed to make sense. It was really confusing and regretful almost immediately.Great points Ben!
Curious -- why was the
Customer
name so confusing? Were there other names in the domain revolving around "people" or was it just thatUser
is so synonymous with "a representation of a human being in code form"?It just seemed like every time we went to talk about the model, we had to clarify that customer was the main user model. Also it was Rails, where conventions are strong and everybody calls it user.
I studied finance in college, and for our final thesis, my thesis partner and I had to do quite a bit of statistical coding in a software program called Stata. We were running heavy calculations on large datasets, and to avoid running out of memory, we frequently had to delete columns containing intermediate results. For that, we had to use the Stata
drop
statement.We started off using meaningful variable names, but we quickly grew bored of that, and as a way of letting of steam, we decided to get more creative with our naming. One night, after a day of particularly tedious programming, we decided to name a variable
theBase
, so when we no longer needed it we could write this glorious statement.drop theBase
In our exhausted minds, it was the most hilarious line of code ever written. Meanwhile, the rest of the campus library was staring rather disapprovingly at those two idiots laughing uncontrollably at their IDE.
Speaking of funny but objectively bad variable names. When he was in grad school in meteorology, a friend of mine had an assignment that required some Python coding. I think he'd reached a point in the class where he stopped caring, and coding wasn't his favorite thing anyway. But he named his variables stuff like
heyo
,heyo1
, and some almost-but-not-quite curse words that I'm not sure I should put here. It perfectly fit with his sense of humor, so we thought they were pretty hilarious. I'm not sure the professor who taught the class did, though.WordPress will use a "nonce" token to prevent duplicate input and CSRF attacks. That's all well and good, but in British English that word has another, sinister meaning. Occasionally we'll have a caching issue and a token will be re-used. WordPress usually displays a nondescript "Are you sure you want to do this?" error, but some plugins will print "Invalid/Bad Nonce" right there on the screen.
I've had to explain to a couple of clients that this is indeed correct and is a technical term. Unfortunately one of those clients was a school.
Hey Jack-
Yep, I've definitely seen
nonce
used in URLs (not just in WP). I had no idea there was that alternate meaning in British English!We had just released Confluence 1.0 back in 2004, and as part of the cleanup we decided to move whatever non-app-specific code might be useful to the next product into shareable libraries.
The plan was to move all the code from the app into a temporary bucket while we untangled the remaining dependencies, and then break that staging library up into its more specific parts (ORM stuff, Spring stuff, app configuration stuff, and so on.)
I wanted to make it obvious to any developer which code was still in need of migration during this process, so I named the bucket library 'bucket', and all the java packages, 'bucket.whatever'.
When I left the company ten years later, we were still depending on 'bucket'.
I do have a pet peeve with abbreviations - for me they tend to drain the concentration I would much rather use on my actual work.
For example, I recently saw "cd" as a variable name, I was able to guess it correctly in the context (change detection), but well, I used the same API just the day before in another project. But I cd to change directory, and still have a nice collection of compact disks, and I had a context definition not so long ago - you see where this leads.
It is a sign of lazyness and not caring about anyone else who might read the code you write. Also, at least if a decent type system and a reasonable IDE is at hand, you don't even save so many key strokes.
What I sometimes actually do - usually in early stages - is to name things I have a hard time finding a really good name for in an manner that pains me while reading - as a marker to not let something so generic that it becomes meaningless or obscure be checked in. So if you pass my screen you might see something of type BlablaEntityIReallyShouldMakeMyMindUpAbout. ;)
I've also done this with a build task once. I had spend half a workday desperately trying out things and in the end it worked and I had a hard time understanding why, so gulp ran for the next two month a task called something like "compile:horrible-horrible-workaround-which-makes-me-question-my-career-decisions".
Generally speaking, I try to avoid making a mess in the first place. ;)
I was recently working on a tool for my internal use and I had no idea what to call it. I ended up just punting and calling it "Seborrhea" in reference to the Dilbert TV series.
This doesn't really answer the question, but it relates to the difficulty of naming things well.
Something I've noticed with basically every new developer that joins our company is that at some point they ask me "what do you think I should call the object that <...>".
Almost always, by coming up with a short description of the object to put into the question, they answer their own question without realising it. So I just repeat that back to them.
method name "dome" .. I puzzled for a long time trying to figure out why they named the function after a semi-spherical shape until another dev pointed out that they just hadn't camel-cased it and it was even worse.
I also get a twitch any time I see a variable named or suffixed with "data" .. the Tautology Club has many members.
Naming computers is fun. Naming variables is not. With computers, you can have some sort of theme going on that partially related to their uses (IE all our servers are named after Greek gods.), and since there are usually so few of them, that's fine. With variables, you have to name them in a useful yet concise manner. That gets hard if you're dealing with similar variables, of functions that are closely tied with these variables.