in reply to Use of uninitialized variables?

I prefer to never initialize a variable as empty. Why? It is the best way to find out if you are actually using the variable in your code. Quick sample,

use warnings; my $retval = 0; warn "Return Value $retval\n";

You have not done anything with this value, and nothing gets reported as being a problem. If you do this,

use warnings; my $retval; warn "Return Value $retval\n";

You are going to get an uninitialized value warning, you will probably be saying WTF? Until you look and realize the code that you wanted was this,

use warnings; my $retval; $retval = get_value_from_subroutine(); warn "Return Value $retval\n";

It also reduces the amount of code that you have, I admit it is small, but do it a couple of hundred times, you get the idea.

Replies are listed 'Best First'.
Re^2: Use of uninitialized variables?
by Zadeh (Beadle) on Jun 11, 2008 at 21:30 UTC
    Hadn't thought of this point -- finding dead code. I think there's gotta be a better way for finding that info though. Next question that pops into my head is if I could write a unit test, run it and get coverage info -- which would also provide enough info to deduce where dead/unused code was.

      You should take a look at Devel::Cover then. I have used it for unit testing and I found it fairly useful.

      I have not used it for program execution but it looks like it supports it. The only reason I thought of for not using it are that you cannot execute all branches of your code by running a program. Maybe it has a way of dealing with it but I am not sure, someone else may have the answer.