473,468 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

`Comparability' of code snippets (from book `Code complete')

The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}
Nov 18 '07 #1
14 1210
lovecreatesbea...@gmail.com said:
The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
It depends on details that are not shown.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 18 '07 #2

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote in message
news:d0**********************************@s36g2000 prg.googlegroups.com...
The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}
Assuming no attempt to alter cond, i or N in the functions, they should have
exactly the same effect.

The question is which is more efficient, and which is better style.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 18 '07 #3
lovecreatesbea...@gmail.com wrote:
The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}
Provided neither DoSomething() nor DoNothing() alter the value of cond
or i, they're identical, and a good optimising compiler should be able
to generate the same machinecode.
Nov 18 '07 #4
In article
<d0**********************************@s36g2000prg. googlegroups.com>,
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote on
Sunday 18 Nov 2007 3:31 pm:
The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}
They have the same effect but only if 'i' and 'cond' are not altered by
either of the functions. If they are going to be volatile the first
form is the correct one.

Nov 18 '07 #5
santosh wrote:
In article
<d0**********************************@s36g2000prg. googlegroups.com>,
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote on
Sunday 18 Nov 2007 3:31 pm:
>The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}

They have the same effect but only if 'i' and 'cond' are not altered by
either of the functions. If they are going to be volatile the first
form is the correct one.
It depends on whether we want to evaluate ``cond'' just once, or once
for each iteration. We just don't have enough information to determine
which one is correct.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 18 '07 #6

"Keith Thompson" <ks***@mib.orgwrote
It depends on whether we want to evaluate ``cond'' just once, or once for
each iteration. We just don't have enough information to determine which
one is correct.
if cond is in fact a macro that has side-effects.

But then maybe "if" has been redefined by the preprocessor, if you're going
to get like that.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 18 '07 #7
lovecreatesbea...@gmail.com wrote:
The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}
The answer depends on weather 'cond' is a loop-invariant, or not.

The compiler can sometimes tell, and might then replace the first code
snippet with the second, during the optimizing.

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Nov 18 '07 #8
Malcolm McLean wrote:
"Keith Thompson" <ks***@mib.orgwrote
>It depends on whether we want to evaluate ``cond'' just once, or once
for each iteration. We just don't have enough information to
determine which one is correct.
if cond is in fact a macro that has side-effects.

But then maybe "if" has been redefined by the preprocessor, if you're
going to get like that.
Get like what?

You snipped part of the article to which I replied, in which santosh wrote:

They have the same effect but only if 'i' and 'cond' are not
altered by either of the functions. If they are going to be
volatile the first form is the correct one.

If "cond" is declared volatile, then the number of times it's evaluated
obviously can be relevant. It needn't be a macro.

For that matter, "cond" could be an abbreviation for the word "condition", and
could refer (in pseudo-code) to any arbitrary expression used as a condition.
It's not entirely clear whether the code was meant to be taken literally. But
even if it was, the issue is volatility.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 18 '07 #9
Malcolm McLean wrote:
"Keith Thompson" <ks***@mib.orgwrote
>It depends on whether we want to evaluate ``cond'' just once, or
once for each iteration. We just don't have enough information
to determine which one is correct.

if cond is in fact a macro that has side-effects.

But then maybe "if" has been redefined by the preprocessor, if
you're going to get like that.
You can't redefine 'if' and have a legitimate program.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 19 '07 #10
CBFalconer wrote:
Malcolm McLean wrote:
....
>But then maybe "if" has been redefined by the preprocessor, if
you're going to get like that.

