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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Actually I strongly strongly disagree with the "put the subs at the bottom" school of thought. (In fact its closely related to the subject that was behind the very first post I ever made at the monastery) I suppose originally this comes from programing systems languages like Pascal and C where you _cannot_ use a function prior to its declaration (contrary to webadepts assertions this has nothing to do with "making you think things through" it has to do with simplifying compiler design, both of these languages can and usually are parsed in a single pass.) In either of these languages you need to place a forward declaration in situations where you have two subs that need to refer to each other.

But when you think it through it makes a lot of sense. You declare variables before they are used so that you (and the compiler) know about them. In fact this is also one of the primary reasons why I still do this in Perl even though I dont need to (I can forget forward decls tho, yah!).

Even in perl legal syntax is different depending on the order of declaration. When a sub is used before it is declared it MUST be referenced with parenthesis ( bar() )or the & (&bar) sigil. It cannot be used in bareword form ( bar ). This alone is enough of a reason to always declare what you use before you use it.

use strict; sub foo{print "Foo\n"} foo; bar; #produces error! sub bar{print "Bar\n"} __END__ Bareword "bar" not allowed while "strict subs" in use at D:\temp\order +.pl line 4. Execution of D:\temp\order.pl aborted due to compilation errors.
And Im sorry to say to those who defend the "subs last" idea, but there are even more serious arguments why putting subs first is a very very good idea. Consider when you write your "main" do you wrap it and all of its variable declarations in a block? If you don't then when you put them at the top of the file _all_ of your subs are now treating your file scoped lexicals as globals. And globals are something to be avoided if at all possible as every good CS student knows. So now this means that you may _think_ that you have written your subs as nice reusable pieces, but in fact they may have subtle dependencies on the "main" that you placed before them, and you wont even know. This is obviously a very bad thing.

Now this is going to sound harsh, in fact there are some that will probably -- me for saying it, but without hearing a very very good justification or seeing a bunch of fiddly stuff (anonymous blocks, BEGIN statements and the like) to resolve the issues above I would usually mark down or consider a programmer to be an amateur/newbie if I see "subs at the end". And if I had to work on the code, the absolute first thing that I would do is reorder the code (and most times when I do it breaks because of the "unintended global" issue) with some very strong comments if the original programmer was near.

Given all of the disadvantages related to "subs last" and the very very few advantages asscoiated I can't see why anyone would do this.

PS:There is even less reason to place use statements at the top of a file than there is to place subs at the top of the file, but its pretty damn confusing figuring out where a subroutine call gets defined if the use statements are scattered all over the place.

PPS:I find it interesting that of the other responders to your post at this point I only agree with one Juerd++ (well, I dont mind mixing code and pod...)

Yves / DeMerphq
--
When to use Prototypes?
Advanced Sorting - GRT - Guttman Rosler Transform


In reply to Re: where do you put your subs by demerphq
in thread where do you put your subs by greenFox

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found