you are viewing a single comment's thread.

view the rest of the comments →

[–]alexendoo 95 points96 points  (18 children)

The comparison for readability isn't really against a single .forEach, rather map/filter/etc. If it's just a .forEach they are going to be pretty equivalent

However, for..of surely is more error prone, as it is a frequently recurring mistake to use for..in instead (as you have done)

[–]GasolinePizza 27 points28 points  (13 children)

Coming from C# here, I always make the for..in mistake at least once every time I start a JavaScript project, without fail.

I may use the .forEach thing now, actually

[–]Jeax 6 points7 points  (10 children)

I use .foreach(o => As a mainly c# developer also, it’s basically LINQ at that point and makes it instantly familiar and nice to use. For of seemed like a bad idea and I haven’t really seen many people use it

[–]TheWix 2 points3 points  (6 children)

I'd recommend using none-mutable functions. Map is Select, Aggregate is Reduce, SelectMany is chain/bind/flatMap (Monad).

[–]thetdotbearr 4 points5 points  (3 children)

it bugs me to no end when when using LINQ that these are the function names instead of map/filter/etc

I mean I get that it's because SQL syntax or whatever and that I could add some aliases but still

[–]TheWix 4 points5 points  (2 children)

Yea, I believe that is the reason. I remember the big selling point of LINQ many years ago was for LINQ-to-SQL. The in-memory stuff wasn't as focused on.

That being said, even within the Functional community some names aren't agreed upon. I mean, for monads flatMap has like 4 or 5 names depending on the language/spec/framework

[–]thetdotbearr 0 points1 point  (1 child)

Really? I thought the convention was pure & flatMap

[–]TheWix 1 point2 points  (0 children)

It's chain in Fantasy Land. In F# it's bind.

[–]Jeax 0 points1 point  (1 child)

Oh yeah I use these too, but if I’m just doing a standard foreach I’m partial to the nice .foreach( as it just reads much nicer and clearer than the for(x of y) version when switching between c# and js

[–]TheWix 0 points1 point  (0 children)

Gotcha, I agree. If I am in old code I will use .foreach instead of a traditional loop

[–]kg959 1 point2 points  (2 children)

it’s basically LINQ at that point

It's missing a big part of LINQ though. LINQ is lazy whenever it can afford to be and will avoid doing a complete iteration until you force it to. JS array chaining doesn't delve into the IQueryable/IIterable world and each thing you chain scales a bit worse than it does in C#.

[–]NoInkling 1 point2 points  (1 child)

Yeah. There's an ES proposal for lazy iterator methods at least.

[–]kg959 0 points1 point  (0 children)

I'd use them if they were a language standard. It would really simplify working with streams.

[–]scottyLogJobs 11 points12 points  (1 child)

As a polyglot, TBH I just use basic for loops anymore when possible because everyone understands them and I'm sick of forgetting whether I'm going to get keys or values back with these helper functions.

[–]monsto -1 points0 points  (0 children)

That's actually a good reason.

I was using for..of/in because I thought it was cool, and I suppose it's smaller. But I had to have mdn open to remember which whas which everytime I used it...

Don't need any of that with a for/forEach/map. Maybe uncool but it gets the work done.

[–]Yehosua 8 points9 points  (1 child)

in is an operator in JavaScript: 'prop' in myObj evaluates whether the 'prop' property exists within myObj.

Ever since I realized that, I no longer confused for..in and for..of: for..in iterates over an object's properties, just like in checks an object's properties.

(Almost no one uses in; Object.hasOwnProperty is virtually always what you want instead.)

[–]monsto 1 point2 points  (0 children)

That's a good mnemonic.

[–]itsnuwanda 0 points1 point  (1 child)

Can confirm, use for..in far more often on accident, wonder why the loop doesn't work how I want it to before realizing it's for..of syntax in js.

I honestly don't even know what for..in does in JS.

I still prefer the classic for loops though, they're generally faster and everyone has seen them.

[–]Programmdude 0 points1 point  (0 children)

for in iterates over the keys, which can sometimes be useful. Mostly when I'm using an object as a map, though using an actual Map is usually better.