CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 53
  1. #31
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Is using goto a bad practice

    Quote Originally Posted by souldog
    here is a very readable way to breakout of nested loops.
    Code:
    for(size_t i = 1, break_loops = 0; i < 10 && !break_loops; i++)
    {
    	for(size_t j = 0; j < 10 && !break_loops; j++)
    	{
    		if(WHAT_EVER)
    			break_loops = 1;
    	}
    }
    If you'd have a little more code in your loop (like in some real life example):
    Code:
    for(size_t i = 1, break_loops = 0; i < 10 && !break_loops; i++)
    {
    	for(size_t j = 0; j < 10 && !break_loops; j++)
    	{
    		if(WHAT_EVER)
    			break_loops = 1;
    	}
    	// MORE CODE HERE
    }
    the part "// MORE CODE HERE" will still be executed, unless, of course, you add multiple tests for "break_loops", wich doesn't help the things. Think about more than two nested loops. If the code is trivial - almost any solution will be readable.
    About SuperKoko's suggestion of multiple return statements - I believe it isn't much better than goto's, and sometimes - much worse.
    P.S. When we are done with goto, lets talk about use of tabs vs. spaces for indentation.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  2. #32
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Is using goto a bad practice

    Quote Originally Posted by VladimirF
    P.S. When we are done with goto, lets talk about use of tabs vs. spaces for indentation.

    Tabs are evil when you have to port code between multiple operating systems.

    flat out evil.


    -Kendall
    John 3:16
    For God so loved the world ...

  3. #33
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Is using goto a bad practice

    Quote Originally Posted by kenrus
    Tabs are evil when you have to port code between multiple operating systems.

    flat out evil.


    -Kendall

    Along those lines...

    One of the best, unintentional, things my employer did for my productivity was to purchase a monitor that can be rotated to portrait orientation.

    I am able to see so much more code on the screen at one time.

    As time went on, 2 (out of 3 - the 3rd is a mainframe programmer, and well you know...) of the programmers around me have also rotated their monitors to the portrait orientation.

    -Kendall
    John 3:16
    For God so loved the world ...

  4. #34
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Is using goto a bad practice

    Quote Originally Posted by kenrus
    Tabs are evil when you have to port code between multiple operating systems.

    flat out evil.


    -Kendall
    yah, I know I am going to pay dearly for that bad habit in the future.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #35
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Is using goto a bad practice

    Quote Originally Posted by kenrus
    Tabs are evil when you have to port code between multiple operating systems.
    What OS today can't handle tabs???
    When you use tabs for indentation, I can view YOUR code the way I like it! And if your editor can't expand them to whatever number of spaces you want - shame on it!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #36
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Is using goto a bad practice

    Quote Originally Posted by kenrus
    Tabs are evil when you have to port code between multiple operating systems.

    flat out evil.
    No. The only flat out evil thing is mixing tabs and spaces for indentation!

  7. #37
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Is using goto a bad practice

    Quote Originally Posted by VladimirF
    What OS today can't handle tabs???
    When you use tabs for indentation, I can view YOUR code the way I like it! And if your editor can't expand them to whatever number of spaces you want - shame on it!
    I am not aware of any operating system that can not handle tabs. My complaint comes from the fact that different IDEs have different spacing for tabs. Most have an option where you can set the spacing for tabs, but that kind of option tends to be obscure.



    -Kendall
    John 3:16
    For God so loved the world ...

  8. #38
    Join Date
    Feb 2006
    Posts
    132

    Re: Is using goto a bad practice

    Ah goto... I remember a lot of gotos on Apple II Basic

    I remember programming my TI 85 calculator in H.S.

    I've heard it is bad and back around the 70's(I think) they moved away from goto and developed structured programming because of lots of bugs and problems tracing those bugs.

  9. #39
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729

    Re: Is using goto a bad practice

    Quote Originally Posted by treuss
    No. The only flat out evil thing is mixing tabs and spaces for indentation!

    i agree =). i use tabs all the time and have never met a situation on any platform where this caused an issue. imo it produces more readable code and is certainly easier than spacespacespace all the time

    as for gotos, i'm pretty sure i commented in one of those other threads back in the day but i think they should be avoided as much as possible in c++. BUT, as someone else has already said, an experienced programmer using gotos is a very different story than a beginner and i think this designation is where the answer lies. for someone who can think through in their head all the implications of using a goto and all the alternatives available it probably isn't that big of a deal (aside from the personal beliefs it offends)

  10. #40
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Is using goto a bad practice

    Quote Originally Posted by filthy_mcnasty
    as for gotos, i'm pretty sure i commented in one of those other threads back in the day but i think they should be avoided as much as possible in c++. BUT, as someone else has already said, an experienced programmer using gotos is a very different story than a beginner
    Yep - it's the difference between a program that simply fails and one that brings down the system with it.
    Quote Originally Posted by filthy_mcnasty
    [...]. for someone who can think through in their head all the implications of using a goto and all the alternatives available [...]
    Another description of the null set
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  11. #41
    Join Date
    Sep 2007
    Posts
    62

    Re: Is using goto a bad practice

    Hi,

    I know that the following is not what throw and catch were designed to do, but is it not possible to use throw and catch as an alternative to goto?

    For example in the code below, method 1 breaks out of the double for loop using the goto, whilst method 2 breaks out of the double for loop using throw and catch.

    What are your thoughts on this? Thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    const int asize = 5;
    
    int main(void)
    {
    
    	//Method 1 - using goto to exit double for loop
    	for(int row = 0; row < asize; ++row){
    		for(int col = 0; col < asize; ++col){
    			cout<<"row = "<<row<<", col = "<<col<<endl;
    			if(row==2 && col==2){ 
    				goto escape;
    			}
    		}
    	}
    escape :
    	cout<<"Finished method 1\n";
    
    	//Method 2 - using throw and catch to exit double for loop
    	int escape=0;
    
    	try{
    		for(int row = 0; row < asize; ++row){
    			for(int col = 0; col < asize; ++col){
    				cout<<"row = "<<row<<", col = "<<col<<endl;
    				if(row==2 && col==2){ 
    					throw escape;
    				}
    			}
    		}
    	}catch(int){}
    	cout<<"Finished method 2\n";
    
    	system("pause");
    	return(0);
    }
    Last edited by artella; September 26th, 2007 at 08:52 AM.

  12. #42
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: Is using goto a bad practice

    goto is a tool and since there is no proof for whether its usage is correct or not, use it where it is needed and be prepared to be wrong in your decision
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  13. #43
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: Is using goto a bad practice

    Consider what happens if another function in try block also happens to throw an integer. That throw will trigger your goto replacement instead of up to the next matching catch block higher up the stack.

    Anyway, if you are going to use goto-like behavior, use a goto! At least you'll be clear in your intentions!

    The problem most people have with goto is that the logic for a goto is disconnected from the logic for a loop. For while, do-while, and for loops, the logic is intimately connected to the loop (specifically where the loop is defined). This connection is important because it makes code more maintainable. Without the connection, it is very easy to make spaghetti code.

    My rule of thumb: The only time one should use goto is when writing time-critical code and profiling shows that it is not possible without gotos. (If your time-critical code will improve by use of goto's, it is likely already going to have some other uncommon constructs that will be difficult to maintain. And yes, I have come across these cases.)

    - Kevin
    Kevin Hall

  14. #44
    Join Date
    Sep 2007
    Posts
    62

    Re: Is using goto a bad practice

    Hi Kevin,

    The problem that you mentioned above can be very easily circumvented by defining a
    unique identifier (e.g. a class), and then throwing that. I have rewritten
    the code below to show what I mean :

    Code:
    #include <iostream>
    using namespace std;
    
    const int asize = 5;
    
    namespace {
    	class unique{
    	} jump;
    };
    
    int main(void)
    {
    
    	//Method 1 - using goto to exit double for loop
    	for(int row = 0; row < asize; ++row){
    		for(int col = 0; col < asize; ++col){
    			cout<<"row = "<<row<<", col = "<<col<<endl;
    			if(row==2 && col==2){ 
    				goto escape;
    			}
    		}
    	}
    escape :
    	cout<<"Finished method 1\n";
    
    	//Method 2 - using throw and catch to exit double for loop
    	int escape=0;
    
    	try{
    		for(int row = 0; row < asize; ++row){
    			for(int col = 0; col < asize; ++col){
    				cout<<"row = "<<row<<", col = "<<col<<endl;
    				if(row==2 && col==2){ 
    					throw jump;
    				}
    			}
    		}
    	}catch(unique){}
    	cout<<"Finished method 2\n";
    
    	system("pause");
    	return(0);
    }

  15. #45
    Join Date
    Feb 2007
    Posts
    186

    Re: Is using goto a bad practice

    I think it the answer is it depends. I think in most languages 99.8 percent of the time you should not be using gotos. That being said once in a very great while you might have nested switches or something where a goto would be easier to read then most other options in that case i think its ok. If your programming in assembly then obviously you cant avoid gotos since a label and a jmp instruction is the same thing as a goto. Gotos are fundamental to computers really so theres no avoiding them completely. I mean its translated to assembly with labels and jumps anyways.

Page 3 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured