There is a countdown timer, which after a time interval of 6, 7, 8 seconds from the beginning of the countdown changes the color of the TextViewer , this is implemented by checking using if , the number of checks depends on the size of the inTimeVydox field, the value of which is divided by seven and we get the number of sevens inKolichestoSemerok .

When inKolichestoSemerok=1 , three checks are performed using if ; if inKolichestoSemerok=2 , then six checks are performed. In total, the code already has nine audit records with if ! With inKolichestoSemerok=3 9 values ​​need to be checked. In the code there will be 3+6+9=18 records, and so on. Nothing, but the value of inTimeVydox can be from 1 to 120.

With an increase in the inTimeVydox and inKolichestoSemerok , the amount of code increases.

Question: How can I reduce the number of used if statements without harming the performance of the code?

  myCountDownTimerVydox = new CountDownTimer(inTimevydox * 1000, MILLIS_PER_SECOND) { @Override public void onTick(long millisUntilFinishedVydox) { long sekUntilFinishedVydox = millisUntilFinishedVydox / 1000; if (inKolichestoSemerok == 1) { if (sekUntilFinishedVydox == inTimevydox - 6) { textview_vremyVydox.setBackgroundResource(R.color.redColor); } if (sekUntilFinishedVydox == inTimevydox - 7) { textview_vremyVydox.setBackgroundResource(R.color.colorblue); } if (sekUntilFinishedVydox == inTimevydox - 8) { textview_vremyVydox.setBackgroundResource(R.color.Teal); } } if (inKolichestoSemerok == 2) { if (sekUntilFinishedVydox == inTimevydox - 6) { textview_vremyVydox.setBackgroundResource(R.color.redColor); } if (sekUntilFinishedVydox == inTimevydox - 7) { textview_vremyVydox.setBackgroundResource(R.color.colorblue); } if (sekUntilFinishedVydox == inTimevydox - 8) { textview_vremyVydox.setBackgroundResource(R.color.Teal); } if (sekUntilFinishedVydox == inTimevydox - 13) { textview_vremyVydox.setBackgroundResource(R.color.redColor); } if (sekUntilFinishedVydox == inTimevydox - 14) { textview_vremyVydox.setBackgroundResource(R.color.colorblue); } if (sekUntilFinishedVydox == inTimevydox - 15) { textview_vremyVydox.setBackgroundResource(R.color.Teal); } } } } 
  • Indians are quietly jealous of your code. - Denis

2 answers 2

How can I reduce the number of used if statements without harming the performance of the code?

  1. Optimize the code, bringing the logic, so to speak, to a common denominator.
  2. Generally get rid of if by replacing with switch under a variety of conditions.

    Try this

     @Override public void onTick(long millisUntilFinishedVydox) { long sekUntilFinishedVydox = millisUntilFinishedVydox / 1000; int inKolichestoSemerok = inTimevydox/7; if (sekUntilFinishedVydox == inTimevydox - (inKolichestoSemerok*7 - 1)) { textview_vremyVydox.setBackgroundResource(R.color.redColor); } if (sekUntilFinishedVydox == inTimevydox - (inKolichestoSemerok*7)) { textview_vremyVydox.setBackgroundResource(R.color.colorblue); } if (sekUntilFinishedVydox == inTimevydox - (inKolichestoSemerok*7 + 1)) { textview_vremyVydox.setBackgroundResource(R.color.Teal); } }