Code Refactoring in C

0

I've been trying to create a simple program and C with some colleagues and one of the questions was: In else if would it really be necessary to put validations precoAtual >= 30 and vendaMedia >= 500 ... since if is checking? Well, quickly looking at yes to check equality on both conditions and another is that since if has one or ( || ) so the condition would not be 100% valid or something ... but we wanted to go deeper and have a better justification ... and nothing came out. Could anyone justify it? Can I actually refactor the code by removing these validations?

PS: I did test the table but could not finish 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;
}
    
asked by anonymous 31.07.2014 / 01:35

2 answers

1

Think like this, if you enter that else if it means that the first if failed. This is equivalent to the expression precoAtual < 30 || valorMedia < 500 have value false . When does a || operation return true? When at least one of the operands is true.

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

Notice 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 operands on the left are true, so what you really decide is what's left over.

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

31.07.2014 / 01:50
0

The first conditions in else if , both for precoAtual and vendaMedia , are the complement of conditions in if . So, you can take the first check, since% of% is not% with% it must be precoAutal and < 30 .

It may be confusing because the conditions in >= 30 intersect with vendaMedia , but since only one is executed in the string 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;
}
    
31.07.2014 / 01:44