It's possible that a < b, a > b and a == b are all false, if either of the numbers is a NaN. Comparisons with NaNs yield false, and the author wanted their function to follow the convention as well.
It's ugly but it works, though I'm not sure why >= was used instead of ==.
An array is a special kind of object, so !![] will be true because any object is true, but when comparing [] == false it converts them both into strings.
Array.prototype.toString just returns this.join(",") so an empty array returns an empty string; and of course false.toString() is an empty string, so they're considered equal.
Seriously though JS typing is a mess and I default to untrusting for anyone who claims to know how best to compare things without leaning on existing isArray/isNumber/isString/isSimpleObject style methods.
That's a good point but rather than the mess that is == you should try to ensure they're the same type. [] == false because if you convert both to strings they're both empty, which is kinda ridiculous if you don't intend for it to happen.
The == operator actually does a comparison before doing any type casting. The ECMAscript standard says that when an engine finds an a == b expression, it's supposed to evaluate a === b first, and then do type conversions is the result is false.
Basically, a == b is the same as a === b, except that it doesn't give up on the first try.
Thank you, I found that the tutorialspoint one was frankly shit, a lot of the examples flat out don't work without modification. And the official ones were very good, but they focused so much time on observable, which is not relevant to me or 99.99% of people.
Sure thing! Yeah that tp one is jank, real tp 🧻...
I'm not even sure why they show the implementation of it. They really should just explain the method. I'm guessing D3 just used ternaries because a minimizer would do the same. This way they save end users bytes right out of the box for a tiny function
There’s nothing inherently wrong with ternary when it’s used in a good situation, imo it cleans up code of anything else and I would say this is fine, ternary just feels weird at first
I like the ternary operator, it's less verbose so why not use it when it makes sense.
Nested ternaries are sus though, it's hard to read and it requires everyone working with you to have their language's order of operations memorized by heart. "Clever" code tricks aren't a good thing; code should be clear and concise, not just concise.
What's so bad about a - b? I mean, technically the code from the screenshot returns Math.sign(a - b), but for a comparator it should be fine either way.
I thought so too at first, but a - b is equal to NaN if either or both operands are non-numeric. Values like booleans will be coerced in with either method (e.g. true > false lol). So, save some edge case I missed, the behavior would be the same.
Apparently, string comparison works with > etc. Wow, so the code in the screenshot actually has a lager domain than a - b. I always use localeCompare or what it is called for strings.
Not every language has sensible operator precedence.
For example, in C++ std::cout << a ? b : c; is incorrect, you need to do std::cout << (a ? b : c);
And even if you know the precedence, it's still additional mental effort over some if statements, which naturally convey nested branching better because they're indented.
I think this is more readable than a set of ifs if you know how ternaries work — however the best idea here would be just to return a - b, or if you need it Math.sign(a - b)
Well, it interprets true as 1 and false as 0, I don't see what is complex about that. There are real weird things in js but this one is pretty simple and logical.
I don't see the need to force these into a single function, and if you do, then it would still be better in my opinion to just check if they're numbers then use a-b and if string use localeCompare
When first learning, I wondered about nesting ternaries so I tried it. It worked, but I looked at the monstrosity that resulted, and decided to never do that again.
Eh, if you're comfortable with nested ternaries, this is a fairly decent shorthand. You could write it out with ifs and else ifs, but this is shorter and not necessarily less legible so long as you understand how ternaries work.
I don't want to be the asshole that wets the party, but this isnt hard to read, and probably preferred in the world of Javascript production code as it occupies less characters than the if else if else versión, which may save probably 10-20 bytes of ram in ascci encoding probably the same or more in utf8 encoding and in the end Javascript code wants to be delivered through the network and save as many bytes of bandwidth as possible, keep making long ass statements and you can end with a file double of the size even after minification.
Although this code seems complex it literally saved me time reading it than reading the if statement for it. Since I would have to look for the if statements and conditionals and lose time moving my eyes.
It's maybe too packed but still a pretty clear statement.
If you re concerned about file size, minification is a to go, as well a minificator that include code rewrites to rewrite your code in shorter statements an example of this would be you write the long if else if chain for this code and it rewrites it as this ternary chain.
If you're worried about saving RAM and you're using JavaScript, you've already lost. And a minifier is a far better use of programmer-time for reducing network usage than little tricks like this which in the grand scheme of things are irrelevant. As is configuring your server to send compressed files.
No one is worried about ram usage and a good minifier already converts code to shorter code, with variable name refactoring and code transformation of other kinds.
And usually a js dev is not the one who has to worry about providing gzip files or other compressed formats.
I stand by my question--I thought the code OP posted looked like JavaScript, and I just checked and it's invalid in both Node and Firefox, so I'm curious what other language or dialect of JavaScript it is
124
u/rochismoextremo Nov 23 '21
Why does it bother to check on the last statement if a >= b when it previously checked a > b lol
26
u/blehmann1 depraved Nov 23 '21
This is from https://www.tutorialspoint.com/d3js/d3js_array_api.htm
38
u/alexanderhameowlton Nov 23 '21
Image Transcription: Text/Code
d3.ascending(a, b)
This method is used to perform the comparator function. It can be implemented as -
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!
16
u/aerosole Nov 23 '21
What's so bad about
a - b? I mean, technically the code from the screenshot returnsMath.sign(a - b), but for a comparator it should be fine either way.30
u/apola Nov 23 '21
I actually don't think this is bad code. It's concise and easy to read if you understand how ternaries work.
5
u/kristallnachte Nov 23 '21
...if it's JavaScript...
Then just
Would do the same...
143
u/BurnedRavenBat Nov 23 '21
Yo dog, I heard you like ternaries, so I put ternaries inside your ternaries so you can ternary while you ternary
4
u/O_X_E_Y Nov 23 '21
This is incredible. '
return a-b? How will those poor students ever understand that? I should make it more easily understandable'3
u/Stromovik Nov 23 '21
Ahhh d3js when combined with vue was a massive pain
5
u/jzia93 Nov 23 '21
Don't chain ternaries kids
2
u/aaronjamt Nov 23 '21
1
u/RyanNerd shameless Nov 23 '21
And for a demonstration of crazy ass JS coercion we are given this example. For more JS insanity see wtfjs
1
u/itsTyrion Nov 23 '21
Wat
1
u/xiipaoc Nov 23 '21
Eh, if you're comfortable with nested ternaries, this is a fairly decent shorthand. You could write it out with ifs and else ifs, but this is shorter and not necessarily less legible so long as you understand how ternaries work.
0
u/migueln6 Nov 23 '21
I don't want to be the asshole that wets the party, but this isnt hard to read, and probably preferred in the world of Javascript production code as it occupies less characters than the if else if else versión, which may save probably 10-20 bytes of ram in ascci encoding probably the same or more in utf8 encoding and in the end Javascript code wants to be delivered through the network and save as many bytes of bandwidth as possible, keep making long ass statements and you can end with a file double of the size even after minification.
Although this code seems complex it literally saved me time reading it than reading the if statement for it. Since I would have to look for the if statements and conditionals and lose time moving my eyes.
It's maybe too packed but still a pretty clear statement.
If you re concerned about file size, minification is a to go, as well a minificator that include code rewrites to rewrite your code in shorter statements an example of this would be you write the long if else if chain for this code and it rewrites it as this ternary chain.
-1
u/great_site_not Nov 23 '21
What language even has a
> =operator?!11
u/[deleted] Nov 23 '21
[removed] — view removed comment