You can't redefine 'if' and have a legitimate program.
What does "legitimate program" mean, in this context? I don't believe
that redefining a keyword has undefined behavior, is a syntax error, or
violates any constraints. It's a very bad idea, but that's a different
question entirely.
Nov 19 '07 #11
On Sun, 18 Nov 2007 02:01:21 -0800 (PST),
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote:
>The book `Code complete' mentions similar code snippets as the
followings , and talks about their advantages respectively.

But I think these code snippets are totally different in logic, and
their functionalities are different. They don't have comparabilities.
Am I right?
/*code 1*/
for (i = 0; i < N; i++){
if (cond)
DoSomething();
else
DoOtherthing();
}

/*code 2*/
if (cond){
for (i = 0; i < N; i++)
DoSomething();
} else {
for (i = 0; i < N; i++)
DoOtherthing();
}
When cond is invariant while the loop is iterating (always true or
always false), they appear to be equivalent. Can you describe a
situation where they do not perform the same?

When cond changes over the course of the loop, they are very obviously
different. code 1 executes DoSomething or DoOtherthing depending on
the *current* state of cond while code2 executes the same function
(either DoSomething or DoOtherthing) for every iteration depending
only on the *initial* state of cond.


Remove del for email
Nov 19 '07 #12
James Kuyper wrote:
CBFalconer wrote:
>Malcolm McLean wrote:
...
>>But then maybe "if" has been redefined by the preprocessor, if
you're going to get like that.

You can't redefine 'if' and have a legitimate program.

What does "legitimate program" mean, in this context? I don't
believe that redefining a keyword has undefined behavior, is a
syntax error, or violates any constraints. It's a very bad idea,
but that's a different question entirely.
I had to search a while. From N869:

6.4.1 Keywords

Syntax
[#1]
keyword: one of
auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool
continue if static _Complex
default inline struct _Imaginary
do int switch
double long typedef
else register union

Semantics

[#2] The above tokens (case sensitive) are reserved (in
translation phases 7 and 8) for use as keywords, and shall
not be used otherwise.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 20 '07 #13
CBFalconer wrote:
James Kuyper wrote:
>CBFalconer wrote:
>>Malcolm McLean wrote:
...
>>>But then maybe "if" has been redefined by the preprocessor, if
you're going to get like that.
You can't redefine 'if' and have a legitimate program.
What does "legitimate program" mean, in this context? I don't
believe that redefining a keyword has undefined behavior, is a
syntax error, or violates any constraints. It's a very bad idea,
but that's a different question entirely.

I had to search a while. From N869:

6.4.1 Keywords

Syntax
[#1]
keyword: one of
auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool
continue if static _Complex
default inline struct _Imaginary
do int switch
double long typedef
else register union

Semantics

[#2] The above tokens (case sensitive) are reserved (in
translation phases 7 and 8) for use as keywords, and shall
not be used otherwise.
My apologies - I should have performed the search, not you. But I was
pretty certain there was nothing to find, which tends to remove the
motivation to look.
Nov 20 '07 #14
James Kuyper wrote, On 20/11/07 05:14:
CBFalconer wrote:
>James Kuyper wrote:
>>CBFalconer wrote:
Malcolm McLean wrote:
...
But then maybe "if" has been redefined by the preprocessor, if
you're going to get like that.
You can't redefine 'if' and have a legitimate program.
What does "legitimate program" mean, in this context? I don't
believe that redefining a keyword has undefined behavior, is a
syntax error, or violates any constraints. It's a very bad idea,
but that's a different question entirely.

I had to search a while. From N869:

6.4.1 Keywords

Syntax
[#1]
keyword: one of
auto enum restrict unsigned
<snip>
> Semantics

[#2] The above tokens (case sensitive) are reserved (in
translation phases 7 and 8) for use as keywords, and shall
not be used otherwise.

My apologies - I should have performed the search, not you. But I was
pretty certain there was nothing to find, which tends to remove the
motivation to look.
Isn't the #define and replacement handled in one of the earlier
translation phases making that section irrelevant as far as whether you
can do a #define of if?
--
Flash Gordon
Nov 20 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mario T. Lanza | last post by:
Greetings, I don't know about you guys but on many occasions I've asked myself whether or not someone else has solved a particular programming issue -- whether or not they developed a clever...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
10
by: Jak | last post by:
Hi, all, I am wondering if there are any addons for VB.NET 2005 to complete the codes automatically after I entered two or three characters, just like Delphi does. Thanks, Jack Zhong
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.