Great read. Point 1 is also my favourite, “+” over “Number()” just makes no sense as a choice to me. The latter is ridiculously clear, the former is just ridiculous.
Granted, the spread operator being used in the returned array like this means you'll be generating copies upon copies upon copies, but you asked for an arity-agnostic refactor, not a performant one 😜
Every single time I use reduce, I forget how whichever language I'm using it in organizes either its own arguments, or the arguments given to its lambda/block. 🤦♂️
Did you read the comment of @josh to which I replied?
Granted, the spread operator being used in the returned array like this means you'll be generating copies upon copies upon copies, but you asked for an arity-agnostic refactor, not a performant one 😜
Syntax bite # 5 for '12px 15px' can be also rewritten as: margin.split(/px/g).map(Number) or margin.split('px').map(Number). Produces a result of [12,15,0]. Still usable in case you only need to do basic calculations (add, subtract).
You missed the whole point of this article. Which that you should not be writing code that way in the first place. Nor should you be suggesting this buggy code to anybody else.
Trouble maker and Problem solver ⚙️🔧 Loves simplicity, hates bullshit 💩. Productivity obsessed, avid learner 🖥🚀 Sport and outdoor freak 🧗⛰ Metalhead 🎸🤘 Father of 2 👨👩👦👦 Opinions are my own
Don't just blindly follow whatever is written online, question yourself, research, test, then double research and double test. "It just works" should never be an excuse! If you don't know why it works, then presume it doesn't.
absolutely love this and agree 100% this would just require a post on its own.
it summarize the right positive investigative attitude to good coding practice and learning in general.
Why would you even create a 'sum' function when there is an operator that does just that? FP does not mean that you have to make functions for everything and your grandmother.
Again, FP does not mean you have to make functions where expressions are readily available. The function actually hides that you're using spread, possibly surprising the callers of the function with unexpected results. They're better off using the appropriate method of concatenation where it is needed.
Whoever says you can use bitwise operators to "round" is just plain lying. But it is an extremely fast way to cast your number to int (in other languages cast to int also truncates).
I have to say I was surprised by this. Good tip!
The main point to take away from this tip is that you don't have to use lambda's all over the place, which a lot of developers do, but that you can supply a function that takes the appropriate number of arguments. BTW, not every higher-order method takes 3 arguments, by definition. Just the ones on Array :)
Find some tricks very helpfull, specially points 4 & 5 🖒. But in point one I prefer using + operator to make conversions to number, is faster to type and is you hardly manage big ints in your app so the unary + operator works perfect. Also I like to use !! to convert to boolean type. Just a matter of personal taste 😁. Very good article 🖒. I'll be waiting gor the 2nd part.
I've been a professional C, Perl, PHP and Python developer. I'm an ex-sysadmin from the late 20th century. These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
Not as an alternative to rounding, which is just wrong. But it's an extremely easy and fast way to cast your value to int. If you're doing fast graphics stuff, bitwise operations can be really useful.
I've been a professional C, Perl, PHP and Python developer. I'm an ex-sysadmin from the late 20th century. These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
If you're in the sort of situation where that optimisation is important, then you're probably keenly aware of what you're doing. I'd never recommend this to anyone who wasn't already an expert.
Got my first computer (ZX81) 1981, and start write programs. I have been working as IT-Technican, but the last 23 year its software development fulltime, in many language and os.
Location
Sweden
Pronouns
Him
Work
Software Engineer on Microsoft Business central 365 (Language AL)
Yep... JavaScript, fun as it is, is also full of these little things that can trip even the most experienced programmers up. Great read, thanks for sharing!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I like that part where you say "if you don't know why it works, then presume it doesn't". Great write up waiting for the follow up post.
Yeah, it is indeed a piece of great advice. Usually, I too, don't believe what is written until I'd myself seen or done practically.
Great read. Point 1 is also my favourite, “+” over “Number()” just makes no sense as a choice to me. The latter is ridiculously clear, the former is just ridiculous.
Granted, the spread operator being used in the returned array like this means you'll be generating copies upon copies upon copies, but you asked for an arity-agnostic refactor, not a performant one 😜
Note that your ordering is a bit weird. You could do this (swap
b
anda
in thereduce
):Every single time I use
reduce
, I forget how whichever language I'm using it in organizes either its own arguments, or the arguments given to its lambda/block. 🤦♂️Ruby:
Elixir:
And now, (thank you,) Javascript:
Haha, yeah. In Scala you have
foldLeft
andfoldRight
, wherefoldLeft
has it as the first argument andfoldRight
as the second argument.This instantiates n+1 new arrays (where n is the number of arrays), when you could have just used a for loop to instantiate 1 new array.
Did you read the comment of @josh to which I replied?
Syntax bite # 5 for '12px 15px' can be also rewritten as:
margin.split(/px/g).map(Number)
ormargin.split('px').map(Number)
.Produces a result of
[12,15,0]
. Still usable in case you only need to do basic calculations (add, subtract).You're still assuming all of the values have a unit and that unit is pixels.
This doesn't work for zero.
Great, thanks for pointing that out. Aside from that, it is an alternative for the specific example using integer casting.
You missed the whole point of this article. Which that you should not be writing code that way in the first place. Nor should you be suggesting this buggy code to anybody else.
absolutely love this and agree 100%
this would just require a post on its own.
it summarize the right positive investigative attitude to good coding practice and learning in general.
A worthy article to read. Thanks!
My two cents:
Find some tricks very helpfull, specially points 4 & 5 🖒. But in point one I prefer using + operator to make conversions to number, is faster to type and is you hardly manage big ints in your app so the unary + operator works perfect. Also I like to use !! to convert to boolean type. Just a matter of personal taste 😁. Very good article 🖒. I'll be waiting gor the 2nd part.
Seems like using TypeScript solves most of these
What's not love about
variable + ''
1+ +a
!!thing
if (!value)
My favourite part was "solution: use a function". How great is functional programming!
Great abstraction with the "bites", a great article to read :)
Do people really do things like exploit bitwise operators as an alternative to rounding? In 2021? (I get to say that today!)
Not as an alternative to rounding, which is just wrong. But it's an extremely easy and fast way to cast your value to int. If you're doing fast graphics stuff, bitwise operations can be really useful.
If you're in the sort of situation where that optimisation is important, then you're probably keenly aware of what you're doing. I'd never recommend this to anyone who wasn't already an expert.
Great post ! Really enjoyed fifth part. Especially to keep "truey" values I used to filter with identity :
arr.filter(item -> item)
But never liked it 😅
Geat article, would like more of this.
I rarely use these tips because they are difficult to understand and remember. Thank you for sharing. Great post
Great post! Cheers 💜
Great Article. I even learned new tricks :-)
Bring on the next 5...
dang
stuffed : )
Awesome read! Awesome tips!! Waiting for the second part! Keep up the good work! :D
Man the round stuff was neat! thanks for sharing!
Yep... JavaScript, fun as it is, is also full of these little things that can trip even the most experienced programmers up. Great read, thanks for sharing!