• kionay
    link
    fedilink
    92 years ago

    I prefer index variable names that are two words. The second word is always ‘index’ and the first word describes the enumerable objects. carIndex, productIndex, thingIndex

    I’m not paid by the character count. Longer and more descriptive is better. Long lines that go past your 1080p monitor are probably not long because of variable names but because you insist on doing many things in one line (quit doin’ that). For small functions this isn’t necessary, but too often I’m shunted to the middle of a big function with two or three indecies doing acrobatics over one another and while working on it I have to constantly remind myself that this i and j mean particular things.

    • @[email protected]
      link
      fedilink
      4
      edit-2
      2 years ago

      I have had too many times where I have been confused trying to figure out a giant nested loop because the writer used i/j/k or x/y/z. It’s even worse when they confused when a particular bug is because they confused what their single letter variables were and used j somewhere instead of i and no one caught it because it is so easy to brush over. Name your stuff what it is, make your life easier, make others lives easier.

      • @[email protected]
        link
        fedilink
        32 years ago

        If you need three iterators, it’s time for at least one function encapsulating that inner loop.

  • Bilb!
    link
    fedilink
    -2
    edit-2
    2 years ago

    WTF, I have never used nor seen “j.”

    I don’t usually have to name these variables these days though. Pretty much everything I use has foreach or some functional programming type stuff.

    And like that, the off-by-one mistakes disappear.

    • @[email protected]
      link
      fedilink
      12 years ago

      foreach is useful when you don’t need to know the index of something. If you do, conventional i, j, k, etc. are useful.

      A lot of it depends what you’re doing (number crunching, for instance) or if you’re in a limited programming language (why won’t BASIC die already?) where parallel arrays are still a thing.

    • @[email protected]
      link
      fedilink
      English
      32 years ago

      It was very common in text books when showing nested loops

      int nWhatTheCount = 0;
      for (int i = 0; i < 10; i++) { 
          for (int j = 0; j < i; j++) { 
              for (int k = 0; k < j; k++) { 
                  for (int l = 0; l < k; l++) { // and on, and on
                      nWhatTheCount++;
                  }
              }
          }
      }
      
  • @[email protected]
    cake
    link
    fedilink
    12 years ago

    I’m honestly prefer short but (usually) complete words. Somewhere along the line I realized that being explicit really helps when you need to change it later.

    • @[email protected]
      link
      fedilink
      52 years ago

      due to convention everybody understands what i and j are, I don’t think they need longer names. If it’s something more complicated than a counter or index then maybe you should be using a foreach loop instead (if language supports it)

      • @[email protected]
        cake
        link
        fedilink
        32 years ago

        I generally use ‘count’ for a counter and ‘idx’ for index.

        I’m not using C or Java languages though - if I were I would probably go with the more classic terse approach.

        Also, if I’m reviewing a PR and I have to load more of the diff context to understand what a variable represents, then that variable has the wrong name.

        • @[email protected]
          link
          fedilink
          32 years ago

          Even as an embedded C developer I use “idx” and “count” instead of “i”. Not just because I’m a member of the “slightly longer but more descriptive names are better” gang, but also for searchability. If I’m trying to track down where an array is accessed in a loop, for example, “idx” is more likely to take me only to the results I’m looking for and not also the “i” in int8_t or whatever.

  • Scratch
    link
    fedilink
    42 years ago

    Int index = 0 But you shorten the name to Int I = 0

  • @[email protected]
    link
    fedilink
    56
    edit-2
    2 years ago

    i is for index. j is simply the next letter and we’re too lazy to think up something meaningful

    • @[email protected]
      link
      fedilink
      72 years ago

      I is short for “index” for a traditional for loop for mapping over an array and looking up by index. J comes after I and is used for nested loops so it doesn’t shadow the outer I.

        • @[email protected]
          link
          fedilink
          12 years ago

          I believe index for the classical need to iterate through an array. E.g.

          for (i = 0; I <= arr.length; i++) { var thing = arr[i] … }

          So to me it stands for “index” for array lookup.

          Before map and iterators were implemented in a lot of languages, this was the defacto way to iterate a list. At least this is how I learned it in java/c back in the day. Nowadays I think most OOP languages including java have implemented the “for … in …” Syntax or similar which deprecates this convention.

  • Spzi
    link
    fedilink
    English
    112 years ago

    I find it hard to read when these are together:

    • i, j, l
    • n, m, u, v, w

    From all the possible character combinations, somehow the lookalike combinations are among the most popular. Yes, probably comes from math. I hated it even more when my math prof’s i and j on the board were indistinguishable.

    • @[email protected]
      link
      fedilink
      English
      22 years ago

      When the practice started, most (if not all) programming languages used capital letters. IIRC the computers that ran early FORTRAN (which is where the I,J,K, etc. convention comes from) didn’t even support lower case letters.

  • TeoTwawki
    link
    fedilink
    4
    edit-2
    2 years ago

    I used starcraft references in mine till the project lead demanded I knock it off.

    The protoss quotes were perfect.

    • @[email protected]
      link
      fedilink
      12 years ago

      If I ever have to write some kind of huge load balancer, I’ll do my best to call it “The Queen Bitch of the Universe”.

  • @[email protected]
    link
    fedilink
    142 years ago

    I always thought i for index when iterating through an array. Then you can’t use i again in a nested loop so j follows.

    Tho sometimes x, y if the array represents coordinates.

    Only a maniac would use a, b.

    • @[email protected]
      link
      fedilink
      22 years ago

      One of the very first lines of code I ever wrote was:

      10 FOR a = 1 TO 70

      In Spectrum Basic. I do tend to use I these days, I’ve calmed down since my childhood days 😀