Click here to Skip to main content
11,578,739 members (63,417 online)
Click here to Skip to main content

Tagged as

The goto-less goto!

, 24 Jan 2011 CPOL 2.2K 2
Rate this:
Please Sign up or sign in to vote.
I'm a C# programmer. I try to avoid goto at all costs, and generally I try to avoid throwing exceptions for the purpose of controlling the flow of execution. I've always been of the opinion that exceptions are for exceptional situations and indicate errors rather than expected conditions.I...
I'm a C# programmer. I try to avoid goto at all costs, and generally I try to avoid throwing exceptions for the purpose of controlling the flow of execution. I've always been of the opinion that exceptions are for exceptional situations and indicate errors rather than expected conditions.

I suggest encapsulating this sort of code in its own method. You can just return from the method early if one of the goto conditions is met.

public void SomeMethod()
{
    MethodThatReturnsEarlyIfCertainConditionsAreMet();
}
 
private void MethodThatReturnsEarlyIfCertainConditionsAreMet()
{
    // Using a random number is for illustrative purposes only;
    // in a real situation you might be testing some other kind
    // of value set elsewhere in the code or whatever.
    Random random = new Random();
    int i = random.Next(10);
 
    if (i < 10) return; // early return; our condition was met.

    DoOtherWork();
 
    i = random.Next(10);
 
    if (i < 5) return; // early return; a different condition was met.

    DoMoreOtherWork();
 
    // etc.
}

While I like the idea of all methods having only one return, in practice I don't feel that it's worth it, especially for situations like this.

I don't know how this sort of thing would behave in C++, but I know in C#, the CLR would handle cleanup of your locals when the method returns, which ultimately includes calling destructors if they are used, though Microsoft's recommendation in .NET languages is to implement IDisposable when you have unmanaged resources to clean up.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Robert S Sharp
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralFine if you have no cleanup. Pin
EFEaglehouse5-Apr-11 4:21
memberEFEaglehouse5-Apr-11 4:21 
GeneralReason for my vote of 3 This is fine if you have no cleanup ... Pin
EFEaglehouse5-Apr-11 4:20
memberEFEaglehouse5-Apr-11 4:20 
GeneralI prefer multiple returns in most cases - the meat of the me... Pin
SimmoTech3-Mar-11 21:21
memberSimmoTech3-Mar-11 21:21 
GeneralAlso, only one return is easier for debugging. Only one spot... Pin
P2Labs14-Feb-11 2:04
memberP2Labs14-Feb-11 2:04 
GeneralReason for my vote of 1 Having one only one return statement... Pin
Wolfgang_Baron9-Feb-11 4:40
memberWolfgang_Baron9-Feb-11 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 24 Jan 2011
Article Copyright 2011 by Robert S Sharp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid