6 ms·
OK, this isn't really important for the quintessence of the article. But I'm surprised again and again how many developers write code like if (weAreConnected
by Rockslide 9y ago
OK, this isn't really important for the quintessence of the article. But I'm surprised again and again how many developers write code like
if (weAreConnected === true) {
this.setState({
isConnected: true
})
}
else {
this.setState({
isConnected: false
})
}
}
instead of
this.setState({
isConnected: weAreConnected
})
- WalterSear 9y agoOr this.setState({ isConnected: Boolean(weAreConnected) }) If you want to be super obvious, and aren't using a type system.
- sheetjs 9y agoor this.setState({ isConnected: !!weAreConnected })
- WalterSear 9y agoI prefer boolean, easier to see what's going on. And, bo-tab( is only two more characters :)
- debaserab2 9y agoIt's not any easier if you understand what !! does. Pretty common usage across many languages.
- gruez 9y ago>It's not any easier if you understand what !! does 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=45044 https://blogs.msdn.microsoft.com/oldnewthing/20150525-00/?p=...
- srssays 9y ago!! is an extremely common idiom, across multiple different programming languages. Code should be written so that it is understandable by other professional programmers, not people who started programming two weeks ago.
- WalterSear 9y agoAs a professional programmer, I've come to prefer code written to be understood by someone who started two weeks ago.
- debaserab2 9y agoThen by that principle you'd want to use !! since casting via function or constructor differs more between languages.
- WalterSear 9y agoWhat it looks like in other languages is only minorly relevant to its obviousness and readability.
- debaserab2 9y agoI don't see the argument that Boolean(val) "reads easier" than !! as long as you know what !! does.
- WalterSear 9y agoOh I understand, it, and used it for years. Boolean() is just easier to spot in unfamiliar code.
- Mc_Big_G 9y agoI couldn't agree more. I wouldn't accept that from even a junior developer and yet you can easily find "senior" devs writing this crap. It seems like some devs will never understand that less code is better or that ease of reading matters. This is either lack of skill or laziness, neither of which are acceptable.
- vmarsy 9y agoWhat if `weAreConnected` is `null` or `undefined`? 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.
- debaserab2 9y agothis.setState({ isConnected: !!weAreConnected })
- hypervis0r 9y agoThis^, plus, you shouldn't really have "null" or "undefined" variables which you treat as bool (if you do, then that's a bug)
- DeonPenny 9y agoDing ding ding right answer. This is the way most senior javascript devs would right it.
- corn13read 9y agoThey'd right it if they were fixing it or write it if they were writing it ;)
- Rockslide 9y agoIt certainly doesn't hurt to "cast" the input to boolean. But your second example is the next code smell, because it should just be if (!this.state.isConnected) { /* do stuff */ } (which still works as expected even when dealing with null or undefined).
- vmarsy 9y agoAgreed, but you can't anticipate what another dev could write :) 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.
- mamcx 9y agoThe replies below show why, because JS. Bad languages don't allow a nice life.
- oceanswave 9y agoIt's funny how you can spot the junior devs, who are accustomed to one thing and refuse to learn another. There is a beauty about the JavaScript language that most people don't get, and how a lot of behavior can be added after the fact or mutated. Design time type safety is not the end all be all of languages
- mamcx 9y agoBut inconsistency is truly a problem. Is not that JS is just "crazy" but with a method on his madness. Is that is just not well designed. So, how we can claim to "developer good software" in front of our customers and defend tools like this?
- ricardobeat 9y agoThat goes to show how much salt you should take with the rest of his opinions on JS. Yes, dependency hell is a problem, yes, no type safety (though you can choose TS), the rest? Not so much unless you're trying to hammer JS into the shape of your old favourite language.
- microcolonel 9y agoThere's also the 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
- joesb 9y agoit depends on how you want to structure your code and give sections to code. "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.