Re: which is better practice

From: Eric Schwartz (emschwar_at_pobox.com)
Date: 07/16/04


Date: Thu, 15 Jul 2004 16:46:59 -0600

Abigail <abigail@abigail.nl> writes:
> Eric Schwartz (emschwar@pobox.com) wrote on MMMCMLXX September MCMXCIII
> __ my ($x, $y); $x = $y = 0; ?
>
> That's more typing. I like the ability to initialize in the same
> statement as I declare.

I do too, but in general, it's the declaring variables in the middle
of an expression (unless it's a for or while, in which case it feels
semantically to me like it's the beginning) has never felt right to
me.

> You need to get out more. ;-)

I won't deny that. :) But I still haven't ever seen it in the wild, as
it were.

> Just as I don't see a point in writing:
>
> my $i;
> $i = 0;
>
> I don't see the point of writing:
>
> my $opt;
> GetOptions "option=s" => \$opt;

If it were only one option, I might be pursuaded, but I've never used
GetOptions without at least four or five options; almost all my
scripts have --help, --verbose, --debug, and --silent. In that case,
my style would have

##############
# GLOBAL OPTIONS
##############
our ($opt_help, $opt_verbose, $opt_debug, $opt_silent);

##############
# Get command-line options
##############
GetOptions("help|h" => \$opt_help,
           "verbose|v" => \$opt_verbose,
           "debug|d:i" => \$opt_debug, # :i lets me specify debug levels
           "silent|s" => \$opt_silent,
           # more options here
           );

I don't know that I can come up with a reason other than personal
preference for not declaring them together with the function call, but
it seems somehow more cluttered than the alternative. I'm glad,
though, that Perl gives us these *ahem* options.

-=Eric

-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
		-- Blair Houghton.


Relevant Pages

  • Re: static array initialization
    ... It is initialized before the program begins execution therefore it ... from this group as to the style/correctness of declaring a static ... The solution is to use a char, in this case "static char option =3D 0" ... variable will be initialize to 0. ...
    (comp.lang.c)
  • Re: question on scope of a variable
    ... >>I've tried declaring a public variable in a module of my project in hope ... >>to be able to initialize it, or have access to it's value if it's ... Yes, it is, but it seems to have a lifetime which I don't understand, and at ... some form or routine can "see" it, ...
    (microsoft.public.word.vba.general)
  • question on scope of a variable
    ... I've tried declaring a public variable in a module of my project in hope ... to be able to initialize it, or have access to it's value if it's ... While this compiles for me, I'm getting a runtime error that indicates the ... I did try declaring it using the STATIC command in the module but the ...
    (microsoft.public.word.vba.general)
  • Re: "with" Coders are Monsters
    ... typing or declaring a temporary variable. ... In Turbo Pascal and Delphi 1 times, ... time and have perfected the art of using them only in very specific ...
    (borland.public.delphi.non-technical)
  • Re: array problem
    ... Peter - I understood that in addition to declaring the array I had ... to initialize each element also. ... DefaultStyledDocument[] doc; ...
    (comp.lang.java.programmer)