6 ms·
Or this.setState({ isConnected: Boolean(weAreConnected) }) If you want to be super obvious, and aren't using a type system.
by WalterSear 9y ago
Or
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.
- joesb 9y agoIn that case, don't write any code. Don't use function. Don't use React. Don't use lodash. Don't use third party library. 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.
- WalterSear 9y agoOh I understand, it, and used it for years. Boolean() is just easier to spot in unfamiliar code.