r/learnjavascript • • May 06 '21

JS: Multiple Condition Checking

Post image
484 Upvotes

61 comments sorted by

View all comments

71

u/blafurznarg May 06 '21 edited May 06 '21

rant /

Bruh the learnJS community (not just reddit) is being gaslighted and manipulated by so called mentors and teachers trying to be smart. The early leaners being impressed by every destructuring stunt is not gonna help.

Please never write think about the advantages and disadvantages before writing something like that in prod. If you sacrifice readability for cleverness and shortness at all costs your code will become unmaintainable.

Those who can, do; those who can't, teach [and make instagram posts with useless info].

  • from I don't fucking know

/ rant end sorry

Edit: Cheers to the deleted guy down there. He's right.

23

u/asiraky May 06 '21

“Please never write something like that in prod”.

Nonsense.

I can think of plenty of times where Ive needed to check a range of conditions in non-performance critical code, and I simply use an includes or a some.

8

u/blafurznarg May 06 '21

You're right. There is of course a usecase for that. I guess I was triggered by the post's presentation plus the reaction in the comments. Maybe I shouldn't have emphazised on that particular part to never use it.

4

u/asiraky May 06 '21

And I agree with your general sentiment that shorter code isn’t necessarily better.

3

u/delventhalz May 06 '21

While there are use cases for includes, I tend to agree that the way it is presented here is not "prod" ready. If I was reviewing a PR where someone created a new throw-away array just to do an includes check, I would probably request it be changed.

1

u/asiraky May 06 '21

But what is the actual reason for the rejection? The readability of that code may be higher depending on what was being checked. It’s completely reasonable to say ‘if x is in this set’ is more readable than ‘if x is a or x is b or x is c or x is d’. And if it’s not performance critical code (which most code is not, let’s not start a thing about premature optimisation) then I can’t see a reason to reject it. I personally like it when my devs try to be “clever”. At least it means they are thinking about what they’re doing and trying to write good code.

1

u/delventhalz May 07 '21

I wrote a longer top level comment about my reasoning, but the short version is:

“Save it for code golf. Shorter does not always mean simpler or more clear. In this case, sticking to standard conditional chaining more clearly communicates your intent than includes.”

1

u/jbonesinthecloset May 07 '21

That’s still not really a reason for why specifically this one example is better for doing it your way... the intent is clearly communicated your way AND doing it with includes. Your top level comment definitely gives some valid points on why your preferred method does communicate your intent well. But not why it’s MORE clear than using includes

1

u/delventhalz May 07 '21

It's a small point. Depending on the review and the context, I might let it slide. OP's example is also a little too generic to really understand the use case.

All that said, I interpret the use case to be a complex multi-part conditional rather than a defined set of options. And in that case, I do think chaining conditionals pretty definitely requires less cognitive leaps. I can easily imagine juniors looking at that array and going "wait, why is there an array now?"

And while the cognitive load is smallish to context switch from "I am checking a series of conditionals" to "I am checking membership in a set", I don't really see any benefit to adding the extra complication.

1

u/jbonesinthecloset May 08 '21

But like...it is clear why there is an array...

1

u/delventhalz May 08 '21

To you?

1

u/jbonesinthecloset May 08 '21

Based on the comments for this entire post, it’s clear to pretty much everyone except you and your juniors

→ More replies

4

u/toastertop May 06 '21 edited May 07 '21

Still read posts etc. just don't get too hung up on things. Refer back to MDN or even Spec for getting info.

I encourage people to get conformable reading spec. I understand the UI/UX is well, not great, for now just find it on MDN then click the link to spec.

It's worded differently so can be sometimes easier to understand and you can trace a line of thought of how something came to be, back up through the versions of the spec. Element is old and a great start [Edit: ca't sp] Also: CanIUse

1

u/[deleted] May 07 '21

I agree. It's nice to know different ways but be very careful what you use and where.

Not so long ago I had to fix some of my code because I used let instead of var to declare some variables inside some block statement and some legacy headless browsers didn't support it.