Posts

Boolean Flag Arguments

Boolean Flag Arguments One thing I keep seeing in code reviews -- and something I used to do myself years ago -- is functions shaped like this: def my_func(condition: bool): if condition: return do_a() else: return do_b() With call sites like: my_func(True) my_func(False) And I almost never think this is a good idea. It looks neat, but it hides the real story. In practice, I’ve found that code like this nearly always reads worse than just having two explicit functions: def do_a(): return ... def do_b(): return ... Then the caller does: if condition: do_a() else: do_b() Callers choose the behaviour explicitly, and you don’t end up passing a random True / False into something and hoping you remember what it means. And the best part: if somewhere else in your code you want to do A unconditionally, you can just call do_a() without manufacturing a boolean or pretending you care about the alternative path. The issue wi...