2

For example, suppose I have a toggle button listener, and it has some tasks to do according to the state of toggle:

style 1 : use many ternary operator:

tutorialModeToggle(isTutorialOn){
    SoundUtility.play(isTutorialOn?"s1.mp3":"s2.mp3");
    this.titleLabel.setString(isTutorialOn?"(Tutorial Mode)":"");
    this.tutorialLayout.opacity=isTutorialOn?255:128;
    isTutorialOn?this.playAnimation1():this.playAnimation2();
    .
    .
    .
}

style 2: use a single if-else:

tutorialModeToggle(isTutorialOn){
    if(isTutorialOn){
        SoundUtility.play("s1.mp3");
        this.titleLabel.setString("(Tutorial Mode)");
        this.tutorialLayout.opacity=255;
        this.playAnimation1();
        .
        .
        .
    }else{
        SoundUtility.play("s2.mp3");
        this.titleLabel.setString("");
        this.tutorialLayout.opacity=128;
        this.playAnimation2()
        .
        .
        .
    }
}

Which one should I use?

Note: I'm not asking about

Ternary operator considered harmful?

Should the ternary operator be used outside of assignment statements?

because they are about ONE ternary operator VS ONE if-else, but I'm asking about MANY ternary operator vs ONE if-else.

Also I'm not asking about

Style for control flow with validation checks

because those question is about MULTIPLE condition check but I'm asking about ONE condition check

5

I your language supports it, I'd suggest a third way: use a tuple, or your language's equivalent. This avoids the need for the code repetition with if/else and avoids testing isTutorialOn multiple times:

tutorialModeToggle(isTutorialOn) {
    (sound, title, opacity, playAnimation) = isTutorialOn
        ? ("s1.mp3", "(Tutorial Mode)", 255, this.playAnimation1)
        : ("s2.mp3", "", 128, this.playAnimation2);

    SoundUtility.play(sound);
    this.titleLabel.setString(title);
    this.tutorialLayout.opacity = opacity;
    playAnimation();
    .
    .
    .
}
  • +1, this is the way to go. Both of your suggestions required multiplying either the if(...) or the play(), setString() etc. This solution is superior because it avoids all duplication. – Kilian Foth Nov 23 '18 at 11:03
  • 1
    Afraid that OP will ask "Should I use ternary operator or if..else for creating tuples_ ;) – Fabio Nov 23 '18 at 18:39
  • +1 - but if the language doesn't have tuples, this could be expanded into four variable assignments, each with a ternary assignment. That's still more readable than either of the initial options. – Alex Reinking Dec 14 '18 at 2:35
  • Or if you language doesn't have tuples, an array of simple structures or records initialized with the proper values for each later function would work. With a little contrivance you probably wouldn't even need test of the boolean. – andy mango Dec 15 '18 at 2:18
5

This is completely arbitrary. When you have a grid of n×m items, it is not important if you order these by the first axis or the second axis in the grid. Here, your two axes are:

  1. Whether the condition is true or false.
  2. The different settings affected by the condition.

Since the choice is arbitrary, it might be better to keep related things close together. But whether the settings within a condition or the two values of one setting are more closely related, is primarily your opinion.

Note that if you consider the distinction by the condition more important, you may be able to avoid the condition by using object oriented techniques. E.g. you could use the State Pattern to switch between normal mode and tutorial mode, compare also the “replace conditionals with polymorphism” refactoring technique.

1

You should use the if/else. It only evaluates the bool once, which isn't only a teeny performance win but also makes your code more robust in the face of race conditions.

And despite recent backlashes against imperative code, a basic un-nested if/else is easy to read and easy to maintain and easier to debug, even for beginner programmers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.