Fog Creek Software
Discussion Board




Now, that really annoys me: variable names

I've found myself again and again wasting hours of time not actually coding, but trying to figure out the data model or reverse engineer someone else's code.

One of the biggest problems with code maintainability is a naming convention. Period. Or no convention.

Probably every single programming book, every serious programming course will teach you what a good naming convention could be. Still most of developers I've met do not follow any convention at all or have their own, pretty meaningless one.

Do names like lvxi or lvoptionc say anything to you? Still they do mean "local variable x(??) I" and "local variable Option character (that's the data type)".

Why the heck has anyone to point out that variable is local, anyway?! I would assume that most of variables in the good code are local. Too often prefixes are just pointless. Everything I care is the type of object, its scope (when its not local) and what is inside it.

Many of us have worked with DB tables and fields called like: ml_pt_mstr.ml_trnbr . Those names sound like 128 bit encrypted to me. Some people just have to take typing course to stop using abbreviations.

Moreover, those cool gunslingers who tend to create all that messy data model never take extra time to put labels or descriptions on all those fields with encrypted names.

It was said enough on naming rules, but who, for the better code sake, will finally hammer down one simple idea into programmers heads: "Choose your objects' names very carefully - often that's only documentation we have left."

I give my vote away for academic course "how to name your code objects properly" being introduced as the absolute mandatory into every CS course, moreover I propose to include it into the final examinations and refuse a job to everyone who fails to give proper object names.

Vlad Gudim
Wednesday, February 11, 2004

To name you have to think. Much code is written
without thinking thus there are no names.

son of parnas
Wednesday, February 11, 2004

Didn't we just have a Hungarian notation debate a week ago?

Elephant
Wednesday, February 11, 2004

Hello,

I think the problem of naming conventions or rather lack thereof is due to that most projects get started as proof of conecept one offs or demo hacks.

Once you get proof of concept this code gets reused and everything becomes a mess. Everybody knows that it shouldnt be done this way, but it is.

When somebody later gets bitten on the ass by this fact, they decide they need a naming convention document that everybody needs to follow. This leads to variable naming along the line of those you mentioned. Its just a strict set of rules written by somebody who was pissed off and didn't consider the need for common sense when naming stuff.

It's just a matter of using common sense in the naming process I guess.

Patrik
Wednesday, February 11, 2004

I work with someone that names all arrays itab1, itab2, itab3.......

Each contains various types of data, yet you'd never know.

It makes maintaining the programs a nightmare.

Joe
Wednesday, February 11, 2004

I've developed a style which could be called 'anal-retentive'.

Even on simple little hacks or tests I force myself to use 'good' style, e.g. descriptive var names, comments when necessary, good use of whitespace, etc.  This is something most everyone else has known for a long time – you perform like you practice. 

MR
Wednesday, February 11, 2004

Patrik,

"proof of concept" and "temporary" doesn't sound as a good excuse for me: some people have a habit of always sticking to giving nice names; secondly, you can always rename those objects later using an editor's replace function very easily, but no one cares for some reason.

What we need - a programmer who will become a politician and, subsequently, a president (probably of USA, for we, programmers, no worse than actors and oil barons). Then he or she will fight all that scum who puts lxmytmpobjnbr34 names in the code. :-/

Unfortunately, then we seriously risk to end up with geek-overflowed jails...

Vlad Gudim
Wednesday, February 11, 2004

Variables, like Servers should be named after exGirlfriends.

That way, when they bite, you can swear at them profusely and feel doubly good about it. :-)

Tapiwa
Wednesday, February 11, 2004

Well as far as cryptic naming goes, and all this abuse on Hungarian notation, what about Java naming conventions?

if (DellOptiplexGX270.Samsung52X-CDROM.IsInUse())

It's like writing a novel.  I've seen production variable names in Java longer than 64 characters.  Made me wish for one of those autocomplete things used for class properties/methods but for my local variables (or whoever came up with those names local variables).  I know this is probably an extreme case, but there has to be a middle ground to this issue.  I believe they call it common sense, but as of late it seems that people have problems practicing it.

Elephant
Wednesday, February 11, 2004

