if (weAreConnected === true) {
this.setState({
isConnected: true
})
}
else {
this.setState({
isConnected: false
})
}
}
instead of this.setState({
isConnected: weAreConnected
}) if (weAreConnected === true) {
this.setState({
isConnected: true
})
}
else {
this.setState({
isConnected: false
})
}
}
instead of this.setState({
isConnected: weAreConnected
})WalterSear's solution solves the issue since both `null` and `undefined` would become `false`:
this.setState({
isConnected: Boolean(weAreConnected)
})
but in your case you could set `isConnected` to `null` or `undefined`so if later in code someone makes the mistake of writing:
if (this.state.isConnected === false) { /* do stuff */ }
they could have a bad surprise. if (!this.state.isConnected) { /* do stuff */ }
(which still works as expected even when dealing with null or undefined).Or if you pass down that `isConnected` down to a library where for some reason they assumed `null` should default to `true`
Of course it's very unlikely, but my point is you can't know all use cases of `this.state.isConnected` in advance on a large project so as you say it doesn't hurt to sanitize the input.
seems 100% equivalent
this.setState({
isConnected: Boolean(weAreConnected)
})
If you want to be super obvious, and aren't using a type system. this.setState({ isConnected: !!weAreConnected }) bo-tab(
is only two more characters :)which only applies if you're writing code for yourself. you can't expect everyone to know that idiom. same with using the + operator to cast to number, or an extreme case, the tadpole operator https://blogs.msdn.microsoft.com/oldnewthing/20150525-00/?p=...
Code should be written so that it is understandable by other professional programmers, not people who started programming two weeks ago.
People who only start coding for two weeks can't understand any code.
People who start has enough experience to work, but only start in this project for two weeks, will understand "!!" idiom.
> I would leave a note to a student for using this to be honest - too unclear, and (as this thread denotes) not well known enough to put in production worthy code.
You've done a total disservice to your students that understood negative unary operations then. Given how many upvotes my initial post on this thread has (currently 38) I don't think the thread denotes what you think it does at all.
The '!!expr' is the idiom most people use whenever the need for a canonical 0/1 representation arises.
if isConnected {
backgroundColor = .green
} else {
backgroundColor = .red
}
bit, which should be a ternary.I figure that a good rule is: if the feature existed in C, you should know and understand it. If that feature is convenient and doesn't sacrifice performance, then you should use it.
Then there's this blatant misuse of the arc4random API, which I'm pretty sure the manpage tells you about.
let weAreConnected: Bool = arc4random()%10 > 4"if (isConnected) {} else {}" emphasizes to me that there are two branches of code which depends on `isConnected` state. It may have only one thing to do right now. But this is major branch. Future git diff will probably show that the code in the body the branch are added/removed.
Ternary expression emphasizes on modification of a single attribute.
Bad languages don't allow a nice life.
So, how we can claim to "developer good software" in front of our customers and defend tools like this?