By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

C++ and goto - can I?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/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.

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

P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.