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')

 
 
James Kuyper
Guest
Posts: n/a
 
      11-19-2007
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.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      11-19-2007
On Sun, 18 Nov 2007 02:01:21 -0800 (PST),
"(E-Mail Removed)" <(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();
>}


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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      11-20-2007
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 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

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-20-2007
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 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-20-2007
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 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
 
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