Is it possible to improve that part of the code?

Asked

Viewed 94 times

3

I need to improve a part of the code.

My problem is it’s too repetitive if and else, the bad news is that I will have more repetitive things on account that are different games.

I can improve this part of the code by something more readable or even smaller?

public Double valuesGames(String game){
    if (game.equals(constantes.getGameDiadeSorte())){
        //Valores referente a Dia de Sorte
        if (getNumbersBalls().size() <= 7){
            value = constantes.getAmountDiaDeSorte();
        }else if (getNumbersBalls().size() == 8){
            value = constantes.getAmountDiaDeSorte() + 3;
        }else if (getNumbersBalls().size() == 9){
            value = constantes.getAmountDiaDeSorte() + 4;
        }else if (getNumbersBalls().size() == 10){
            value = constantes.getAmountDiaDeSorte() + 5;
        }else if (getNumbersBalls().size() == 11){
            value = constantes.getAmountDiaDeSorte() + 6;
        }else if (getNumbersBalls().size() == 12){
            value = constantes.getAmountDiaDeSorte() + 7;
        }else if (getNumbersBalls().size() == 13){
            value = constantes.getAmountDiaDeSorte() + 10;
        }else if (getNumbersBalls().size() == 14){
            value = constantes.getAmountDiaDeSorte() + 11;
        }else if (getNumbersBalls().size() == 15){
            value = constantes.getAmountDiaDeSorte() + 15;
        }
    } else if (game.equals(constantes.getGameQuina())){
        //Valores referente a QUINA
        if (getNumbersBalls().size() <= 5){
            value = constantes.getAmountQuina();
        }else if (getNumbersBalls().size() == 6){
            value = constantes.getAmountQuina() * 3;
        }else if (getNumbersBalls().size() == 7){
            value = constantes.getAmountQuina() * 4;
        }else if (getNumbersBalls().size() == 8){
            value = constantes.getAmountQuina() * 5;
        }else if (getNumbersBalls().size() == 9){
            value = constantes.getAmountQuina() * 6;
        }else if (getNumbersBalls().size() == 10){
            value = constantes.getAmountQuina() * 7;
        }else if (getNumbersBalls().size() == 11){
            value = constantes.getAmountQuina() * 8;
        }else if (getNumbersBalls().size() == 12){
            value = constantes.getAmountQuina() * 9;
        }else if (getNumbersBalls().size() == 13){
            value = constantes.getAmountQuina() * 10;
        }else if (getNumbersBalls().size() == 14){
            value = constantes.getAmountQuina() * 11;
        }else if (getNumbersBalls().size() == 15){
            value = constantes.getAmountQuina() * 12;
        }
    }else if (game.equals(constantes.getGameMega())){
        //Valores referente a MEGA-SENA
        if (getNumbersBalls().size() <= 6){
            value = constantes.getAmountMega();
        }else if (getNumbersBalls().size() == 7){
            value = constantes.getAmountMega() * 3;
        }else if (getNumbersBalls().size() == 8){
            value = constantes.getAmountMega() * 4;
        }else if (getNumbersBalls().size() == 9){
            value = constantes.getAmountMega() * 5;
        }else if (getNumbersBalls().size() == 10){
            value = constantes.getAmountMega() * 6;
        }else if (getNumbersBalls().size() == 11){
            value = constantes.getAmountMega() * 7;
        }else if (getNumbersBalls().size() == 12){
            value = constantes.getAmountMega() * 8;
        }else if (getNumbersBalls().size() == 13){
            value = constantes.getAmountMega() * 9;
        }else if (getNumbersBalls().size() == 14){
            value = constantes.getAmountMega() * 10;
        }else if (getNumbersBalls().size() == 15){
            value = constantes.getAmountMega() * 11;
        }
    }
    return value;
}
  • Yes and much, both in structure and logic.

  • you can give me a light on how to make this change ?

  • One of the things you can do is call these getAmount functions only once and then treat the result of it. Same thing p get number Balls function

  • @Edi you can also replace the else ifs by a switch case.

  • @William this would not lessen the repetition.

  • A game could not be a base class where each type of game implements it and has its own 'valuesGames()'? This would decrease the code within a single function. There would be 3 classes: one for Lucky Day, another Quina and another Mega sena

Show 1 more comment

1 answer

4


You can swap performance for thinner code. There is a pattern there and you can generalize everything that is in this pattern. If it were a fully regular pattern it could solve with simple mathematics, but there is an exception that is the first check and the values that are used to sum or multiply need to be in a table. Check if I did not eat ball in something or if the original code has no problems, for example what should do if the number is higher than these checked. Reinforcement that this will be slower, not too much, but will be:

int[] somadoresDiaDeSorte = new int[] { 3, 4, 5, 6, 7, 10, 11, 15 };
int[] multiplicadoresQuina = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int[] multiplicadoresMega = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11 };
if (game.equals(constantes.getGameDiadeSorte())) {
    if (getNumbersBalls().size() <= 7) value = constantes.getAmountDiaDeSorte();
    else {
        for (int i = 0; i < i < multiplicadoresDiaDeSorte.length; i++) {
            if (getNumbersBalls().size() == i + 8) {
                value = constantes.getAmountDiaDeSorte() + somadoresDiaDeSorte[i];
                break;
        }
    }
}
if (game.equals(constantes.getGameQuina())) {
    if (getNumbersBalls().size() <= 5) value = constantes.getAmountQuina();
    else {
        for (int i = 0; i < i < multiplicadoresQuina.length; i++) {
            if (getNumbersBalls().size() == i + 6) {
                value = constantes.getAmountQuina() + multiplicadoresQuina[i];
                break;
            }
        }
    }
}
if (game.equals(constantes.getGameMega())) {
    if (getNumbersBalls().size() <= 6) value = constantes.getAmountMega();
    else {
        for (int i = 0; i < i < multiplicadoresMega.length; i++) {
            if (getNumbersBalls().size() == i + 7) {
                value = constantes.getAmountMega() + multiplicadoresMega[i];
                break;
            }
        }
    }
}

I put in the Github for future reference.

You can generalize a little bit more and make the three of them see one, but I think it starts to parameterize too much for normal codes. It would take a very strong reason to do this, it would involve lambda (to establish which methods should be used to take the data to be compared and the amount of the elements of each operation) and just to write less code I don’t think it looks cool, and there’s a number that would need to be parameterized as well, but I gave the tips if you want to do.

  • Thank you very much, with that I got a 50% code reduction.

Browser other questions tagged

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