0

I got probably "micro optimization" problem.

I got "History number", "Next Number", "Reset" buttons, as well "label" for text.

Every time I click At "Next number" button I would like to show random number in the label, and add this number to history array where I keep track of the past number.

Every time I click at "History number" it would randomly show one of the random numbers from history array.

Every time I click at "Reset" button, it would just clear history array.

And you can see that, that clicking at the "History number makes only sense when the history array is NOT empty.

And I wonder how to do this, without redundant "if" statement at the "history number" callback.

Or

Deactivating "history number" button at the start of the program. Then activating it EVERY time the "next number" is clicked (which is uuugly). And deactivating this button again on "Reset" button.

This scheme on whiteboard demonstrate what I mean :

Scheme that demonstrate buttons label and concept

And I have JavaScript code where I implemented one of ugly solution (latter one, with deactivating and activating on appropriate callbacks).

http://jsbin.com/aJESAYu/17/edit

PS: I stumbled upon this problem writing in Objective-C, but I though, that it would be stupid to publish repository with iOS Objective-C code, while not everybody have to have Xcode. So I done Javascript Example. So don't restrict your answers to only on language.

PS2: I though about having two "next number" functions.

1) One that would have the line of code that enable "history number" button and

2) second without.

At the start the 1) function would be handler. And at the end of that 1) function, the handler would replace itself to the 2) function.

Also the "reset" handler would put again handler to the 1) function.

PS3: I know, this could be overkill. But I just several times struggled with this kind of situation. And I wonder if there are better solution.

Thank you very very much in advance.

  • Maybe, just maybe, this is more related to ux.stackexchange.com? – Pierre Arlaud Jan 23 '14 at 16:14
  • @ArlaudPierre No. It isn't. Have you read whole question ? – Paul Brewczynski Jan 23 '14 at 16:21
  • 1
    @bluesm, please don't scatter formatting throughout your post. It's significantly difficult to determine what you're actually asking from your question, between every quotation being bolded and your three post-scripts. – DougM Jan 23 '14 at 19:06
  • I would love to write a nice little state machine here and show you how to do some neat monadic control flow structures which would be great, but I'm wrestling myself away from writing an answer because this is the absolute definition of micro-optimization, wayyyy more optimization than could possibly be necessary. Check yourself before you wreck yourself. – Jimmy Hoffa Jan 23 '14 at 19:54
  • @JimmyHoffa Maybe just for the sake of exercise ? – Paul Brewczynski Jan 28 '14 at 14:24
1

Setting a value to true every time you press "Next number" is not really expensive. Definitely not expensive enough to warrant even more code. Even though your event handler switching would work it would make the code unnecessarily complex.

It's really matter of performance versus maintainability, two quality aspects that often conflict.

You could wrap your history array in an object and have it raise an event like "change". To decouple the button activation from the 'Next number' handler.

So, anyways, what happens to the label when you click 'Reset' ? :-)

  • When you click reset, you empty the Array, and deactivate "History Number" button. – Paul Brewczynski Jan 23 '14 at 16:18
  • @bluesm the label keeps displaying whatever it was showing eventhough the array is now empty? – Joppe Jan 23 '14 at 16:45
  • Is should be cleared. But I think this is not important in this question ;) – Paul Brewczynski Jan 24 '14 at 11:29
0

From the point of view of performance, branching via an if statement is not much different from branching via a mutable event handler so I don't see the difference.

However, I would prefer the explicit if statement most of the time. Event handler are hidden, implicit state and I think its easier to read the code if I set them at the start and then don't change them anymore. (I would rather have the event handler dispatch to a mutable variable holding callback than mutating the handler itself)

// I enable this button again, EVERY TIME - it is ugly and inefficient

I think its exactly the opposite. Its the cleanest way to say that you want the button to be enabled, irrespective of whether it used to be enabled or not. And its most likely not inefficient, since you aren't doing it inside a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.