Re:Named Booleans prevent C++ bugs and save you time
by greggyb on Sunday February 19, 2023 @11:05AM
(#34858706)
One challenge with the chosen example language, Python, is that functions are truthy. So if you forget to call the function that computes your boolean test, you end up with a default true.
Taking the last top-level statement, the `if`:
if var_contains_a() and var_shorter_than_5: #note the lack of parens for the second
print(var)
This will print a string of any length that contains 'a' (or any other collection that implements `in` and does, in fact, contain an element, 'a'). The second function is passed directly as a boolean expression, and being a function, it resolves to true.
Taking the last top-level statement, the `if`:
This will print a string of any length that contains 'a' (or any other collection that implements `in` and does, in fact, contain an element, 'a'). The second function is passed directly as a boolean expression, and being a function, it resolves to true.