I'll tell you what the problem is.  I'm a programmer, though I don't make a programmer's wages.  I'm about to get a CompSci certificate with which, supposedly, I'll have the piece of paper I need to get a real programming job.

My classes are populated by morons.  Not all of them, but a good number.  But the classes aren't hard enough to drive the morons away.  Most of them (e.g. C, Java, VB) start at a level well below where I'm at, and usually don't even get any harder than what I already knew how to do.  Thus the idiots don't flunk out; they just keep making their way through the program thinking they know how to program.

So when you say:

"I give my vote away for academic course 'how to name your code objects properly' being introduced as the absolute mandatory into every CS course, moreover I propose to include it into the final examinations and refuse a job to everyone who fails to give proper object names."

My reply is:  No, dumbass, the very _last_ thing we need is another course that anyone with half a functioning brain can easily pass.  What we need is for programming classes to be made _harder_ so that stupid people will get discouraged and stay out of it.

Kyralessa
Wednesday, February 11, 2004

>Unfortunately, then we seriously risk to end up with geek-overflowed jails...

We make it a shooting offence :-) Just take 'em out back :-)

>no one cares

I think you hit the nail on the head. Most people I've seen using the 42389csdh-type names is people who are in the
"9-5 mainframe programmer mindset". That means they dont care about their stuff, they just do what someone else tells them.

I've seen basically two types of programmers:

1) The architect type, that gets frustated and bored if he can not take part in the overall design of the system, and understand how his contributions fit in "the big picture"

2) The programmer that does his little part and is happy with that and couldnt care less about the overall picture.

In my experience the bad-variable-names-people is category 2.

Patrik
Wednesday, February 11, 2004

> What we need is for programming classes to be made
> _harder_ so that stupid people will get discouraged and
> stay out of it.

From what I've seen, asking programming students to actually name variables in a sensible fashion WOULD make programming classes harder. In fact, if that was the only thing you *ever* taught or expected in the class, it would probably still make them harder than the crap I see getting taught.

"Here is the problem. Here is the solution. Here is the code that implements the solution. Here is a printout of that code. Now go to the lab and write the program; you will be graded in two weeks."

Wow, two weeks to retype a page and a half of someone else's code, and as long as I don't make a typo I'll get an A? I am learning so much in this class. It's really preparing me for a high-paying job in the fast-paced computer industry.

Caliban Tiresias Darklock
Wednesday, February 11, 2004

> What we need is for programming classes to be made
> _harder_ so that stupid people will get discouraged

This got me thinking about what someone said in an earlier thread...

Some professor in a programming class handed out an assignment to everybody in the class. Next assignment was to add some functionality to the program in the first one.

He handed the assignments back out to the students, but switched them around, so that you had to add stuff to someone elses code.

Next lecture was on writing maintainable code :-)

Patrik
Wednesday, February 11, 2004

Kyralessa,

I appreciate your ability to excel within your college or university group, nevertheless if you want to further communicate your undoubtly interesting ideas on education level within the school you personally attend, please consider reading some nice book on good manners, as insulting people doesn't help creating high opinion about your own intellegence level.

Vlad Gudim
Wednesday, February 11, 2004

I had a professor in college who taught the required entry-level CS programming course that STRICTLY enforced good variable names, comments, etc.

If you had crappy commenting/variable names/etc. you got a zero on the programming assignment (although errors of this type on the *first* assignment only would be marked but not deducted). 

As it was a programming course getting a zero on more than one assignment would doom you to failing the course.  I think it worked well; I don’t recall him ever giving any zeros due to style issues. 

I wish all programming courses in every school would include strict punishments like that one.

MR
Wednesday, February 11, 2004

Kyralessa, do you find that calling people in this forum "dumbass" and referring to your peers at college as "morons" and "idiots" (while telling us how much better than them you are) has been helpful in your chosen career?

Breandán Dalton
Wednesday, February 11, 2004

When I was in CS, I used crazy variable names in my code and even on written tests. The professors hated me!

int ILOVELLAMAS = 5;
String myInteger = "Hello!";

Of course, I don't do that these days.

Joe
Wednesday, February 11, 2004

In defense of Kyralessa, I understand.  I think back at some of the poeple that I worked on projects with, and where they are in the industry, and sometimes it scares me.  I remember being woken up at 6:00am by a pounding on my window the day that a project was due, because my partner couldn't get his portion of the project to complete.  He's currently employed by the Navy designing weapons systems, and it scares me.  Granted it was an embedded system that we were working on, but an hour of poking at it and I had it mostly working.  He'd been working on it for days, and then all night to no avail.  I'm not defending the language choice of Kyralessa, but I understand.

Elephant
Wednesday, February 11, 2004

Elephant, well on any loosely regulated job market you will certainly have a group of "professionals" and, unfortunately, we have more than a dozen of them in IT, but all I ask - for proper objects names.

Personally, if I were interviewing all those people I'd be able to distinguish between bad and good applicants by giving a small coding exercise. Still is where any chance of educating "sinful" colleagues? Any success stories?

Vlad Gudim
Wednesday, February 11, 2004

> Still is where any chance of educating "sinful" colleagues? Any success stories?

Do code reviews of new employees' work. When doing that, whenever you see anything that you want them to improve, then tell them. Eventually, stop reviewing their code after 6 months have gone by without your seeing anything questionable.

Christopher Wells
Wednesday, February 11, 2004

With older programming languages one used to be limited in the length of variable names, thus the tendency towards obscure names.  With newer languages such as Java (yes Elephant Java!) we can now have very long variable names.

Whatever your naming convention on any given project there is no valid argument against long descriptive variable names.  I use them for pretty much everything except maybe the ones I am incrementing in loops.  If your editor or IDE doesn't offer auto-complete for these I suggest you get a new editor.

As for the naming conventions in Java, I'm not sure what Elephant is trolling about unless it is the fully qualified names of classes in which case the answer is that it exists to provide unique names for distributed applications which may use components from more than one company.  If it confuses you try the import statement.

name withheld out of cowardice
Wednesday, February 11, 2004

I agree, not with Kyraleesa's language, but to the concept.  I do find the courses in the colleges here are too easy - When I did my CS we had to work with very strict guidelines by the instructor and a lot more projects and assignments - the students now are not grilled even 10% the grilling i went through to complete my course. 

It starts at school here - for all tests - guides are made available, questions are made availabe - and anytime a question is slightly different from what was provided the student flunks

KS
Wednesday, February 11, 2004

Christopher,

sounds like a very good solution to me.

Three years ago a company I worked for was "outsourcing" some of its work. To be more accurate, we were "buying" programmers from our partners. Of course, they were "selling"  their worst coders (who would possibly sell best ones during dot-com era?!).

We had loads of outstanding work and I was in charge of technical management. Now I have to admit: guilty; being too inexperienced in management and too busy coding myself I had assumed that if code written by them worked - there was no need to review it: just keep moving ahead.

Most of a functionality we were implementing was "absolutely needed by yesterday". Finally, near the point of all those "code-slaves"  going "home" I finally had to actually change something inside the code written by one of them... That was a strike and a very good lesson for young and ambitious developer like me.

Needless to say that later we had to re-write all of this particular person code. We were lucky that not all of them very so professionally "dedicated cut&pasters".

Given that, best point of eliminating such candidates is before they actually start to write any production code. Most probably no one from your core team will have time and nerves to review their code in addition to everyday's tasks.

But what  would you suggest when you find yourself not in the position to review others code?

I've moved to different industry and even country now. Although my job title still has magic prefix "Sr." in it I don't have much formal power other anyone else in the team yet. My boss is a "salesmen", who couldn't care less about naming details.

Vlad Gudim
Wednesday, February 11, 2004

And, the student will provide an adverse feedback - "tests were too difficult" which the instructor has to defend....

KS
Wednesday, February 11, 2004

I didn't mean to troll.  I just have worked for shops where their local variable names (which happened to be in Java, although it could have been any language) were incredibly long.  If my IDE had an auto complete feature for local variable names, and not just class methods/properties, I wouldn't complain.  It's just a lot to type when you've got excessively long variable names.  That's my only complaint.  It is perfectly reasonable to have 10-20 character variable names, but crossing some threshold which I can't define, it becomes a bit much.  I'm sure in those cases it would be perfectly reasonable to come up with a shorter version of the variable name that conveyed all necessary information.  Like I said, I didn't mean to Troll, just saying that there is a happy middle ground between excessively short/cryptic variable names and excessively long/verbose variable names.

Elephant
Wednesday, February 11, 2004

> I finally had to actually change something inside the code written by one of them...

I was the senior programmer. I knew that if something didnt work, and the programmer left, and I couldn't fix it, then I'd be out of a job. That was one of several incentives to do code reviews.

> Most probably no one from your core team will have time and nerves to review their code in addition to everyday's tasks.

I didn't agree with everyone who my boss chose to hire, but for as long as he was paying them I felt it part my job to help them produce something worthwhile.

The time I spent was a good long-term investment (as well as necessary for the short-term health of the product), for anyone who did turn out to be a long-term "peer": as I said, after a while, there was no longer need for such close scrutiny.

> But what  would you suggest when you find yourself not in the position to review others code?

Hire people whose code you don't need to review, or find yourself a reviewer you can trust? Except by reviewing the code, I don't see any other way to ensure useful variable names in the source.

Or learn about refactoring to fix it after the fact.

> I don't have much formal power other anyone else in the team yet

Ah ... politics.

At my most recent (short-term) contract, I added a self-contained module to an existing system. I read and then ignored the company's style guidelines (which called e.g. for Hungarian). The (long-term) programmers who reviewed my work after I'd written it all then complimented me on how well-written it was. :-)

Christopher Wells
Wednesday, February 11, 2004

> Here is the problem. Here is the solution. Here is the code that implements the solution. Here is a printout of that code. Now go to the lab and write the program; you will be graded in two weeks.

Caliban, that is extremely interesting. Where do they have these sorts of courses? Is this a Coleman/DeVry thing, or what?

Dennis Atkins
Wednesday, February 11, 2004

Elephant- I agree there are almost no cases in which a 64 character name is needed.  As for 20 character names, I would have to pull more of my hair out if I actually had to type those as well.  Maybe I just take it for granted but which IDE are you using that doesn't do code completion for local variables?  Are you unable to switch?

name withheld out of cowardice
Wednesday, February 11, 2004

"Kyralessa, do you find that calling people in this forum 'dumbass' and referring to your peers at college as 'morons' and 'idiots' (while telling us how much better than them you are) has been helpful in your chosen career?"

No.  Nor has it been harmful in my chosen career.  What a bizarre question.  Do you find that what you have for breakfast in the morning has been helpful in your chosen career?

"I appreciate your ability to excel within your college or university group, nevertheless if you want to further communicate your undoubtly interesting ideas on education level within the school you personally attend, please consider reading some nice book on good manners, as insulting people doesn't help creating high opinion about your own intellegence level."

Patience with stupid people is not among my myriad virtues.  But thanks for caring.  I will entertain the possibility that you didn't express yourself exactly as you would have liked, and that you didn't intend to use the word "course" to refer to what you're talking about.

