Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 02:55 AM
Lilith
Guest
 
Posts: n/a
Default There's got to be a more elegant way

I was working on a routine that requires me to parse out a list of
attributes of an object in a text line. An attribute could be empty,
causing it to revert to it's current value, or the list of attributes
could terminate without completing and all unlisted attributes revert.
The structure of the program at this point looks like a string along
these lines....


set pointers
copy attribute
check for termination
if terminated, break
set pointers
copy attribute
check for termination
if terminated, break
set pointers
copy attribute
check for termination
if terminated, break
...

Unfortunately the only means I could find of skipping over the
remaining code without using a goto was to enclose it thusly....

do {
set pointers
copy attribute
check for termination
if terminated, break
set pointers
copy attribute
check for termination
if terminated, break
set pointers
copy attribute
check for termination
if terminated, break
...
} while (false);

Is this klugie? Is there a better way? I could make the whole piece
of code a function and escape out with a return but that seems just as
extraneous. I could make it a for loop and use the termination as the
exit condition but that would mean setting up a table of pointers to
read from in each loop, just as convoluted.

So, is there a better way?

--
TIA,
Lilith
  #2  
Old August 31st, 2005, 03:55 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

Lilith wrote:[color=blue]
> I was working on a routine that requires me to parse out a list of
> attributes of an object in a text line. An attribute could be empty,
> causing it to revert to it's current value, or the list of attributes
> could terminate without completing and all unlisted attributes revert.
> The structure of the program at this point looks like a string along
> these lines....
>
>
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> ...
>
> Unfortunately the only means I could find of skipping over the
> remaining code without using a goto was to enclose it thusly....
>
> do {
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> ...
> } while (false);
>
> Is this klugie? Is there a better way? I could make the whole piece
> of code a function and escape out with a return but that seems just as
> extraneous. I could make it a for loop and use the termination as the
> exit condition but that would mean setting up a table of pointers to
> read from in each loop, just as convoluted.
>
> So, is there a better way?[/color]

Since you say nothing what "set pointers", "copy attribute", "check for
termination" mean, how can we suggest anything of value? GIGO.

If you can put the "set-copy-check" into a function with arguments and
make it return the state, you can write it

revert_all_attributes();
while (termintated != set_copy_check(stream))
;


V


  #3  
Old August 31st, 2005, 03:25 PM
Howard Hinnant
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

In article <8p1ah15sna318ialgdcdatfm431vikktks@4ax.com>,
Lilith <lilith@dcccd.edu> wrote:
[color=blue]
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> ...
>
> Unfortunately the only means I could find of skipping over the
> remaining code without using a goto was to enclose it thusly....[/color]

I just can't leave this one alone... :-)

I don't have enough information to truly guide you well. Your own
suggestion later sounds promising to me:
[color=blue]
> I could make the whole piece
> of code a function and escape out with a return[/color]

That being said, I've seen code before that was especially convoluted
just for the purpose of avoiding the goto keyword. This is a Bad Thing
(tm).

goto is your friend. Use it wisely. Use it sparingly. Use it when it
is the best tool for the job. Do not abuse it. And most importantly of
all in today's climate: Don't gratuitously complicate your code just to
avoid typing those overly maligned 4 letters g-o-t-o.

But be prepared: Middle level programmers will snicker at you for being
so backwards as to use a goto in your code. After all, everyone who's
anyone knows that goto is evil and should never be used. "Modern"
languages don't even bother with it. Listen to their advice. If they
can show you a way to code without using goto that is more readable, not
larger code size, and not more expensive at run time, then they are
right. If they can't, then snickering alone is not enough reason to
remove a goto. As your programming skill advances (and it will if you
keep considering the advice of your peers), you'll find that you are
hardly ever tempted to use goto. But when you are, it is the perfect
tool for the job - and you'll be able to defend its use to anyone who
may snicker.

-Howard
(who has been snickered at more than once for using goto)
  #4  
Old August 31st, 2005, 04:05 PM
Lilith
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

On Tue, 30 Aug 2005 22:48:44 -0400, "Victor Bazarov"
<v.Abazarov@comAcast.net> wrote:
[color=blue]
>Lilith wrote:[color=green]
>> I was working on a routine that requires me to parse out a list of
>> attributes of an object in a text line. An attribute could be empty,
>> causing it to revert to it's current value, or the list of attributes
>> could terminate without completing and all unlisted attributes revert.
>> The structure of the program at this point looks like a string along
>> these lines....
>>
>>
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> ...
>>
>> Unfortunately the only means I could find of skipping over the
>> remaining code without using a goto was to enclose it thusly....
>>
>> do {
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> ...
>> } while (false);
>>
>> Is this klugie? Is there a better way? I could make the whole piece
>> of code a function and escape out with a return but that seems just as
>> extraneous. I could make it a for loop and use the termination as the
>> exit condition but that would mean setting up a table of pointers to
>> read from in each loop, just as convoluted.
>>
>> So, is there a better way?[/color]
>
>Since you say nothing what "set pointers", "copy attribute", "check for
>termination" mean, how can we suggest anything of value? GIGO.[/color]

I was trying to be concise. Sorry. The pointers were essentially
source and destination pointers along with a pointer to a string of
potential parsing tokens (commas, braces, parenz, etc.) that needed to
be passed to the CopyTil() function. The return value is a pointer to
the character in the source string where the token (or end of string)
was found. The character pointed to by that pointer was what I was
testing. i.e., if the character wasn't a separator then it was a
termintor and there was no sense in checking for more parameters in
the list.
[color=blue]
>If you can put the "set-copy-check" into a function with arguments and
>make it return the state, you can write it[/color]
[color=blue]
> revert_all_attributes();
> while (termintated != set_copy_check(stream))
> ;[/color]
[color=blue]
>V[/color]

--
Lilith
  #5  
Old August 31st, 2005, 04:35 PM
Lilith
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

On Wed, 31 Aug 2005 14:21:05 GMT, Howard Hinnant
<hinnant@metrowerks.com> wrote:
[color=blue]
>In article <8p1ah15sna318ialgdcdatfm431vikktks@4ax.com>,
> Lilith <lilith@dcccd.edu> wrote:
>[color=green]
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers[/color][/color]

[snip]
[color=blue]
>I just can't leave this one alone... :-)[/color]
[color=blue]
>I don't have enough information to truly guide you well. Your own
>suggestion later sounds promising to me:[/color]
[color=blue][color=green]
>> I could make the whole piece
>> of code a function and escape out with a return[/color][/color]
[color=blue]
>That being said, I've seen code before that was especially convoluted
>just for the purpose of avoiding the goto keyword. This is a Bad Thing
>(tm).[/color]
[color=blue]
>goto is your friend. Use it wisely. Use it sparingly. Use it when it
>is the best tool for the job. Do not abuse it. And most importantly of
>all in today's climate: Don't gratuitously complicate your code just to
>avoid typing those overly maligned 4 letters g-o-t-o.[/color]
[color=blue]
>But be prepared: Middle level programmers will snicker at you for being
>so backwards as to use a goto in your code. After all, everyone who's
>anyone knows that goto is evil and should never be used. "Modern"
>languages don't even bother with it. Listen to their advice. If they
>can show you a way to code without using goto that is more readable, not
>larger code size, and not more expensive at run time, then they are
>right. If they can't, then snickering alone is not enough reason to
>remove a goto. As your programming skill advances (and it will if you
>keep considering the advice of your peers), you'll find that you are
>hardly ever tempted to use goto. But when you are, it is the perfect
>tool for the job - and you'll be able to defend its use to anyone who
>may snicker.[/color]

When I started programming (a personal pleasure, not a career) I
expected to program mostly in BASIC but found more enjoyment in
assembler for the 8080/Z80 CPU. Not lots of choices WRT goto there.
My hobby led to being put in charge of the new computer department of
the small tooling distributor I was working for at the time. I
modified program in a language called CPL 5 (I think there's another,
more common, language called CPL) that could only do gotos and gosubs
on conditions. There wasn't any kind of basic looping available so my
only option was spaghetti code.

With CPL 6 they added some rudimentary FOR and WHILE statements and I,
for the first time, began to use them extensively. In my opinion they
greatly helped me keep my code neat and it also helped with
development. I've avoided the goto since that day. At that time I
read lots of articles on programming technique and tried to adjust my
methods based on any number of "authoritative" pronouncements. Some
of them I agree with others I had problems with.

AAR, if I'd had any input in the design of C/C++ I would have liked to
have seen a break statement that would essentially escape the local
set of enclosing parentheses, regardless if it was in a loop or a set
of code to be skipped over.
[color=blue]
>-Howard
>(who has been snickered at more than once for using goto)[/color]

--
Lilith (who won't snidker at her betters)
  #6  
Old August 31st, 2005, 05:35 PM
akarl
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

Lilith wrote:[color=blue]
> I was working on a routine that requires me to parse out a list of
> attributes of an object in a text line. An attribute could be empty,
> causing it to revert to it's current value, or the list of attributes
> could terminate without completing and all unlisted attributes revert.
> The structure of the program at this point looks like a string along
> these lines....
>
>
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> ...
>
> Unfortunately the only means I could find of skipping over the
> remaining code without using a goto was to enclose it thusly....
>
> do {
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> set pointers
> copy attribute
> check for termination
> if terminated, break
> ...
> } while (false);
>
> Is this klugie? Is there a better way? I could make the whole piece
> of code a function and escape out with a return but that seems just as
> extraneous. I could make it a for loop and use the termination as the
> exit condition but that would mean setting up a table of pointers to
> read from in each loop, just as convoluted.
>
> So, is there a better way?[/color]

To me it seems like your pseudo code above is equivalent to

do {
set pointers
copy attribute
check for termination
} while (not terminated);

Am I wrong?


August
  #7  
Old August 31st, 2005, 09:35 PM
Newbie
 
Join Date: Aug 2005
Age: 25
Posts: 4
Default

Quote:
To me it seems like your pseudo code above is equivalent to

do {
set pointers
copy attribute
check for termination
} while (not terminated);

Am I wrong?
Yep that looks like a winner to me. :D
Personally i've never used a goto in any program that i've made in c++.
In fact the primers that I use for reference don't even list it. :p
  #8  
Old August 31st, 2005, 11:45 PM
Lilith
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

On Wed, 31 Aug 2005 16:27:58 GMT, akarl <fusionfive@comhem.se> wrote:
[color=blue]
>Lilith wrote:[color=green]
>> I was working on a routine that requires me to parse out a list of
>> attributes of an object in a text line. An attribute could be empty,
>> causing it to revert to it's current value, or the list of attributes
>> could terminate without completing and all unlisted attributes revert.
>> The structure of the program at this point looks like a string along
>> these lines....
>>
>>
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> ...
>>
>> Unfortunately the only means I could find of skipping over the
>> remaining code without using a goto was to enclose it thusly....
>>
>> do {
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> set pointers
>> copy attribute
>> check for termination
>> if terminated, break
>> ...
>> } while (false);
>>
>> Is this klugie? Is there a better way? I could make the whole piece
>> of code a function and escape out with a return but that seems just as
>> extraneous. I could make it a for loop and use the termination as the
>> exit condition but that would mean setting up a table of pointers to
>> read from in each loop, just as convoluted.
>>
>> So, is there a better way?[/color]
>
>To me it seems like your pseudo code above is equivalent to
>
> do {
> set pointers
> copy attribute
> check for termination
> } while (not terminated);[/color]
[color=blue]
>Am I wrong?[/color]

'Fraid so. Each of the repeating segments above involve a different
set of pointers, or at least one is set, another is manipulated and
the third is constant. So as I finish up with one segment and check
to see if the result is a termination there's no way to
programatically calculate the destination pointer for the next segment
without resorting to a some other resource, like a table, so as to
make it a brief loop. Also, making it a loop dependent on a
termination character would complicate the setting of the source
pointer, which is returned by the copy function and manipulated, for
the next round.
[color=blue]
>August[/color]

--
Lilith
  #9  
Old September 1st, 2005, 03:55 AM
akarl
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

Lilith wrote:[color=blue]
> On Wed, 31 Aug 2005 16:27:58 GMT, akarl <fusionfive@comhem.se> wrote:[color=green]
>>Lilith wrote:[color=darkred]
>>>Unfortunately the only means I could find of skipping over the
>>>remaining code without using a goto was to enclose it thusly....
>>>
>>>do {
>>> set pointers
>>> copy attribute
>>> check for termination
>>> if terminated, break
>>> set pointers
>>> copy attribute
>>> check for termination
>>> if terminated, break
>>> set pointers
>>> copy attribute
>>> check for termination
>>> if terminated, break
>>> ...
>>>} while (false);[/color][/color][/color]
....[color=blue][color=green]
>>
>>To me it seems like your pseudo code above is equivalent to
>>
>> do {
>> set pointers
>> copy attribute
>> check for termination
>> } while (not terminated);[/color]
>
>[color=green]
>>Am I wrong?[/color]
>
>
> 'Fraid so.[/color]

Then I think it needs more detail to be of any use.
[color=blue]
> Each of the repeating segments above involve a different
> set of pointers, or at least one is set, another is manipulated and
> the third is constant. So as I finish up with one segment and check
> to see if the result is a termination there's no way to
> programatically calculate the destination pointer for the next segment
> without resorting to a some other resource, like a table, so as to
> make it a brief loop. Also, making it a loop dependent on a
> termination character would complicate the setting of the source
> pointer, which is returned by the copy function and manipulated, for
> the next round.[/color]
  #10  
Old September 2nd, 2005, 07:45 AM
Dave Rahardja
Guest
 
Posts: n/a
Default Re: There's got to be a more elegant way

On Wed, 31 Aug 2005 14:21:05 GMT, Howard Hinnant <hinnant@metrowerks.com>
wrote:
[color=blue]
>But be prepared: Middle level programmers will snicker at you for being
>so backwards as to use a goto in your code. After all, everyone who's
>anyone knows that goto is evil and should never be used. "Modern"
>languages don't even bother with it. Listen to their advice. If they
>can show you a way to code without using goto that is more readable, not
>larger code size, and not more expensive at run time, then they are
>right. If they can't, then snickering alone is not enough reason to
>remove a goto. As your programming skill advances (and it will if you
>keep considering the advice of your peers), you'll find that you are
>hardly ever tempted to use goto. But when you are, it is the perfect
>tool for the job - and you'll be able to defend its use to anyone who
>may snicker.
>
>-Howard
>(who has been snickered at more than once for using goto)[/color]

The situations you must get into where goto is the only answer do exist.
However, they are extremely rare in my experience. So if you do want to use
goto, you're likely wrong. Very likely.

I've found that code that wants to use goto often needs to be a state machine.

-dr
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles