9 ms·
The world's two worst variable names
- allend 17y agoI don't know if $data is the 'worst'. I've used 'int nothing', and 'int anything' before.
- iuguy 17y agoOh thank god it's not the stuff I use. My code is so bad that every time I declare a variable a fairy dies. At least there's something I'm not doing wrong.
- prog 17y agoWhile programming in C I use str quite a bit. Moving to Python, that conflicts with the str builtin function, so I now use s.
- etherael 17y agofoo & bar
- nailer 17y agoParticularly in teaching code.
- conover 17y agoI completely agree. I don't understand why foo and bar are so prevalent. Not only are you trying to describe something abstract (computer code), you are now adding another layer of human language abstraction on top of it. In many cases why not use a simple scenario people can relate to rather than foo and bar.
- fr0sty 17y agofoo and bar are the Lorem Ipsum of code. They let you look at the flow without getting caught up in certain details. You look at 'for', 'while', 'if', and other operators more closely when the identifiers are place-holders.
- roundsquare 17y agoI think I once used stuff as a variable name before.
- fleitz 17y agoWhat it is about code written in languages that prefix variables with $ that results in unmaintainable code? I'd personally vote for i and j Mea Culpa: I've written my fair share of horrid code in PHP and Perl.
- sketerpot 17y agoWhen you use i and j as loop indices, everybody knows what you're talking about. It's idiomatic. How is that bad variable naming if its the clearest thing for everyone?
- nailer 17y ago> How is that bad variable naming if its the clearest thing for everyone? The clearest thing for me would be: channel.open() Which indicates to me that a channel is being opened. Rather than i.open() Which means nothing to me unless I find and read some other line.
- alextgordon 17y agoNobody's suggesting that i and j be used for anything other than loop counters: integers. In fact they come from FORTRAN, where a variable starting with a letter [i-n] was an integer, whereas variables starting with other letters were floats. Personally, I much prefer the certainty of the current situation to the relative uncertainty of a free-for-all. That and for (counter = 0; counter < N; counter++) seems very longwinded.
- RodgerTheGreat 17y agoMaybe this is just a personal thing, but it always struck me as odd that the recommended variable names for loop counters are the two letters that look most similar to a semicolon. I tend to use x and y.
- nailer 17y ago> Nobody's suggesting that i and j be used for anything other than loop counters: integers You're right, I missed the 'indices' part of your original comment. Unfortunately I've worked at a couple of places where they're used in every for loop.
- mseebach 17y ago$data is a perfectly fine variable name, if it's used in the correct context. Such as pulling a result set out of a database. In that case, it's exactly like using i in a for loop. Anything postfixed with 2 is more shady, I agree, but the absolute worst variable names are the ones that say they are something they're not - such as a column named "tablename_key" - which is actually a foreign key, but not in "tablename".
- roundsquare 17y agoSuch as pulling a result set out of a database. Generally, I'd disagree, as does the article. If you can specify what kind of data it is, thats useful. customer_data, sample_data, etc... The only time I'd say you might be right is if you are using it in a generic method for retrieving data, but even then I think result_data is better.
- mziulu 17y agoI'm guilty of using tmp quite a lot. On my defense though, it's usually just that, temporary: I end up refactoring it down the road (most of the times)!
- reitzensteinm 17y agoI disagree, depending on the context. Data is an acceptable variable name to refer to binary blobs that don't have a type, eg, this is roughly the low level class that holds data that I'm streaming in (files may either be in a pack file or downloaded while the game is played): class packFile { var name:String; var location:String; var downloadPriority:Int; var data:Array<Byte>; } There may be a better name than data, but I can't think of one off the top of my head, and it's certainly not a bad one.
- rlpb 17y agoSimilarly, when dealing with a small finite number of data, an integer at the end makes sense too. For example: int add(int operand1, int operand2) { return operand1+operand2; }
- cperciva 17y agoI generally go with buf for a buffer containing unstructured data. It seems a bit more logical, although I'm not really sure why.
- khafra 17y agoIt's more informative. "buf" gives you a few more bits of information about the source and purpose of the data than "data" does.
- dmlorenzetti 17y agoAnother example: in my work group, "data" specifically means "measurements made in the real world." Numbers generated by a computer simulation don't qualify. So in our code, "data" could be a perfectly meaningful variable name. Personally, the worst variable name I've ever seen was "C". This was a vector of concentrations. Unfortunately, the language was Fortran-77-- where a "C" in column 1 indicates a comment line.
- reitzensteinm 17y agoWow, I think you actually may have won. That's a terrible name for a variable given the context. You would probably have nightmares for years after maintaining a project like that :)
- nailer 17y agoYou forgot 'i'. Yes, I know you're iterating over something. Best case scenario: you named that something well so all I have to do in your great big for loop is scroll up, work out what you meant, and apply that to the code I care about, being mildly annoyed. Worst case scenario: the something is also badly named and I have to open up a debugger and set a trace to work out whether the iterable is people, channels, cows, or dinosaurs. Yes I know this is common behaviour. So are many other bad things.
- panic 17y agoIf you can't figure out what the "i" is for, the code has other problems.
- nailer 17y agoI didn't say it can't be figured out at all - please don't put works into my mouth. Code is read much more than it's written and should be created for maximum comprehension. A simple one-line method call should be understandable without having to read any surrounding line.
- crpatino 17y agoRespectfully disagree >Best case scenario: ... all I have to do in your great big for loop is scroll up... Once you have to scroll up, you are toast. I mean within a function, not within a loop. There is a rule of thumb of how many things can the mind process at the same time: It is 7 plus-minus 2. You can treat the loop as one element in the function context and analyze the loop context at a different stack frame inside your head. While working in a loop, you have to think about the list, the index and the element, so you are down to 4 more things you can do before "running out of memory". I think that's why the "for each" loop has been successful across many languages. I also think that that's why "i" is preferred over "index"; it is a known idiom that helps you think about the "i-th element" and saves one slot in your brain. >Worst case scenario: the something is also badly named and I have to open up a debugger and set a trace to work out whether the iterable is people, channels, cows, or dinosaurs. Eventually, anyone who has been around for a while inherits some abomination like that. It is not funny. However, no amount of coding standards prevent morons for doing their thing. I would not sacrifice an useful and well known artifact for the sake of idiot-proofing.
- donaq 17y agoI use $dat, so I'm off the hook.
- garply 17y agoI think $data2 is worse than $data.
- stretchwithme 17y agohow about $thing1 and $thing2
- jokull 17y agoThat's exactly what I thought. $data is okay if the context is very obvious but enumerating is confusing, unmaintainable.
- sev 17y agoWorse than the worst variable names? Good variable names where, for example, $profit = $revenue
- topbanana 17y agoIn a code review I once got pulled up for using 'i' in a 2-line function called something like DoSomethingToThings(Thing[] things)
- Aegean 17y agoData is not that bad. When you're reading a device it may have a data and status register and data becomes perfectly distinct in this context.
- extension 17y agoActually, status is probably the one thing worse than data. Runners up include value, bytes, and number.
- sunkencity 17y agoI think that names that are wrong are worse, like something called $xmldata which doesn't contain data in xml format, or $price_in_cents which actually contains $price_in_dollars but nobody has bothered to change it.
- araneae 17y agoHaha, I use data all the time. Usually when I'm doing data analysis.
- erikstarck 17y agoA really bad variable name should confuse the reader as much as possible. If I remember my compiler design classes correctly Pascal (?) allows you to name variables the same as language constructs. That means you can write something like FOR FOR = WHILE WHILE WHILE = FOR Now THAT'S bad variable names. :) Isn't there an example like this in the Dragon Book?
- Enlightenment 17y agoMany many years ago, I took over a project that had a variable called "LongOrShort". It was an integer that was treated like a boolean. I kept forgetting whether TRUE was long or short. One day I finally got ticked off and spent the entire day renaming the variable throughout the entire project.
- aphyr 17y agoOften when I read something from a file or socket, prior to parsing, it's called data. Same goes for the information to be inserted into a template, or intermediate structures when transforming datasets or documents without knowing their type. Data is the variable name you use when you don't know, or can't specify, what you're dealing with.
- crpatino 17y ago$r2d2 and $c3po ...seriously