Home > Back-end >  Is there a way to simplify this nested if-else statements?
Is there a way to simplify this nested if-else statements?

Time:12-19

I have this pseudocode which all plays different audio files. I read that a nested if has a time complexity of O(m n) which I think is okay-ish, but if there's a way to simplify it or reduce the time complexity, that would be great.

btn_G.onTouchListener(){
  Case Motion.Action_Down:
    if(high_octave){ 

      if(move){ //if the phone is moving
      
        if(sharp){ //if it's a sharp note
        
          if(vibrato){ //if it's vibrato
            Gstream = G.play(loop) //loop a vibrato, sharp, high octave G note

          } else { //if there is no vibrato
            Gstream = G.play(loop) //loop a normal, sharp, high octave G note
          }
          
        } else { //if all are normal notes, no sharps
        
          if(vibrato){ //if there is vibrato
            Gstream = G.play(loop) //loop a vibrato, normal, high octave G note

          } else { //if there is no vibrato
            Gstream = G.play(loop) //loop a normal, high octave G note
          }
          
        }
      } else { //if the phone is not moving
      //if the phone doesn't move, doesn't matter if there is vibrato or not
      
        if(sharp){ //if it's a sharp note
          Gstream = G.play(once) //play a sharp, high octave G note only once

        } else { //if all are normal notes, no sharps
          Gstream = G.play(once) //play a normal, high octave G note only once
        }
        
      }
    } else if(mid_oct){ #middle octave
        //repeat all of that but with middle octave G note

    } else { #low octave
        //repeat all of that but with low octave G note
    }
  Case Motion.Action_Up:
    Gstream = G.stop() // stop the audio
}

That's just for one button. And I have like 8 buttons I need to do this for. I thought of using hash list but to create the list I'll to check conditions like this too so it's gonna be the same, isn't it?

I also found this, but isn't me separating the conditions like that the same as me doing nested if? As in, it would also be really long and repetitive because I have 8 buttons?

Is there a way to shorten it, even with all the conditions?

Sorry, I'm kinda new at this.

CodePudding user response:

You can use bitmasks for conditions, and group them for a switch statement:

int cond1 = 0x1;
int cond2 = 0x2;

switch(cond) {
  case cond1|cond2:
    // both  are true
    break;
  case cond1|0x0:
    // only cond1 true
    break;
  case 0x0|cond2:
    // only cond2 true
    break;
  case 0x0|0x0:
    // both are false
    break;
}

CodePudding user response:

I ended up separating them into different methods like so:

G_btn.onTouch{
  Case Motion Down:
    if(up){
      horizontal_method(G5, G#5, gStream)
    } else if (down) {
      horizontal_method(G4, G#4, gStream)
    }
  Case Motion Up:
    stop_audio_method(gStream)
}

public void horizontal_method(note, sharp, stream){
  if(horizontal){ //if phone is moving
    loop_num = -1 //looping
  } else { //phone not moving
    loop_num = 0 //play once
  }
  rotate_method(note, sharp, stream, loop_num)
}

public void rotate_method(note, sharp, stream, loop_num){
  if(rotate){ //if sharp
    stream = soundpool.play (sharp, loop_num) //can be looping or not, depends on the loop_num
  } else { //not sharp
    stream = soundpool.play (note, loop_num)
  }
}

public void stop_audio_method(stream){
  stream = soundpool.stop() //stops that specific stream
}
  • Related