Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Why is '??' an operatore in C#?

Name: Anonymous 2010-02-28 18:55

/prog/, I want to know what is more more accepted method of two different if statements sharing an identical routine. I'm not good at explaining, so I think you'll work out what I mean by the following examples:

So, method one:
void func_main(){
    int i = 0;

    if (...bool1...){
        i = 1;
    }
    else if (...bool2...){
        i = 2;
    }
    else {
        return;
    }
   
    ...functions...
}


Method two:

void func_sub(){
    ...functions...
}

void func_main(){
    int i = 0;
   
    if (...bool1...){
        i = 1;
        func_sub();
    }
    else if (...bool2...){
        i = 2;
        func_sub();
    }
}

Method three:

void func_main(){
    int i = 0;
   
    if (...bool1... || ...bool2...){
        if (...bool1...){
            i = 1;
        }
        else if (...bool2...){
            i = 2;
        }
        ...functions...
    }
}

Name: Anonymous 2010-02-28 19:01

Method 3 seems redundant. There is no need for the extra if.

Both method 1 and 2 are ok. I see no reason why one would be better than the other

Name: Anonymous 2010-02-28 19:09

int i = bool1 ? 1 : bool2 ? 2 : 0;

Name: Anonymous 2010-02-28 19:26

>>3

The i is completely redundant, just imagine it as a few routines which are specific for the if statement.

Name: Anonymous 2010-03-01 15:54

>>1
It depends. If there really is a common block with some extra code for each condition, I would go with method 3. It's probably the most efficient, provided you really do pull out your conditions and store them in bools. This way there is no function call overhead.

If there are significant differences in the conditional code, I'd go with method 2, and pass the arguments it needs based on which if branch is run. This is one of the reasons I wish C supported nested functions, and why I use them in Python a lot. With nested functions you can properly organize the sub just before the code that uses it and the sub doesn't leak out of the scope of the containing function.

And definitely don't use method 1, because the control flow is non-obvious. I use early returns liberally, but only on condition of a single if; it makes it obvious that "this is a condition for the rest of the function body to run". For this reason I'd suggest a method four:

void func_main(){

    // one of these needs to be true
    bool bool1 = ...condition...;
    bool bool2 = ...condition...;
    if (!bool1 && !bool2)
      return;

    int i = 0;
    if (bool1){
        i = 1;
    } else if (bool2){
        i = 2;
    }
  
    ...functions...

}


The third if is not needed here of course (you could just use else), but it makes it more obvious, and any optimizer worth its salt will remove it so I'd leave it in. And of course give the bools meaningful names.

Name: Anonymous 2010-12-10 15:07

Name: Anonymous 2010-12-21 21:12

Don't change these.
Name: Email:
Entire Thread Thread List