Last Revision — April 19, 2022
2 Min Read
Boolean in Method Parameter
Change Preventers
Conditional Logic
Within a Class
- Binary Operator in Name (caused)
- Long Method (causes)
- Long Parameter List (causes)
- Conditional Complexity (causes)
- Loops (caused)
- Null Check (caused)
- Side Effects (antagonistic)
Robert C. Martin in book (2008):
"Clean Code: A Handbook of Agile Software Craftsmanship"
Martin Fowler in book (1999):
"Refactoring: Improving the Design of Existing Code"
Martin Fowler defines Flag Arguments as a "kind of function argument that tells the function to carry out a different operation depending on its value." [1] There are two reasons why this is smelly. First of all - it can be a candidate for Boolean Blindness Code Smell. Fowler gives a great example with Concert class and the book(customer: Customer, is_premium: bool)
method. While reading the code, without knowing much more context, one will be stopped by invocation of this method: book(marcel, false)
- excuse me, but precisely what "false
"? The situation is clear if the method is divided into two separate parts instead of using a flag argument. Then, calling a method that provides more meaning through a name like regularBook(marcel)
is much better in terms of comprehensibility.
The second problem is that it might be a cocoon phase before it develops into a beautiful full-fledged Conditional Complexity. First, what you see is is_premium: bool
. The second time you come by, it is already transformed to ticket_type: str,
switching through different options based on the value and the smell of the Primitive Obsession on top.
Quick dirty tweaking can cause them for new features because it might be as easy and tempting as adding else if
clause to the conditional-checking block. The developer felt that it was just a tiny difference and didn't bother to create a separate method for its implementation.
In most cases, you will not know what false
or what true
is going on until you hover over the method.
Boolean as a flag argument implies that a method has two ways of working.
class Concert:
def book(self, customer: Customer, is_premium: bool):
if is_premium:
...
else:
...
book(marcel, false) # ? false what
class Concert:
def book_premium(self, customer: Customer):
...
def book_regular(self, customer: Customer):
...
book_regular(marcel)