Repetition of if/Else how to reduce and improve code?

Asked

Viewed 1,899 times

11

I’m starting my programming and a question has arisen during my conditions. I do a check of how many times the result was true, and how many times it was false. Problem is you’re not giving me the real result.

The only way I’ve found to work is this:

if (Lstr_Resultado1 == "Verdadeiro")
{
    Lint_TotalVerdadeiro++;
}
else
{
    Lint_TotalFalso++;
}
if (Lstr_Resultado2 == "Verdadeiro")
{
    Lint_TotalVerdadeiro++;
}
else
{
    Lint_TotalFalso++;
}
if (Lstr_Resultado3 == "Verdadeiro")
{
    Lint_TotalVerdadeiro++;
}
else
{
    Lint_TotalFalso++;
}

I believe there’s a more performative way to do that. I know I’m starting and the application is simple but I try to reduce the use of machine processing. What would be the best way to resolve that amount of if / Else?

  • 1

    You cannot change your question. If you have a new question, ask a new question. Probably your code might be a lot better than this, but in your original question you didn’t have any of this, people can’t have to keep adapting the answers every time you change the question.

  • Lack of context and responses creating structures is nothing performatic. But as it lacks context, maybe this can be.

  • @Maniero Certo. was a new doubt that has already been solved.

3 answers

17


You can add your results into a lista type of your result (string, int, etc...), then make a foreach to go through your list by checking whether the field is true or not:

var resultados = new List<String>();

resultados.Add(Lstr_Resultado1);

foreach (var resultado in resultados)
{
  if(resultado == "Verdadeiro")
    Lint_TotalVerdadeiro++;
  else
    Lint_TotalFalso++;
}
  • 2

    I suggest the same thing, but I should turn this Lstr_resultado1...2...3 into a single array and then do a go to increment the true/false total, unless I always have only three lstr_results

  • 1

    I was able to add results.Add(Lstr_resultado2) results.Add(Lstr_resultado3) and tidy up the foreach parameters (var result in results), I don’t know why it made a difference but it was error before

  • Only one question, in your test you validate three "fields"/variables. Always will be these three or has can be "N"?

  • @Pedroduca that’s right, I accidentally switched (I already corrected), it is pq you basically declare a variable resultado and uses the in to browse the list of resultados.

  • @Leandroamarilha It will always be these three

  • @Pedroduca, then I don’t see the need to create another variable (results) and make a loop for what you want. I would continue with Ifs, simple, clear and objective.

Show 1 more comment

10

If you have to keep the variables Lstr_Resultado_N can add them to a list and use the method Count to count the number of occurrences according to a condition specific.

Take an example:

public class Program {
    public static void Main() {                     
        string 
            resultado1 = "Verdadeiro", 
            resultado2 = "Verdadeiro", 
            resultado3 = "Falso", 
            resultado4 = "Verdadeiro";

        var valores = new List<string> {
                resultado1,
                resultado2,
                resultado3,
                resultado4,
        };
        var qtdVerdadeiro = valores.Count(x => x.Equals("Verdadeiro"));
        var qtdFalsos = valores.Count(x => !x.Equals("Verdadeiro"));

        WriteLine($"Qtd verdadeiro: {qtdVerdadeiro}");
        WriteLine($"Qtd falso: {qtdFalsos}");
    }
}

Exit:

True Qty: 3
Fake Qty: 1

See working on .NET Fiddle.

  • I used your example but returned me 3 values and nothing else. Perhaps I replied that I would keep the variables in the sense that I have only 3 variables. I will put the code in full so that it becomes clearer the code.

  • I go around 100 files in this method.

6

if ((Lstr_Resultado1 == "Verdadeiro") || (Lstr_Resultado2 == "Verdadeiro") || (Lstr_Resultado3 == "Verdadeiro")){
   Lint_TotalVerdadeiro++;
}
else{
   Lint_TotalFalso++;
}
  • I tried to do it this way earlier, but it turns out that when the value of Lstr_resultado1 = true and Lstr_resultado2 = true it will consider the first, nullifying if the two are true try only 1 result. I needed in the case of Lint_totaltrue to be equal to 2.

  • that’s right young.

  • Maybe I haven’t been clear enough in my doubt. Come on, I have situations where I can get 3x the result as true. Result 1 = true, Results2= true and Results3= true. Doing so I will have only 1 true in total, being that I have 3 true.

  • I had voted in response, but then I thought the same thing. The point is that you have to test each of the variables, which are different, so there’s no way out of your solution. One question, does the value assigned to the variables have to be string? If it is bool would look more "elegant".

  • @Leandroamarilha is why I fill a spreadsheet in Excel with variables, so I left as string.

  • 1

    Understood. Albertt has clarified his doubt, so

  • 1

    @Pedroduca I get it! So that’s right, I see no other solution.

Show 2 more comments

Browser other questions tagged

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