• @StudioLE@programming.dev
    link
    fedilink
    302 years ago

    A useful tip I picked up was to use ii instead of j for an inner loop. It’s far more distinct than j.

    If for some terrible reason you have even more inner loops you can easily continue the trend i, ii, iii, iiii, iiiii - or iv, v if you’re feeling roman

    • exu
      link
      fedilink
      22 years ago

      Two or three "i"s is readable, but any more and you’re counting.
      I’Ve started using i, k, m, n that’s usually enough.

    • @hstde@lemmy.fmhy.ml
      link
      fedilink
      352 years ago

      If you have the need to nest 5 levels of for-loops, I suggest taking a step back and rethinking your approach, my friend.

      Even if that other approach is just refactoring it into separate methods.

    • @Gork@beehaw.org
      link
      fedilink
      12 years ago

      At this point we might as well go full Roman as you suggested. MXMCIIV to MXMCCVII as indices.

    • @barsoap@lemm.ee
      link
      fedilink
      12 years ago

      When you have multiple indices you’re also bound to have multiple cardinals those indices count up to, say foo.length and bar.length, so foo_i and bar_i are perfectly legible and self-documenting. A bit Hungarian but Hungarian is good in small amounts. Unless you’re dealing with width and height in which case it’s x and y but it’s not that width_i would be incomprehensible.

  • TheWoozy
    link
    fedilink
    142 years ago

    In old FORTAN variable starting with I…N are integers. This is how the practice began.

    • @pazukaza@lemmy.ml
      link
      fedilink
      12 years ago

      Oh wow, I thought it was because “i” was a short way of writing “index”. Then “j” was just logical after that.

      • TheWoozy
        link
        fedilink
        12 years ago

        You are still correct! The letters I & n are the first letters on Index.

  • @dark_stang@beehaw.org
    link
    fedilink
    292 years ago

    x is used for map, filter, etc. a and b are used for sorts, comparisons and merges. y might be used if I’m doing multiple lambda expressions (but that means I’m in a bad place already). I have no idea why, but these are firm rules in my brain.

    • russ
      link
      fedilink
      12 years ago

      Yes! I love using x (and xs) for functions over whatever the thing is (or things are).

    • @DogMuffins@discuss.tchncs.de
      link
      fedilink
      122 years ago

      I’ve gotten used to using the singular form as in…

      records.filter((record) => …)

      Not saying this way is better but it works for me.

  • Scratch
    link
    fedilink
    42 years ago

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

  • @psysop@lemmy.world
    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.

    • @jdaxe@infosec.pub
      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)

      • @psysop@lemmy.world
        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.

        • @LetsGOikz@lemmy.ml
          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.

  • @throwsbooks@lemmy.world
    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.

    • @rarkgrames@lemmy.world
      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 😀

    • @soloner@lemmy.world
      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.

        • @soloner@lemmy.world
          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.

  • @Sam@sh.itjust.works
    link
    fedilink
    842 years ago

    When I’m doing coding interviews I always like to start off and say I’m a big fan of very long variable names. “As descriptive as you can be” I say. Then I get to my first for loop. Instead of i I use “iterator” and then when I start a nested loop I use “jiterator” and it always gets a laugh.

    • Dandroid
      link
      fedilink
      212 years ago

      I used to conduct coding interviews at my old job. If someone came in and had some humor like that, it would be big bonus points in my book. Being someone I would like to be on a team with is very important. Plus, I think it shows confidence and being comfortable in situations that make most people nervous.

      • @Sam@sh.itjust.works
        link
        fedilink
        52 years ago

        And even if it didn’t help my chances directly like that, even getting a small chuckle would help me be more comfortable and confident.

      • @Hazama@lemmy.world
        link
        fedilink
        162 years ago

        I’ve been at two start ups and they had me interview people. Honestly this is what I looked for. I’d ask basic questions to prove you had an idea about coding, but I can teach someone to code, I can’t teach someone to be someone I like working with.

        • KairuByte
          link
          fedilink
          132 years ago

          You can teach them to code if there is an underlying level of logic to build off. I’ve met a few people in life who I know for a fact will never code, no matter how smart they generally are.

      • DreamButt
        link
        fedilink
        42 years ago

        Honestly finding someone who can relax and intergrate into your team culture is arguably more important that anything

  • @menturi@lemmy.ml
    link
    fedilink
    English
    22 years ago

    I started using the first letter of the thing I am iterating over. This is particularly helpful with nested loops so I can easily remember which index variable corresponds to which thing.

  • 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.

    • @GhostlyPixel@lemmy.world
      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.

  • 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.