You’re reading an excerpt from my book on clean code, “Washing your code.” Available as PDF, EPUB, and as a paperback and Kindle edition. Get you...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Be VERY careful here.
Math.floor
and~~
are not the same thing, and are not interchangeable.Math.trunc
and~~
ARE pretty much the same though.More reasons to avoid
~~
!We should not just 'blanket ban' it without understanding it, and knowing that it could be advantageous. Depending on what you are doing,
~~
may be a superior choice - it is MUCH faster (up to 50% faster on Chrome). I know people will say that premature optimisation is evil, or that we shouldn't favour performance over readability... but that is not always true and sometimes speed IS essential.Dogmatically preaching like this decreases understanding of the language and makes for less capable developers.
Lol
I did change it though, so thanks for pointing this out:
github.com/sapegin/washingcode-boo...
Math.trunc() works on 64 bit floats (double), while ~~ works on 32 bit integers (JS converts it before applying the bitwise operators), so
while
👍 Yup! That's why I said 'pretty much' the same (didn't have time to write out the full explanation - thanks for doing that).
It's also worth noting that if you're doing a large amount of processing, and the precision (or lack thereof) of using
~~
is acceptable... then you should probably use it as it can be (environment dependent) much faster thanMath.trunc
... on Chrome it is about 50% faster.Sometimes performance trumps 'cleanliness' in requirements.
Sure. However, I think in that case, you will likely use some more performant way like in32 array buffer to store results, since it would likely matter only when you do a lot of processing of this particular type, otherwise both operations would disappear in the noise...
You should try adding this to your test cases for a big surprise:
I do a lot of reverse engineering for work when the original developers have left and no-one truly knows how the code does what it does. I'm brought in to give new developers something to start with, to understand where to start to look in the code.
A couple of things i'm a fan of is
Imho I using that paralell code as arrow function, which has only one return.
In my taste is ; put on same column as ? and : because in later much easier change that two lines and don't need to be worry about ; after name
This could be a good topic to discuss some ten years ago, and I do agree it's very readable. Now with Prettier it doesn't make any difference though.
(There's a chapter on code style in the book with similar examples.)
Example 4 is wrong. The two snippets of code do 2 different things
Claiming something is wrong without explaining why isn't very helpful.
You need
||
not&&
. The second example is checking that both arrays are not empty. The first just checks that at least one has content.My preference here would be:
You're right! And yet — another reason to avoid clever code ;-)
But the problem was in your 'clean' version. You had no problem describing what the 'non-clean' version did
A balance needs to be struck here though... Avoiding 'clever' code can actually have a detrimental effect on the quality of developers over time (and also on the efficiency of the code):
Preaching 'Clean Code' is Lowering the Quality of Developers
Jon Randy 🎖️ ・ Jun 25 '22