JavaScript is full of surprises and from time to time you may see strangely written source code that is not easy to understand at first glance. Such difficulttounderstand yet effective code phrases are called Idioms and one such idiom is shown in the following line:
Code Editor
You might wonder, what on earth does the ~ symbol mean. Short answer: A bit wise NOT operation. See the following link to read more about JavaScript bitwise operations and take the 3 bit system as an example of the following table:
Easy. Value 1 in 3 bit binary system is represented as 001, value 2 as 010 and so forth. Now, why would anyone use this in JavaScript condition evaluation? To answer this, we have to go a bit further and understand how negative numbers are represented in binary system. The table above shows only positive integers. Let's explore negative integers.
One of the most popular and perhaps most spread way to represent negative integers in computing is called Two's complement.
Two's complement is a mathematical operation on binary numbers, as well as a binary signed number representation based on this operation
By its definition, to convert a given integer into a negative integer we have to flip the bits by applying the NOT operation and add 1. Two's complement can handle a range expressed by the following formula:
−(2^N−1) to +(2^N−1 − 1)
Our 3 bit example system would therefore cover values from 4 to +3. Its binary representation is shown in the following table:
Now when two's complement method is clear let's get back to JavaScript. I'm sure you're aware of JavaScript's indexOf method. Whether used on arrays or strings it always returns an index of searched element. When an element is not found the function returns 1.
The indexOf() method returns the first index at which a given element can be found in the array, or 1 if it is not present.
I'm convinced things start to get sense to you now. In the binary table above we can clearly see that negative 1 integer will be represented as 111. As we also know the ~ symbol which is a bitwise NOT operation that inverts the bits the question now is:
if we are looking for an element in an array and it is not being found  what will the indexOf return? Right, the 1 (or 111 binary). What will the ~ symbol cause? Right, flip the bits and make it 000. What does the 0 equal to in JavaScript's expression evaluation? Right, it's evaluated as falsy expression and thus the FALSE.
So by the following code:
..we're asking if the value does exist in the array by asking if it's not equal to false by using JavaScript's truthy & falsy evaluation where 0 means false and whatever else means true.
In other words  if it doesn't exist  the indexOf function will return 1 which equals to binary 111 which equals to 000 when inverted by ~ symbol which then equals to FALSE when evaluated by JavaScript's truthy/falsy algorithm. In any other case the condition will succeed because only 1 will give binary 000 when inverted. So when an element is not found the condition can be also understood as following code:
Code Editor
Such idioms are sometimes hard to read but once fully understood it becomes usually a shorter way to write a code. Some programmers don't like these constructs as they are harder to read. I'm not going to agree or defend these opinions as in the end they are question of preferences. The thing is that It's important to be able to read them because you never know where you might need them.
0 Comments
Of course, the whole idea about insertion sort being done in TSQL is absurd. The following lines were written just for fun and also to point out that it happens surprisingly often that I see other programmers approaching the SQL in a procedural way. SQL is a declarative language and works the best when is approached in a declarative way. It's not C# or object oriented language in general. Rather a set oriented language. How often have you seen usage of cursors, loops and other similar constructs in TSQL on cases where it could have been done just using a plain SQL? It wasn't so rare in my career. Such an usage is sometimes necessary however many times it was a quick and of course a dirty solution for a given task. If you don't know what the insertion sort is check the wikipedia page about insertion sort Consider the insertion sort algorithm that is in O(N^2). The following TSQL code tries to sort an array of integers by implementing a couple of LOOP statements. First, let's prepare example data
This simply adds 1000 of random integers to an array. No big deal. Now, let's implement the insertion sort. TSQL implementation of insertion sort
Terrible, right? I know. When you could simply do the following instead: ORDER BY
Here comes the best part. Compare the times of both methods. On my Core i7, 8GB RAM machine the results are as follows:
This is of course an extreme case. I believe nobody would really consider the sorting being done manually. This article only demonstrates that TSQL implementations quite often defeat the holy idea of SQL.

AboutBlog about my programming experiments, tests and so on. Categories
All
Archives
November 2016
