Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi all,

I'm quite used to declaring variables as follows:

my ($title, $page, $chapter);
That is, without initialising them with default values.

I also find it quite common in other people's code that I've come across.

I'm wondering, after reading about variables being set to old values under mod_perl, if it might be better in general to initialise each and every variable. So, instead of the above, we now have:

my $title = ''; my $page = 0; my $chapter = 0;
It's more work definitely but it generally makes the code more readable (for instance, if I've no idea what 'page' means, I at least know the variable $page is going to be used for some numerical value). There're probably also some gains in performance as the interpreter doesn't have to make guesses about those values.

Is it merely a preference thing? Or is one method better than the other?

I look forward to reading your views. Many thanks in advance :)

update: It's been very enlightening reading your comments on the node. It seems not initialising the variables is the normal thing to do in most circumstances. Thanks once again for your sharing :)


In reply to To initialise or not to initialise? by kiat

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 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? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-10-20 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (79 votes). Check out past polls.

    Notices?