Results 1 to 2 of 2

Thread: Striving for Elegance

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Question Striving for Elegance

    Elegance, I suppose, can be subjective. I view

    PHP Code:

    <?php

    if     ( $here === 'arson' )
        echo 
    '<body class = "arson">';

    elseif ( 
    $here === 'badcheck' )
        echo 
    '<body class = "badcheck">';

    elseif ( 
    $here === 'lewdness' )
        echo 
    '<body class = "lewdness">;

    elseif ( $here === '
    posswpn' )
        echo '
    <body class = "posswpn">;

    else echo 
    '<body>';

    ?>

    as less elegant than...

    PHP Code:

    <?php

    switch ($here) {

        case 
    'arson':
            echo 
    '<body class = "arson">';
            break;

        case 
    'badcheck':
            echo 
    '<body class = "badcheck">';
            breaK;

        case 
    'lewdness':
            echo 
    '<body class = "lewdness">';
            break;

        case 
    'posswpn';
            echo 
    '<body class = "posswpn">';
            break;

        default:
            echo 
    '<body>';
    }

    ?>
    ...even though I'm not sure I can verbalize why. Whether you agree or not, I suspect there are coding methods that improve on both of these snippets, with regard to elegance. I would welcome thoughts and suggestions.

    A.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    What are your goals, and what are the requirements?

    Personally I find switch statements to be somewhat annoying because they have odd syntax and take up a lot of space. However, you're right that they're a little more elegant than a long string of ifs. But ifs are more versatile than just checking the values on one variable, so I much more often find myself ignoring the option of switch statements.

    I think the reason you find it more "elegant" is because it's one statement rather than a but of unrelated ifs. Of course your use of elseif actually does make it tied together in a way too, so that's not too bad. (Technically all you need there are "if" statements because the variable will only ever have one value, so no more than one can be true. But doesn't quite work out with the else, though, so what you have is fine and perhaps slightly more efficient rather than comparing all of the values each time.)

    As a general rule, writing clean/clear code is important, even if just to stay organized.


    Now... on to the details here. You're mixing logic and content. You should run your PHP decisions at the top of the script then use templating techniques to output all of that later; in other words, you should avoid mixing HTML in the middle of your PHP stuff like that.

    Or, if you need to do it, try to limit the PHP.

    Here's a simpler way to rewrite your code. I don't know if your situation will allow for this to work, but if it does this is the best way probably:
    PHP Code:
    <?php
    if (in_array($here,array('arson','badcheck','lewdness','posswpn'))) {
        echo 
    '<body class = "'.$here.'">';
    }
    else {
       echo 
    '<body>';
    }
    ?>
    By the way, I always use {} with if. Even if it's not needed (if there's just one line) I strongly prefer it for clarity and to visually offset the chunks.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    marain (10-02-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •