4 ms·
I prefer to have readable code first, and performance improvement later and only if needed.
by G4BB3R 5y ago
I prefer to have readable code first, and performance improvement later and only if needed.
- edgyquant 5y agoAre you saying you find Array.map(e => f(e)) Easier to read than for(e in Array) { f(e) } I don’t see how they really are any more or less readable, although I’ll admit map gave me a day of rethinking when I switched from 100% python to using react on the frontend.
- lkschubert8 5y agoI think Array.forEach(...) is the more apt analog there as it wouldn't be generating a new array of the return like map would.
- edgyquant 5y agoMaybe, I only went with map since it was in the GP.
- hnedeotes 5y agoTechnically you would use Array.forEach(e => f(e)) If you don't care about transformations. Which is basically the same as your for Map is when you want to return a new collection with each original item in it mapped to a "different" thing.
- brundolf 5y agoforEach is an outlier, because most of these methods transform something into something else and can be chained, and that's where the readability increase comes in. I usually prefer for loops over forEach because it emphasizes the fact that forEach doesn't return a value; it's used purely for the sake of side-effects.
- pwdisswordfish0 5y agoProgramming punditry makes a lot more sense if, when you see someone talking about the code being more readable, you understand that most of them are lying to themselves about what they really mean. Which is code that is more writable. In other words, something that's easier for them, the lazy person who is punching keys and derping around in a text editor.
- brundolf 5y agoOther languages like Rust can achieve basically the same syntax with lazy evaluation/composition of phases, only collecting the results into a new data structure as the very last step.