News for hackers. Stuff that matters.
Re:Why I'm not a React Native developer by Rockslide on Saturday August 19, 2017 @03:06PM (#15055029)
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
  })
Re:Why I'm not a React Native developer by vmarsy on Saturday August 19, 2017 @03:33PM (#15055134)
What 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.
Re:Why I'm not a React Native developer by debaserab2 on Saturday August 19, 2017 @03:40PM (#15055166)
this.setState({ isConnected: !!weAreConnected })
Re:Why I'm not a React Native developer by hypervis0r on Saturday August 19, 2017 @03:46PM (#15055207)
This^, plus, you shouldn't really have "null" or "undefined" variables which you treat as bool (if you do, then that's a bug)
Re:Why I'm not a React Native developer by DeonPenny on Saturday August 19, 2017 @07:37PM (#15056218)
Ding ding ding right answer. This is the way most senior javascript devs would right it.
Re:Why I'm not a React Native developer by corn13read on Sunday August 20, 2017 @03:40AM (#15057381)
They'd right it if they were fixing it or write it if they were writing it ;)
Re:Why I'm not a React Native developer by Rockslide on Saturday August 19, 2017 @03:41PM (#15055176)
It 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).
Re:Why I'm not a React Native developer by vmarsy on Saturday August 19, 2017 @03:50PM (#15055224)
Agreed, 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.

Re:Why I'm not a React Native developer by mikeycgto on Saturday August 19, 2017 @05:48PM (#15055810)
That's why there's PropTypes so you can define what type a given prop should be. An error is raised if a higher level component passes in a prop of an incorrect type.
Re:Why I'm not a React Native developer by maxxxxx on Saturday August 19, 2017 @04:04PM (#15055289)
this.setState({ isConnected: (weAreConnected === true) })

seems 100% equivalent

Re:Why I'm not a React Native developer by xamuel on Sunday August 20, 2017 @01:10AM (#15057039)
Another flavor of this surprise: JSON.stringify doesn't "see" undefined. JSON.stringify({foo:undefined}) returns "{}" :)
Re:Why I'm not a React Native developer by oceanswave on Sunday August 20, 2017 @07:38PM (#15060837)
Because type (prototype) and data (Json) are two different things, if you need it -- JSON.stringify(obj, (k, v) => v === undefined ? null : v);
Re:Why I'm not a React Native developer by WalterSear on Saturday August 19, 2017 @03:09PM (#15055041)
Or

  this.setState({
    isConnected: Boolean(weAreConnected)
  })
If you want to be super obvious, and aren't using a type system.
Re:Why I'm not a React Native developer by sheetjs on Saturday August 19, 2017 @03:21PM (#15055088)
or

    this.setState({ isConnected: !!weAreConnected })
Re:Why I'm not a React Native developer by WalterSear on Saturday August 19, 2017 @03:37PM (#15055150)
I prefer boolean, easier to see what's going on. And,

   bo-tab(
is only two more characters :)
Re:Why I'm not a React Native developer by debaserab2 on Saturday August 19, 2017 @03:41PM (#15055173)
It's not any easier if you understand what !! does. Pretty common usage across many languages.
Re:Why I'm not a React Native developer by gruez on Saturday August 19, 2017 @03:47PM (#15055208)
>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=...

Re:Why I'm not a React Native developer by srssays on Saturday August 19, 2017 @04:34PM (#15055442)
!! 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.

Re:Why I'm not a React Native developer by WalterSear on Saturday August 19, 2017 @05:36PM (#15055766)
As a professional programmer, I've come to prefer code written to be understood by someone who started two weeks ago.
Re:Why I'm not a React Native developer by joesb on Sunday August 20, 2017 @02:59AM (#15057293)
In 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.

