Fighting Boolean Traps

Fighting Boolean Traps

Presented at WaffleJS meetup. Jan 10, 2018. San Francisco.

Ever encounter some code that looks like new Slider(true /*horizontal*/)? Perhaps you are confused by the semantics exhibited by setState(true, false)? Did you get a headache when trying to unpack double negatives as in filter.caseInsensitive = false? These are typical API blunders often denoted as Boolean Traps. Learn how to recognize such a mistake and also more importantly, how to prevent it from happening!

0284b8950e0f4a57bcc092d4dbb98d97?s=128

Ariya Hidayat

January 10, 2018
Tweet

Transcript

  1. 7.
  2. 14.

    s1 = new Slider({ orientation: "vertical" }); s2 = new

    Slider({ orientation: "horizontal" });
  3. 17.
  4. 21.

    Ping-pong, Anyone? // Expand + animate treeItem.setState(true, true); // Expand

    + don't animate treeItem.setState(true, false); // Collapse + animate treeItem.setState(false, true);
  5. 23.
  6. 25.

    this.callMe = "Adam"; flight.from = SFO; flight.to = JFK; this.name

    = "Adam"; flight.departure = SFO; flight.destination = JFK;
  7. 26.

    var x = Math.sqrt(-2) isNaN(x) true x === NaN false

    isNaN('hola') true x == NaN false
  8. 31.
  9. 32.

    SHStripMneumonic “Why is the function SHStripMneumonic misspelled?” Raymond Chen, Microsoft

    http://blogs.msdn.com/b/oldnewthing/archive/2008/05/19/8518565.aspx
  10. 34.
  11. 38.
  12. 40.