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