Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Some suggestions on coding style - a chance to critique

by jarich (Curate)
on Jun 28, 2002 at 01:15 UTC ( #177904=note: print w/ replies, xml ) Need Help??


in reply to Some suggestions on coding style - a chance to critique

Juerd had some reasonable points against the use of CGI. He said:

The short version:
It is slow (huge), has awful parsing (I just don't like it), a very annoying hybrid interface and too much junk code that is there for (backwards) compatibility. And it doesn't use strict, and has many globals.

Well I personally strongly support CGI, avoiding parameter cleansing and all the rest sounds great to me. However Juerd mentions some very good reasons against, which I can't refute. So, since you don't need all the things that CGI provides perhaps you should consider CGI::Simple which is faster, uses strict and is a very clean and nice reimplementation of CGI, by our very own tachyon :).

As everyone else says, I also recommend using more modules. Particularly something like Mail::Sendmail.

Things like this:

$$redirect = $$formdata{'redirect'} unless ($$formdata{'required'} eq +"");
can be better (more clearly) written as:
$$redirect = $formdata->{redirect} if ($formdata->{required} ne "");
And if it's appropriate, you might write it like this instead:
# sets unless $formdata->{redirect} is # a) undefined b) "" or c) 0 $$redirect = $formdata->{redirect} if $formdata->{required}; # this sets it only if $formdata->{redirect} is defined # ($formdata->{redirect} may be "" or 0 if already # initialized to that value) $$redirect = $formdata->{redirect} if defined($formdata->{required};
Either way, notice that the -> notation makes it much easier to see that $formdata is a reference to a hash of scalars. I've changed your unless eq to an if ne because in my experiences it's easier for someone to read. Trailing unlesses are great if the condition is simple such as next unless $name; but they start cluttering things if the reader has to evaluate the condition in their head and then take the complement of that.

Another style note, is that your indentation really ought to be consistant, although I hope that the inconsistancies I see here are due to cut-n-paste effects, for example "sent_email" is very difficult to read.

Hope it helps.

jarich

Update: Juerd is of course correct, the two lines are technically equivalent. I'd choose the version I've suggested because I expect that none of my students would understand the first straight off. Of course few of the students would understand the second straight off either... but it'd be easier for me to explain. ;)


Comment on Re: Some suggestions on coding style - a chance to critique
Select or Download Code
Re: Re: Some suggestions on coding style - a chance to critique
by Juerd (Abbot) on Jun 28, 2002 at 06:33 UTC

    $$formdata{redirect} can be better written as: $formdata->{redirect}

    Technically, they're equivalent, so the second cannot be better. In my opinion, the arrow-notation is clearer, but it's still just a matter of taste.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-11-29 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls