Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
crisz82365y@devTea that's actually not a dash, it's a concatenation of minus operator, plus and minus again
-
crisz82365y@rendezvous yes, but the point is that, in the process, both sides get coerced to number
-
crisz82365y@akamaru It actually doesn't work, in this case:
'5'+-+1
is equal to the string "5-1", since the "+" in first position is interpreted as concatenation.
A good "minus with wings" could be:
'3'-+-+-'1' // 2 -
@irene A common use case could be adding stuff like positions and offsets of DOM objects which are string properties of the objects.
Doing it cleaner requires checking whether the properties exist, parseint to get the integer part if they exist or 0 otherwise, adding up, then adding "px" to the result. -
@irene sure - I don't mind such type conversions where necessary, but some people who grew up with potpourri languages don't even really know what types are.
-
@irene I'm actually a fan of the sentinel pattern to reduce runtime checks especially in hot spots.
-
@irene Say you have an unsorted integer array and look for a specific number, i.e. linear search. So you make a for loop over the array and break out if the desired integer has been found.
OR! You could initialise the element after the last array element with the desired integer, i.e. the sentinel, and then do a while loop. This saves the boundary check for the loop variable because it will latest terminate at the sentinel.
If you can't reserve one element more actual space behind the list, you can still check whether the last element is the desired one. If so, you're done. Otherwise, you overwrite the last element with the sentinel. Or you could save the last element in a temporary variable if the array must not be modified by the callee.
The underlying pattern is surprisingly versatile and not only suitable for array looping. -
@irene if the data must not be overwritten, you can save that in a temporary variable and restore it later. That's irrelevant with larger arrays, but you spare a comparison in every loop run.
If you design the caller so that there's always room for at least one more element than the list has, then you can even cut out the temp variable.
I just invented a new JavaScript operator. It's named "plus with wings", and it's used to sum to numbers without ambiguity or any need of type conversion, for example:
3 -+- "2" // 5
"2.1" -+- "4" // 6.1
"-1.1" -+- "" // -1.1
So, from now, you won't have to wonder anymore what type is that variable.
Tested on all browsers
rant