mardi 28 septembre 2021

How to simplify if statements

How can I simplify below if statements?, I'm trying to achieve the possibly most efficient code.

// doSomething based on x value and y value   
function doSomething(x int, y int) {

      //x not zero and y not zero
      if x != 0 && y != 0 {
        do a

        //do b if x greater or equal y
        if x >= y {
            do b
            return
        }
        
        //do c if x lower than y
        if x < y && x != 0 && y != 0 {
            do c
            return
        }
        
        //do b if x not zero and y zero
        if x != 0 && y == 0 {
            do b
            return
        }

        //do d if x zero and y not zero
        if x == 0 && y != 0 {
            do d
            return
        }
      }

      //do e if both x and y zero
      if x == 0 && y == 0 {
          do e
          return
      }
    }

What is the most concise and efficient way to simplify?

Aucun commentaire:

Enregistrer un commentaire