• Raw (unregistered)

    As others have said, T, L W & H are very common, especially in UI code, and should be recognizible even as one letter names.

    Also, the lines which they are used in (especially when you write code to rescale windows in an intelligent way) tend to be quite long and often have these variables occurring many times, which is a good reason to not clutter them up with unnecessarily long names.

    I consider this nitpicking, where the letter of the law is more important than the intent of the law, and the change was probably just a "Screw you!"-gesture.

    No real WTF, in other words.

  • mikko (unregistered) in reply to dnm
    dnm:
    Single letter variables shouldn't be acceptable anyway -- even as temporary loop iterators. Its silly and ambiguous.

    If you have ANY idea what the damn code is supposed to do, (and you should, or you shouldn't be fucking with it), then you shouldn't need to have the damn code read like a fucking novel. 1 or 2 or 3 letter variables work just fine for real programmers.

  • iMalc (unregistered)

    It could have been worse:

    int aa, Aa, aA, AA; //top left height width

    They're all at least one character different from each other. Hmm, reminds me of a recent 'cOut' wtf actually

  • (cs)

    Oh god, this is lame. Such l t w h or x y w h single letter variables are no problem (i'm porting java games) at least you know they really mean what they are - positions and lengths. In the really doomed source codes, you can find variables like boolean makePaintWait which actually makes the sprite to move, skipXYZ=true which makes the xyz go, etc. etc.

    P.S. What comes next ? Moaning about single letter i variable used in "for" loops :-D ?

  • zbigg (unregistered) in reply to dnm
    Single letter variables shouldn't be acceptable anyway -- even as temporary loop iterators. Its silly and ambiguous.

    Yeah, let's call all iterators pui_iteratorOverTableOfHPC.

    Yeah, let's call all "x & y & z"s horizontal_position & vertical_position (coordinate is longer so better ?)

    Name according to context and purpose and do it reasonable this should be coding rule. No "always", "never" and no limits.

  • zbigg (unregistered) in reply to Blablah
    What about when you see a single T, a hundred lines of code later?

    That's othe rule. No context with so simple names should be so long. Nothing to do with local variable name.

    Members, global variables (no!!), classes and all other global entities should be named descriptively.

    Leave out x,y,z in context of geometry, t,l,w,h in context of windows/rectangles i,j,k for iterators as long as entity is recognizable. They must be as local as it could be. And it's not naming error if they are used "hundred lines of code later" it's conception error or just stupid optimizalization(?).

  • hog (unregistered)

    I'd suggest:

    rect<int> r; // whatever

    preceded by:

    template <class C> class rect { // or Rect if you will C _left,_top,_width,_height; public: ... C x() const { return _left; } C y() const { return _top; } ... };

    doesn't say function names need to differ by more than 1 letter

  • hog (unregistered) in reply to hog

    or maybe just:

    int r[4]; // ...

  • Frank (unregistered) in reply to Zygo

    You are right! Comments can make sense. I had comments in mind like

    int loopCount //count variable for the loop

    or this real world example, i've seen for a while!!!

    // returns the id int getId(){}

    such comments add noise to the code, no value.

  • Loren Pechtel (unregistered) in reply to Dana
    Dana:
    I disagree that comments are NEVER needed.

    If your code is well written and descriptive, comments may not be needed to tell you WHAT the code is doing, but they are extremely helpful when you need to know WHY the code is doing what it is doing.

    As far as I'm concerned this is the only good reason for comments.

  • Worf (unregistered) in reply to zbigg
    zbigg:
    Leave out x,y,z in context of geometry, t,l,w,h in context of windows/rectangles i,j,k for iterators as long as entity is recognizable. They must be as local as it could be. And it's not naming error if they are used "hundred lines of code later" it's conception error or just stupid optimizalization(?).

    OTOH, you can end up with some really horribly written loops.

    I've seen contraptions like:

    for (i=0; (j < 100) && (i < 30); j=newval(j), i++, k--)

    which makes you wonder WTF the code is doing - yes, they're all indices, but to different arrays and stuff, and it's not immediately obvious what each is supposed to do. (worse yet, j and k are often updated in the middle of the loop, as well).

    It's worse when you have N dimensional arrays (N > 1), and the indices used aren't in a consistent order of array[i][j][k] and crap.

  • kastein (unregistered) in reply to Me
    Me:
    kastein:
    I dunno. I'm kinda split on this one...

    in this case, I'd say a comment explaining what each one is should be enough. there are exceptions to every rule. Also, I say smack him AND THEN buy him a beer.

    No get him the beer first then hit him when he is drunk
    one beer gets you drunk?

    Loren Pechtel:
    I definitely agree. My philosophy on coding is virtually zero comments. If a comment is needed that normally means the code isn't clear enough. This does occasionally mean extra procedure calls for no reason other than naming what I'm doing but the runtime cost of that is trivial.
    you seriously will write something into a function so you can explain it with the function name instead of a comment? jesus christ, you scare me. That is (a) circumventing your own self-made rule of writing clear enough code to not need comments, and (b) completely missing the point of comments.
    Dana:
    If your code is well written and descriptive, comments may not be needed to tell you WHAT the code is doing, but they are extremely helpful when you need to know WHY the code is doing what it is doing.
    Definitely agreed there. what vs. why is a major differentiation that far too many coders miss entirely in their commenting: i++; // increments i i++; // move to next device control structure in table oh, and Loren's method, too... MoveToNextDeviceControlStruct(&i);

    I'm a fan of single letter (and double letter, I guess, as in ii) index/iterator names because they un-clutter the loop and thus make it easier to read, even though the variable names are shorter.

    Greyhound:
    But try searching a large body of text for t, l, x and y tt, ll, xx, yy is much easier to work with.
    there's a good reason. never thought of that before...

    </monster-post>

  • (cs) in reply to jaspax
    jaspax:
    When I see something like this I wonder: is the coder stupid? Did they truly not understand the point of the coding standard? Or are they deliberately trying to undermine it?
    THe latter, my boss calls "Malicious Compliance"
  • Koos (unregistered) in reply to Vector

    This programmer was working on the Superdoll3000(tm), and it concerned her states...

    "Top" (for those who like that), "Bottom" (default position) "Left" (you failed to pay the rent on this expensive toy) "Right" (will remind you for weeks about this single fact and brazenly ignore anything to the contrary)

    So those of you who thought it was good comment or code should probably think twice..... :p

  • Frank (unregistered) in reply to FredSaw

    I always use 'ii' and 'jj' and 'kk' for iteration indices. My reason is that I sometimes know a piece of code contains a for-loop, but don't know quite where. I can just look for 'ii' with text search, which would be harder with single letters.

  • Ornedan (unregistered) in reply to Worf
    Worf:
    zbigg:
    Leave out x,y,z in context of geometry, t,l,w,h in context of windows/rectangles i,j,k for iterators as long as entity is recognizable. They must be as local as it could be. And it's not naming error if they are used "hundred lines of code later" it's conception error or just stupid optimizalization(?).

    OTOH, you can end up with some really horribly written loops.

    I've seen contraptions like:

    for (i=0; (j < 100) && (i < 30); j=newval(j), i++, k--)

    which makes you wonder WTF the code is doing - yes, they're all indices, but to different arrays and stuff, and it's not immediately obvious what each is supposed to do. (worse yet, j and k are often updated in the middle of the loop, as well).

    It's worse when you have N dimensional arrays (N > 1), and the indices used aren't in a consistent order of array[i][j][k] and crap.

    Are you certain that the problem lies with the single-letter index variable names and not with the code that uses them merely being a steaming pile of shit?

  • AnotherAnon (unregistered)

    For those of your that think descriptive variable names are always better than i, j, k for local loop indices, I would point you to Section 1.1 in "The Practice of Programming" by Kernighan and Pike. It recommends descriptive variable names with a proviso against local loop index names.

    And this is the example they trotted out to illustrate their point: for (theElementIndex = 0; theElementIndex < numberOfElements; theElementIndex++) { elementArray[theElementIndex] = theElementIndex; }

    to

    for (i = 0; i < nelems; i++) { elem[i] = i; }

  • (cs) in reply to poopdeville
    poopdeville:
    anon:
    Strider:
    As a side note, I don't know how many times i've switched up my i's and j's in nested loops and totally befuddled myself for hours...or minutes...they look so similar, who thought to use those two anyway?

    I'm surprised no one has mentioned it yet, but it goes way back to FORTRAN on punchcards where i-n were automatically typed integers and therefore handy for for loops.

    And why do you think Fortran chose those?

    Using i-n to store natural numbers predates programming by at least 60 years. Probably more like 120 years, though I don't have access to any original, c. 1800 books to verify. They're used as indices for countable sequences in mathematics.

    Grade school mathematics, perhaps. Proper mathematics from, say, Gauss onwards ... hmmmm.

    As a former programmer of Fortran IV, I think this is a ridiculous argument. Indices are indexes. There is no semantic reason for this in the ISO-85... damn, can't be bothered to look it up ... Latin alphabet at all.

    Or, to put it another way: find a book from 1800 onwards that denominates things with an index of "l".

    Go on.

  • eugene (unregistered)

    Using the full names would only expose the rigid application of this standard in this case for the farce it is:

    int Top, Left, Bottom, Right; // Top, Left, Bottom, Right

    Rigid farce it may be, but that looks like the best version so far to me, except the comments should be more explicit.

  • Frank Goodman (unregistered)

    The code is perfectly understandable. The worse part is that he's using capital letters for variables, when they should be reserved for macros.

    Personally, I like his "fix": it's a nice jab at silly cargo cult coding standards.

  • Anders (unregistered) in reply to Josh

    The problem with the lowest common denominator is that it is to low.

  • anonymous coder (unregistered)

    Rules like these are useless.

  • zbigg (unregistered) in reply to Worf
    Worf:
    OTOH, you can end up with some really horribly written loops.

    I've seen contraptions like:

    for (i=0; (j < 100) && (i < 30); j=newval(j), i++, k--)

    which makes you wonder WTF the code is doing - yes, ...

    What would you say for this ... ?

    for (int o = 0; o < objs->size() > 0; o++) ... 

    The Real WTF (haha I've used this sentence) is that it works as normal loop.

  • Earl Purple (unregistered)

    Everybody knows though that i is for index in an array/vector used as part of a loop counter, and possibly an operator overload definition.

    Just as everyone knows that T is the template parameter for a generic type, eg

    template < typename T > class Foo { // etc };

  • j (unregistered) in reply to Strider
    Strider:
    As a side note, I don't know how many times i've switched up my i's and j's in nested loops and totally befuddled myself for hours...or minutes...they look so similar, who thought to use those two anyway?

    As a side note to side note anyone else hate when someone uses x and y for indices that are not related to coordinates?

  • (cs)

    i is for index. j and k are used simply because they are the letters that come next in the alphabet.

    Similar to using T for type, and if you need a second one U is a common choice.

    Not sure why x was chosen for the archetypal variable. I know when you first learn algebra at the basic level at school (i.e. using letters in equations to represent unknowns) x is typically chosen. (Always seems a bad choice as it is slightly ambiguous to the multiplication sign). Of course to eliminate that you write xy to mean x multiplied by y but that means you cannot have multi-letter identifiers and when you run out of English letters (or Latin letters if you prefer to call them that) you usually start using Greek ones, (in fact Greek ones are often used in the first instance).

    Typically a and b are used as limits to integrate between.

    f is used as the archetypal function (after all function begins with the letter f). g is usually therefore the next function, but in group theory g is your archetypal group element (with G being the group), and H is the next-defined group.

    n is used as the archetypal count (number of items) but then you tend to go backward to m. I guess o is avoided because it is too much like 0.

    p is the archetypal probability. q is typically the next one used.

    t is generally used to denote a period of time. In fact if you talk about a function on variable x you will typically refer to dx/dt without even defining t - time is always assumed.

  • (cs) in reply to Foosball Girl In My Dreams
    Foosball Girl In My Dreams:
    One of my personal pet peeves are unfilled comments like those below - takes up 80% of a file and adds nothing:
    package x.y.z;
    /**
     * File:         ClassName.java
     * Author:  
     * Written: 
     * Purpose:
     * Usage:
     * Dependencies:
     * Notes:
     * History:
     */
    
    /**
     * Function:     theFunction
     * Author:  
     * Written:
     * Purpose: 
     * Parameters: 
     * Return:
     * Exception:
     * History:
     */
    public int theFunction() {
      ...
    }
    
    /**
     * Function:     theOtherFunction
     * Author:  
     * Written: 
     * Purpose: 
     * Parameters: 
     * Return:
     * Exception:
     * History:
     */
    public int theOtherFunction() {
      ...
    }
    
    

    All that space to put those comments then can't afford just one more line to make the braces match vertically?

  • D (unregistered)

    I would do that... Nobody tells me how to name my Ts!

  • Antiviola (unregistered) in reply to stevekj

    Viola Merda! Forza Rapid!

  • Beaver (unregistered) in reply to Grimoire

    Start using words like 'left' and 'right' as variable names and you can run into collisions with reserved words in certain languages, eh?

    I dont like to type long variable names.

    I prefer lower-case names, they just look better. Upper case names SHOUT OUT at you.

    Sometimes, I get cute with variable names, e.g. 'for dead_guy in street...'

    The private, somewhat antisocial humor or the american programmer...

  • Beaver (unregistered) in reply to Grimoire

    Start using words like 'left' and 'right' as variable names and you can run into collisions with reserved words in certain languages, eh?

    I dont like to type long variable names.

    I prefer lower-case names, they just look better. Upper case names SHOUT OUT at you.

    Sometimes, I get cute with variable names, e.g. 'for dead_guy in street...'

    The private, somewhat antisocial humor or the american programmer...

  • Beaver (unregistered) in reply to Da' Man

    The rigid coding standards are typical. Comments, dear friends, are subject to 'comment drift' - where the comment says one thing and the code does another. n'est ce pas?

    Best thing is to keep it simple. Comments where needed.

    Comments everywhere clutter up the code. The worst is when the developers 'pecker-tag' up the code with: /* 3/15/06 J.Jones added clause to prevent double-clutching... */

    So you get 20,50 100 lines of comments at the top of your proc. Makes is tough to navigate

    That stuff belongs in PVCS or VSS...

  • Basalt (unregistered) in reply to Slasher

    I and J are actualy the same letter.

    For the Romans the letter I had both a vowel sound and a consenent sound. Sometime in the Middle ages scribes got into the habbit of adding a hook to the bottom of the I for the consonent. Thus J.

    The original J sound was more of a 'y' or 'yh' sound, which is how "Yeshua" became "Jesus".

    Mark Iennaco <= pronounced Yhen-na'-ko

  • ELIZA (unregistered) in reply to Andrew
    Andrew:
    Strider:
    FeepingCreature:
    stevekj:
    Strider:
    viola, problem solved, new coder learns.

    How did violas get into this?

    For some reason they seem to show up a lot more often than you'd expect for an otherwise underappreciated harmonic string instrument, and in the oddest places.

    -- A viola player

    I'm not sure, I think it's the setup for a joke. I first found it in the Wizardry series of comedic fantasy books (made of awesome), but I'm sure its roots lie elsewhere.

    <person1> BlaBlaBLABLAblablaaaablaBLA, and viola! <person2> It's voila. <person1> Not the way I play it!

    Doh! Those two words should have at least a two letter difference!

    If only english followed such strict c0ding standards...

    As a side note, I don't know how many times i've switched up my i's and j's in nested loops and totally befuddled myself for hours...or minutes...they look so similar, who thought to use those two anyway?

    Somebody in the 17th or 18th century mutated I into J. Latin has no J. "Jehova" in Christian texts starts with I.

    Mathematician's notes have early J references. 3D unit vectors are (i, j, k). Complex numbers can be written with sqrt(-1) as +i or +j.

    Speaking of Jehovah, all we know about the name of the Lord is that His initials were IHVH, now either YHVH or JHVH (consonant I in early Latin is a Y sound), and the vowels of a different word were inserted in the gaps in what amounts to a Hebrew equivalent of L33t-sp34k, like writing J3sus or 4ll4h so as not to commit profanity, or in English speaking a neologism such as Zounds rather than the perhaps-offensive phrase "God's wounds".

  • Valued Service (unregistered) in reply to Dana
    Dana:
    I disagree that comments are NEVER needed.

    If your code is well written and descriptive, comments may not be needed to tell you WHAT the code is doing, but they are extremely helpful when you need to know WHY the code is doing what it is doing.

    That isn't always immediately obvious in the case of bug fixes, workarounds, and things that are done because of stupid policy decisions that overrode sane technical practice.

    As clear as you think your code might be, it is never going to be immediately transparent to even the sharpest coder who tries to maintain your code. Do them a favor and provide a heads up.

    Captcha: cognac. Sure could use one right now!

    var personId = person.Id; if (!ValidPersonId(personId)) throw exception... personId = PersonIdFix336(personId); if (NotARealPerson(personId)) throw dogException... if (ManagementDecisionOnPersonIdToAvoidCosmicRayRedux(personID)) throw justWriteCommentHereException...

  • 🤷 (unregistered)

    I used to work at a place where there was a coding convention in place that apparently was created to make code as hard to read as possible. I am not even kidding: Variable names could only be 3 characters long. Which is especially useful in languages like PHP where you don't declare variables like "int num" or "string str", but where they just get the type of the object that is assigned to them. So, a variable like "dat" could be a "date" or maybe "data" or even "Datei" (german for "file"). And indentation was to be set to two spaces. Yeah, have fun reading that shit after staring at code for 8 hours. Luckily those conventions where no longer in use when I started working there. So, the very first thing I did when I had to open an old project was to go through all the classes and correct the indentation to 4 spaces.

Leave a comment on “Coded to the Letter”

Log In or post as a guest

Replying to comment #:

« Return to Article