473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Clarity of boolean logic

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
7 1692
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kurt Krueckeberg | last post by:
In the book C++ Gothcas, Gotcha #7 is an example of using boolean logic to simply code. My question follows this snippet from the book. "Do you have to count to eight when presented with the...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
76
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the...
2
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and...
4
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Hi folks, Boolean.Parse("true") returns true, but what if you need to parse something such as - Boolean.Parse("(false && false) || (false || (true && true))"); Does something exist in the...
3
by: Kevin Audleman | last post by:
I wrote a piece of boolean logic that doesn't work and I don't know why. The code should ONLY do something if the session variable is equal to 900123: if( ! $_SESSION == '900123') { //do some...
2
by: dpapathanasiou | last post by:
If I define a dictionary where one or more of the values is also a dictionary, e.g.: my_dict={"a":"string", "b":"string", "c":{"x":"0","y":"1"}, "d":"string"} How can I use the output of...
11
by: KiranJyothi | last post by:
Hello All, I am writing a code in C where I have to use the Boolean data type. I am doing - typedef int flag; #define FALSE 0 #define TRUE 1 void main()
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.