Skip to content

Instantly share code, notes, and snippets.

@syranez
Created May 9, 2012 19:29
Show Gist options
  • Save syranez/2648211 to your computer and use it in GitHub Desktop.
Save syranez/2648211 to your computer and use it in GitHub Desktop.
avoid boolean parameters.

You have:

function foo () { /* do something */ }

and then you find that foo should do something else when condition baz is true. So you do some bullshit like:

function foo (baz) { if (baz) { /* do something else */ } else { /* do something */ } }

And later all I see is

foo(true) /* some code later */ foo()

So, if I see that the first one (and second and third and fourth, cos I do not remember and others do not too), I do not know what foo does. I do not know what this shit of code does nor why it is. So I have to look at the definition of foo to check out. Bad. Are you kiddin' Me?

Y U DO NOT LIKE ME?

So you ask: How to make it better? So:

var doSomethingElse = true; foo(doSomethingElse); /* some code later */ foo()

YEAH! That is better! I now see the effect of the boolean parameter: does something else. But... see, this is the best (classy) solution:

var klass = ... klass.enableDoSomethingElse(); klass.foo(); /* some code later */ var klass = ... klass.foo()

And... of course, Y U JUST NOT WRITE ANOTHER FUNCTION?

function foo () { /* do something */}; function fooDoSomethingElse () { /* do something else */ };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment