• 9point6@lemmy.world
      link
      fedilink
      arrow-up
      12
      ·
      edit-2
      1 year ago

      Except i right? Something like counter or index seems unconventional and unnecessarily verbose

      • SolarMech@slrpnk.net
        link
        fedilink
        arrow-up
        6
        ·
        1 year ago

        Yeah, but it’s easy to overuse it. If your for loop is much longer. For a few lines I’d agree, don’t bother using something longer.

        Code should scream out it’s intent for the reader to see. It’s why you are doing something that needs to be communicated, not what you are doing. “i”, “counter” or “index” all scream out what you are doing, not why. This is more important than the name being short (but for equal explanations of intent, go with the shorter name). The for loop does that already.

        If you can’t do that, be more precise. At the least make it “cardIndex”, or “searchIndex”. It makes it easier to connect the dots.

      • hellishharlot@programming.dev
        link
        fedilink
        arrow-up
        5
        ·
        1 year ago

        Index can be useful but start looking for mapping and sorting functions. Or foreach. If you really must index, sure go use index or I if it’s conventionally understood. But reading something like for I in e where p == r.status is really taxing to make sense of

        • 9point6@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          1 year ago

          Oh yeah, I map, filter and reduce pretty much everywhere I can. But sometimes you need the index and i is so commonly understood to be that, I’d say it could even be less legible to deviate from that convention

          • hellishharlot@programming.dev
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            1 year ago

            In what world is

            for (int index = 0; index < objectToIterate; index++)
            {
                // DO YO THANG
            }
            

            less coherent than

            for (int i; i < objectToIterate; i++)
            {
                // DO YO THANG
            }
            
                • 9point6@lemmy.world
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  1 year ago

                  It’s not incoherent, it just takes a tiny bit more effort to mentally parse as it’s not a stereotypical for loop. Maybe it’s just me, but let me try and explain

                  With the i example if you’re familiar enough with a language, your brain will gloss over the unimportant syntax, you go straight to the comparison and then whether it’s incrementing or decrementing.

                  With the other example, the first my brain did was notice it’s not following convention, which then pushes me to read the line carefully as there is probably a reason it doesn’t.

                  I’m not saying it’s a huge difference or anything, but following code conventions like this makes things like code reviews much easier cumulatively.

      • UnfortunateShort@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

        I’d say except indices in general. Just bloats every line where you need to use them. Imagine writing CUDA C++ where you regularly add and multiply stuff and every number is referenced via (usually) 1-3 indices. Horrible.

    • verstra@programming.dev
      link
      fedilink
      arrow-up
      7
      ·
      1 year ago

      I have a convention to correlate the size of variable scope with its name length.

      If a variable is used all over the program, it will be named “response”. If it is <15 lines, then it can be “res”. If it is less than 3 lines, it can be only “r”.

      This makes reading code a bit simpler, because it makes unimportant, local vars short and unnoticeable.

    • space_comrade [he/him]@hexbear.net
      link
      fedilink
      English
      arrow-up
      7
      ·
      edit-2
      1 year ago

      Counterpoint: using anything other than ‘i’ as your index in a for loop in C or C++ is obnoxious as fuck.

      At most I’ll go with ‘it’ for C++ iterators.

    • Thoth19@lemmy.world
      link
      fedilink
      arrow-up
      7
      ·
      1 year ago

      Guess I’ll have to name all of my iterators “it” instead of “I”. That will fix things.

      Definitely a hot take though. Bc there are definitely times when it is totally acceptable.

    • jvisick@programming.dev
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      Mostly agree. I’m ok with single characters in a one line / single expression lambda, but that’s the only time I’m ok with it.

    • uralsolo [he/him]@hexbear.net
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 year ago

      I understand this conceptually, but there’s also a gremlin in my brain that wants me to make every line as short as possible.