Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it a good idea to use a switch with fallthrough to handle default arguments in Javascript?
    primarykey
    data
    text
    <p>I have recently learned that you can use a neat switch statement with fallthrough to set default argument values in Javascript:</p> <pre><code>function myFunc(arg1, arg2, arg3) { //replace unpassed arguments with their defaults: switch (arguments.length) { case 0 : arg1 = "default1"; case 1 : arg2 = "default2"; case 2 : arg3 = "default3"; } } </code></pre> <p>I have grown to like it a lot, since not only is it very short but it also works based on parameters actually passed, without relying on having a special class of values (null, falsy, etc) serve as placeholders as in the more traditional versions:</p> <pre><code>function myFunc(arg1, arg2, arg3){ //replace falsy arguments with their defaults: arg1 = arg1 || "default1"; arg2 = arg2 || "default2"; arg3 = arg3 || "default3"; } </code></pre> <p><strong>My inital though after seeing the version using the switch was that I should consider using it "by default" over the <code>||</code> version.</strong></p> <p>The switch fallthough makes it not much longer and it has the advantage that it is much more "robust" in that it does not care about the types of the parameters. In the general case, it sounds like a good idea to not have to worry about what would happen with all the falsy values ('', 0, null, false ...) whenever I have to make a function with default parameters.</p> <p>I would then reserve the <code>arg = arg || x</code> for the actual cases where I want to check for truthyness instead of repurposing it as the general rule for parameter defaulting.</p> <p>However, I found <a href="http://google.com/codesearch#3E7G2GTfLyM/trunk/WebContent/js/dojotoolkit/dojox/grid/_Scroller.js&amp;q=switch%5C%28%5Cs%2aarguments.length%5Cs%2a%5C%29%20lang%3a%5Ejavascript%24&amp;l=75" rel="nofollow">very few</a> examples of this pattern when I did a <a href="http://google.com/codesearch#search/&amp;q=switch%5C%28%5Cs%2aarguments.length%5Cs%2a%5C%29%20lang%3a%5Ejavascript%24&amp;type=cs" rel="nofollow">code search</a> for it so I had to put on my skeptic hat. <strong>Why didn't I find more examples of this idiom?</strong></p> <ul> <li>Is it just now very well known?</li> <li>Did I not search well enough? Did I get confused by the large number of false positives?</li> <li>Is there something that makes it inferior to the alternatives?</li> </ul> <p>Some reasons that I (and some of the comments) could think of for avoiding <code>switch(arguments.length)</code>:</p> <ul> <li><p>Using named parameters passed via an object literal is very flexible and extensible. Perhaps places where more arguments can be optional are using this instead?</p></li> <li><p>Perhaps most of the time we <em>do</em> want to check for truthyness? Using a category of values as palceholders also allows default parameters to appear in the middle instead of only at the end : <code>myFunc('arg1', null, 'arg3')</code></p></li> <li><p>Perhaps most people just prefer the very short <code>arg = arg || "default"</code> and most of the time we just don't care about falsy values?</p></li> <li><p>Perhaps accessing <code>arguements</code> is evil/unperformant?</p></li> <li><p>Perhaps this kind of switch case fallthrough has a bad part I didn't think about?</p></li> </ul> <p><strong>Are these cons enough to avoid using <code>switch(arguments.length)</code> as a staple default argument pattern or is it a neat trick I should keep and use in my code?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload