So many times I have come across the question of How do I implement this conditional setting?. I know this might sound really confusing, so let me explain what I mean using code.
Essentially, I want to know which of the following would allow for the fastest execution time and least memory consuming, and why this is the case (hopefully with using references to the stack and/or registers - and possibly the components of the computer such as the time it takes for the ALU to process the comparison every time, verses the time it takes for the CU to store the variable every time).
// Variables won't be like this all the time...
boolean toggles = false;
boolean pressed = true;
void isPressed() {
boolean ret = pressed;
// Case one - test for toggling
if(!toggles) pressed = false;
// Case two - test for toggling and if it's true
if(!toggles && pressed) pressed = false;
return ret;
}
Some background on the situation: I'm designing how key combinations could potentially be implemented into a game but I want to make the Combination
class as generic as I can rather than having two separate classes such as CombinationToggle
and CombinationAlwaysOff
, however, I - and probably many others - have come across this sort of situation.
I'm trying to determine that if this specific combination is toggled, and if it's not, turn the pressed
boolean off. But I'm unsure as to whether I should include a test to determine if it needs to be turned off.