8

Start the new year off right.

Comments
  • 4
  • 8
    Ok I’ll use cOuNT or iNdEx
  • 0
    @red-knot you do that
  • 1
    i j k l m n o p
  • 3
    well, i've never used "i" in "for loops". why would i? there is, though, an "i" in "while":
  • 9
    No, not going to stop, because it makes perfect sense to use 'i' in a for loop.
  • 1
    I’ll move to ‘n’ then.
  • 0
    Why? Is math too hard?
  • 4
    Stop using for loops
  • 5
    For(a = 0; a <= b; a++){
    return a;
    }

    As you wish 🤷‍♂️
  • 1
    Why? To create extra confusion?
  • 1
    This has got to be the most stupid thing ever. What is the alternative (int iteration= 0; iteration < range; iteration = iteration + 1)?

    I is perfectly suited to be used as a convention for something that is done hundreds of times. I cannot imagine a single valid reason to think otherwise but would love to be enlightened...
  • 1
    @JoaoOliveira85 I think in most cases i refers to an index that is used to access a value in an array, so "index" instead of i would be appropriate.
    However, in that case, you should be using forEach instead.
    And in that very rare case that you actually need i, representing iteration or index, you should still use forEach, iterating over a sequence of indices. And of course give it a proper name like index instead of i.
  • 1
    First of all, those are language specific methods. Neither necessarily exist in other languages while "i" is pretty much language agnostic.

    Secondly, the second parameter of the forEach method callback is the index. There you can call it what you want, but the post is about for loops in general so "i" doesn't specifically mean index and that depends on case.

    Thirdly, if we want to be nitpicky forEach is much less performant than using an imperative for loop.

    Fourth-ly, a for loop isn't specifically to iterate through an array, it's to do something iteratively X number of times. You can start counting from the end if you want to go backwards, you can count every other number, you can do a number of things with a for loop and only some of those cases are suited as and index.

    Like I said. This makes no sense at all. Not even as a meme. Unless the meme is about entry level developers with strong opinions about things they don't completely understand.
  • 0
    @Lensflare what's a better option?
  • 1
    @JoaoOliveira85 what do you mean?
    As I said, forEach is better because it iterates over the elements of a collection and gives you the element in each step. This allows you to pick a proper name for that element rather than using i or index, incrementing it manually, checking for max value and using it to access the value that you actually need.
  • 1
    @JoaoOliveira85 I’ve just read your other comment.

    Discussions about performance should be avoided for various reasons:
    It doesn’t matter most of the time and clarity is more important than premature optimization.
    It’s language dependent.
    We had this discussion with @kiki a while back where she did a test in JS and surprisingly found out that forEach is actually faster than the classic for loop.

    The vast majority of loops is just for iteration over a collection.
    So, i is almost never appropriate.
    For the remaining rare cases, you should use some more descriptive name anyway.
  • 0
    The for loop is only less performant if you run it a couple of times. Since the forEach creates a callback for each iteration after a couple or runs it starts to lag behind and quickly is surpassed by for.

    The legibility argument is very very weak since pretty much anyone and their grandma have heard for int i a million times.

    I'd actually argue the for loop is easier to read than the bullshit use of chained utility functions like forEach.

    The legibility of code is much more complex than how you name one variable.
  • 1
    @JoaoOliveira85 unlike in the clown language JS, the foreach loop is built into most languages as first class control flow syntax and is not less legible than the classic for loop. It doesn’t require callbacks.

    And even with callbacks, it’s not always trivial to know if it hurts or helps performance. Languages like c++, Rust, Swift can optimize a lot at compile time and make callback based code perform equally or even better.
Add Comment