Multiple if statements

Suppose, for example, that I use a single if statement to account for ten different possible button clicks that send an event value to an event listener:

public boolean onTouch(int v) { //this is my only listener for all ten buttons if(event.getAction() == MotionEvent.ACTION_DOWN){ if(v==button_one){pool.play(bass1, 1f,1f, 1, 0, 1f);} if(v==button_two){pool.play(bass2, 1f,1f, 1, 0, 1f);} if(v==button_three){pool.play(bass3, 1f,1f, 1, 0, 1f);} if(v==button_four){pool.play(snare1, 1f,1f, 1, 0, 1f);} if(v==button_five){pool.play(snare2, 1f,1f, 1, 0, 1f);} if(v==button_six){pool.play(snare3, 1f,1f, 1, 0, 1f);} if(v==button_seven){pool.play(hh1, 1f,1f, 1, 0, 1f);} if(v==button_eight){pool.play(hh2, 1f,1f, 1, 0, 1f);} } return false; } 

Would it be more efficient to classify them? let's say ... one onClick event for traps and one for bass and one for hi hats, so when you click the button, the program should not check every if statement, only the one inside the listener for the fired event?

+4
source share
6 answers

How about something like that?

 HashMap<int,int> soundMap = new HashMap<int,int>(); soundMap.put(button_one, bass1); soundMap.put(button_two, bass2); soundMap.put(button_three, bass3); soundMap.put(button_four, snare1); soundMap.put(button_five, snare2); soundMap.put(button_six, snare3); soundMap.put(button_seven, hh1); soundMap.put(button_eight, hh2); 

Have a HashMap as a class variable and map it to initialization in onCreate or something like that. Then you can just use this for your listener:

 public boolean onTouch(int v) { if(event.getAction() == MotionEvent.ACTION_DOWN) { pool.play(soundMap.get(v), 1f, 1f, 1, 0, 1f); } return false; } 

The advantage of this is that if you need to add more buttons in the future, you only need to change the way the map is initialized with a new sound display; the listener will not require any changes.

+5
source

Just to add to the rich vein of advice and opinions here, and agreeing with everyone before, “about whether this is really a performance issue,” I would go for a structure that makes the code easiest to read and serve you and anyone else. who can support him. When servicing, also consider expanding it. What happens after 3 months when you want to add 5 more sound blocks?

Reducing the minimum number of lines can give you an almost immeasurable increase in performance and save a few bytes in your APK, but in most cases I would trade this for readability.

All that said, I like kcoppocks solution. for me it is short sweet and elegant and how I will do it, but if your level of experience is different and you can’t just look at it and say “yes, I understand” and then save your ifs or better switch.

+1
source

I would not worry about breaking it until you know that this is a problem. The switch will help reduce the repeating if :

 public boolean onTouch(int v) { if(event.getAction() == MotionEvent.ACTION_DOWN) { switch (v) { case button_one: pool.play(); break; case button_two: pool.play(..); break; ... } } return false; } 
0
source

In this case, do not worry about performance; the time spent on the conditional statement will be completely overshadowed by the event handling code around it. The first and third laws of execution are measurement, measurement, measurement, and I am skeptical about the chances of finding a difference.

I cannot help but notice that the only thing that changes is the first argument to pool.play. Is there a connection between bass1, bass2, etc. And the corresponding v?

0
source

I don't think this is necessary, but I would definitely recommend the switch and case. You can order as the most common, least common, but such small changes that will not be noticed. It does not take very long to go through these if statements.

0
source

I don’t know how many times this if works, but to me it seems ineffective. This boolean test is very fast. In any case, if you want to make it as efficient as possible, I see two options:

  • Easy: use else if instead of if .
  • Complex: use an array of ActionIf objects to do what you want:

 public interface ActionIf { public void go(); } public class ActionBass1 implements ActionIf { @Override public void go() { pool.play(bass1, 1f,1f, 1, 0, 1f); } } public class ActionBass2 implements ActionIf { @Override public void go() { pool.play(bass2, 1f,1f, 1, 0, 1f); } } ... public ActionIf[] actions = {new ActionBass1(), new Action Bass2(), ...); public boolean onTouch(int v) { //this is my only listener for all ten buttons if(event.getAction() == MotionEvent.ACTION_DOWN && v >= 0 && v <= (button_eight-button_one)){ actions[button_one+v].go(); } return false; } 

code>

0
source

Source: https://habr.com/ru/post/1414382/


All Articles