I am developing a large scale (at least for me) gui with a lot of components. There is some quite complex mechanics behind the enabling rules (meaning what component has to be enabled/disabled depending on the status of other components). Every time I do an update of the whole user interface (which is not that often but sometimes neccessary, for instance after loading up a settings file), I have to go through the rule sets to set every component. What I basically do is the following:
meaningfulName1.setEnabled(conditionForMeaningfulName1());
meaningfulName2.setEnabled(conditionForMeaningfulName2());
meaningfulName3.setEnabled(conditionForMeaningfulName3());
// etc
I ask myself if it makes sense to instead do the following.
boolean temp = conditionForMeaningfulName1();
if (meaningfulName1.isEnabled != temp) meaningfulName1.setEnabled(temp);
temp = conditionForMeaningfulName2();
if (meaningfulName2.isEnabled != temp) meaningfulName2.setEnabled(temp);
temp = conditionForMeaningfulName3();
if (meaningfulName3.isEnabled != temp) meaningfulName3.setEnabled(temp);
// etc
The idea behind that would be that it saves some performance to not set the flag if the state is already the desired one and therefore save some graphical updating (as well as a function call).
Do you think this makes sense on a large scale or doesn’t it save any time at all and just makes the code less readable?
Following the comment of Andrew Thompson I created a small test. I did not use profiling or so, just very simple
System.currentTimeMillis(). Obviously the first few runs have to be neglected, but after that it seems like there is no difference to the two methods.