Linting standards: Bitwise operators

Colin Clark colinbdclark at gmail.com
Mon Apr 7 09:59:36 EDT 2014


If I remember correctly, Crockford’s prohibition on them in JSLint involved trying to remind developers coming from other languages that using these operators in “clever” ways isn’t actually as fast as they might be in C or Java. Here’s what he said in the Good Parts:

"In Java, the bitwise operators work with integers. JavaScript doesn't have integers. It only has double precision floating-point numbers. So, the bitwise operators convert their number operands into integers, do their business, and then convert them back. In most languages, these operators are very close to the hardware and very fast. In JavaScript, they are very far from the hardware and very slow. JavaScript is rarely used for doing bit manipulation. As a result, in JavaScript programs, it is more likely that & is a mistyped && operator. The presence of the bitwise operators reduces some of the language's redundancy, making it easier for bugs to hide.”

The argument “JavaScript is rarely used for doing bit manipulation” is increasingly not the case. I suspect that use of typed arrays may even allow a runtime to optimize on integers where appropriate. So I see no reason why we shouldn’t allow them. I use "cattt | 0" to truncate numbers in Flocking quite a lot.

+1 for allowing bitwise operators.

Colin

On Apr 6, 2014, at 11:20 AM, Antranig Basman <Antranig.Basman at colorado.edu> wrote:

> Checking with the community about a jshint option that came up in the channel on Friday - do we agree that the prohibition against the bitwise operators (which I believe was simply inherited into jshint from the Crockford days) is irrational?
> 
> Their annotation simply reads
> http://jshint.com/docs/options/#bitwise
> 
> "This option prohibits the use of bitwise operators such as ^ (XOR), | (OR) and others. Bitwise operators are very rare in JavaScript programs and quite often & is simply a mistyped &&."
> 
> IMO just because they are "rare" doesn't mean that they are not to be recommended - also the XOR operator seems indispensable without which we would write monstrosities such as if (!!a !== !!b).
> 
> Are we ok to just turn off this option in our linting?
> 
> Cheers,
> a



More information about the fluid-work mailing list