Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: What can I do to improve my code - I'm a beginner

by AnomalousMonk (Chancellor)
on Aug 10, 2017 at 14:16 UTC ( #1197172=note: print w/replies, xml ) Need Help??


in reply to What can I do to improve my code - I'm a beginner

my $input_file = undef;
...
my @files = undef;

These are examples of types of statements that I see in several places in your code and that I consider programming tics that should be addressed with psychotherapy or powerful behavior modifying drugs.

The first,
    my $input_file = undef;
defines a lexical scalar variable that is default-initialized to undef — and then explicitly initializes the variable to undef. I see no point to the explicit initialization, but it can do no harm.

The second type of statement,
    my @files = undef;
can potentially do some damage because it doesn't do what I think you think it does. A lexical array is defined in an empty state by default, but this statement explicitly initializes the array with a single undef element.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @files = undef; ;; dd \@files; ;; print 'number of elements in array: ', scalar(@files); " [undef] number of elements in array: 1
Is this what you expect and want? I thought not. In the example code you posted, this semantic error, by good fortune, does no harm (that I can see), but it is the kind of error that can bite you in the ass at any time given the opportunity.

Update: If you want to go the explicit useless initialization route for list-type variables, the correct syntax is
    my @array = ();
    my %hash  = ();
but again, these statements | initializations would IMHO just be more evidence of a need for medical intervention. The only circumstance I can see in which such statements | initializations can barely be justified is when an initial empty state is vital to the correct operation of a succeeding algorithm and you don't want to bother emphasizing this fact by going to all the trouble of typing a comment.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: What can I do to improve my code - I'm a beginner
by Anonymous Monk on Aug 11, 2017 at 08:43 UTC
    To the psychiatrist, I go for my conversion therapy then! But seriously thank you for the constructive criticism. As I say I'm still very much a beginner and it is comments like these which are going to make me better. Could you please explain something for me though? Regarding setting the array to undef - I completely understand what you are saying. I was wondering why the array was 1 element longer than what it was supposed to be and now I know. I also have an understanding with what you are saying with the lexical scalar variable $input_file. But I am not sure what I need to do to get around these. As you can see from my code, I need them to be global variables, so I define them outside of the WHILE loops. If you say that you see them as redundant can you tell me how to get around doing this? Once again, thanks!
      ... I am not sure what I need to do to get around these. ... I need them to be global variables, so I define them outside of the WHILE loops. If you say that you see them as redundant can you tell me how to get around doing this?

      Please understand that what I see as redundant is useless explicit initialization of a variable. In the case of
          my $input_file = undef;
      the assignment does exactly what is done by default; it is purely redundant.

      Semantically erroneous statements like
          my @files = undef;
      are redundant in the present circumstances because the very next thing you do with these arrays (as far as I can see) is to assign them valid data, thus undoing the erroneous initialization. In other circumstances, the erroneous initialization may lead to a nasty bug.

      ... I need them to be global variables, so I define them outside of the WHILE loops.

      Of course, you need to define lexical variables where you need to use them. On this note, you only use the  @dt lexical within the scope of the  if($firstline==1){ ... } statement block, so that's exactly where I would define it:

      if($firstline==1){ ... my @dt = $header[0] =~ /(\d+)/g; ... } else { <IN> for 1..1 }
      (Incidentally, the  <IN> for 1..1 statement is needlessly involved: if you just want to read and discard one line, the simple  <IN>; statement does the trick.)


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1197172]
help
Chatterbox?
[zentara]: ... an unatural darkness descends on the realm
[shmem]: eclipse? Java? omg...!
[chacham]: The darkness here is quite natural. Sunset was 1.5 hours ago ...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-08-21 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (324 votes). Check out past polls.

    Notices?