Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Re: where do you put your subs

by demerphq (Chancellor)
on Mar 08, 2002 at 13:52 UTC ( [id://150312]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: where do you put your subs
by derby (Abbot) on Mar 08, 2002 at 14:38 UTC
    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
      

        I find this kind of logic to be ... less than satisfactory.
        1. Maintenance includes enhancements.
        2. Every single program will always need maintenance. Every single one.
        3. A maintainer will want to be able to read the code easily, to be able to determine where best to make the change.
        Global variables are a feature. If you misuse this feature, then you will be bitten. This is the same if you misuse any feature. There are perfectly good reasons to make some variables global. However, just like injudicious use of map and grep and $_ (some or all of which some code shops ban, to improve maintainability), misusing globals can reduce maintainability. Using them intelligently can IMPROVE maintainability.

        For example, let's say that you have an object that you use to do your logging. It's a one-stop-logging machine that you use in every single function you have. What options do you have to make sure it's in scope in every function?

        1. You could pass it in to every function. That starts looking like tramp data.
        2. You could make it a Singleton and have it grabbed every time. This simply makes the classname a hard-coded value in your code. (Or, the classname is now a global .....)
        3. You could make the logger (or logger class) global. (Of course, heavy commenting would help here, as well as a set of standards.)
        Remember, the only difference between a constant and a global is that the global can change. But, if you use 2-5 globals in your entire application and they're well-named and well-documented up-front, you shouldn't have any problems.

        Remember - you can always try to get the idiot out of the programmer by force, but it's better just to educate and document. You'll have more success that way.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found