We Can't Even Write for Loops Anymore???

In C# I like and use LINQ so the idea of using something to extend my abilities in iterating over enumerables is not foreign to me. And typically I do gravitate toward a foreach loop for some, basically, syntactic sugar that keeps me from having to use an indexer. I've also never in 12 years had a problem reading an indexer as part of something being iterated over a for loop. If I had to do something with say parallel arrays, I'd probably be inclined to use a for loop there vs. LINQ. Or if I needed finer control over the enumeration, such as say parsing command line arguments. In that case, my work history has made me someone who likes to not like to cede control to some helper and I've never had a problem reading an indexer. Or writing indexers where needed.

So in JavaScript, why did I find myself getting so opposed to changing a for loop to use a third party library? Something that behaves like LINQ, which I again, do use?

All I wanted to do was find the first instance of a key in an array to look up something in priority order in a hash. So I wrote what was basically:

for(var i = 0; i < keys.length; i++){
     if(source[keys[i]]) {
          return source[keys[i]];
     }
}

return safeFallback;

I guess an alternative would have been something like:

var result = keys.findFirst(function(key) {
     source[key] !== undefined;
});

if(result) {
     return source[result];
}

return safeFallback;

Which is "better"? I guess the double ]] indexer in the first isn't esthetically pleasing but is it wrong? More difficult to maintain than a function callback? I'm sure some would say so. But for they like me, their answer is their opinion.

As a contractor I do aim to yield to the opinions of those that work at my contract. I missed that today cause I didn't identify a .findFirst style method before I ran out of time. :/ Kicker is one came through in a code review I looked at. That my answer was staring at me in the face just didn't click. Had it, I'm sure I would have changed my code.

There is something about surrendering a basic thing like looping to a third party library as a rule that bugs me. Writing this I do think some of my hesitation is due to when I interview people, I like to ask LINQ questions cause if you don't know what you're doing with LINQ you can really easily write lousy performing software.

foreach(var item in collection) {
     var foo = usesItemAsIndexer.ElementAt(item);
}

I've seen something like the above in production code and performance has a bad time because of it. So, yeah, how does that findFirst method I called above work under the hood?

Given that a for loop is so basic and fundamental to programming, I think it being so should count for some ease of use and maintainability too. So in the end, because it was a big deal to my client I do wish I'd found a way to convert the loop. For me, I'll never complain about such a thing and use a for loop in my own for me work whenever I like. 🙂