Re:Why I'm not a React Native developer by debaserab2 on Saturday August 19, 2017 @06:05PM (#15055881)
Then by that principle you'd want to use !! since casting via function or constructor differs more between languages.
Re:Why I'm not a React Native developer by WalterSear on Saturday August 19, 2017 @06:22PM (#15055959)
What it looks like in other languages is only minorly relevant to its obviousness and readability.
Re:Why I'm not a React Native developer by debaserab2 on Saturday August 19, 2017 @06:48PM (#15056067)
I don't see the argument that Boolean(val) "reads easier" than !! as long as you know what !! does.
Re:Why I'm not a React Native developer by goatlover on Saturday August 19, 2017 @07:39PM (#15056223)
Wouldn't any programmer recognize that Boolean(val) is a cast to a True/False value that's most likely an object, given the uppercase?
Re:Why I'm not a React Native developer by inimino on Sunday August 20, 2017 @07:13PM (#15060713)
Perhaps, but it's a primitive, not an object.
Re:Why I'm not a React Native developer by prewett on Saturday August 19, 2017 @07:05PM (#15056116)
I'm curious, which languages? I've never seen any of my colleagues write !!, ever, in C/C++, Java, Python, or Swift. I've also not seen it C# or Go, but my exposure is limited.
Re:Why I'm not a React Native developer by ashark on Saturday August 19, 2017 @07:46PM (#15056243)
I've been paid to write code in Perl, PHP, Ruby, JS, Go, ObjC, and Java, plus a little C and Python, for going on 15 years. I'm pretty sure I've never written "!!" and I think I could count the times I've seen it used on one hand (maybe never in any codebase I've worked on, but I'm not entirely sure)
Re:Why I'm not a React Native developer by oceanswave on Sunday August 20, 2017 @07:40PM (#15060842)
I feel sorry that they paid you then
Re:Why I'm not a React Native developer by xamuel on Sunday August 20, 2017 @01:05AM (#15057027)
Re:Why I'm not a React Native developer by savanaly on Saturday August 19, 2017 @07:31PM (#15056199)
It's one of the earliest things I can remember learning in my intro to programming classes in college. Those were in Java.
Re:Why I'm not a React Native developer by hirsin on Sunday August 20, 2017 @12:34AM (#15056969)
I spend chunks of every day looking at a C/C++ codebase and have never seen this. Taught Java for a year in college, I have never seen this. 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.
Re:Why I'm not a React Native developer by debaserab2 on Sunday August 20, 2017 @02:56AM (#15057286)
I'm pretty surprised to hear this from someone who taught a programming course. It actually makes a ton of sense when you evaluate what is happening: it's the negative unary value and then the negative unary result of that value - there's no possible way it could be anything other than true or false. It's more of a side affect of the unary operator ! than it is a language feature, which I believe is why it sees such ubiquitous implementation.

> 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.

Re:Why I'm not a React Native developer by empthought on Sunday August 20, 2017 @10:19AM (#15058481)
You're kidding, right? "!!foo" is a complete no-op in Java. It is never needed and never correct.
Re:Why I'm not a React Native developer by lomnakkus on Saturday August 26, 2017 @07:36PM (#15108074)
You're basically wrong on every single point. It's a syntactic hack in JS to get a guaranteed boolean coersion, but for anything else it's an absolutely pointless abomination. (Maybe PHP has a similar thing, but I haven't programmed in PHP, so I wouldn't know.)
Re:Why I'm not a React Native developer by lomnakkus on Sunday August 20, 2017 @08:15AM (#15058099)
In Java? What purpose would it serve there?
Re:Why I'm not a React Native developer by GordonS on Sunday August 20, 2017 @05:17AM (#15057637)
What? I've been coding in multiple languages for multiple platforms for almost 30 years and have never come across it. It seems rather unlikely that it's 'extremely common'
Re:Why I'm not a React Native developer by slrz on Wednesday August 23, 2017 @05:39PM (#15085441)
It is very common in C code, where for most of its history you hadn't any explicit boolean data type but used int, following the rule that zero is false and anything else is true.

The '!!expr' is the idiom most people use whenever the need for a canonical 0/1 representation arises.

Re:Why I'm not a React Native developer by WalterSear on Saturday August 19, 2017 @05:35PM (#15055758)
Oh I understand, it, and used it for years. Boolean() is just easier to spot in unfamiliar code.
Re:Why I'm not a React Native developer by ricardobeat on Saturday August 19, 2017 @05:02PM (#15055583)
That 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.
Re:Why I'm not a React Native developer by microcolonel on Saturday August 19, 2017 @08:01PM (#15056277)
There'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
Re:Why I'm not a React Native developer by joesb on Sunday August 20, 2017 @03:45AM (#15057395)
it 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.

Re:Why I'm not a React Native developer by mamcx on Saturday August 19, 2017 @04:37PM (#15055449)
The replies below show why, because JS.

Bad languages don't allow a nice life.

Re:Why I'm not a React Native developer by oceanswave on Sunday August 20, 2017 @07:44PM (#15060862)
It'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
Re:Why I'm not a React Native developer by mamcx on Monday August 21, 2017 @04:38PM (#15067673)
But 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?

Re:Why I'm not a React Native developer by Mc_Big_G on Saturday August 19, 2017 @03:26PM (#15055113)
I 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.