Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by broquaint (Abbot)
on Jun 26, 2002 at 15:05 UTC ( #177396=note: print w/ replies, xml ) Need Help??


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

Just a couple of comments on coding style

  • You don't really *need* to comment what the purpose of your variables are if they're clearly labeled (they were pretty clear to me at least), although this is more of a personal preference than anything.
  • It's good practice to keep your line-length under 80 columns for several reasons
    • it shows that your expression is too long and probably needs to be simplified (as always there are exceptions)
    • 80 cols is the standard width for terms which leads on to ...
    • code is a lot easier to read when the lines are nice'n'short (again this is personal opinion)
  • Here's a good rule of thumb for the amount arguments to pass to functions - if you've got 7+ arguments you probably don't have enough[1]. I think you could reduce the amount arguments you pass about by taking advantage of perl's aggregate data types.
  • Ack - no use strict in sight! This is pretty much a must for anything but the smallest of perl programs, and the warnings pragma is also muchly helpful.
  • Is there any reason why you're passing some of your scalar variables as references? While this is helpful and sometimes necessary when dealing with hashes and arrays, scalars are passed by value so don't needed to be passed by reference (unless you've got a good reason to of course).
And do make sure you get onto using CGI and watching out for potential security holes (see. opening a pipe to sendmail).
HTH

_________
broquaint

[1] this is indeed sarcasm but unfortunately I cannot take credit for this masterful piece of wit[2] which was introduced to me by a co-worker
[2] that, however, is mine ;-)


Comment on Re: Some suggestions on coding style - a chance to critique
Re: Re: Some suggestions on coding style - a chance to critique
by emilford (Friar) on Jun 26, 2002 at 15:16 UTC
    Thanks for the suggestions. First, I do use strict and -w, it just happened to get cut off when I was cutting and pasting my code.

    Also, if scalar variables are passed by reference, does this mean that they can be modified from within a subroutine? I thought that I would have to pass a reference to the scalar in order to modify them (i.e. - \$variable). Is this not true?

    About passing variables, I could push everything into a seperate data type, but wouldn't that make things a little less clear and require "not necessary" lines of code?

    Thanks for the suggestions.
      I thought that I would have to pass a reference to the scalar in order to modify them (i.e. - \$variable). Is this not true?
      You don't *have* to pass by reference to modify variables as @_ is just a list of aliases (it's magic you see) e.g
      sub foo { $_[0] = "modified in foo()"; print "in foo()\n"; } my $var = "a string"; print "\$var is $var\n"; foo($var); print "\$var is $var\n"; __output__ $var is a string in foo() $var is modified in foo()
      However passing by reference is more explicit, but that then introduces in the issue of 'dirtying' your arguments which is generally agreed to be avoided if possible. I believe the reasoning is that entering a function shouldn't modify the state of your program, although this can usually be ignored with more complex data e.g throwing around hashes is potentially pricey.
      I could push everything into a seperate data type, but wouldn't that make things a little less clear and require "not necessary" lines of code?
      At the worst it will require a line or two of code, but organising your data is far more likely to *reduce* your code. Let's take send_mail() for example. Currently you have 8 arguments, whereas I'd probably reduce that to 2 arguments with all the sendmail-related information in a hash or even a hash of hashes. But then again I think that all this throwing about of data is a symptom of a mis-organised program, to which I think part of the solution would be re-structuring the data as once you have well-defined data structures the program tends to follow.
      HTH

      _________
      broquaint

        I'm confused to what the purpose of passing a variable by reference is since you claim that you don't *have" to in order to modify it. Maybe my mind is stuck in C/C++ mode where you have to pass a parameter by reference (I believe) in order to modify it. I thought the purpose of not passing by reference is so that a subroutine will only be able to use the variable and not modify it. If the subroutine can modify this variable no matter how you pass it, what's the point of distinguishing between the two? See where I'm going with this?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-07-24 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (159 votes), past polls