break out of if

J

John Rivers

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

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

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

Jon Skeet [C# MVP]

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

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

Marc Gravell

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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

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

Ignacio Machin ( .NET/ C# MVP )

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

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 :)
 
J

Jon Skeet [C# MVP]

Marc Gravell said:
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.

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top