Ugh! Your shorthand is too short!

Started by
42 comments, last by l0calh05t 10 years, 1 month ago

Nobody tell this guy about codegolf.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Advertisement

I was trying to learn about b-trees, and so I looked around and found this implementation, full of one letter variable names:

http://attractivechaos.awardspace.com/kbtree.h.html

OK, that page is *truly* worthy of being called a Code Horror. I'm absolutely certain I can write more readable code in assembly.

I was trying to learn about b-trees, and so I looked around and found this implementation, full of one letter variable names:
http://attractivechaos.awardspace.com/kbtree.h.html


that macro is just...insane.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Meh... that's just what happens when you try to write type-generic containers in C. I've seen worse.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

So, if 'i' is just some loop variable, that's fine with me. for i = 0; i < SomeMeaningfulNameHere; ++i

If the variable names are used because they are formulas to an equation, that's mostly okay, though I prefer it when at least the signature of the function is meaningful, even if later on in the function, distance is shortened to d, and startPoint is shortened to p0 or whatever. If a link to the formula is in the code, that's super handy.

I hate it when someone decides to get cute with variable names, there was a guy who had all his packet structures named after anime characters. Or when every single variable is just a,b,c,d,e,f,g,h and so on, until it wraps around to aa,ab,ac. (Though that's a sign that the code is probably overcomplicated, when you have more variables than the alphabet.)

I was trying to learn about b-trees, and so I looked around and found this implementation, full of one letter variable names:

http://attractivechaos.awardspace.com/kbtree.h.html

Hahaha. Wow! I've seen code like this before. I can't tell if that was intentional or not, because there is so much that is off about that code.

They call me the Tutorial Doctor.

It's not really off, nor is it even particularly ugly, once you look past the heavy use of complex macros. It's just code from a different time. Looking at the copyright dates, you can see that original versions of it date back to 1997. 1997 was eons ago, in terms of language development. It was written in C first of all, and even if they'd have used C++, template support in C++ compilers of the day was nowhere near what it is now. Like Apoch said, it was just someone trying to write a generic tree type in the C language. To achieve that type of genericity in C took some arcane contortions, as you can see, but in many cases it was well worth the effort; and in fact, an old-school C programmer would not in any way be all that intimidated by that code, being more accustomed to that style of programming. Remember that in C, macros were not the pariahs they are now; in fact, they were quite integral to advanced usage of the language.

One letter variables are well suited for loops, or simple math formula, otherwise it's best to give an accurate variable name, even if it's long imo.

Use longer names instead of single letter variables. Consider replacing

a with alpha

b with beta

d with delta

etc.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I understand single letter variables for constants and loops, but I have seen code like the one posted prior that look like alien language. I just don't get it.

I mean, in math we represent quantities with variables, usually when those quantities are unknown or variable. But in programming we have freedom to give the variable a more descriptive name. I mean, it is useful to use single letter variables for formulas and perhaps this would be a justifiable use of single letter variables also.

m =
 
x =
xx = x*x
 
b =
a =
c =
 
m*xx + b*x + 4*a*c

And if these variables represented numbers that are generated by some arbitrary process, then I can see how code can get real cryptic looking but still be understood easily.

But it seems sometimes, for the sake of brevity, people use single letter variables or even non descriptive abbreviations all over the place. I'd have a hard time myself if I didn't make it recognizable for later on.

They call me the Tutorial Doctor.

This topic is closed to new replies.

Advertisement