473,326 Members | 2,196 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,326 software developers and data experts.

C++ and goto - can I?

I wrote:
....
for(int i = 0; i < somevar; i++)
{
bool b = true;
for(deque<int>iterator it = freeID.begin(); it != freeID.end(); it++)
if(i == (*it))
{
b = false;
break;
}
if(b)
ret.push_back(i)
}
....
Can I:
....
for(int i = 0; i < somevar; i++)
{
for(deque<int>iterator it = freeID.begin(); it != freeID.end(); it++)
if(i == (*it))
goto myfunction_find;
ret.push_back(i);
myfunction_find:
;
}
....

For me it's shorter, faster and more simple, but I've herd that goto in
c++ program could make problems.
Is it work everywhere and always?
Regards.
--
Linux user: #376500 (see http://counter.li.org/)
Jul 23 '05 #1
6 1427
* Uzytkownik:
I wrote:
...
for(int i = 0; i < somevar; i++)
{
bool b = true;
for(deque<int>iterator it = freeID.begin(); it != freeID.end(); it++)
if(i == (*it))
{
b = false;
break;
}
if(b)
ret.push_back(i)
}
...
Can I:
...
for(int i = 0; i < somevar; i++)
{
for(deque<int>iterator it = freeID.begin(); it != freeID.end(); it++)
if(i == (*it))
goto myfunction_find;
ret.push_back(i);
myfunction_find:
;
}
...

For me it's shorter, faster and more simple, but I've herd that goto in
c++ program could make problems.
It not only can make problems, it does make problems.

In your case, it's got you thinking at a too low abstraction level so
you've turned something extremely simple into something less simple.

Try (off the cuff):
typedef std::deque<int>::const_iterator Iterator;

for( Iterator it = freeId.begin(); it != freeId.end(); ++it )
{
if( 0 <= *it && *it < somevar )
{
ret.push_back( *it );
}
}
In my view this is both more clear and more efficient.
Is it work everywhere and always?


Yes, technically it's well-defined, and it will nearly always have the
effect of obscuring your code and making you do unsimple things like above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach napisa³(a):
* Uzytkownik:
I wrote:
...
for(int i = 0; i < somevar; i++)
{
bool b = true;
for(deque<int>iterator it = freeID.begin(); it != freeID.end(); it++)
if(i == (*it))
{
b = false;
break;
}
if(b)
ret.push_back(i)
}
...
Can I:
...
for(int i = 0; i < somevar; i++)
{
for(deque<int>iterator it = freeID.begin(); it != freeID.end(); it++)
if(i == (*it))
goto myfunction_find;
ret.push_back(i);
myfunction_find:
;
}
...

For me it's shorter, faster and more simple, but I've herd that goto in
c++ program could make problems.

It not only can make problems, it does make problems.

In your case, it's got you thinking at a too low abstraction level so
you've turned something extremely simple into something less simple.

Try (off the cuff):
typedef std::deque<int>::const_iterator Iterator;

for( Iterator it = freeId.begin(); it != freeId.end(); ++it )
{
if( 0 <= *it && *it < somevar )
{
ret.push_back( *it );
}
}
In my view this is both more clear and more efficient.

Is it work everywhere and always?

Yes, technically it's well-defined, and it will nearly always have the
effect of obscuring your code and making you do unsimple things like above.


More simple :)
freeID become set and
for(int i = 0; i < somevar; i++)
if(freeID.find(i) == freeID.end())
ret.push_back(i);
In one my code and in your code (may be you don't now whot I'd like to
do) is error.
This code should return in ret all used ID's less then somevar.
Regards.
--
Linux user: #376500 (patrz http://counter.li.org/)
Jul 23 '05 #3
Uzytkownik wrote:
Alf P. Steinbach napisa³(a):
if( 0 <= *it && *it < somevar )
{
ret.push_back( *it );
}

More simple :)
freeID become set and
for(int i = 0; i < somevar; i++)
if(freeID.find(i) == freeID.end())
ret.push_back(i);
In one my code and in your code (may be you don't now whot I'd like to
do) is error.


Alf performed on your code what is called a "Refactor". That is a
transformation of structure that leaves behavior exactly the same.

You performed what might playfully be called a "refeature". You changed
structure and behavior at the same time.

As you learn to design, and learn not to want to use 'goto', you will know
when you are refactoring, and how to strictly change only structure.

Tip 1: Don't refactor and refeature at the same time. Only do one or the
other.

Tip 2: Write lots of unit tests, and run them every 1~10 edits. If they
fail, use Undo.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #4
* Phlip:
Uzytkownik wrote:
Alf P. Steinbach napisa³(a):
if( 0 <= *it && *it < somevar )
{
ret.push_back( *it );
}

More simple :)
freeID become set and
for(int i = 0; i < somevar; i++)
if(freeID.find(i) == freeID.end())
ret.push_back(i);
In one my code and in your code (may be you don't now whot I'd like to
do) is error.


Alf performed on your code what is called a "Refactor". That is a
transformation of structure that leaves behavior exactly the same.


Uh, I'm afraid I slipped up: I misunderstood the intent.

Uzytkownik is interested in, and Uzytkownik's original code produced, the
complement wrt. [0...somevar] of the set my code produces.

Without knowing more it's difficult to give advice about efficiency
here, but there are potentially some problems with that (as I'm sure you
noted): Uzytkownik's code has execution time on the order of
somevar*freeId.size(), and passing a large list of integers around is probably
not very efficient.

Tip 1: Don't refactor and refeature at the same time. Only do one or the
other.

Tip 2: Write lots of unit tests, and run them every 1~10 edits. If they
fail, use Undo.


I can only agree with both points, and _apologize_ for the incorrect code!

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
Alf P. Steinbach wrote:
Alf performed on your code what is called a "Refactor". That is a
transformation of structure that leaves behavior exactly the same.


Uh, I'm afraid I slipped up: I misunderstood the intent.

Uzytkownik is interested in, and Uzytkownik's original code produced, the
complement wrt. [0...somevar] of the set my code produces.


I didn't actually read your code closely enough to see if it qualified for
the Refactor Lecture ... before leaping in. ;-)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #6
On Mon, 04 Apr 2005 20:54:52 +0200, Uzytkownik
<uz*********@poczta.onet.pl> wrote:
More simple :)
freeID become set and


It doesn't have to -- you can use "find" algorithm on deque too.

have a nice day
bye
--
Maciej "MACiAS" Pilichowski http://bantu.fm.interia.pl/

M A R G O T --> http://www.margot.cad.pl/
automatyczny t³umacz (wczesna wersja rozwojowa) angielsko-polski
Jul 23 '05 #7

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

Similar topics

30
by: Hayri ERDENER | last post by:
hi, what is the equivalent of C languages' goto statement in python? best regards
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
37
by: Tim Marshall | last post by:
From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo be part of an OnError process) for these will lead you...
52
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
45
by: Debashish Chakravarty | last post by:
K&R pg.66 describes two situations when using goto makes sense. Has anyone here come across situations where using goto provided the most elegant solution. --...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
3
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to...
17
by: SoftEast | last post by:
Hi Buddies, I have read a lot of stuffs regarding not using GOTO statements to opt a good programming style http://david.tribble.com/text/goto.html]. Can anybody give a particular lines of code...
59
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.