Design principles: no bool arguments

Stefan Behnel stefan_ml at behnel.de
Thu Aug 25 04:29:41 EDT 2011


Maarten, 25.08.2011 09:52:
> On Aug 25, 9:13 am, Steven D'Aprano wrote:
>> One design principle often mentioned here (with a certain degree of
>> disagreement[1]) is the idea that as a general rule, you shouldn't write
>> functions that take a bool argument to switch between two slightly
>> different behaviours.
>>
>> This is a principle often championed by the BDFL, Guido van Rossum.
>>
>> Here's a Javascript-centric article which discusses the same idea, and gives
>> it a name: the Boolean Trap.
>>
>> http://ariya.ofilabs.com/2011/08/hall-of-api-shame-boolean-trap.html
>>
>> No doubt there are counter arguments as well. The most obvious to me is if
>> the flag=True and flag=False functions share a lot of code, it is poor
>> practice to implement them as two functions with two copies of almost
>> identical code.
>
> A simple one: C and C-like languages only have arguments, not keyword-
> parameters. That alone makes a world of difference.

Right. It's totally unreadable to find this in the code:

     data1.merge_with(data2, true);

Requires you to either a) know the underlying signature by heart, or b) 
look it up before understanding the code.

It's a lot harder to argue against this:

     data1.merge_with(data2, overwrite_duplicates=True)

Stefan




More information about the Python-list mailing list