0

Let's say we have two ifs that depend on each other:

if var exists {
    if var is array {
        //Do stuff with var
    } else {
        //Resolve the problem
    }
} else {
    //Resolve the problem in the exact same way as above
}
//Continue execution with the assumption that var is in the appropriate state

How can I refactor this to remove the duplicated code without using gotos or functions/methods?

Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
Mikayla Maki
  • 603
  • 1
  • 5
  • 14
  • Take a look: https://stackoverflow.com/questions/24430504/how-to-avoid-if-chains/24470369#24470369 – glampert Aug 07 '14 at 03:05
  • 4
    Use `goto` of course! – svidgen Aug 07 '14 at 03:09
  • 4
    ... _why_ do you want to avoid a function? That's one of the things they're meant to do - keep you from writing the same thing multiple times. As the accepted answer shows it can be mitigated here, but the fact that you're avoiding a reasonable tool is somewhat troubling. – Clockwork-Muse Aug 07 '14 at 04:21
  • 2
    `if var exists && var is array` – user253751 Aug 07 '14 at 05:37
  • 1
    @Clockwork-Muse Sometimes the else block(s) might contain nothing more than "var = someFunction('String', 1)" which, when turned into a function, makes less sense. Why have such a small function when you can inline the whole thing? My other reason for asking is because it seemed like I was missing something. Something about the way I structured my control flow felt wrong and I couldn't figure what the problem was. And it didn't seem like a function would fix it, it would just patch the problem. – Mikayla Maki Aug 07 '14 at 06:12
  • @immibis Sorry /(((wo)?man)|(.*trans.*))/i You're about 3 and 1/2 hours too late, DaveNay beat you too it. – Mikayla Maki Aug 07 '14 at 06:17
  • @TrentonMaki good to know i can be an intertranslation. – user253751 Aug 07 '14 at 07:05
  • @immibis Whatever you need to be. – Mikayla Maki Aug 07 '14 at 14:04
  • @TrentonMaki Isn't that already using a function? Yes it is, that was a bad example. Perhaps something like: `var = new Object("String", 1)` – Mikayla Maki Aug 07 '14 at 22:16

3 Answers3

12

Am I missing something more complicated here, or shouldn't this just be:

if var exists and var is array {
    //Do stuff with var
} else {
    //Resolve the problem
}

//Continue execution with the assumption that var is in the appropriate state

The check for if var exists will short-circuit and fall through to the else resolution block if it evaluates to false.

Dave Nay
  • 3,809
  • 2
  • 18
  • 25
  • +1 I'm the author of the other answer (http://programmers.stackexchange.com/a/252569/61852) but this one is better, unless we are missing something. – Tulains Córdova Aug 07 '14 at 01:57
  • Thanks! I noticed this could be a solution but I had heard rumors that it's actually faster on modern systems to NOT short circuit the operation and I was nervous about relying on it. On reading your answer I realized that that might be a bit ridiculous. – Mikayla Maki Aug 07 '14 at 03:30
  • @TrentonMaki, short-circuting can be slower yes. But in your case, you just wrote your own short circuiting logic. That's certainly not faster. – Winston Ewert Aug 27 '14 at 15:54
1

If you don't mind "do stuff with var" runs every time:

if var does not exist {
    //Resolve the problem
}

if var is array {
    //Do stuff with var
} 
//Continue execution with the assumption that var is in the appropriate state

If you don't want "do stuff with var" to run every time (as in original code):

if var does not exist {
    //Resolve the problem
    flag = true
}

if var is array && flag == true {
    //Do stuff with var
} 
//Continue execution with the assumption that var is in the appropriate state
Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
1

Assuming the tests cannot be joined in one expression I would write:

bool ok = false;
if var exists {
    if var is array {
        //Do stuff with var
        ok = true;
    }
}
if (!ok) {
    //Resolve the problem
}
//Continue execution with the assumption that var is in the appropriate state
Florian F
  • 1,127
  • 1
  • 6
  • 13
  • I did consider this as an option but I don't like polluting the context with one-off variables. But there might be situations where this is the best option, say where the checks are non trivial or you need to do something in between the ifs (assuming all other things being equal). – Mikayla Maki Aug 27 '14 at 19:28