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
-
Tenary shouldn’t exceed one line and one check. Anything bigger than this should be good ‘ol if-statement.
-
Jedidja10053ywut?
like I do like me some ternary but isn't that totally unneccesary?
simple if(a&b&c)... -
I like ternary operators but I also like adding sufficient parentheses that I can make sense of it the next time I look at that project
-
@Jedidja
I'd probably go with
filter(element => ["view", "use", "admin"] .includes(element))
Easier to read in my opinion than ternaries or a bunch of if statements. -
@bittersweet yes...
Using ternaries as a kind of dict filter for several values should be punished.
One ternary, not more.
Never nest ternaries, you're just begging to geta hell of bugs for no gain whatsoever. -
@IntrusionCM
Yeah and I think as soon as you're checking multiple values, it's better to chuck it into an array.
Extensibility and all.
There's clearly some kind of "collection" going on there, some kind of "permissibleElements" thing, where the elements seems to depend on the Role of the viewer or something like that.
Depending on the rest of the code, maybe there should even be some kind of Map<Role, ElementArray> thing.
That way you can request which array of elements should be displayed for which role. -
(Sorry... I like to mess with bots. Some day, they will murder us all, so let me have my fun while it lasts)
-
@bittersweet as long as it isn't a brussel sprouts butt... Curry smells delicious.
-
@IntrusionCM
You clearly have not cleaned my child's poopy diaper after curry.
Worse than a thousand nested ternaries. -
@bittersweet but that's not the butt, that's fecal matter.
You're a programmer, you know that specs need to be specific... :)
But yes, babies are tiny weapons of mass destruction. Even their excrements are useable as bio weapons. -
Sidenote: isn't ternary operator bit of a misleading name? In ternary logic there's three possible states as opposed to boolean logic with two. The ?: operator is really a boolean conditional operator amirite
-
@anux Well in my little brain three operators is something conceptually completely different from three logical states?
-
anux7363y@daglundberg that's alright. It's just that the ary-ness of operator depends on the number of operators. Unary for one operand (!a), binary for two (a+b) and so on.
-
irene33913y@highlight Hah. Yeah I did the same simplification before seeing you already did it. There is some other stuff that I would bug them to change too with naming. Keeping the same naming first.
role.filter(elem => ["view", "use", "admin"].includes(elem))
What I would ask for as a naming fix.
roles.filter(role => ["view", "use", "admin"].includes(role))
More since roles is a string list they could always just do roles.includes("view") when they need to know if the user has view ability. -
irene33913y@bittersweet and I just read your comment now. That seems like the conventional way to do it to me.
Related Rants
Why have you not yet approved my PR?!!?!
The PR:
```
role.filter(elem => elem !== "view" ? elem !== "use" ? elem !== "admin" ? false : true : true : true)
rant
pull requests
code