-1

Say I have a method A that works something as follows:

methodA() {
    while(some condition) {
        do computation...
        if(something 1) {
            do computation...
            if(some condition) {
                return result;
            }
        }
        else if(something 2) {
            do computation...
            if(some other condition) {
                return result;
            }
        }
    }
    return something;
}

This method can return in both of the conditions in the while loop, or after the while loop.

Now say I want some other method, call it methodB(), to be activated before returning. I can do something like this:

methodA() {
    while(some condition) {
        do computation...
        if(something 1) {
            ...
            if(some condition) {
                methodB();
                return result1;
            }
            else if(something 2) {
                ...
                if(some condition) {
                    methodB();
                    return result2;
                }
            }
        }
    }
    methodB();
    return result3;
}

But I find it ugly. Is there a nicer way to do something like that in C#?

Dai
  • 141,631
  • 28
  • 261
  • 374
sel
  • 483
  • 5
  • 16
  • Call `methodB()` before your if statement? – maccettura Apr 05 '17 at 20:58
  • See http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – David Brossard Apr 05 '17 at 21:00
  • 3
    this sounds like [XY Problem](https://meta.stackexchange.com/a/66378/246449), what are you trying to achieve with this? maybe there are other ways to solve your problem but we can't know without knowing what is the actual goal here. – Selman Genç Apr 05 '17 at 21:00
  • maybe something involving try/finally? or just wrap the method so that the one method just does `var val = Whatever(); MethodB(); return val;` – Marc Gravell Apr 05 '17 at 21:00
  • There are a lot of ways to do it. Please see [this answer](http://stackoverflow.com/questions/24430504/how-to-avoid-if-chains#24442170) for five different options (scroll to the section entitled ***How to add common code at the end***).. – John Wu Apr 05 '17 at 21:01

4 Answers4

1

Does methodB depend on any internal-state of methodA? If not, then wrap methodA and call methodB after the the original methodA (now renamed to originalMethodA) returns:

methodA() {
    var result = originalMethodA();
    methodB();
    return result;
}

originalMethodA() {
    // your original methodA goes here, without calls to methodB
}
Dai
  • 141,631
  • 28
  • 261
  • 374
0
methodA() {
var result = result3;
while(some condition) {
    do computation...
       if(something 1) {
           ...
           if(some condition) {
               result = result1;
               break;
           }
           else if(something 2) {
               ...
                if(some condition) {
                  result = result2;
                  break;
               }
           }
       }
   }
   methodB();
   return result;
}
Gene Stempel
  • 285
  • 1
  • 5
0

This is a perfect use for try-finally. Code in the finally block will execute after all three of your return statements.

methodA() {
    try
    {
        while(some condition) {
            do computation...
            if(something 1) {
            ...
                if(some condition) return result1;
                else if(something 2 && some condition) return result2;
            }
        }
        return result3;
    }    
    finally {
        methodB();
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

I think the solution is straight forward, you should not be calling methodB(); anyway in methodA(); it does not generate clean code.

For your solution, if you are not doing multi threading, just take out the methodB(); call and place it right after you call methodA();.

var result = methodA(); methodB();

mfahadi
  • 419
  • 2
  • 10