Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Which is better software design

by hippo (Bishop)
on Mar 27, 2015 at 10:37 UTC ( [id://1121490]=note: print w/replies, xml ) Need Help??


in reply to Which is better software design

Personally I would try not do this:

search_by_tags(split ',', $cgi->param('tags'));

because it does not take account of the possibility that there is no param "tags" and then you would be calling split on an undefined value. This isn't really bad in and of itself but it encourages the use of this pattern for other, potentially more dangerous operations than split which may have worse consequences. I'd pass the string/undef and let the sub check for undef and only then attempt the split. eg:

search_by_tags ($cgi->param('tags')); #... sub search_by_tags { my $tagstr = shift; return unless defined $tagstr; my @tags = split (/,/, $tagstr); }

This also gives you the opportunity within the subroutine to raise an exception in the undef case which might be helpful.

Replies are listed 'Best First'.
Re^2: Which is better software design
by einhverfr (Friar) on Mar 27, 2015 at 11:01 UTC

    This of course leads to the question of whether it should receive a csv list or whether another component is responsible for this aspect of processing.

    The answer of course depends not on tactics but on strategy. If you want your tag search to be closely tied to this representation then your suggestion is good, but if you want to make it more general and allow that representation to change, then you need to structure it differently.

      Completely agree. The change in structure is a much bigger job but one worth doing (or at least considering).

      Without knowing the detail of the OP's system it is hard to suggest anything specific. That said, generally the best approach IMHO to vanilla CGI is to collect, validate and sanitise the inputs right at the start so that by the time any real processing occurs the script has a known clean data set to work with. This is almost always worth the code refactoring work to achieve it and results in the rest of the code being leaner for having to do less validation at that later stage.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1121490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found