How to improve my code (many if's and else's)

3

I have a function to fetch a json locally and change the data of some fields. but I have to do the test if the field is with the status code and depending on I change with the correct value for visualization in the frontend.

follow example:

  $http.get("http://localteste:18888/tess").success(function (dados) {


         local_locations = $.map(dados, function (dev) {
            //Verifica Status da Bateria

            if (dev.status) {
                if (dev.status <= 10)
                    dev.status= "Muito Baixo";
               else if (dev.status<= 25)
                    dev.status = "Baixo";;
               else if ((dev.status== 50) || (dev.status>= 25))
                    dev.status= "Medio";;
              else  if ((dev.status== 75) || (dev.status>= 50))
                    dev.status= "Alto";;
               else if ((dev.status== 100) || (dev.status> 75))
                    dev.batUseBattery = "Muito alto";
       }
 });

It would be more or less that, but there are many tests like this. How could I improve?

    
asked by anonymous 23.09.2014 / 17:00

4 answers

10

One of the things I did not understand in your code is why you are using two comparisons for if . Suppose a test in each is enough, because if any of the comparisons are true, you will not arrive in the following else if (as already said in @Wakim's answer):

// O if externo pode ser removido se você tem certeza de ser numerico
   if (dev.status <= 10) {
      dev.batUseBattery = "Muito Baixo";
   } else if (dev.status <= 25) {
      dev.batUseBattery = "Baixo";
   } else if (dev.status <= 50) {
      dev.batUseBattery = "Medio";
   } else if (dev.status <= 75) {
      dev.batUseBattery = "Alto";
   } else { // não precisa comparar nada aqui, afinal ja é maior que 75
      dev.batUseBattery = "Muito alto";
   } 

Another alternative, if it has many values, is to use a loop and array to test the tracks (but in the amount of options in your example, I would with if...else same).

dev.batUseBattery = "Muito baixo";
var niveis = [
    [10,'Baixo'], // Maior que 10 é "Baixo"
    [25,'Medio'], // Maior que 25 é "Medio" etc...
    [50,'Alto'],
    [75,'Muito alto'],
];
for (var i = 0; i < niveis.length; i++) {
   if( dev.Status > niveis[i][0] ) {
       dev.batUseBattery = niveis[i][1];
   }
}

Test your JS Fiddle .

    
23.09.2014 / 17:54
4

You can reduce these% with% by checking only the upper limit of the range, and taking advantage of the order in which conditions are evaluated to fit the value into the correct range.

Be careful because if if's is dev.status , it will not sort correctly.

if ((typeof dev.status) !== 'undefined') {
    if (dev.status <= 10)
        dev.batUseBattery= "Muito Baixo";
    else if (dev.status <= 25)
        dev.batUseBattery = "Baixo";
    else if (dev.status <= 50)
        dev.batUseBattery= "Medio";
    else  if (dev.status <= 75)
        dev.batUseBattery= "Alto";
    else if (dev.status <= 100)
        dev.batUseBattery = "Muito alto";
}

I did a JSFiddle for you to see the result.

    
23.09.2014 / 17:13
2

I do not think you have much to do. You can remove the ELSEs and make breaks in the IFs but I honestly do not see any gain in organization or optimization.

if ((typeof dev.status) !== 'undefined') {
  if (dev.status <= 10)
    dev.batUseBattery= "Muito Baixo";
  if (dev.status > 10 && dev.status <= 25)
    dev.batUseBattery = "Baixo";
  if (dev.status > 25 && dev.status <= 50)
    dev.batUseBattery= "Medio";
  if (dev.status > 50 && dev.status <= 75)
    dev.batUseBattery= "Alto";
  if (dev.status > 75)
    dev.batUseBattery = "Muito alto";
}

With switch does not give because they are ranges and not exact values.

    
23.09.2014 / 17:45
-2

Sometimes I put in variables to be able to view and if you need to change text or values you change in the string. And together letting the IF equal the Wakim spoke saves code.

muitoBaixo = ['Muito Baixo',10]
baixo = ['Baixo',25]
....

if (dev.status) {
    if (dev.status <= muitoBaixo[1])
        dev.status= muitoBaixo[0]
    else if (dev.status<= baixo[1])
        dev.status= baixo[0]
        ......
}
    
23.09.2014 / 17:28