Wireshark-dev: Re: [Wireshark-dev] Style question passing boolean parameters
From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Thu, 21 Jun 2012 12:33:03 -0700
On Jun 19, 2012, at 10:12 PM, Joerg Mayer wrote:

> The following commit retriggered an allergic reaction to controlling function
> behaviour via booleans:
> 
>        if (cf_save_packets(&cfile, file_name8->str, filetype, FALSE/*compressed */, FALSE/*discard_comments */, FALSE/* dont_reopen */) != CF_OK) {
> 
> To me, this is sort of unreadable without the comments and ugly looking with
> the comments. My favourite nightmare in this regard in wireshark source is
> dissect_ieee80211_common which has 4 boolean parameters and no comments anywhere
> where it is called.
> 
> What ways are there to fix this? Is replacing the boolean types by an enum with
> speaking elements a valid solution?

It's one.

Another would be to pass a single flags argument, e.g.

       if (cf_save_packets(&cfile, file_name8->str, filetype, NOT_COMPRESSED|DONT_DISCARD_COMMENTS|DONT_REOPEN) != CF_OK) {

(with some of the flag values #defined to be 0, so that, regardless of the value of the flag, the value is indicated in the call).

Fewer arguments means, in a call, either fewer pushes onto the stack or fewer registers required for passing parameters.  I'm not sure whether it's significantly more expensive (or more expensive at all) to test a single bit in a flags word than to test a Boolean variable on various architectures.