4 ms·
If you're referring to |0 then I would disagree. It shows how horrible JavaScript is. This is very confusing if you're not familiar with this trick or strange b
by takeda 11y ago
If you're referring to |0 then I would disagree. It shows how horrible JavaScript is. This is very confusing if you're not familiar with this trick or strange behavior of the language.
suit = int(i / 13)
suit = (int)(i / 13)
suit = math.floor(i / 13)
Are much easier to understand for someone not familiar with the code.
- TazeTSchnitzel 11y agoThe behaviour of JavaScript's bitwise operators isn't that unreasonable, but the excessive use of this trick for tiny performance gains at the expense of readability is a shame.
- riebschlager 11y agoI take your point, but that's some crazy non-JS typecasting in your first two examples :)
- adamkochanowicz 11y agosuit = Integer(i / 13) suit = Math.floor(i / 13)
- fenomas 11y agoLanguages are what they are, and if you're going to use one you might as well use it idiomatically. Maybe it's just me, but I see "|0" all the time in JS, not as some kind of crazy performance hack, but used idiomatically to mean "truncate". And on the flip side with JS's lack of number types, any code that looks like it's doing type casts (rather than math) looks out of place. If one must mess around with JS number types then all you can really do is give hints to the optimizing compiler, in which case "|0" is the standard way to hint that you want a small int. Obviously what looks clean is a matter of opinion, but FWIW.