Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
If context or variable name doesn't make clear what a variable means, or is used for, one should add a comment. What would be your default value for a variable that's going to hold a reference to an object? Or a variable that could hold both a string and a numerical value? And if you assign 0, does that mean the variable will only hold integers? Only non-negative integers? Any numerical value? But even if you know, what insight to the program did it give to you?

I initialize variables only if it makes sense. I also tend to declare variables at their first use, which usually means the first time something is assigned to it - or it's done a line above the first assignment. Note that assigning default values will take time, although for larger blocks it won't matter too much:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; cmpthese -5 => { init => 'my $title = ""; my $page = 0; my $chapter = 0; $title && $page && $chapter', def => 'my ($title, $page, $chapter); $title && $page && $chapter', }; __END__ Rate init def init 1165121/s -- -38% def 1876595/s 61% --


In reply to Re: To initialise or not to initialise? by Abigail-II
in thread To initialise or not to initialise? by kiat

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?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-01-23 18:22 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.