Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to break/return method execution from another method?

I have something like this:

void MethodToBreak()
{
    // do something

    if(something)
    {
        MethodThatBreaks();
        return;
    }

    // do something
}

void MethodThatBreaks()
{
    // do something
}

So, I was wondering: is it possible to break execution from: MethodThatBreaks()? Then, I would have: if(something) MethodThatBreaks(); and if the condition inside if is true, nothing after that row would be executed.

NOTE: I know it's possible with else in this case, but I don't want that.

like image 443
avance70 Avatar asked Nov 06 '22 08:11

avance70


1 Answers

It would be a nightmare to maintain if you were to upset execution of one method from another. Trying to figure out why your control flow is all over the place six months down the line, or for a another developer, would be aneurysm-inducing.

There's nothing wrong with what you're already doing there, although personally I'd use an else. Is there a particular reason why you don't want to use else? If it's that the remaining code is too long that's perhaps and indication you should refactor.

like image 99
Dave D Avatar answered Nov 09 '22 13:11

Dave D