Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

My goal is to add some functionality to the bxslider script - in this case, I want to be able to enable/disable a keyboard event handler AFTER a slider is instantiated.

I guess this code is self explanatory, but it's ugly, it doesn't seem to be a good practice, and I have no idea of how to properly implement it. As you can see I'm very uncomfortable with that boolean argument which I don't know how to name it.

    el.toggleKeyboard = function(a_boolean_variable){
        if(!slider.initialized) return;
        slider.settings.keyboardEnabled = a_boolean_variable;
    }

I should call it slider.toggleKeyboard(true) to enable it and slider.toggleKeyboard(false) to disable it.

The private method related to it, which is called when the slider is instantiated:

  var setupKeyboard = function(){
         $(document).keydown(function(e){
             if (e.keyCode == 39) {
                 if (slider.settings.keyboardEnabled)
                    clickNextBind(e);
                 return false;
             }
             else if (e.keyCode == 37) {
                 if (slider.settings.keyboardEnabled)
                    clickPrevBind(e);
                 return false;
             }
         });
    }

Could anyone be so kind and shed a light on this for me? What design pattern should I use?

Thanks a lot!

EDIT

I came up with the following solution:

el.toggleKeyboard = function(shouldEnable){
    if(!slider.initialized) return;
    slider.settings.keyboardEnabled = (shouldEnable == undefined) ? !slider.settings.keyboardEnabled : shouldEnable;
}

It toggles keyboard navigation on/off and you can force by sending a boolean parameter (true to enable, false to disable).

share|improve this question

marked as duplicate by gnat, MichaelT, Dynamic, World Engineer Apr 8 at 0:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
2  
oh yes, I just thought about that: something like a 'set' method. thanks –  zok Apr 4 at 13:42

1 Answer 1

up vote 0 down vote accepted

Martin Fowler has an article on using toggles that may be relevant to what you're working on.

He starts with:

I've seen a lot of names for this concept tossed around: feature bits, flags, flippers, switches and the like. There seems no generally accepted name yet.

So the pattern you're looking for appears to be common but not with an agreed upon name. He calls it a FeatureBranch but also uses other names depending upon what is being toggled.

While his article focuses on hiding features to support continuous integration, the principles he discusses will be relevant to what you're working on.

Regarding naming the function, consider different verbs that more accurately describe the action taking place. Using a different verb can make it more obvious what the action is or isn't depending upon the boolean. I'd suggest that you're not "toggling" the keyboard but are "displaying" the keyboard.

slider.displayKeyboard( boolean shouldDisplay )

Makes it very clear what will result and how the parameter should affect things.

share|improve this answer
    
Thanks for answering! But why "displayKeyboard" ? This method is intended to toggle the ability to use keyboard shortcuts to navigate. –  zok Apr 9 at 17:40
    
@zok - just an example to illustrate the renaming. –  GlenH7 Apr 9 at 17:45
    
Ah, sorry then. I came up with a solution based on your answer (I edited my post). Thanks! –  zok Apr 9 at 17:48
    
ps: can't vote cause I got no reputation :) –  zok Apr 9 at 17:49
    
@zok - With a few more contributions you'll be well on your way to picking up privileges and then being able to more actively participate in the community. Glad to hear my answer helped you resolve your problem. –  GlenH7 Apr 9 at 17:56

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