Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 Dec 2009 02:46:11 +1100 (EST)
From:      Bruce Evans <brde@optusnet.com.au>
To:        John Baldwin <jhb@freebsd.org>
Cc:        Luigi Rizzo <luigi@freebsd.org>, src-committers@freebsd.org, Luigi Rizzo <rizzo@iet.unipi.it>, Bruce Evans <brde@optusnet.com.au>, svn-src-user@freebsd.org
Subject:   Re: svn commit: r201063 - user/luigi/ipfw3-head/sys/netinet/ipfw
Message-ID:  <20091231021707.J48242@delplex.bde.org>
In-Reply-To: <200912290746.59011.jhb@freebsd.org>
References:  <200912272213.nBRMDJAC069043@svn.freebsd.org> <20091229021846.U46429@delplex.bde.org> <20091228232151.GA39294@onelab2.iet.unipi.it> <200912290746.59011.jhb@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 29 Dec 2009, John Baldwin wrote:

> On Monday 28 December 2009 6:21:51 pm Luigi Rizzo wrote:
>> On Tue, Dec 29, 2009 at 02:38:15AM +1100, Bruce Evans wrote:
>>> On Sun, 27 Dec 2009, Luigi Rizzo wrote:
>>>
>>>> Log:
>>>> use a less obfuscated construct to call the hook/unhook functions
>>>>
>>>> Modified:
>>>> user/luigi/ipfw3-head/sys/netinet/ipfw/ip_fw_pfil.c
>>>
>>> Better unobfuscation:
>>>
>>>> Modified: user/luigi/ipfw3-head/sys/netinet/ipfw/ip_fw_pfil.c
>>>
>> ==============================================================================
>>>> --- user/luigi/ipfw3-head/sys/netinet/ipfw/ip_fw_pfil.c	Sun Dec 27
>>>> 21:58:48 2009	(r201062)
>>>> +++ user/luigi/ipfw3-head/sys/netinet/ipfw/ip_fw_pfil.c	Sun Dec 27
>>>> 22:13:19 2009	(r201063)
>>>> @@ -329,18 +329,17 @@ ipfw_divert(struct mbuf **m0, int incomi
>>>> static int
>>>> ipfw_hook(int onoff, int pf)
>>>> {
>>>> +	const int arg = PFIL_IN | PFIL_OUT | PFIL_WAITOK;
>>>
>>> Don't add this obfuscation (a constant used only once stored in a variable
>>> used only once, just to avoid 2 long lines (1 after my change).
>>
>> It is not just that.
>>
>> I want to tell humans reading the code that the value used in the
>> two calls is exactly the same, beyond any chance of misspelling or
>> misreading the two long lines.
>>
>> Then whether or not to store it in a variable is compiler's business,
>> same as if i use the constant FOO ( #define FOO 0x11122334455667788LL )
>> 20 times in a piece of code.
>
> Bruce's cute ?: trick for picking which function pointer to invoke avoided
> that problem FWIW as the flags were only passed once.

?: also works for all the other function parameter -- you don't have to read
them all and compare them to see that they are the same:

 	if (foo)
 		func1(arg1, FOO | BAR | BAZ, arg3, ... argn);
 	else
 		func234(arg1, FOO | BAR | BAZ, arg3, ... argn);

It is no easier (or harder) to read all the identifiers and operators
to see that arg2 = FOO | BAR | BAZ is the same in both calls as it is
to read all the identifiers to see that all the parameters are to same.
Not very easy for either when there are lots of identifiers and/or
more worse lining up than above.

The above lines could be misformatted to make the correspondence clear,
provided the function call lines are short:

The ?: trick works especially well for variant function calls when all
except 1 of the function and the parameters are the same.  I also prefer it
when it allows writing an assignment in 1 statement of N lines instead of
as N statements in > 2*N lines, no matter how many ?:'s this takes.

Bruce



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20091231021707.J48242>