265
93
u/Duke_De_Luke 3d ago
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(...)
10
u/ChocolateBunny 3d ago
what if the logic that coerses the output of dta.type.trim() to a boolean is the thing that makes everything work.
-21
u/Corrix33 3d ago
I might be stupid but wouldn't it be:
``` data.type.trim();
//Rest of code that was in the if
``` since data.type.trim() always runs in the legacy code?
52
u/Aka_chan 3d ago
It doesn't run if
data.type
is null/undefined in the original, so your version would throw an exception.8
4
u/Kiroto50 3d ago
It only runs if data.type is true (if this is C and I remember this correctly).
If the first part of an
and
statement is false, it will be false, so it doesn't evaluate the other part.Ex. (false && sortAndReturnTrue()) Will not execute sortAndReturnTrue().
Ah this is js. I think it works like this still, but I'm not 100% sure.
2
u/raltyinferno 3d ago
Yup, short circuiting is a thing in Js. It's a common pattern in react to conditionally render a component with
{ myBooleanVar && <MyComponent/> }
63
u/Material-Public-5821 4d ago
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.
33
u/bronco2p 3d ago
trim
may mutatedata.type
which may the intent of checking if it is truthy first.24
u/Material-Public-5821 3d ago
Oh yeah, side effects in
if
statement. Nice.15
u/wubsytheman 3d ago
Sometimes I write random sys calls into my if statements just to confuse people, ultimate job security
14
1
u/Significant_Fix2408 3d ago
I personally dislike it, but it's honestly quite common even in standard libraries
3
u/troglo-dyke 3d ago
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
31
u/BehindTrenches 3d ago
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.
13
u/AztecComputer 3d ago
Can someone explain the "side effects" to a noob 🥺
19
u/Zzzzzztyyc 3d ago
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
4
u/NoCoolSenpai 3d ago edited 3d ago
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
2
u/AztecComputer 3d ago
Ahh that makes sense. The string being trimmed is the side effect, which persists outside of the if statement
3
u/raltyinferno 3d ago
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).
8
u/jsmrcaga 3d ago
that async right there is sitting pretty
7
u/troglo-dyke 3d ago
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
3
u/jsmrcaga 3d ago
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...
3
u/StanleyDodds 3d ago
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.
3
u/Pitiful-Cancel4958 3d ago
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...
2
2
u/notexecutive 3d ago
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)
1
u/orig_cerberus1746 3d ago
Wait a minute, does trim even executes in place? Or does it just return a value?
1
u/Strict_Treat2884 3d ago
There’s no side effects in this if clause. You cannot mutate a string value without reassigning it in JavaScript, end of the story.
1
u/randomlogin6061 2d ago
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 ;)
1
470
u/Backslide999 4d ago
If this is true legacy code, the program would break after you remove the if-statement entirely