Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > `Comparability' of code snippets (from book `Code complete')

Reply
Thread Tools

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

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      11-18-2007
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();
}
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-18-2007
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      11-18-2007

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-18-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-18-2007
In article
<(E-Mail Removed)>,
(E-Mail Removed) <(E-Mail Removed)> wrote 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-18-2007
santosh wrote:
> In article
> <(E-Mail Removed)>,
> (E-Mail Removed) <(E-Mail Removed)> wrote 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) <(E-Mail Removed)>
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"
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-18-2007

"Keith Thompson" <(E-Mail Removed)> wrote
> 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

 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      11-18-2007
(E-Mail Removed) 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 <(E-Mail Removed) | tr i-za-h a-z>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-18-2007
Malcolm McLean wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote
>> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-19-2007
Malcolm McLean wrote:
> "Keith Thompson" <(E-Mail Removed)> wrote
>
>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Committing Code Snippets windsurfing_stew@yahoo.com.au ASP .Net 2 12-17-2005 10:22 PM
Code snippets, tool to convert basic to .snippet xml? Edwin Knoppert ASP .Net 0 11-30-2005 03:45 PM
No code snippets built in yet for C# ASP.NET 2.0 beta? Steve Franks ASP .Net 2 09-11-2005 10:36 AM
Where are my Code Snippets? Pat ASP .Net 4 04-23-2005 01:29 PM
Reusing Code Snippets - Steve - ASP .Net 2 10-01-2004 11:29 AM



Advertisments