http://www.perlmonks.org?node_id=1143893


in reply to Making program readable

I have no OO techniques to suggest, just write clearer code.

Don't repeat yourself so much. This :-

if ($intermediate_array[12] eq "GSM" && $intermediate_array[9] + eq "MSubmit") { $intermediate_MO_cnt++; } if ($intermediate_array[12] eq "GSM" && $intermediate_array[9] + eq "MSubmit" && $intermediate_array[7] eq "MsgAccepted") { $total_MO_success++; }

could be written as :-

if ($intermediate_array[12] eq "GSM" && $intermediate_array[9] eq +"MSubmit") { $intermediate_MO_cnt++; if ( $intermediate_array[7] eq "MsgAccepted") { $total_MO_success++; } }

It's clearer and easier to see what your intent was, and there are fewer places for bugs to hide.

Variable names like $del_user1_error , $del_user2_error, .. $del_userN_error are always a sign that you should have used any array and you're writing too much code. So instead you could do something like this :-

my @del_user_error_count; for my $i (0..4) { if ($cdr_post_array[11] eq $del_user_errors[$i]) { $del_user_error_count[$i]++; last; } }

Replies are listed 'Best First'.
Re^2: Making program readable
by ravi45722 (Pilgrim) on Oct 06, 2015 at 10:47 UTC

    I replaced the my if-else loops with these.

    my @del_user_error_count; foreach my $i (0..4) { if ($cdr_post_array[11] eq $del_user_errors[$i]) { $del_user_error_count[$i]++; last; } }

    Its decreased 42 lines but its increasing the execution time. I see this through benchmark.

    For if-else loop it gives

     the code took:52 wallclock secs

    After replacing the code with foreach

     the code took:67 wallclock secs
      "...I see this through benchmark..."

      You might try Iterator::Simple:

      #!/usr/bin/env perl use strict; use warnings; # use feature qw (say); use Iterator::Simple qw(iter); my $iterator = iter( [ 1 .. 4 ] ); while ( defined( my $i = $iterator->() ) ) { # warp action goes here }

      See also Bleeding edge as well as Re^2: Useful number of childs revisited.

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      Its decreased 42 lines but its increasing the execution time. I see this through benchmark.

      Ignore it, its not important :)

Re^2: Making program readable
by ravi45722 (Pilgrim) on Oct 06, 2015 at 09:18 UTC

    Ya... I understand what you said. Thank you. I will change it. Is there any more changes that can increase my program readability or to reduce the code???

      Is there any more changes that can increase my program readability or to reduce the code???

      What does your program look like now ravi45722?