I have noticed a common issue in code reviews, that takes this form:

// "arr" is an array
for (i = 0; i < arr.length; ++i) {
    if (i == 3) {
        // do something with arr[i]
    if (i == 8) {
        // do something else with arr[i]

I call this the "array = loop" mindset. Somehow they have in their heads that if you want to work with an array, you have to use a loop. I'm wondering what the root of this mindset is... does it have to do with education? What concept are they missing? How can this be untaught?

  • 1
    Too much Haskell? I hear it warps your brain... that said, have you asked the coders writing that the 'why' question themselves?
    – user40980
    Nov 17, 2015 at 17:43
  • 1
    What language? Some languages this is the natural way of interacting with arrays. Other languages have more functional approaches which change this. At the end of the day, this question can only be answered with an opinion.
    – user156792
    Nov 17, 2015 at 17:45
  • 6
    This is a rant. The purpose of the code review is that : fix problems, and teach the author. See a difference between good and bad code review. Try to be direct and blunt, and they will learn. Nov 17, 2015 at 17:53
  • 1
    @jdv: If I understand the OP's code, I think his complaint is that he's seeing people iterate over entire arrays just to operate on two specific elements, which could be done directly. (Doesn't make it any less a rant, and BЈовић's comment is spot on.)
    – Blrfl
    Nov 17, 2015 at 18:27
  • 1
    @Rachel the edited question "Is there any reason" is now a too broad question. This question really is a rant and the only person that can explain why they wrote the code that way is the one who wrote the code that way. Everyone else is guessing.
    – user40980
    Nov 17, 2015 at 20:56

1 Answer 1


Your question title and the code in the question don't match.

The answer to your question in the title is: how else would you find a value? Unless you have some extra knowledge about the structure of the array, iterating it is the only way to find a value. If you do know something extra, e.g. that the array is sorted, then you can do something more efficient, e.g. binary search. But in the worst-case, you need to iterate over the entire array, in the average case, over half the array.

Now, about the code you posted: this code isn't trying to find a value. It's not trying to find anything, really. It already knows the indices it wants to work on, there is no need at all to iterate.

Sometimes, there are legitimate reasons to do extra work, though. For example, in security-sensitive applications, the time it takes to do some work may leak information about the data. (This is called a timing attack, which is a special case of the more general class of side-channel attacks.) For example, a string comparison will usually abort and return false as soon as it finds the first difference between strings. However, this means that an attacker can, e.g. determine how many characters at the beginning of the password he guessed correctly, thus reducing the complexity of brute-forcing the password from ** (i.e. exponential complexity) to * (i.e. polynomial complexity).

So, this would be an example, where you always need to loop through the entire array and cannot abort early, because you need to spend the same time regardless of where the difference occurs.

This doesn't appear to be the case here, though.

  • 1
    I am concerned that there is other code in the for loop that is missing in the example. That the loop is ordered and operations need to be done on all elements, but special things need to be done for element 3 and 8 (as the loop is being processed - not before or after).
    – user40980
    Nov 17, 2015 at 23:04
  • The title is incorrect. I did not write it. Obviously the code is not checking for values but for indices (I guess not obvious to the person who changed my title). And no, there is no "other code" that I left out... all it is doing is checking for specific values of indices
    – JoelFan
    Nov 18, 2015 at 0:26
  • @JoelFan I'm sorry if I edited the title incorrectly, please feel free to rollback the changes or edit it further. The question did get reopened, however was closed again... too bad because I like answers like explaining why something might have been done a specific way :)
    – Rachel
    Nov 18, 2015 at 14:32
  • @Rachel, that's ok... rolled back
    – JoelFan
    Nov 18, 2015 at 14:37
  • 2
    @JoelFan The way it reads now does sound like a rant though, if you do want to try to get it reopened, or post in the future, you may want to avoid that and try to focus on a specific software-development related question :)
    – Rachel
    Nov 18, 2015 at 15:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.