Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

First, let me say that I gave this node a ++ despite the fact that I'm about to disagree with most of it, and take offense at some part of it. But you make your arguments in a clear, readable fashion :-).

That having been said, I consistently put subroutines after their first use. Except in a few cases where the cross-referencing was so thorough that I resorted to alphabetical ordering1 for lack of a better scheme. Even then, the subroutines come after the main logic. In a later node, you tweaked someone for saying that there were good reasons, but that he hadn't offered any. Here are some of mine.

The flow of thought tends from the least-specific to the most specific. I am first interested in knowing that the program opens a file, operates on the content, and then outputs a report. From that point, I will (depending on what I'm trying to learn-- am I debugging this? Am I adding new features?) choose to delve more deeply into what type of processing, exactly, is being done. Now, that's probably in a subroutine somewhere. And from where I'm at in the source code, it's not important whether the sub is above me or below me. But I really don't want to scroll through several pages of subroutines to finally find the main logical loop, and then find that I only need to look at one subroutine which, by the way, I passed about 3 pages ago. I don't generally know what subroutine I care about when I first start reviewing code. After that, when I know a specific name, the placement is irrelevant-- I'm going to /-search in vi or Ctrl-S in XEmacs, depending on what's available to me.

Forget not that there is a use subs (); just as there is a use vars (); pragma. I try to pre-declare all my subroutines, because I generally plan before I start banging out code anyway. I want to know within the first 15-20 lines of code what all the subroutines and package-global variables are in a program or module. I should be able to get that much information with a simple head invocation. Again, where the subs themselves are doesn't even matter here. If you don't use the subs pragma and do all your declaration management by ordering, I'm going to have to read all of your code to find out what is there. Think you would gripe at me if you had to read my code with the subs at the end? If you're one of my assigned engineers and you bring me code for review that has to be read all the way through just to know what subroutines are defined and declared, we'll be having a conversation.

Want more? How about this: The end of the file in a Perl script is not the same as the end of the program. What about modules that use AutoLoader2, or scripts that use SelfLoader3? Scripts often embed data (other than subroutines) after __END__ or __DATA__. In those cases, putting subroutines "at the end" isn't even technically possible. Worse, if the actual logical loop of the program is sandwiched somewhere between subroutine blocks and whatever follows __END__, how much more intuitive is that to the reader?.

What something like this ultimately comes down to, is in fact an evaluation of how well-designed your code is (or was) before typing began. Consistency in style is more important than adherence to a specific style. As programmers, we will always be exposed to styles different than our own. Whether or not we can read them and derive the information we seek from the code, that is what we need to judge the programmer against.

And lastly, show some caution when making heavily-generalizing statements like, 'I would usually mark down or consider a programmer to be an amateur/newbie if I see "subs at the end"'. I do take offence at it, and I imagine there are other Perl coders who would, as well. Constructs like:

exit main(@ARGV);

only serve to make Perl code look more C-like. But C is not a scripting language, and everything in C, including main, is explicitly a subroutine. You don't have a situation in which a C program will start at the first execution instruction, thus requiring you to wrest control avay from the errant Program Counter and direct it to where you want it. Instead, a linked C program includes code that starts execution at the linker-provide entry point for main()4.

I've been programming Perl as a full-time job for almost 10 years, well before 5.000. When I write C, it looks like C. When I write Tcl or Lisp, that's what the code looks like. And my Perl looks like Perl, surprisingly enough.

--rjray

1 Yes, I have done this once or twice, only when there was no other clear and obvious way to order them. If the order is going to be arbitrary anyway, then it at least helps to know that read_file will come somewhere before report_error.

2 What I Want, When I Want It: Using AutoLoader and AutoSplit. Originally in The Perl Journal Issue #6.

3 Ibid.

4 In truth, a lot of stuff generally happens before the actual entry into the main routine, none of which is really relevant to the argument at hand.


In reply to Re: Re: where do you put your subs by rjray
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 goofing around in the Monastery: (4)
As of 2024-03-28 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found