Clarity of boolean logic

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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

Eric Sosman

Christopher said:
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.
 
B

Bruno Desthuilliers

Christopher said:
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
 
C

Christopher Benson-Manica

Eric Sosman said:
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.)
 
P

Peter Shaggy Haywood

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"?
 
C

Christopher Benson-Manica

Peter "Shaggy" Haywood said:
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.
I don't know what you mean.

"Can you tell what I'm trying to do?"
Then what does?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top