By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Clarity of boolean logic

P: n/a
Given

int g(); /* prototype */
int h(int); /* prototype */

void f( int x, int y ) /* x is either 0 or 1 */
{
if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 ) )
/* do stuff */
}

, would you consider the following change obfuscatory?

if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */
...

(The "magic numbers" don't exist in the real code.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Christopher Benson-Manica wrote:

Given

int g(); /* prototype */
int h(int); /* prototype */

void f( int x, int y ) /* x is either 0 or 1 */
{
if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 ) )
/* do stuff */
}

, would you consider the following change obfuscatory?

if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */
...

(The "magic numbers" don't exist in the real code.)


I'm not sure what you're driving at: are you worried
about having too many conditions &&-ed together, or is there
something about the final line that seems bothersome? On
the first point, I'll just note that conditions with large
numbers of terms can become confusing, not least because
the meaning of a possible `else' becomes hard to describe.

On the other hand, if your *goal* is obfuscation you
can surely do better (if "better" is the right word). One
simple transformation:

if ( (/* other conditions */)
&& (/* other conditions */)
&& (x ? y ? y == h(120) : x : g() < 120) )

.... which I may have botched, since I'm proud to say that
I'm out of practice in using such a style.

--
Er*********@sun.com
Nov 14 '05 #2

P: n/a
Christopher Benson-Manica wrote:
Given

int g(); /* prototype */
int h(int); /* prototype */

void f( int x, int y ) /* x is either 0 or 1 */
{
if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 ) )
/* do stuff */
}

, would you consider the following change obfuscatory?

if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */
...


<stop me if I say something stupid but...>

(I'v snipped the other conditions for more clarity)

If x == 1, 'g() < 120' will be short-circuited. Then, as x != 0, 'y == 0
|| y==h(120)' will be evalued. So we have in fact

if (x == 1 && (y == 0 || y==h(120))

Now if x == 0, 'g() < 120' is evalued. If it's false, we stop here. But
even if it's true, as x == 0, 'y == 0 || y==h(120)' will be
short-circuited.

So it seems that it ends up as :

if ( (x == 1 && (y == 0 || y == h(120) ))
|| g() < 120
)

Or did I miss something ?

my 2 cents
Bruno

Nov 14 '05 #3

P: n/a
Eric Sosman <Er*********@sun.com> spoke thus:
I'm not sure what you're driving at: are you worried
about having too many conditions &&-ed together, or is there
something about the final line that seems bothersome?


Well, the thing about the final line that was "bothersome" was that I
ended up having a discussion with my boss about whether it even worked
or not - if my boss is confused by my code, that's not a good thing ;)
(He eventually agreed that it worked.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4

P: n/a
Bruno Desthuilliers <bd***********@tsoin-tsoin.free.fr> spoke thus:
Christopher Benson-Manica wrote:

&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */
So it seems that it ends up as : if ( (x == 1 && (y == 0 || y == h(120) ))
|| g() < 120
)


I believe that's correct.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5

P: n/a
Christopher Benson-Manica wrote:
Bruno Desthuilliers <bd***********@tsoin-tsoin.free.fr> spoke thus:
Christopher Benson-Manica wrote:
&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */


So it seems that it ends up as :


if ( (x == 1 && (y == 0 || y == h(120) ))
|| g() < 120
)

I believe that's correct.


If yes, that's more readable IMHO.

Bruno
Nov 14 '05 #6

P: n/a
Groovy hepcat Christopher Benson-Manica was jivin' on Tue, 23 Dec 2003
21:47:59 +0000 (UTC) in comp.lang.c.
Clarity of boolean logic's a cool scene! Dig it!
Given

int g(); /* prototype */
Not! A prototype is a function declaration that declares the number
and types of its parameters. What you have here is a K&R style
function declaration.
In addition, these comments are utterly pointless. They not only
don't help in understanding the purpose of the code, but (in the above
line) are actually wrong, as I have just pointed out. So such useless
comments should be left out.
int h(int); /* prototype */
Now this one is indeed a prototype. However, as stated above,
comments like this are utterly superfluous. They just clutter code.
void f( int x, int y ) /* x is either 0 or 1 */
{
if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 ) )
/* do stuff */
}

, would you consider the following change obfuscatory?
I don't know what you mean.
if( (/* other conditions */)
&& (/* other conditions */)
&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */
...

(The "magic numbers" don't exist in the real code.)


Then what does?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #7

P: n/a
Peter "Shaggy" Haywood <sh****@australis.net.stop.spam> spoke thus:
Not! A prototype is a function declaration that declares the number
and types of its parameters. What you have here is a K&R style
function declaration.
Yeesh... yes. Thank you.
, would you consider the following change obfuscatory? I don't know what you mean.
"Can you tell what I'm trying to do?"
&& ( x == 1 || g() < 120 )
&& ( x == 0 || y == 0 || y==h(120) )) /* added line */
(The "magic numbers" don't exist in the real code.)

Then what does?


Additional function calls that aren't germane to the question I'm
asking.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.