473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Aug 31 '05 #1
9 1306
Lilith wrote:
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?


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
Aug 31 '05 #2
In article <8p********************************@4ax.com>,
Lilith <li****@dcccd.edu> wrote:
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....
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:
I could make the whole piece
of code a function and escape out with a return


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)
Aug 31 '05 #3
On Tue, 30 Aug 2005 22:48:44 -0400, "Victor Bazarov"
<v.********@comAcast.net> wrote:
Lilith wrote:
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?
Since you say nothing what "set pointers", "copy attribute", "check for
termination" mean, how can we suggest anything of value? GIGO.


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.
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


--
Lilith
Aug 31 '05 #4
On Wed, 31 Aug 2005 14:21:05 GMT, Howard Hinnant
<hi*****@metrowerks.com> wrote:
In article <8p********************************@4ax.com>,
Lilith <li****@dcccd.edu> wrote:
set pointers
copy attribute
check for termination
if terminated, break
set pointers
[snip]
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: I could make the whole piece
of code a function and escape out with a return

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.
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.
-Howard
(who has been snickered at more than once for using goto)


--
Lilith (who won't snidker at her betters)
Aug 31 '05 #5
Lilith wrote:
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?


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
Aug 31 '05 #6
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
Aug 31 '05 #7
On Wed, 31 Aug 2005 16:27:58 GMT, akarl <fu********@comhem.se> wrote:
Lilith wrote:
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?
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?
'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.
August


--
Lilith
Aug 31 '05 #8
Lilith wrote:
On Wed, 31 Aug 2005 16:27:58 GMT, akarl <fu********@comhem.se> wrote:
Lilith wrote:
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);
....
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?

'Fraid so.


Then I think it needs more detail to be of any use.
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.

Sep 1 '05 #9
On Wed, 31 Aug 2005 14:21:05 GMT, Howard Hinnant <hi*****@metrowerks.com>
wrote:
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)


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
Sep 2 '05 #10

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

Similar topics

0
by: Pham Nuwen | last post by:
Ok I have a bit of a messy problem, and I can code this solution but I'm hoping someone here can help me find a bit more elegant way to do this. basically I have a table in in the DB (postgresql)...
5
by: Steve | last post by:
Hey, well I'm really pleased with myself for writing a little script today that changes variables according to a radio button. The thing is, I have this feeling part of my script could be a lot...
4
by: Kamilche | last post by:
''' Is there a more elegant way of doing this? I would like to have the arguments to pack automatically taken from lst. ''' def pack(self): lst = return struct.pack('<IIIiiiBBBHHH', \...
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
4
by: Pierre Couderc | last post by:
I want to share enum between 2 classes C1 and C2: class C1 { public: enum shut {AAA,BBB, CCC}; shut m_C1; void set(shut &s) { m_C1=s;} };
4
by: Tamir Khason | last post by:
How can I convert (elegant way) Collection to Array without looping and new instances. E.G: I want to add elements of one menu to other, so secondMenu.MenuItems.AddRange(firstMenu.MenuItems);...
28
by: Rob Cowie | last post by:
Hi all, I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba', 'abb', 'abc' etc. all the way to 'zzz'. How would you construct a generator to acheive this? A simple,...
5
by: Helmut Jarausch | last post by:
Hi, I'm looking for an elegant solution to the following (quite common) problem: Given a string of substrings separated by white space, split this into tuple/list of elements. The problem...
4
by: Bob | last post by:
Hi All, Was wondering if in C# there is an elegant way of displaying and or calculating fractions. The case: we have an app that measures/slices dices etc and all our internal measures and...
0
by: Lie Ryan | last post by:
On Wed, 01 Oct 2008 10:46:33 -0400, Luis Zarrabeitia wrote: No (or I'm not aware of any). Why? Because for some iterable, it is not possible to know in advance its length (video data stream,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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,...

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.