Skip to main content
added 230 characters in body
Source Link
Kevin
  • 844
  • 1
  • 7
  • 16

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        break;
    
    case shouldDoAction2:
        action2();
        break;
    
    default:
        return false;
    }
    return true;
}

Is it bad practise to write it like this:

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        return true;
    
    case shouldDoAction2:
        action2();
        return true;
    
    default:
        return false;
    }
}

Edit: I don't see how this is a duplicate of the linked question, I'm asking about the best practise of writing a function with a single switch statement, I'm not at all asking about using a single or multiple return statements.

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        break;
    
    case shouldDoAction2:
        action2();
        break;
    
    default:
        return false;
    }
    return true;
}

Is it bad practise to write it like this:

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        return true;
    
    case shouldDoAction2:
        action2();
        return true;
    
    default:
        return false;
    }
}

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        break;
    
    case shouldDoAction2:
        action2();
        break;
    
    default:
        return false;
    }
    return true;
}

Is it bad practise to write it like this:

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        return true;
    
    case shouldDoAction2:
        action2();
        return true;
    
    default:
        return false;
    }
}

Edit: I don't see how this is a duplicate of the linked question, I'm asking about the best practise of writing a function with a single switch statement, I'm not at all asking about using a single or multiple return statements.

[Edit removed during grace period]
Source Link
Kevin
  • 844
  • 1
  • 7
  • 16
Cleaning up curly brackets that confused me a bit
Source Link

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        break;
    }
    
    case shouldDoAction2:
        action2();
        break;
    }
    
    default: {
        return false;
    }
    return true;
}

Is it bad practise to write it like this:

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        return true;
    }
    
    case shouldDoAction2:
        action2();
        return true;
    }
    
    default: {
        return false;
    }
}

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        break;
    }
    
    case shouldDoAction2:
        action2();
        break;
    }
    
    default: {
        return false;
    }
    return true;
}

Is it bad practise to write it like this:

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        return true;
    }
    
    case shouldDoAction2:
        action2();
        return true;
    }
    
    default: {
        return false;
    }
}

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        break;
    
    case shouldDoAction2:
        action2();
        break;
    
    default:
        return false;
    }
    return true;
}

Is it bad practise to write it like this:

bool executeSomeAction(someValue) {
    switch (someValue) {
    case shouldDoAction1:
        action1();
        return true;
    
    case shouldDoAction2:
        action2();
        return true;
    
    default:
        return false;
    }
}
Source Link
Kevin
  • 844
  • 1
  • 7
  • 16
Loading