Wireshark-dev: Re: [Wireshark-dev] Style question passing boolean parameters

From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Fri, 22 Jun 2012 13:07:19 -0700
On Jun 21, 2012, at 12:33 PM, Guy Harris wrote:

> 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.

Also, I'm not sure whether any compilers would warn about

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

where the various indicators aren't passed in the right order, because enums aren't "real" data types in C, so if you don't remember the order correctly, the compiler might not warn you about it.

Bitwise OR is commutative, so the difference between

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

and

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

is a matter of taste rather than of correctness; there's no order of indicators that you have to remember.