Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

break out of if

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Rivers - 28 Mar 2008 15:03 GMT
it seems a shame you can not do this:

if (blah) {
if (blob) break;
}//if

i know it seems like you can do

if (blah && !blob) {
}//if

but what if you want to do this:

if (blah) {
bunch of code
if (blob1) break;
bunch of code
if (blob2) break;
bunch of code
if (blob3) break;
bunch of code
if (blob4) break;
bunch of code
}//if

without break you have to

if (blah) {
    bunch of code
    if (!blob1) {
        bunch of code
        if (!blob2) {
            bunch of code
            if (!blob3) {
                bunch of code
                if (!blob4) {
                    bunch of code
                }//if
            }//if
        }//if
    }//if
}//if

or you can put the code into a method and use return

if (blah) syntaxworkaround();

void syntaxworkaround() {
bunch of code
if (blob1) return;
bunch of code
if (blob2) return;
bunch of code
if (blob3) return;
bunch of code
if (blob4) return;
}//method

but what if there are 20 local variables that you are using?
now you have to pass them all
or create a new argument class for them
or move them up to class scope
but that might not be sensible

why not just implement break for all blocks ?

using (something) {
if (problem) break;
}//using

and on that subject

why not have multiple breaks?

while (true) {
while (true) {
 if (fedup) break(2);
}//while
}//while

breaks out of two enclosing blocks

if there is amiguity between break out of loop or break out of block

maybe use a different keyword "leave" or something

so it is always clear which class of block you wish to exit

why? that's what I want to know ...
Marc Gravell - 28 Mar 2008 15:18 GMT
> why not just implement break for all blocks ?

That would change the meaning of an existing "break" used validly to break
from a loop.

> why not have multiple breaks? [example: break(2)]

That would be very susceptible to introducing bugs when innocently adding an
extra nest level... you'd need some kind of named break point, but then
you're back at "goto considered harmful"

> why? that's what I want to know ...

Most of the time it isn't an issue... break etc are mainly useful to exit
loops; as you say, for regular logic you can always refactor into a method,
and the IDE can do most of this for you by selecting the block you want to
move (i.e. it should handle the args etc).

If you *really* want, you could semi-refactor it in-place using a delegate;
this will /essentially/ use captured variables to do something like the "new
argument class" idea you mentioned, but keeps the code in-place; slightly
more expensive, though.

           MethodInvoker someCode = delegate {
               // foo
               if(blob1) return;
               // bar
               if(blob2) return;
               // blip
           };
           someCode(); // exec

Marc
Jon Skeet [C# MVP] - 28 Mar 2008 15:24 GMT
<snip>

> If you *really* want, you could semi-refactor it in-place using a delegate;
> this will /essentially/ use captured variables to do something like the "new
[quoted text clipped - 9 lines]
>             };
>             someCode(); // exec

My eyes! My eyes! (And yes, that's after pointing out the availability
of goto.)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 28 Mar 2008 15:35 GMT
lol; we'll have to host an "ugliest trivial code" compo some time ;-p
I never said it was great, just that it would technically work...

For myself, it'd be: right-click, refactor, extract method, [fix up any
oddities], move on.

Marc
Jon Skeet [C# MVP] - 28 Mar 2008 16:08 GMT
> lol; we'll have to host an "ugliest trivial code" compo some time ;-p

Tonight? :)

> I never said it was great, just that it would technically work...

Sounds like the motto for the ugliest code competition.

> For myself, it'd be: right-click, refactor, extract method, [fix up any
> oddities], move on.

:)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 28 Mar 2008 16:21 GMT
Nah, if there are any perl devs they'll win hands down... ;-p
Ignacio Machin ( .NET/ C# MVP ) - 28 Mar 2008 15:41 GMT
> <snip>
>
[quoted text clipped - 18 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

hey, My opinion is that IF it does exist is for a reason and you can
use it where it makes sense.
That said, I have never used it :)
Jon Skeet [C# MVP] - 28 Mar 2008 15:18 GMT
<snip>

> why? that's what I want to know ...

Well, there's always "goto" if you really want it:

using System;

public class Test
{
   static void Main()
   {
       int i = 5;
       int j = 10;
       
       if (i==5)
       {
           if (j==10)
           {
               goto done;
           }
           Console.WriteLine("Oo!");
       }
       done:
       Console.WriteLine("Done");
   }
}

However, I'd suggest that if you get into that kind of situation (with
20 local variables and nesting 6 levels deep) then you'd be a lot
better off refactoring into smaller methods anyway.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ignacio Machin ( .NET/ C# MVP ) - 28 Mar 2008 15:39 GMT
> it seems a shame you can not do this:
>
[quoted text clipped - 92 lines]
>
> why? that's what I want to know ...

You can use return instead, refactor that piece of code in a method
alone.
You can refactor each of the pieces of code in a separated methods.
Or you can wrap the entire thing in a while(true) , then you can use
break; as y ou want, just make sure to put a break at the end of the
piece of code.
John Rivers - 28 Mar 2008 19:08 GMT
thanks for your thoughts

i learnt a thing or three

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.