Booleans should be just True and false
TL;DR: Don't do magic castings to boolean. You will regret on a friday night.
Problems
Hiding Errors
Accidental complexity coupled to one particular language.
Readability
Difficulty to hop among languages.
Solutions
Be explicit.
Work with booleans for boolean conditions. Not integers, not nulls, not strings, not lists. Just booleans.
Sample Code
Wrong
virus = ['MERS', 'SARS']
vaccines = []
if vaccines:
print ("let's get vaccinated")
else:
print ("We have no vaccines yet. Keep researching")
if virus:
print ("There are some virus around. Take extra care")
else:
print ("We are free to get out. Not masks are necessary")
#equivalent
if not vaccines:
print ("We have no vaccines yet. Keep researching")
else:
print ("let's get vaccinated")
if not virus:
print ("We are free to get out. Not masks are necessary")
else:
print ("There are some virus around. Take extra care")
Right
if len(vaccines) == 0:
print ("We have no vaccines yet. Keep researching")
else:
print ("Let's get vaccinated")
if len(virus) == 0:
print ("We are free to get out. Not masks are necessary")
else:
print ("There are some virus around. Take extra care")
Detection
This is a language feature. Some strict languages show warnings with this magic wizardry.
Tags
Coercions
Primitive
Conclusion
Some languages encourage doing some magic abbreviations and automatic castings. This is a source of errors and a Premature Optimization warning.
We should always be as explicit as possible.
Relations
Code Smell 06 - Too Clever Programmer
Maxi Contieri ・ Oct 25 '20 ・ 2 min read
Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)
Maxi Contieri ・ May 4 '21 ・ 2 min read
More Info
It is not the language that makes programs appear simple. It is the programmer that make the language appear simple!
Robert Martin
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20 ・ 13 min read
This article is part of the CodeSmell Series.
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21 ・ 4 min read
Last update: 2021/07/07
Discussion (17)
Go doesn't allow conversions from and to boolean type altogether.
Meanwhile in C, it's normal to test an integer as boolean, but I'd rather write
if (i == 0)although I know the compile will optimize the comparison away.Shouldn't that be
if (i != 0)...?what is 'i' ?
what real world entity does it represent ? the letter 'i' ? Why would you compare a letter with a number ?
Considering the example talks about C,
iis probably a counter variable for a numeric loop.i think we should name it 'counter' or 'index'
We are doing software, not math
My real code uses the variable name
res.github.com/usnistgov/ndn-dpdk/blob...
That
likelyis not "premature optimization". This code is for a high speed router that processes millions of packets per second.I don't know what 'res' stands for.
But if you performed a real benchmark on the code and found out it really needed to be optimized it does not classify as 'premature'.
I'm ok with that since in this very special case speed is more important than readability
resmeans result. It is common in C code.ok. that's why i don't like low level languages. They are more cryptic.
I try to specialize on higher level languages. Most code smells apply to them.
I suggest not to use 'res' 'i' ir 'result' as variable names since they have no business meaning.
more examples here:
maximilianocontieri.com/what-exact...
certain things like
resandiare so common that I'd consider them expressive variable names, but there's not many. Forres, I really like the pascal feature of assigning to the subroutines name, because presumably that's already a descriptive name for the result value.re not liking low level languages, it seems to me like high- and low-level languages have completely different appeals (I happen to like both of them), so one might like one but not the other. Thinking in systems and poking at bits are as much different areas as back-end and front-end.
you are absolutely right. They are different tools for different purposes
Depending on whether you want true or false. I usually test for error condition and return early.
Exactly ! Let the compiler do it for you if it is safe.
Go is much stricter than C. I think Go lacks several important features like Exceptions or Full Closures. Once it catches up with older languages it would be an excellent option
I hate languages that treat "empty" values as falsey. It's dumb and leads to unexpected errors. An empty array is still an array, an empty string is still a string, zero is still a number. Neither of them has anything "false" about them and languages that treat them as such are just poorly designed (low-level languages like C being the exception, for obvious reasons)
I really prefer how this would work in ruby:
It's closer to human thinking.
i agree.
empty is much better than strlen()=0
I'm a bit confused here. You say to not use coercion, but your 'right' example treats an array as a boolean.