Comments on: Character saving trickery and bitwise revelations https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: David Aurelio https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2513 Tue, 11 Sep 2012 06:56:39 +0000 https://j11y.io/?p=2143#comment-2513 @Andreas: I agree on binary operators, but the truth is that high level scripting languages are often used by people who never had to do any „binary work“. As soon as they need to, they will hit the binary operator wall.

I get what you mean by switcher variable now. In case of toggling there is certainly a use for ^=, although I wouldn’t use it. To bad the != operator does something completely different …

I have to admit that I was a big friend of smart operator trickery in the past. Working in a team changed my mind. It’s better to make code as easy to grasp as possible. People usually have a better time understanding the underlying concepts and algorithms, if there is less need to explain what the code is actually doing.

In addition, today’s minifiers do a great job. I even abstain from certain JS “idioms”, because the minifier will replace longer constructs with them.

Example (with closure compiler):

if (!n) n = defaultValue; → n||(n=defaultValue);

Sure, every halfwhat experienced JS programmer could or should have seen the short form. But I think the longer version is easier to get. And as of today there is no longer a file size penalty. I just try to keep the cognitive demands of my code as low as I can.

]]>
By: Andreas Göbel https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2512 Mon, 10 Sep 2012 15:55:04 +0000 https://j11y.io/?p=2143#comment-2512 @David:

I think the programming language should dictate my “halfwhat experienced programmer” statement more. However, binary operators are/should be pretty much “above” the layer of a language, hence, if you know how they work you can apply that knowledge in any language.

The “switcher” actually IS a nice geeky thing to have a state variable beyond standard boolean values:

var isReady = 0;
 
// somewhere else
if( isReady ) { }
 
// somewhere else, set isReady state to 1
isReady ^= 1;
 
// somewhere else, set isReady state to 0
isReady ^= 1;

Of course those things should always come along with own little specs (for a team), anyways this specific example shows demonstrates how to accomplish a state-shift without booleans. Is that more readable? No, I don’t think so, but its still very understandable imo. I could also argue that someone just sets a boolean variable to “foobar hey 42 I screwed your code”, so the human factor is always the biggest opportunity for failure.

]]>
By: David Aurelio https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2511 Mon, 10 Sep 2012 14:06:37 +0000 https://j11y.io/?p=2143#comment-2511 It totally depends on the purpose of the code.

Constructs like `~~(num)` are bad, because they hide the intention of the code and might not even work as desired.

If the number to round is already referenced by a variable, it could be floored using n - n % 1. This might not be as readable as floor(n), but it allows to get the intention.

I love to use bitwise operators aswell and I don’t think they decrease readabilty (at least not to any halfwhat experienced programmer I guess).

I think the algorithm should set the level, not the syntax used. Within the topic of parsing unicode and identifying surrogate pairs code like (codePoint & 0xFC00) == 0xDC00 is totally acceptable, because the topic requires knowledge about unicode. And programmers with that knowledge are likely to have knowledge of encodings, i.e. binary representations.

I’m not sure whether I got the term “switcher variable” correctly, but if it is only a condition, using ^= 1 is just a geeky try to appear smart with much opportunity for failure.

false ^ 1 // 1
true ^ 1 // 0
2 ^ 1 // 3 (intended truthyness?)
{} ^ 1 // 1 (whoospie or intended?)

The speed reason for external libraries: Why not benchmark it with

]]>
By: James https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2510 Fri, 31 Aug 2012 13:30:57 +0000 https://j11y.io/?p=2143#comment-2510 @Mathias, I guess I agree regarding drop-in libraries — it’s probably okay to use in those situations. I’m just wary of future maintainers. If a project’s team is known and all are aware of these bitwise idioms then there’s probably no problem.

@Sujay, I would prefer to use the more verbose/slower Math.floor instead of adding a comment for what should be such a rudimentary operation. IMO Having a comment there would just make the situation even more seemingly complex for maintainers.

And `-~` looks cool. Not considered that before.

]]>
By: Mathias Bynens https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2509 Fri, 31 Aug 2012 09:48:52 +0000 https://j11y.io/?p=2143#comment-2509 @Sujay: If it’s code you’re working on in a team, I’d prefer having a readable self-explanatory code snippet instead of a magical but more compact/efficient snippet that needs explanation. But that’s just me.

]]>
By: Sujay https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2508 Fri, 31 Aug 2012 09:28:12 +0000 https://j11y.io/?p=2143#comment-2508 @James @Andreas @Mathias don’t you guys feel that a comment will help your team-mates understand the operation?
Would you go with a comment + bit-wise operation or a “more readable” method/expression?

]]>
By: Sujay https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2507 Fri, 31 Aug 2012 09:22:46 +0000 https://j11y.io/?p=2143#comment-2507 Do you also use -~ for ceiling? Or do you have a better way of doing it?

]]>
By: Mathias Bynens https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2506 Fri, 31 Aug 2012 09:08:04 +0000 https://j11y.io/?p=2143#comment-2506 I have the same problem 🙂 It’s a double standard, really — I know I shouldn’t be using these hacks as they make the code harder to read, but I still do it whenever I can get away with it.

Some of my open-source JavaScript projects are drop-in libraries, and in those cases I make use of bitwise operators for non-bitwise use cases as I see fit. Even if it only helps performance a little bit.

In Punycode.js for example, at some point I need to check if a given BMP code point (guaranteed to be in the range from `0x0000` to `0xFFFF`) is a low surrogate (i.e. in the range from `0xDC00` to `0xDFFF`) or not. You could use the following piece of code for that:

codePoint >= 0xDC00 && codePoint <= 0xDFFF

The following has the exact same effect for all BMP code points, but is much more efficient:

(codePoint & 0xFC00) == 0xDC00

It’s not as readable as the first option, but it does the job and it’s faster. Speed is more important for a drop-in library IMHO.

OTOH, for code that is developed in a team, it’s probably not a very good idea to ‘obfuscate’ the source like this in the name of performance. Sure, there might be a run-time performance gain of a few nanoseconds, but you’ll probably end up confusing your colleagues and wasting minutes of developer time.

]]>
By: James https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2505 Thu, 30 Aug 2012 21:01:54 +0000 https://j11y.io/?p=2143#comment-2505 @Andreas, I agree, they’re really useful. The flooring technique, though, is quite unconventional. I guess it’s not a typical bitwise use-case, and so it might confuse people. I’ve nothing against using bitwise in situations where bits actually need to be manipulated.

]]>
By: Andreas Goebel https://j11y.io/javascript/character-saving-trickery-and-bitwise-revelations/#comment-2504 Thu, 30 Aug 2012 19:46:49 +0000 https://j11y.io/?p=2143#comment-2504 I love to use bitwise operators aswell and I don’t think they decrease readabilty (at least not to any halfwhat experienced programmer I guess).

I also prefer checking for odd/even numbers with “num & 1” instead of math modulo, or switcher variables with “number ^= 1” instead of setting a boolean to true or false explicitly.

Infact, I truly believe bitwise operators used in the rights spots are very convinient and useful at the same time (the performance gain is just sugar on top)

]]>