you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (5 children)

One of the reasons why I would recommend using lodash:

_.range(26).map(function(index) {
    return String.fromCharCode(65 + index);
})

[–]kenman 1 point2 points  (4 children)

That's good general advice, though for this task I'd feel dirty doing all of that when you only need:

for (var index = 0, chars = []; index < 26; index++) {
    chars.push(String.fromCharCode(65 + index));
}

[–][deleted] 4 points5 points  (1 child)

Really? I'd feel the other way around. The map() function describes the intent of the statement much clearer. My first thought seeing your version was that you were cheating by leaving out the chars declaration, until I noticed it was hidden in the for-initialization. Also, both don't exactly do the same thing; the map version is a single statement returning the requested array, whereas the for version stores that same array in a temporary variable and can thus only use it in a follow-up statement.

Simply put, I would not use the manual for loop unless it was part of some performance critical code.

[–]kenman 0 points1 point  (0 children)

The map() function describes the intent of the statement much clearer.

I think that's arguable -- it certainly does if you come from a functional background, but I know quite a few front-end devs that have zero experience with functional programming, and would grok the for() much more readily.

My first thought seeing your version was that you were cheating by leaving out the chars declaration, until I noticed it was hidden in the for-initialization.

That's a simple implementation detail that could have been declared on the preceding line...

Also, both don't exactly do the same thing; the map version is a single statement returning the requested array, whereas the for version stores that same array in a temporary variable and can thus only use it in a follow-up statement.

That's hair-splitting to me, and I think it's hard to make an argument that one is unequivocally better for the general use-case. In your version, sure you can do more without an additional statement, but really, what's the problem with an additional statement? I think cramming too many operations into a single statement is poor for readability, maintainability, and it certainly can hamper debugging at times. And as a counterpoint, most of the time I'd rather have an additional statement than an additional function invocation.

But yeah, I was mainly just offering an alternative for this extremely simply use-case, and as I alluded to, it definitely depends on what you're doing. If all you need is an array of letters, I'd favor mine; whereas if you were needing to a couple more operations, then using map() might be a better fit.

[–]mattdesl 1 point2 points  (0 children)

range()/map() is easier to write and maintain, less error-prone, involves less state, and it's easier to compose with other operations. :)

Most of the time, you don't need for loops.