Smarter Switch Statements

  • strict warning: Non-static method view::load() should not be called statically in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/views/views.module on line 842.
  • strict warning: Declaration of content_handler_field::options() should be compatible with views_object::options() in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/cck/includes/views/handlers/content_handler_field.inc on line 208.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/views/handlers/views_handler_filter.inc on line 589.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/views/handlers/views_handler_filter.inc on line 589.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/views/plugins/views_plugin_style_default.inc on line 25.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /nfs/c04/h03/mnt/64527/domains/jacksonkr.com/drupal-6.15/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.

Recently, a coworker showed me a development script from Apple and we both took an interest in how the web developers used switch statements for the script he had found.

Traditionally, switches have been used in place of simple (yet lengthy) if/else blocks, because a switch will lay your logic steps in a way that is easier to use than a lengthy if/else block. If you're unfamiliar with switch blocks then take a look at the W3C JavaScript switch statement page and a switching example I put together with JSfiddle.

Now for an example of what I'm referring to as "Smart switching". I call this smart switching because it accomplishes everything an if/else does and also allows the pairing of cases by allowing you to choose where you want to insert case breaks.

Basic example of "Smart switching" :

var num = 7;

/**
 * you always use "switch(true)" -- "Smart swithing" you do your logic 
 * in the case instead.
 */
switch(true) {
    case num % 2 == 0:
        document.write(num + " is an even number.<br />");
        break;
    case num % 2 == 1:
        document.write(num + " is an odd number.<br />");
        break;
}

switch(true) {
    case num > 10:
        document.write(num + " is > 10.<br />");
        break;
    case num > 5:
        document.write(num + " is > 5.<br />");
        break;
    case num > 0:
        document.write(num + " is > 0.<br />");
        break;
}

 

 

If you're anything like me, you have already planned to replace your complex if/else statements with this "smart switches".

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <pre>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.
  • E-Mail addresses are hidden with reCAPTCHA Mailhide.

More information about formatting options

CAPTCHA
Complete this form and then pat yourself on the back.