Most developers would be 10 times better developers if only they'd spend some time learning Functional Programming principles. Sadly, majority just learn OOP and never look past it throughout their careers...
Not saying FP is a panacea, but bad code like this would be avoided 99% of the time if the dev was aware of problems with mutating and "side-effect"ful code.
Yeah tho mutually exclusive since OOP can't exist without side effects, you can improve your code so much by learning why FP avoids mutation and side effects.
In EVERY location the developer wanted to put an array of something this is how they did it. As I find them I am refactoring. I do not usually shit on the devs I work with but I honestly do not think this is a mistake even a junior dev with 1 hour knowledge of map would make.
I get that higher order functions can be confusing, but being a senior developer and not understanding them means you're only there for a paycheck and probably don't enjoy coding at all
Just to clarify, whilst this is the wrong use for map and they should at best assign the map output to the variable or at worst use a for each, its not killing anyone. I'm guessing the code still runs and the outcome is the same.
Again, I'm not advocating for it, just saying it's hardly the end of the world.
It's not the end of the world, but it does show a pretty clear misunderstanding of how a tool in the language works, which you would hope not to see from someone getting paid senior rates.
You can see that the new arrays are instantiated in the screenshot, so there is nothing funny going on. The way this is done constructs and immediately discards an array of undefined for no reason while also being longer.
The interesting thing is that, presumably, at some point this developer has decided to move on from forEach and start using map. I wonder what they thought was the difference.
I'm guessing code that was not understood was nonetheless copy-pasted and tweaked. Or a tech lead told them to use map instead of for each and they never questioned why, made the change, saw it ran and worked, shrugged, and moved on.
Every time I see a post in this sub I’m just glad it’s not any of the embarrassing crap some of my coworkers come out with. One day a fellow senior at my place will snap in a PR and just name and shame the lot.
The point of map in JavaScript is to create a new array with the results. Pushing to an array inside of a map is redundant. If map didn't exist you would do this but with foreach. For a senior dev to do this anytime they wanted to populate a new array is bad.
Array.map is supposed to return a new array, not to cause side effects like this by accessing and mutating variables outside of its scope. If you want to do side effects/mutations in a loop, use forEach or for of loops, not map or filter or reduce.
This is not unoptimized code, (optimization does not even come into play here), this is also not correct code, in fact, this is extremely bad code, indicating an incompetent developer who does not know the language they are coding in.
This code is suboptimal at worst. But for you to extrapolate that into a statement about how the dev is "incompetent" and "doesn't understand the language" is disingenuous. Have you never made a mistake?
Maybe I have a lack of imagination but I'm really not sure how a senior developer could mistakenly use map instead of for each. I've never made a mistake like this at least. I've made plenty of mistakes, mind you, but this would be a new one. They did it twice....
As OP mentioned in his comment, this is written by a senior dev. This could be forgiven for someone new to programming, or using map for the first time, but a "senior" dev writing this! Holy hell they are vastly incompetent.
I'm sure their mouth got them into senior role, coz their code sure as hell did not... Most devs don't bother to properly learn the languages they write code in, and don't bother to learn other programming paradigms and that is why they write garbage like this.
If I was a hiring someone, and they wrote this during their interview, it will be instant rejection right on the spot.
You could use from() to achieve the same thing as map. from() is usually used to take an array like object and make a copy of it in to an actual array thus giving you access to array functions. A good example for its use HTMLCollections! However I am a backend developer (Elixir) so if anyone else has input...
Please people, learn some basic FP. Not like "drink the KoolAid and never do anything else" FP, but just like map, reduce, and the importance of limiting side-effects in code.
I really hope author explains what's bad in a post. I don't understand what's bad in lot of posts. May be I am beginner. But it won't hurt to explain something, Isn't it?
I'm sure there exists a linter for this kind of thing. At the very least this is a code smell, but given the fact there is objectively a 100% in all cases better way to write the code, I'd call it a bug.
38
u/downrightcriminal Sep 03 '21
Most developers would be 10 times better developers if only they'd spend some time learning Functional Programming principles. Sadly, majority just learn OOP and never look past it throughout their careers...
Not saying FP is a panacea, but bad code like this would be avoided 99% of the time if the dev was aware of problems with mutating and "side-effect"ful code.
21
u/[deleted] Sep 02 '21
Wtfff?
7
u/skilledpigeon Sep 02 '21
Just to clarify, whilst this is the wrong use for map and they should at best assign the map output to the variable or at worst use a for each, its not killing anyone. I'm guessing the code still runs and the outcome is the same.
Again, I'm not advocating for it, just saying it's hardly the end of the world.
41
u/I_Am_Solar_Powered Sep 02 '21
What is wrong with the code exactly? And what the correct code should be?
29
u/alexanderhameowlton Sep 02 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
11
u/Isvara Sep 03 '21
The interesting thing is that, presumably, at some point this developer has decided to move on from forEach and start using map. I wonder what they thought was the difference.
8
u/Angrydroid21 Sep 02 '21
Every time I see a post in this sub I’m just glad it’s not any of the embarrassing crap some of my coworkers come out with. One day a fellow senior at my place will snap in a PR and just name and shame the lot.
3
u/manyQuestionMarks Sep 03 '21
I'm gonna guess that maybe the interpreter will still optimize that and won't waste any more processing time with those declarations.
This is not so stupid. I expect this sub to contain code aberrations, not unoptimized, yet correct code?
10
u/Host127001 Sep 02 '21
I've seen this done more often than it being done correctly. I am somewhat happy I am not a web-dev anymore.
1
u/Ko0jo Sep 03 '21
Still learning JS but couldn't you also use a .from() function to create a new set array?
1
u/hullabaloonatic Sep 03 '21
The word "Data" is always superfluous in a variable name.... Don't ever use it. Also if the variable stores a collection please name it in plural.
1
u/shizzy0 Sep 03 '21
“Why do I have to do everything myself! I wish there were some tools around here that would help me do what I want to do, ya know?”
1
u/MrClottom Sep 03 '21
This is horrendous, he could've at least used forEach instead of map.
1
u/throwaway8u3sH0 Sep 03 '21
barftastic.
Please people, learn some basic FP. Not like "drink the KoolAid and never do anything else" FP, but just like map, reduce, and the importance of limiting side-effects in code.
1
u/truemario Sep 03 '21
When I see code like this. I feel like the developer has a fundamental lack of understanding of that function. In this case map.
1
u/[deleted] Sep 03 '21
I really hope author explains what's bad in a post. I don't understand what's bad in lot of posts. May be I am beginner. But it won't hurt to explain something, Isn't it?
1
u/RecDep Sep 03 '21
Should have used
traverse_.1
u/bistr-o-math Sep 03 '21
Since some people asked what’s wrong… actually, correct code, but uses (absolutely unnecessarily) side effects. Better/correct use of
mapwould be:1
u/frayien Sep 02 '21
Dont this clear the original array ?