DEV Community

Explicit is better than tricks

therealnrf on January 15, 2022

When writing code, it is always better to be as explicit as possible instead of going for tricks. This concept is best explained by examples so let...
Collapse
 
mistval profile image
Randall

I pretty much agree with this, but what is and isn't a trick is largely a matter of perception, and may change with time. Specifically, I remember learning !! to convert something to a boolean, and I didn't regard it as a trick. To me, it was just the way you convert something to a boolean. It's what I and the people around me did. I don't think I even knew about Boolean() then.

I think the perception that it's a fancy trick is one that has grown and become more common over time. Now I too regard it as one, and use Boolean() instead.

Collapse
 
therealnrf profile image
therealnrf

That is exactly what we wish to rectify.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Very bad idea. Will only drag down the quality of developers over time.

Teach people all about the language, don't blindly preach dogma and opinions. Show them the ins and outs of it, how it works, and different ways of doing the same things. Don't patronise them and treat them like children unable to make decisions about which way to do it is best for them or their project.

Readability is purely subjective, and marking parts of the language as 'off limits' or 'bad practice' inhibits knowledge, and potentially the performance of software.

It's this kind of thinking that has progressively dragged down and overcomplicated software over the past decade or more, particularly in web development.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

A lot of the time, they do add performance benefit - and it can be considerable (a quick test showed !! to be consistently faster than Boolean - more than twice as fast in fact)

Collapse
 
aminya profile image
Amin Yahyaabadi • Edited

Boolean is as fast as !!. Your test is not accurate.
github.com/solidjs/solid/pull/500#...

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Run this in both Chrome and Firefox. !! is faster in both, and on Firefox it is almost twice as fast

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

If you want the simplest test possible, try this - not quite such a pronounced difference, but !! still faster

Thread Thread
 
aminya profile image
Amin Yahyaabadi • Edited
Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Interesting. It's faster for me both on Mac, Linux, and Android - on both Firefox and Chrome

Collapse
 
therealnrf profile image
therealnrf

That is some quality input. Appreciated!

Just a side note about type casting. Kind of have to deal with it in JavaScript since all incoming data from the DOM is of type string!

Collapse
 
moopet profile image
Ben Sinclair

I agree with all your points, but could you change the links to something more descriptive than, "click here"?

A screen reader listing links out will speak them all the same.

Collapse
 
therealnrf profile image
therealnrf

Thanks for highlighting this. I've updated the link texts.