Every damn course I've taken except the current one has included a review of binary, decimal, and hexadecimal, as if we haven't done it already in the previous five or six courses.  So doubt my own intelligence if you like (it's a free country; well, depending on where you are) but experience backs me up; many (not all) of these people are genuinely way too slow on the uptake and should not be pursuing a programming career.

Actually, we have something in common, Vlad; we're both venting about situations we can't change.

Kyralessa
Wednesday, February 11, 2004

Kyralessa

"Every damn course I've taken except the current one has included a review of binary, decimal, and hexadecimal, as if we haven't done it already in the previous five or six courses."

Perhaps you should have chosen to attend a better school.

Jimmy Jo-jo
Wednesday, February 11, 2004

"No.  Nor has it been harmful in my chosen career.  What a bizarre question.  Do you find that what you have for breakfast in the morning has been helpful in your chosen career?"

Hello! I'm an elitist bastard! I can be found in any field, including software development, plumbing, and lawyering.

I'm really a nice person, but only when I can't hide behind miles of cable and electrons. In real life I have need for personal networking and friends, but not here!

Thanks for your time!

Joe
Wednesday, February 11, 2004

---"binary, decimal, and hexadecimal"----

We have an "Introduction to Computers" course, that really dates back to when none of the students would have had a PC in the house. And a third of it is dedicated to that crap, which they are never going to use as we don't teach IT yet. They even have pages of conversion exercises from binary to decimal, and teach a totally mechanical way of doing it that means you never grasp the concept.

I could rewrite the course in a month but I'm nothing to do with the Business Studies departement which teaches computers so why bother?

However what Kyralessa has come up against here is the immense inertia that surrounds tertiary education, particularly state-owned (private has its own faults). You have syllabuses that are taught year after year, even though every single member of the department agrees they are crap!

As another poster said: change to a better college - you can do that as a student. For faculty it's not so easy!

Stephen Jones
Wednesday, February 11, 2004

> Caliban, that is extremely interesting. Where do they
> have these sorts of courses? Is this a Coleman/DeVry
> thing, or what?

Computer Learning Center of Alexandria. I would expect that other trade schools are similar. Part of it is simple time constraints; four weeks was just not enough time for anyone to learn IBM 360 assembler, but rather than admit they couldn't teach it that quickly, they preferred to fake it so you could pretend they did long enough to get a job.

The COBOL classes were a lot like this, too -- but the program was parceled out in single procedures over the project timeline, and each procedure was explained in detail; if you actually stayed awake during class and listened, you ended up with a good understanding of things, but there was no way to tell the difference between someone who actually paid attention and someone who just copied the handout. We spent a full twelve weeks on COBOL, though, so I actually sort of like it.

I was particularly irked when my C programming instructor gave me a D on the final project because he didn't understand it. He couldn't fail me, because it worked... but having just taken a course in C himself, he was completely lost when the program wasn't what he had given us on the printout. (I actually suspect he photocopied them from his own coursework. He certainly didn't understand the concepts behind them.)

Caliban Tiresias Darklock
Wednesday, February 11, 2004

You know what?

I apologize.

Vlad, I am rather irritated at my school and its poor excuse for a CompSci program, but obviously that's not _your_ fault, and I think I took you as an excuse to vent about it.  I'm sorry.  It was uncalled for.


Oh, and Jimmy Jo-jo:  Actually, the trouble is that rather than being in the regular day courses, where you learn real computer science, I'm in the "continuing education" evening courses for people with a degree or getting one later in life, and it's painfully easy.  I even tried to set up independent study with the day dept. head, but our own head nixed it, and this goes into a long story you don't want to hear about.

The only bright spot is that I work at the school too, so it costs me $50 a course.  And let me tell you, that's about what they're worth.  *sigh*

(By the way, this is a large private university, not a community college or something.)

Kyralessa
Wednesday, February 11, 2004

The meaning of the name "lvxi" is quite obvious. The programmer obviously made a little typo and intended to write "lxvi"; in which case, how could anyone doubt that somewhere near the start of the relevant source file we'll find something along the lines of

    static const int lxvi = 66;

? I often wonder how anyone managed to obey the "no magic numbers in the source" rule before Roman numerals were invented.

In full compliance with coding standards
Wednesday, February 11, 2004

Karelessa,

your apologies are taken.

Nevertheless I won't agree with the statement: "Actually, we have something in common, Vlad; we're both venting about situations we can't change. "  As I go through the code mending it I usually take care of replacing every local variable name and re-factoring it. Many times I do that prior to my changes, as it makes them easier.

Anonymous Friend, your "const int lxvi = 66;" theory does deserve good attention. It well might be true, I go check it..

Vlad Gudim
Thursday, February 12, 2004

The second requesite computer science course at my University was, "Introduction to System Software." The major project was to develop an assembler, perhaps a virtual machine to read in the binaries you created, and maybe even a debugger for this language.

I'd say only about 30% of the incoming CS class made it pass this course, the others moved on to something else. It wasn't that the course was overly difficult, the material required a lot of work.

SG
Thursday, February 12, 2004

*  Recent Topics

*  Fog Creek Home