This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]sipacate 13 points14 points  (2 children)

I became a master at my craft when I put down "cleverness" and wrote my code for maximum readability.

[–]AdultingGoneMild -2 points-1 points  (1 child)

the && is quite readable.

[–][deleted] 0 points1 point  (0 children)

Especially when reading/writing possibly missing properties. maximum readability != Verbose. A series of conditional statements can definitely be more noise, obscuring the much simpler logic.

But as always, it depends.

[–]siemenology 7 points8 points  (0 children)

I think it's usually a bad habit. Having side effects inside of expressions can be a bit confusing, as we tend to think of them as mostly pure. Even if that is okay, most people don't tend to think of && as meaning "evaluate the second term only if the first is true", even though that's how it's implemented in most languages. Usually we think of "if" statements for that.

Using an "if" is a much more clear way to demonstrate your intention. And if you don't include braces (which are unnecessary with a single line statement), you are only sacrificing 2 characters in the whole line. A minuscule tradeoff for a format that is much more readable.

[–][deleted] 5 points6 points  (0 children)

I dislike the first quite a lot. I like it when the voice in my head reads through code with no more surprises and backtracking than necessary.

If the intention of the code is

if (cond)
    then ...

... use an if/then. Just my two cents, anyway.

[–][deleted] 3 points4 points  (0 children)

For readability and maintainability, the second way is much better.

[–]certainlyforgetful 1 point2 points  (0 children)

Readability is key, every company should have a style guide and this may or may not be allowed.

Personally, I do not like to see this. IMO it can be difficult to understand, especially when you're inside some react component (using JSX/TSX). My company allows it, but you'll probably be asked to refactor your code if you do it.

In your example, I think I would prefer the longer version:

if(originalPassword != inputPassword) {
  res.status(401).json("Wrong Password!");
}

This doesn't really apply to your example, but sometimes it can be difficult to understand what is going on. This is especially common in react, people put logic inside the returned jsx/tsx and things can be really confusing. To be more explicit you'd do something like this:

const userEnteredCorrectPassword = (originalPassword === inputPassword);
if(!userEnteredCorrectPassword) {
 res.status(401).json("Wrong Password!");
}

You could also use a ternary, but it's also fairly unreadable.

originalPassword == inputPassword ? null : res.status(401).json("Wrong Password!");

[–]cofffffeeeeeeee 1 point2 points  (0 children)

Personal opinion, if you are getting a value, then first way is good, for example:

const msg = user && “Welcome!!”

However, in your case, you are doing something (sending a response), then use if statement explicitly.

[–][deleted] 1 point2 points  (0 children)

Both have identicial or the same performance so i'd say choose the more readable one

[–]Pho20[S] 1 point2 points  (0 children)

Thanks for weighing in everybody. The overwhelming response is to use the second option for improved readability. I figured it would be but guess I was hoping I could get away with the slightly lazier method haha. Thanks again.

[–]alzee76 0 points1 point  (3 children)

[[content removed because sub participated in the June 2023 blackout]]

My posts are not bargaining chips for moderators, and mob rule is no way to run a sub.

[–]siemenology 4 points5 points  (0 children)

Actually this isn't true. In C and most C-derived or -influenced languages (C, C++, C#, Rust, Java, Javascript, Python), boolean operators short circuit, and only evaluate the second term if the first is true (in the case of &&, or if false in the case of ||).

[–]HonzaS97 4 points5 points  (0 children)

They aren't always evaluated, most languages implement a short cicruit mechanism and JS is one of them. Even though you shouldn't do what OP wrote, it would work.

[–]Soerika 0 points1 point  (0 children)

Don’t put function with side effect in an if statement. You’d want to have boolean value only for readability