DEV Community

Discussion on: How I Made A Gradient Generator

Collapse
 
deadcoder0904 profile image
Akshay Kadam (A2K)

I think its a good opportunity to familiarize yourself with double tilde. Or let alone a single one. I remember it like this ~x = -(x+1) & ~~x = x (sort of but double tilde only results in 0 or 1).

For example, ~-1 = 0, ~0 = -1, ~2 = -3, ~~1 = 1, ~~0 = 0, ~~true = 1 & ~~false = 0.

I know its difficult to understand but it sometimes make your code simpler. I didn't use it before but now use it all the time. Its easier to write if (~x) then if (x !== -1) 😝

Collapse
 
vas_stergioulis profile image
Vasilis Stergioulis

Using a bitwise operator for explicit conversions is a hack. The complicated answer you are giving is the exact reason for not doing it in the first place. Six months later someone (even the future you) will struggle to understand what you did.

Anyway, if you feel that it's better for you, continue with that but explain it with remarks in the code. The added remarks will be a lot more than writting it simpler. And you will do a big favor to the future you.

Thread Thread
 
deadcoder0904 profile image
Akshay Kadam (A2K)

Naah, I only use it for small things. If I complicate it a lot, you're right I won't get it. As long as KeyStrokes are reduced & Code is easy to read, its all good like in the above post.