in reply to Re: where do you put your subs
in thread where do you put your subs

demerphq

Normally I agree with your post but on this one I have to disagree. ++ for a well articulated post, I just don't agree with it. I think your argument hinges on one tiny aspect of perl - using a sub name to invoke it without any syntactic sugar. I like the sugar! I think most perl devs spend too much time playing golf - that sugar helps me. I like the visual clues. When I see bar its hard for me to know what the heck that is but when I see bar() (my pref) or &bar, I instantly know what that is.

Now for my reasoning on why subs should be after (an implicit or explicit main) - it's easier to read. I love to read - newspapers, magazines, web sites, books, code, books on code. There is a definite well designed, well thought out, and well documented approach to writing - it's called the funnel method where the first paragraph is your general outline (moving from general to hypothesis with each sentence). Succeeding paragraphs are the specifics that support your hypothesis. I strongly believe code should follow this method because coding is just another form of writing and communicating. Think about it.

Now for you personally, the subs first is great but for someone coming behind you, you're asking them to disavow every other type of writing and succumb to your approach. Much like reading a James Joyce novel, a lot of people will praise you but only a select few will read you the whole way through and even less will emulate you. Hemingway is a better approach - simple, straightforward and very little verbosity. I only ask you to think about those poor slob maintenance programmers who have to come behind you. It's much easier to start at the top and see the general outline of the writing then to skip to the last chapter.

Being able to read the code is of much greater value than how fast it runs, how few resources it uses, and definetly less than how the compiler or interpreter goes about it's business. Needless to say we definetly cannot reduce down to "See Dick Run" but we can definetly employ a straightforward, almost shaker, style as oppossed to a baroque one.

-derby

update: Also, I would never think one was an amatuer for putting sups at the bottom but I would think I was dealing with a pascal convert if I saw them up front (and I would re-arrange it).

Replies are listed 'Best First'.
Re: Re: Re: where do you put your subs
by Juerd (Abbot) on Mar 08, 2002 at 13:44 UTC
    The parens/ampersand are not much of a real problem. The unintended globals are! If you declare a lexical in the body of your script, and you have the subs declared after that, those subs can access the lexical, thus having global-like behaviour (in a way). A very, very good programming concept is to keep things to yourself if you're a piece of code.

    Normal writing does not have the details at the beginning, that's true. But they don't have the conclusion and flow at the beginning! The introduction in writing can be compared to loading modules, using strict. Then, explanation comes, so you can understand the rest of the text. When that's done, body and conclusion follow.

    Subs, in my opinion, are not footnotes, they form information _required_ to understand the rest of the code. That's another good reason for putting small subs that just encode data, and don't have to do much with program flow in separate modules (footnotes...).

    A Perl program is executed in order. Unless you want it otherwise, it will end after having evaluated the last statement, which should be logically the end of the file. And it's a reason to put your subs at the top too! For the same reason you put use statements at the beginning: they are required pieces of knowledge to be able to fully comprehend the rest of the code.

    44696420796F7520732F2F2F65206F
    7220756E7061636B3F202F6D736720
    6D6521203A29202D2D204A75657264
    

      The parens/ampersand are not much of a problem...
      I just wanted to point out that there is a side effect to not using parens on a subroutine/method call: the called subroutine "inherits" the @_ array of the caller. While most of you are probably aware of this it is something that can have some unintended consequences - hence my preference for always using the parens on subroutine calls.

      Michael

        I just wanted to point out that there is a side effect to not using parens on a subroutine/method call: the called subroutine "inherits" the @_ array of the caller.

        Not entirely true. While &foo passes @_, foo and foo() and &foo() do not. Hence, imho for not passing @_ the best solution is to avoid the ampersand where possible (which means you pretty much only use it to create references).

        Proof:

        sub test { print "\@_: @_\n"; } @_ = qw(a b c); print 'test; # '; test; print '&test; # '; &test; print 'test(); # '; test(); print '&test(); # '; &test(); __END__ test; # @_: &test; # @_: a b c test(); # @_: &test(); # @_:

        44696420796F7520732F2F2F65206F
        7220756E7061636B3F202F6D736720
        6D6521203A29202D2D204A75657264
        

Re: Re: Re: where do you put your subs
by demerphq (Chancellor) on Mar 08, 2002 at 13:52 UTC
    where the first paragraph is your general outline (moving from general to hypothesis with each sentence). Succeeding paragraphs are the specifics that support your hypothesis. I strongly believe code should follow this method because coding is just another form of writing and communicating. Think about it.

    Your analogy doesnt makes sense. The main is in essence the conclusion of an argument. So what you are saying is that when I write an argument I should put the conclusion before the introduction.

    Also, you are talking about aesthetic reasons. I am talking about best practice. Best practice is that which minimizes the chance of bugs and error, not that which reads like a novel. If you want to read nice text then read the comments. The code should be written so as to be as maintainable and error free as possible.

    And if you rearranged my code as you said there is a very good chance it wouldnt run. I do like not using parens when they arent necessary as they often end up occluding the intent in a mess of parens, much as lisp is practically unreadable for most mere mortals. Consider

    print join("\n",map(join(",",map{s/\.//;$_}@$_),@strings); #vs print join "\n",map{join",",map{s/\.//;$_}@$_}@strings;
    Err. maybe not the best example but I know which of the two I find easier to write and to read.

    UPDATE:
    A better example is one that we use all the time, that of finding out how many values are in a hash. Which would you rather use

    my $count=scalar keys %hash; #or $count=scalar(keys(%hash));
    END UPDATE

    BTW: I am a Pascal programmer by background. But that is neither here nor there. In C you cant use a sub until it has been declared either,(Both allow the use of forward declarations, as does perl,) in fact most true compiled languages share this trait.

    Cheers,

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

      demerphq

      What a can of worms. When writing with the funnel method, your initial paragraph is an overview of what you're going to do - to me this is just like a main that is nothing more than a driver for meaty subs (who said anything about footnotes?)

      I'm not talking about aesthetics, I'm talking about maintenance. We'll just have to agree to disagree. (And I promise not to re-arrange your code!)

      -derby

        I'm not talking about aesthetics, I'm talking about maintenance.

        I'm glad we all want easy maintenance!

        subs at bottom => sharing of ALL variables used out of the subs => greater chance of having unintended behaviour => more maintenance
        subs at top => sharing of explicitly chosen variables => less unintended behaviour => less maintenance

        (I assume strict)

        44696420796F7520732F2F2F65206F
        7220756E7061636B3F202F6D736720
        6D6521203A29202D2D204A75657264
        

Re: Re: Re: where do you put your subs
by mugwumpjism (Hermit) on Mar 08, 2002 at 16:35 UTC
    Now for my reasoning on why subs should be after (an implicit or explicit main) - it's easier to read. I love to read - newspapers, magazines, web sites, books, code, books on code. There is a definite well designed, well thought out, and well documented approach to writing - it's called the funnel method where the first paragraph is your general outline (moving from general to hypothesis with each sentence). Succeeding paragraphs are the specifics that support your hypothesis. I strongly believe code should follow this method because coding is just another form of writing and communicating. Think about it.

    I guess that depends whether you're a "top down" or a "bottoms up" programmer.