r/learnjavascript • u/Sweet_Technician_778 • Aug 14 '24
Simplify Your JavaScript Conditionals with Short-Circuiting!
Hey everyone, I wanted to know if there is any way to make your code clean other than short-circuiting in conditionals.
I used to write down like this before using short-circuiting in conditionals
if (user && user.isLoggedIn) {
greetUser(user.name);
}
After using short-circuiting in conditionals
user?.isLoggedIn && greetUser(user.name);
23
u/againey Aug 14 '24
It's cute, but it somewhat masks the intention of the code. Just write the code so that it says clearly and directly what is happening. Other coders (including future-you) will thank you.
if (user?.isLoggedIn) {
greetUser(user.name);
}Or if you want it on one line and your code standards allow it, I often go with the following (but mostly only when the statement is a simple return or continue, or break):
if (user?.isLoggedIn) greetUser(user.name);7
u/jeremrx Aug 14 '24
This.
Code must be readable. Here we can read "if user is logged in, greet user name" (which could be improved by "... greet user") but much better than "user is logged in and also greet user name" : nobody talks like that !
1
u/azhder Aug 14 '24
I would mention that OP may be learning JS from older sources, ones that don’t have
?.in examples or at all
2
u/red-giant-star Aug 14 '24
The first one can be read effortlessly but in the second one there is some mental processing, just a little.
0
u/shgysk8zer0 Aug 14 '24
I write my code to be explicit and avoid implicit type coercion. Other than user?.isLoggedIn, I would not do things like if (user /*...*/). I'd use typeof or instanceof.
Or, another way could be using object destructing:
function something({
isLoggedIn = false,
name,
} = {}) {
if (isLoggedIn) {
greetUser(name);
}
}1
u/Sweet_Technician_778 Aug 14 '24
Thanks for sharing your approach! I can see the value in being explicit and avoiding implicit type coercion, especially for making the code more predictable and easier to debug. Using
typeoforinstanceofcertainly helps with that.I also like the idea of using object destructuring with default values. It’s a clean way to handle potentially undefined properties and makes the function signature more self-documenting.
1
u/shgysk8zer0 Aug 15 '24
It's always a bit embarrassing or something when I say something reasonable and considered, but it's generally downvoted. Especially when not a single person even bothers to give any reason for it.
Best I can tell is that this community is overrun by noobs looking to be lazy and just do things the way their bootcamp told them. Pretty sure anyone with any actual knowledge/experience would just agree with me, especially in the context of JS vs TS. Or anyone publishing a library who understands that TS types have literally zero effect at runtime.
1
u/Sweet_Technician_778 Aug 19 '24
I appreciate you taking the time to share your thoughts. It's always good to have diverse opinions and experiences in the discussion. I understand how frustrating it can be when your contributions don't get the feedback or recognition you'd expect.
As for the JS vs. TS debate, it's a topic where many people have strong opinions, often shaped by their own experiences and the tools they've been trained to use. The conversation is valuable because it brings out different perspectives from seasoned developers or those newer to the field. Let's keep the discussion open and respectful so everyone can learn and grow from it.
1
u/shgysk8zer0 Aug 20 '24
I'm only disrespectful to those instantly antagonistic against me. I have informed reason for how I do things, and have no respect for those instantly condescending and critical of me for doing things differently than they're familiar with.
My disrespect is targeting those already disrespectful. Those who automatically say I'm wrong with caring about why I do things that way at all.
All benefits of TS are lost once you use
<script type="importmap">instead of having everything locally installed. I use Dependabot, an importmap package (allows multiple updates everyone, all at once) and got sick of a dozen PRs for each of my like 200 projects every week. I needed my things to run well on hardware limited machines where running a build on scripts for every change just wasn't feasible (any change could require significant time). I have good reason for my decisions... And I have used eg JSDocs and destructuring of arguments to compensate. I'm not some Neanderthal for using JS instead of TS.
3
u/azhder Aug 14 '24
The second one is not OK in certain circumstances.
What if the value of
user?.isLoggenInends up being a0due to a bug or maybe you getting so used to that idiom that apply it to a number?You should always make sure the value before
&&, whenever falsy, does not return unwanted type/value. There are a few ways to do this, all of them easy.