News for hackers. Stuff that matters.
Re:Backbone has made me a better programmer by jarek-foksa on Friday August 24, 2012 @03:01PM (#4429089)

  matcher: function (item) {
    return ~item.toLowerCase().indexOf(this.query.toLowerCase())
  }
Why there is "~" bitwise operator used in this method?
Re:Backbone has made me a better programmer by ahoge on Friday August 24, 2012 @08:53PM (#4430615)
It turns -1 (indexOf's "not found" result) into 0 (i.e. something falsy). Any other value is turned into something which isn't 0 (i.e. something truthy).

Basically, it's a smug version of:

    return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1;
Writing it like this is a lot clearer. Furthermore it even returns `true` and `false` instead of some truthy/falsy integer.
Re:Backbone has made me a better programmer by voyou on Friday August 24, 2012 @08:50PM (#4430611)
indexOf returns -1 if the query isn't found in the string; -1 is usually represented as all bits 1, so a bitwise not on -1 is 0, which when converted to a Boolean is false. So ~x is more-or-less equivalent to x != -1; but its a bit obscure, and seems like bad style to me (indeed, I'm not even sure if the ECMAScript standard guarantees that -1 is stored as all bits 1, so it may not even by correct).
Re:Backbone has made me a better programmer by ahoge on Friday August 24, 2012 @08:57PM (#4430629)
Numbers are IEEE 754 floating point. It's a separate standard which they just had to reference.

I do agree that it's bad style, because it doesn't clearly communicate the intention. If you want something that returns true or false, you should return true or false.

Re:Backbone has made me a better programmer by voyou on Friday August 24, 2012 @09:12PM (#4430661)
Yeah, but -1 isn't represented in IEEE 754 floating point as all bits one, is it? The ECMAScript standard specifies that the bitwise operators work by first converting their arguments to 32-bit signed integers, but at least how I read it, it doesn't actually say anything about how these 32-bit signed integers are represented (which implies that they continue to be represented as IEEE 754, which isn't how browsers behave, and wouldn't be very useful).
Re:Backbone has made me a better programmer by deafbybeheading on Saturday August 25, 2012 @03:47AM (#4431397)
It's not. And you're right, the spec isn't clear. On the other hand, the current behavior of pretending the bits are arranged in 32-bit two's-complement fashion for appropriate integer values when applying bitwise operators is really the only sensible interpretation (especially since a 64-bit float can represent all 32-bit ints).
Re:Backbone has made me a better programmer by kevincennis on Friday August 24, 2012 @08:55PM (#4430621)
`indexOf` returns -1 if there's no match.

Bitwise NOT on -1 will give you 0 -- for any other number, it'll return a truthy value.

It's the same as:

`return item.toLowerCase().indexOf(this.query.toLowerCase()) !== -1`

If you're really dead set on doing things this way, it's safer to prefix with !! to coerce a real boolean:

`return !!~item.toLowerCase().indexOf(this.query.toLowerCase())`