Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: My coding guidelines

by fruiture (Curate)
on Nov 25, 2002 at 18:51 UTC ( #215696=note: print w/replies, xml ) Need Help??

in reply to My coding guidelines

I think it's no great use making guidelines in general. You'll have to agree on guidelines in one project and it's good to have concrete ideas. The actual decisions will depend on the group.

Some of the things you mention are in my eyes means of sane programming (system calls, exit code). I don't think you can work seriously with someone who has to be told these things. Also, I'd never say "SHOULD". Say "MUST", so first of all there is no doubt: "Use strict! No exceptions. That's it." Someone who _really_ knows when to disable strictness, will do it. Someone who doesn't will struggle for a solution and ask for help and then someone else may tell him "This is the exception, you may disable strict here."

So there are some general rules i'd make (and in general i agree with you):

  1. use strict! Always.
  2. use warnings! Always.
  3. Doing CGI? use! Always.
  4. Parsing commandline options? use Getopt::Long and Configure() it POSIXLY_CORRECT! Always.
  5. check system calls! Always.
  6. Use comments to comment, use POD to document. Don't mix these two.
  7. Avoid Globals!
  8. Globals are all variables with a scope that is larger (in the sourcecode) than an average screen.
  9. Name Globals wisely. Follow perlstyle rules
  10. Use spaces to make things clearer

These are the rules I consider "general". Other things are matter of personal taste but should be equal inside a project:

  1. open the curly on same line as keyword. Indent next line.
  2. closing curly on single line, indented as deep as keyword of opening curly.
  3. curly-rule exceptions are special short blocks (map,grep,sort)
  4. No space between function name and opening parens.
  5. No space between array identifier and opening square bracket.
  6. No space between hash identifier and opeing curly

Now finally the Tab-issue: I really think that one should use tabs, but only for indentation. Don't use tabs for alignement.

if( $a > $b ){ T---return T---T---cond1 ? val1 : T---T---cond2 ? val2 : T---T--- default T---; }

This way your alignement won't break in someone else's editor using a different tab-width, but logical indentation is still present. I am really convinced that this is a solution.


Replies are listed 'Best First'.
Re: Re: My coding guidelines
by mpeppler (Vicar) on Nov 25, 2002 at 19:16 UTC
    Also, I'd never say "SHOULD". Say "MUST", so first of all there is no doubt: "Use strict! No exceptions.
    And I was just going to commend Abigail for using SHOULD for a lot of the guidelines.

    They are guidelines after all - and there are situations where this or that rule is not appropriate. The programmer should not have to go through hoops to code around the limitation imposed by the rule (but should provide appropriate reasoning why the rule should not be applied).


Re: My coding guidelines
by Abigail-II (Bishop) on Nov 27, 2002 at 11:52 UTC
    I object to those "always" you are using in your list. You wouldn't be able to create useful modules like Exporter and Memoize, if you always had to use strict. Same with warnings. Perl generates warnings if it things the programmer makes an error. But sometimes, Perl is wrong. The right thing to do is turn warnings off, instead of having to go through hoops to satisfy Perl.

    As for point 3, I couldn't agree less. is a monster, doing far more than just CGI (come on, producing HTML shouldn't be a part of a module calling itself CGI). In the rare cases I do any CGI programming, I tend to avoid it like the plague.

    As for you tab issue, it's a good example why tabs should be avoided. Your example indicates a tabwidth of 4. Any idea how that's going to look like on a terminal with a default tabstop of 8? Sure, the left indents will all be lined up, but can you garantee none of the lines will exceed 80 characters? Any idea what's going to happen if one of the tabs is replaced by 4 spaces, and someone using tabstops of 8 is going to view it? It won't align correctly anymore.

    Tabs are evil. The savings in disk space and I/O might have been significant 30 years ago, but they aren't nowadays. With spaces, the indentation will always look the way the author intended it to be (given fixed width fonts). With tabs, it will only look right if all authors and viewers used the same tabstops.


      No, this is a misunderstanding: The rule says "you must", because it doesn't want to leave any doubts to somebody who doesn't know enough to break it with sense. I know that if i want to mess around in some symbol tables (e.g. Export or an Accessor Method Creator...) I will turn off strict 'refs' as well, and I want anybody else to do that then. But someone who isn't sure, must not break the rule.

      Same with There's a "must", because you should not question until you know better.

      Tabs: This is not about disk space, but it's no use disuccing this. Tabs aren't evil. :)

      To clarify: whom are you making these rules for? Do you need to tell yourself "use strict, you(=I) know when I may disable it!"? Such guidelines are usefull in two ways: a) for people who learn Perl, for them I wrote the "MUST". To say "SHOULD" tells them there are exceptions, but there are no exceptions they need to know about yet. b) For a project, to keep it all consistent and easier to make many people work on the same Code. There the "MUST" fits because an exception should be rare and known, because it is an exception. And: I wouldn't work with people who need to be told to use "strict" and when to disable it.

        As I said in the article, these are my rules, and I don't enforce them on anyone (I don't work with people who need other peoples guidelines - and usually I don't work with anyone). It's my code that needs to adhere to the given guidelines.

        Furthermore, I was using SHOULD and MUST in the same sence RFCs use them (that's why they are capitalized). MUST means MUST. It doesn't mean "most of the time, but there are exceptions". That's what SHOULD means. MUST means the rule should be followed always. SHOULD means the rule should be followed usually, but there might be exceptions.

        You don't want to discuss tabs, but I'm interested why your preference for tabs. What do tabs give you that spaces don't give you?


Re: Re: My coding guidelines
by strat (Canon) on Nov 27, 2002 at 12:21 UTC

    Consering strict, I use the following rule:

    Always use strict. If you can't do something with strict, think about if you really need to do it without strict, and if you really need to, disable the part of strict locally and explain why this has to be done, e.g.
    if ( ... ) { # disabling strict refs temporary because of blabla..... no strict 'refs'; *{ $AUTOLOAD } = $codeReference; use strict 'refs'; }
    ++ Abigail-II

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

      That's exactly right. use strict always does not mean you shouldn't no strict when appropriate :)

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2023-12-09 12:09 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.