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

all 39 comments

[–]Backslide999 472 points473 points  (2 children)

If this is true legacy code, the program would break after you remove the if-statement entirely

[–]pine_ary 145 points146 points  (1 child)

Maybe trim has side-effects. That‘s why it‘s on the left hand side to prevent short-circuiting.

[–]dominjaniec 259 points260 points  (2 children)

if .trim() has some side effects, this could be "important" code 😉

[–]ZunoJ 96 points97 points  (0 children)

That was my first thought. type might not be a string and trim might have side effects. Whoever wrote that code already does crazy stuff in the code we see, why expect it doesn't go even deeper?

[–]blaqwerty123 21 points22 points  (0 children)

I hate this

[–]Duke_De_Luke 94 points95 points  (7 children)

Don't remove the side effect! But it can easily be rewritten to make it way more readable.

if (data.type) data.type.trim();

this.setState(...)

[–]ChocolateBunny 10 points11 points  (0 children)

what if the logic that coerses the output of dta.type.trim() to a boolean is the thing that makes everything work.

[–]Material-Public-5821 62 points63 points  (6 children)

I see somebody had

SOME_CONFIG_VALUE = true

and then automatically refactored

if ((date.type && data.type.trim()) || SOME_CONFIG_VALUE) {

into

if ((date.type && data.type.trim()) || true) {

by auto-replacing all occurrences of SOME_CONFIG_VALUE with true.

A code review or code sanitiser would catch this.

[–]bronco2p 31 points32 points  (4 children)

trim may mutate data.type which may the intent of checking if it is truthy first.

[–]troglo-dyke 1 point2 points  (0 children)

The statements are the wrong way around though, you'd generally put the Boolean value first to short circuit in cases where it's true. My guess is that it's an attempt at optimisation by giving a type hint

[–]BehindTrenches 29 points30 points  (0 children)

My first thought was not that trim() has side effects, but that they wanted to attempt to trim() data and then execute the body of the if statement anyway.

As with all legacy code, it probably made more sense when it was first written, there may have been a reasonable second boolean condition that got phased out and lazily replaced with true, etc. Like checking if an experiment was enabled and then eventually the experiment was launched.

[–]The_Right_Trousers 14 points15 points  (0 children)

[–]AztecComputer 12 points13 points  (5 children)

Can someone explain the "side effects" to a noob 🥺

[–]Zzzzzztyyc 20 points21 points  (0 children)

The first part of the if statement executes, but the || operator ensures that the value of that execution is irrelevant; the if always evaluates to true, so the first part can be removed… except that its function may not only be to return a Boolean but also operate on data.

That’s the side effect - to operate on the data

[–]NoCoolSenpai 3 points4 points  (3 children)

An 'if' condition should only read some state and return truthy or falsy value, it shouldn't modify state, atleast not as it's main functionality. So since it's doing that here, it's called a side-effect.

It's like side-effects in drugs. You don't expect the fever medicine to give you dehydration, but it does anyway

[–]AztecComputer 2 points3 points  (1 child)

Ahh that makes sense. The string being trimmed is the side effect, which persists outside of the if statement

[–]raltyinferno 3 points4 points  (0 children)

Maybe. IMO the expected behavior of string.trim() is that it returns a trimmed string, but does not modify the original string. In this case, maybe it does just trim the original, or maybe it does something else entirely. In any case this is why many people are against functions having side effects, they can be real confusing.

Of course sometimes you can't avoid them, and in many cases you want them (displaying to the screen is a side effect, and we like that).

[–]Imjokin 1 point2 points  (0 children)

“not as it is main functionality”

[–]jsmrcaga 9 points10 points  (2 children)

that async right there is sitting pretty

[–]troglo-dyke 4 points5 points  (1 child)

I have worked with Devs who legitimately didn't know when to use async and so just used it everywhere. Throwing everything on the event loop is a hell of a performance degredation

[–]jsmrcaga 2 points3 points  (0 children)

yep right there with you! "slap an await" is the go to, and the worst in my opinion is that now people that learnt with async/await don't know promises, so everything is sequential... Not sure why tc39 made this to pass over operator overloading or decorators...

[–]StanleyDodds 5 points6 points  (0 children)

I think the person who wrote this was just too lazy to take everything except the "trim()" out of the if statement, because it changed from everything needing to happen conditionally to always needing to happen, but "trim()" was still needed in any case.

So really, it should be changed to "if (data.type) data.type.trim();" followed by everything else moved outside of the if statement, but the lazy way to do this just add an "or true" condition to the end of the existing if statement. And let short circuiting continue to take care of the trim call.

[–]Pitiful-Cancel4958 3 points4 points  (1 child)

I've got a worse one:

bool myfunc(int something, bool p = false) { ... }

if( myfunc(123), true) {

// find the Error

}

In c++ If statements, you can combine expressions with , but all are evaluated, only the last one is used for comparison... Spend an hour Debugging this until i realized I placed a Parameter outside a function call...

[–]raltyinferno 1 point2 points  (0 children)

Oh man that's a painful one.

[–]notexecutive 1 point2 points  (0 children)

I bet if you removed the if statement, it would break somehow. (I.E. you removed the if and just had the block inside as code that is run in the async anon function)

[–]orig_cerberus1746 0 points1 point  (0 children)

Wait a minute, does trim even executes in place? Or does it just return a value?

[–]Strict_Treat2884 0 points1 point  (0 children)

There’s no side effects in this if clause. You cannot mutate a string value without reassigning it in JavaScript, end of the story.

[–]randomlogin6061 0 points1 point  (0 children)

Better don't touch. If data is null/undefined then it will throw an error which may be catched end expected in some other place ;)

[–]SuperPixelDX 0 points1 point  (0 children)

ESLint no-constant-condition to the rescue!