Code refactoring in C

Asked

Viewed 148 times

0

I was trying today to create a simple program and C with some colleagues and one of the questions was: No else if it would really be necessary to put the validations precoAtual >= 30 and vendaMedia >= 500...since the if are you checking? Well, looking quickly yes to verify the equality in both conditions and another is that as the ifhas a or (||) so the condition would not be 100% valid or something like ...but we wanted to go deeper and have a justification with more property.. and nothing came out. Would anyone know how to justify it? I can actually refactor the code by removing these validations?

PS.: I did table test but could not complete anything.

  #include <stdio.h>
  #include <stdlib.h>

 int main(){
  float vendaMedia, precoAtual;
  printf("Digite o preco atual:");
  scanf("%f", &precoAtual);
  printf("Digite o valor da venda media:");
  scanf("%f", &vendaMedia);
  if(precoAtual < 30 || vendaMedia < 500){
     precoAtual = precoAtual * 1.10;
     }
  else if((precoAtual >= 30 && precoAtual < 80) || (vendaMedia >= 500 && vendaMedia < 1200)){
     precoAtual = precoAtual * 1.15;
     }
  else{
     precoAtual = precoAtual * 1.20;
     }
  printf("%.2f \n", precoAtual);
  system("pause");
  return 0;
}

2 answers

1


Think like that, if you go into that else if that means the first if failed. This equates to expression precoAtual < 30 || valorMedia < 500 be of value false. When an operation || returns true? When at least one of the operands is true.

If this returned false, the two are false. If the two operands are false you deny them and see what you get. It is easy to see that the denial of both is just precoAtual >= 30 and valorMedia >= 500. These checks are therefore not necessary.

Note that these two expressions appear respectively in

precoAtual >= 30 && precoAtual < 80

valorMedia >= 500 && valorMedia < 1200

Both are true as long as the two operands are true. You already know that the two left operands are true, so what really decides is what’s left on the right.

In general, when you want to know if a check is even needed in a else if you think of this line "if you got there then the previous check failed" and then you see what conditions are required for that check to fail.

0

The first conditions in the else if, so much for the precoAtual how much to vendaMedia, are the complement of conditions in if. Therefore, Voce can take the first check, since if precoAutal it is not < 30 he has to be >= 30 and the same for vendaMedia.

Maybe it gets confused because the conditions in else if has intersection with that of if, but as only one is executed in jail there is no problem.

  #include <stdio.h>
  #include <stdlib.h>

 int main(){
  float vendaMedia, precoAtual;
  printf("Digite o preco atual:");
  scanf("%f", &precoAtual);
  printf("Digite o valor da venda media:");
  scanf("%f", &vendaMedia);
  if(precoAtual < 30 || vendaMedia < 500){
     precoAtual = precoAtual * 1.10;
     }
  else if(precoAtual < 80 || vendaMedia < 1200){
     precoAtual = precoAtual * 1.15;
     }
  else{
     precoAtual = precoAtual * 1.20;
     }
  printf("%.2f \n", precoAtual);
  system("pause");
  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.