D
David Gale
Quoth Ben Morrow said:It is a trivial waste of runtime, and a non-trivial waste of a
reader's time when they try to figure out *why* you're explicitly
initializing something which doesn't need it.
Wait, you're saying that the perl interpretter doesn't just skip the
unnecessary assignment? That's a surprise to me; I've always had the
impression that perl optimizes fairly well (I've never had a problem with
its running speed), and since most early perl programmers were coming from
languages which did require explicit initialization, I would've expected
that this trivial optimization would've been one of the first features.
Granted, I haven't run any tests to see if there is, in fact, a noticeable
difference, but I'd be shocked to find there is one.
As for wasting a reader's time: a) Anyone as familiar with perl as you would
immediately realize that this is an extraneous hold-over, and thus not waste
any time wondering about the initialization; b) anyone not as wise and
experienced, or whose primary experience is in a different language,
wouldn't even notice the initialization as being out-of-place. In neither
case would it slow the reader down, and, in fact, can often aid in
interpretation--if I initialize a scalar to 0, then it's clear that I intend
that variable to be numeric; if I initialize it to '', then it's clear that
I intend to use it as a string. Granted, wise perl programmers like
yourself would know that 0 and '' are both false values, and thus
equivalent--but the lesser mortals would benefit from the extra tip. Think
of it as a small form of self-commenting code.
Thus I would strongly disagree here. Write less code, unless that
makes things less clear.
And, as argued above, I think that it does help keep things clear.
my $var;
my $var = 0;
my $var = '';
are all equivalent statements, but, as pointed out above, the first tells
you *nothing* about how the variable is intended to be used; that has to be
determined through code examination. The second and third, however, show
their purpose immediately.
Know The Language You're Writing In. It *never* pays to carry habits
across from one language to another.
Ah, the great philosophy of, "If you can't become an instant expert in
whatever language your boss determines to be appropriate for the job, you
might as well flip burgers at the local burger joint." Code is code.
Often, habits that are helpful in one language are helpful in others--or do
you not use subroutines, because they are too similar to C's functions? I
terminate my lines in TCL with semi-colons, even though it's completely
superfluous to do so, because I think it makes it clearer, and it doesn't
slow TCL down at all. Proper code commenting should be a habit, regardless
of language; granted, the character sequence to delimit a comment changes,
but that doesn't change the fact that it's a good habit. And so on. That's
why most programming classes try to teach programming styles, rather than
specific language features--yes, it's great that I learned how to program
Dyllan. But if I couldn't transfer the skills and habits from there, I'd be
forced to start from scratch when dealing with Scheme, Lisp, etc.
I'll grant that there's one language which specifically *tried* to break
totally away from all previous languages (Intercal), but I've never seen
familiarity with that listed as a required skill on a job posting.
You do. Perl guarantees variables to be correctly initialized to what
you expect. If you don't trust Perl to leave $x undef after 'my $x;',
why do you trust it after 'my $x = undef;'?
I do, in perl. But not in most other languages. And, again, 'my $x;' tells
me nothing of the expected use of the variable, whereas 'my $x = 0;' does.
(Incidentally, 'my $x = undef;' suffers from the same problem, and so I
never use it. So, *in my opinion*, 'my $x;' and 'my $x = undef;' are both
equivalent unclear constructs, and ought to be avoided.)