Below I posted two pieces of the code. The first one is checking for defines, the second - doesn't. The second one is more nicer, because if we don't check for undef values, we can delegate function calls to each other without having intermediate variables. If I start checking for defines, my code becomes full of if and unless sentences.

The code is creating a menu object and setting the active menu for it, which it has to display say in a different color. Of course, the checking for the active menu could be done inside the Menu object, but in my design it's outside. So, my code has to check for the undef value from the CGI param() function and from my get_active_menu_id() functions as well, because they don't always return a defined value.

I lose the flexibility of perl and I worry pretty much about it. What should you do in such a situation?

The checks for defined version:

my $main_menu = new Menu("main"); my $menu_item = param('menu_item'); if(defined($menu_item)) { my $active_menu_id = get_active_menu($menu_item); $main_menu->active_menu_id($active_menu_id) unless !$active_menu_id; } $main_menu->output;

No checks for defined version:

my $main_menu = new Menu("main"); $main_menu->active_menu_id(get_active_menu(param('menu_item'))); $main_menu->output;

So what do you think, is it worth checking for defines on such a code or not?


In reply to Re: The -w switch on a web application by Heidegger
in thread The -w switch on a web application by Heidegger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.