Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declaration of variables

Reply
Thread Tools

Declaration of variables

 
 
Tomás
Guest
Posts: n/a
 
      03-06-2006

> It's only worth the ugliness of doing that if it's demonstrably worth
> that ugliness in terms of *needed* performance.


When I write extra-special long complicated loops, I resort to brute force.
Here's a copy-paste of some of my latest code:


manual_loop_scope:
{
std::size_t i = 0;
std::size_t i_word = NumericInfo::max_divisions_euro - 2;

manual_loop_begin_iteration:
{
bool const& condition = i < (NumericInfo::max_divisions_euro -
1);

if ( !condition ) goto manual_loop_end;

manual_loop_body:
{
if ( p_digit[0] || p_digit[1] || p_digit[2] )
{
if (others_beforehand)
{
StringSystem::AppendPALIncCounter( p_char, ( p_digit
[0] ? lang_attribs.separator_comma : lang_attribs.separator_and ) );
}

others_beforehand = true;
StringSystem::AppendPALIncCounter( p_char,
GetSubThousandWithUnitName( p_digit[0], p_digit[1], p_digit[2],
lang_attribs.unit_names[i_word] ) );
}
} // close manual_loop_body
}//close manual_loop_iteration

manual_loop_continue:
{
++i;
p_digit += 3;
--i_word;

goto manual_loop_begin_iteration;

}//close manual_loop_continue

manual_loop_end: ;
}


Beauty is irrelevant.

-Tomás
 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      03-07-2006

Tomás wrote:

> > It's only worth the ugliness of doing that if it's demonstrably worth
> > that ugliness in terms of *needed* performance.

>
> When I write extra-special long complicated loops, I resort to brute force.
> Here's a copy-paste of some of my latest code:
>
>
> manual_loop_scope:
> {
> std::size_t i = 0;
> std::size_t i_word = NumericInfo::max_divisions_euro - 2;
>
> manual_loop_begin_iteration:
> {
> bool const& condition = i < (NumericInfo::max_divisions_euro -
> 1);
>
> if ( !condition ) goto manual_loop_end;
>
> manual_loop_body:
> {
> if ( p_digit[0] || p_digit[1] || p_digit[2] )
> {
> if (others_beforehand)
> {
> StringSystem::AppendPALIncCounter( p_char, ( p_digit
> [0] ? lang_attribs.separator_comma : lang_attribs.separator_and ) );
> }
>
> others_beforehand = true;
> StringSystem::AppendPALIncCounter( p_char,
> GetSubThousandWithUnitName( p_digit[0], p_digit[1], p_digit[2],
> lang_attribs.unit_names[i_word] ) );
> }
> } // close manual_loop_body
> }//close manual_loop_iteration
>
> manual_loop_continue:
> {
> ++i;
> p_digit += 3;
> --i_word;
>
> goto manual_loop_begin_iteration;
>
> }//close manual_loop_continue
>
> manual_loop_end: ;
> }


You're joking, right? Have you ever worked on code that's developed or
maintained by more than one person?

> Beauty is irrelevant.


If by "beauty" you mean "readability through conformance to accepted
normal practice" then, no it's not irrelevant. It's more important than
anything else.

You're trolling aren't you.

Gavin Deane

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-07-2006

"Tomás" <(E-Mail Removed)> wrote in message
news:Or3Pf.6836$(E-Mail Removed)...
>
>> It's only worth the ugliness of doing that if it's demonstrably worth
>> that ugliness in terms of *needed* performance.

>
> When I write extra-special long complicated loops, I resort to brute
> force.
> Here's a copy-paste of some of my latest code:
>
>
> manual_loop_scope:
> {
> std::size_t i = 0;
> std::size_t i_word = NumericInfo::max_divisions_euro - 2;
>
> manual_loop_begin_iteration:
> {
> bool const& condition = i < (NumericInfo::max_divisions_euro -
> 1);
>
> if ( !condition ) goto manual_loop_end;
>
> manual_loop_body:
> {
> if ( p_digit[0] || p_digit[1] || p_digit[2] )
> {
> if (others_beforehand)
> {
> StringSystem::AppendPALIncCounter( p_char, (
> p_digit
> [0] ? lang_attribs.separator_comma : lang_attribs.separator_and ) );
> }
>
> others_beforehand = true;
> StringSystem::AppendPALIncCounter( p_char,
> GetSubThousandWithUnitName( p_digit[0], p_digit[1], p_digit[2],
> lang_attribs.unit_names[i_word] ) );
> }
> } // close manual_loop_body
> }//close manual_loop_iteration
>
> manual_loop_continue:
> {
> ++i;
> p_digit += 3;
> --i_word;
>
> goto manual_loop_begin_iteration;
>
> }//close manual_loop_continue
>
> manual_loop_end: ;
> }
>
>
> Beauty is irrelevant.
>
> -Tomás


Thank you. I haven't seen a good example of spagetti code for a number of
years.


 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-07-2006
Gavin Deane posted:

>
> Tomás wrote:
>
>> > It's only worth the ugliness of doing that if it's demonstrably
>> > worth that ugliness in terms of *needed* performance.

>>
>> When I write extra-special long complicated loops, I resort to brute
>> force. Here's a copy-paste of some of my latest code:
>>
>>
>> manual_loop_scope: {
>> std::size_t i = 0;
>> std::size_t i_word = NumericInfo::max_divisions_euro - 2;
>>
>> manual_loop_begin_iteration: {
>> bool const& condition = i <
>> (NumericInfo::max_divisions_euro - 1);
>>
>> if ( !condition ) goto manual_loop_end;
>>
>> manual_loop_body: {
>> if ( p_digit[0] || p_digit[1] || p_digit[2] )
>> {
>> if (others_beforehand)
>> {
>> StringSystem::AppendPALIncCounter( p_char, (
>> p_digit
>> [0] ? lang_attribs.separator_comma : lang_attribs.separator_and ) ); }
>>
>> others_beforehand = true;
>> StringSystem::AppendPALIncCounter( p_char,
>> GetSubThousandWithUnitName( p_digit[0], p_digit[1], p_digit[2],
>> lang_attribs.unit_names[i_word] ) ); }
>> } // close manual_loop_body
>> }//close manual_loop_iteration
>>
>> manual_loop_continue: { ++i;
>> p_digit += 3; --i_word;
>>
>> goto manual_loop_begin_iteration;
>>
>> }//close manual_loop_continue
>>
>> manual_loop_end: ; }

>
> You're joking, right? Have you ever worked on code that's developed or
> maintained by more than one person?


No.


>> Beauty is irrelevant.

>
> If by "beauty" you mean "readability through conformance to accepted
> normal practice" then, no it's not irrelevant. It's more important than
> anything else.


I don't go for "accepted normal practice" -- I write my own code my own way.
It's efficient, portable and bug free.

My labels make it quite clear what's going on.

> You're trolling aren't you.


No, I'm actually writing loops using labels and the "goto" keyword. If you
find it that scary, maybe you sould Google for "goto" and see what all the
grown-ups do with it.


-Tomás
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      03-07-2006

Artie Gold wrote:
> Gaijinco wrote:
> > I have always felt that you should only declared variables as needed,
> > and implicitily it seems many authors to encourage it, but the other
> > day a friend told me that declaring variables inside a loop wasn't good
> > practice, something like:
> >
> > for(int i=0; i<size-1; ++i){
> > int aux = array[i];
> > array[i]=array[i+1];
> > array[i+1]=aux;
> > }
> >
> > should be written like:
> >
> > int aux;
> > for(int i=0; i<size-1; ++i){
> > aux = array[i];
> > array[i]=array[i+1];
> > array[i+1]=aux;
> > }
> >
> > What do you think? Thaks!
> >

> Since `aux' is only meaningful inside the loop why not limit its scope
> to the block inside the loop?
>
> On the other hand, there are situations where you wouldn't want to do
> this, when, for example, the variable in question is of a type that
> requires significant construction overhead. This, however, seems not to
> be one of those situations.


Even in those cases you should go for the innermost scope until
performance and experimentation tells you otherwise. If e.g. the
variable was a std::string, semantics differ when you put the string
outside the loop, and the extra work to reset the string at each
iteration might well be comparable to the cost of one constructor and
one destructor.

/Peter
>
> HTH,
> --ag
>
> --
> Artie Gold -- Austin, Texas
> http://goldsays.blogspot.com
> "You can't KISS* unless you MISS**"
> [*-Keep it simple, stupid. **-Make it simple, stupid.]


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      03-07-2006

Tomás wrote:
> Gavin Deane posted:
>
> >
> > Tomás wrote:
> >
> >> > It's only worth the ugliness of doing that if it's demonstrably
> >> > worth that ugliness in terms of *needed* performance.
> >>
> >> When I write extra-special long complicated loops, I resort to brute
> >> force. Here's a copy-paste of some of my latest code:
> >>
> >>

[snip what supposedly should be C++ code}
> >
> > You're joking, right? Have you ever worked on code that's developed or
> > maintained by more than one person?

>
> No.
>
>
> >> Beauty is irrelevant.

> >
> > If by "beauty" you mean "readability through conformance to accepted
> > normal practice" then, no it's not irrelevant. It's more important than
> > anything else.

>
> I don't go for "accepted normal practice" -- I write my own code my own way.
> It's efficient, portable and bug free.
>
> My labels make it quite clear what's going on.
>
> > You're trolling aren't you.

>
> No, I'm actually writing loops using labels and the "goto" keyword. If you
> find it that scary, maybe you sould Google for "goto" and see what all the
> grown-ups do with it.


These must be the same grown-ups that never take back-ups and don't do
unittests?
Well.... theres not much worth backing up anyway.

/Peter

>
>
> -Tomás


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-07-2006
Tomás wrote:

> No, I'm actually writing loops using labels and the "goto" keyword. If you
> find it that scary, maybe you sould Google for "goto" and see what all the
> grown-ups do with it.


Instead of priding yourself on obfuscation, researching how to write the
_simplest_ possible code for a set of features will improve the odds that
you could add value to a team of software engineers.

Specifically, code that is absurdly elaborate in one direction becomes
hopelessly resistant to unexpected changes in other directions. Preserving
flexibility is the heart of the software process because it allows customers
to request unexpected new features.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      03-07-2006

Tomás wrote:
> Gavin Deane posted:
> > Tomás wrote:
> >> Beauty is irrelevant.

> >
> > If by "beauty" you mean "readability through conformance to accepted
> > normal practice" then, no it's not irrelevant. It's more important than
> > anything else.

>
> I don't go for "accepted normal practice" -- I write my own code my own way.
> It's efficient, portable and bug free.


Which proves my point that you've never had to work on a code base with
more than one developer. You left out the most important factor -
readability. Yes, that is more important than bug-free.

If you write clear code that conforms to the principle of least
surprise, but it has bugs, I can understand the code and fix the bugs,
and modify the code with a low risk of introducing new bugs, all very
easily. If you write the sort of obfuscated code you posted in this
thread and it has no bugs, the chance of me being able to modify it
without introducing new bugs, and the chance of quickly identifying and
fixing those bugs are greatly reduced.

> > You're trolling aren't you.

>
> No, I'm actually writing loops using labels and the "goto" keyword. If you
> find it that scary, maybe you sould Google for "goto" and see what all the
> grown-ups do with it.


If that's not trolling I don't know what is. Please stop.

Gavin Deane

 
Reply With Quote
 
TB
Guest
Posts: n/a
 
      03-07-2006
Tomás skrev:
>> It's only worth the ugliness of doing that if it's demonstrably worth
>> that ugliness in terms of *needed* performance.

>
> When I write extra-special long complicated loops, I resort to brute force.
> Here's a copy-paste of some of my latest code:
>
>
> manual_loop_scope:
> {
> std::size_t i = 0;
> std::size_t i_word = NumericInfo::max_divisions_euro - 2;
>
> manual_loop_begin_iteration:
> {
> bool const& condition = i < (NumericInfo::max_divisions_euro -
> 1);
>
> if ( !condition ) goto manual_loop_end;
>
> manual_loop_body:
> {
> if ( p_digit[0] || p_digit[1] || p_digit[2] )
> {
> if (others_beforehand)
> {
> StringSystem::AppendPALIncCounter( p_char, ( p_digit
> [0] ? lang_attribs.separator_comma : lang_attribs.separator_and ) );
> }
>
> others_beforehand = true;
> StringSystem::AppendPALIncCounter( p_char,
> GetSubThousandWithUnitName( p_digit[0], p_digit[1], p_digit[2],
> lang_attribs.unit_names[i_word] ) );
> }
> } // close manual_loop_body
> }//close manual_loop_iteration
>
> manual_loop_continue:
> {
> ++i;
> p_digit += 3;
> --i_word;
>
> goto manual_loop_begin_iteration;
>
> }//close manual_loop_continue
>
> manual_loop_end: ;
> }
>


I'm speechless... ugh

--
TB @ SWEDEN
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-07-2006
TB wrote:

>> goto manual_loop_begin_iteration;
>> }//close manual_loop_continue
>> manual_loop_end: ;
>> }
>>

>
> I'm speechless... ugh


I have seen worse. Take a programmer who grew up with assembly, and let them
write C. Then, without a little education, and besotted with the belief that
"goto is faster", they might write an entire, brilliant application using
assembly-style C, with all kinds of run-on functions, looping with goto,
etc.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


 
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
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments