Hacker News new | ask | show | jobs
by SimonSelg 3561 days ago
Great article. On a side node, however: please don't write code like this:

  var weAreConnected = Math.floor(Math.random() * 10) > 5
  if (weAreConnected === true) {
    this.setState({
      isConnected: true
    })
  } 
  else {
    this.setState({
      isConnected: false
    })
  }
Instead, simplify the logic

  var weAreConnected = Math.floor(Math.random() * 10) > 5
  this.setState({
    isConnected: weAreConnected
  })
The random choice is also overy complicate (and on a side node, the value is more likely to be false then true due incorrect comparision operator usage)

  var weAreConnected = Math.random() < 0.5
  this.setState({
    isConnected: weAreConnected
  })
If we use ES6, we can use the shorthand notation to simplify the code even more

  const isConnected = Math.random() < 0.5
  this.setState({
    isConnected
  })
1 comments

I'm so glad you noticed this - it was bugging me.

I've noticed a pattern in "X vs. Y" articles where the examples of the technology the author doesn't favor are unnecessarily complex.