Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: Passing / evaluating / dieing on one line...

by zengargoyle (Deacon)
on Apr 05, 2002 at 15:27 UTC ( [id://156951]=note: print w/replies, xml ) Need Help??


in reply to Re: Passing / evaluating / dieing on one line...
in thread Passing / evaluating / dieing on one line...

Not if it will end up like this:

(my $label_colnum = &column_exists( \@data, $_[1] || 0 )) == -1 and di +e; (my $key_colnum = &column_exists( \@data, $_[1] || 1 )) == -1 and di +e; (my $cross_colnum = &column_exists( \@data, $_[1] || 2 )) == -1 and di +e; # ...

It's easier to cut-n-paste, it's obvious to a maintainer how to add another colnum variable.

If you later want to refactor it's also easier to automate the conversion if the statements are grouped on one line. Simple s/blah (\w) blah (\d) blah die (".*");$/refactorsub(\1,\2,\3);/; or such, no multi-line wierdness ;).

I tend to think of long identicalish lines as a template or macro or pseudo sub type of thing.

Which would you prefer?

do_A; do_B; do_C; do_D; do_A; do_B; do_C; do_E; do_A; do_B; do_C; do_C; or do_A; do_B; do_C; do_D; do_A; do_B; ...

The yaddayaddayadda (...) is going to be one of my favorite operators in Perl 6 ;)

Replies are listed 'Best First'.
Data structures drive logic! (was Re3: Passing / evaluating / dieing on one line...)
by dragonchild (Archbishop) on Apr 05, 2002 at 16:05 UTC
    Your example is spurious, because it's bad code.
    my %Columns = ( 'label' => 0, 'key' => 1, 'cross' => 2, ); foreach my $col_name (keys %Columns) { $Columns[$col_name} = column_exists( \@data, $_[1] || $Columns{$co +l_name} ); die if $Columns{$col_name} == -1; }
    Now, adding a new column is a matter of changing the %Columns hash. In addition, it's much easier to factor out the %Columns hash to some configuration file this way than doing it your way.

    In addition, this lends itself to even further improvements. For example, consider

    my %Columns = ( 'label' => { default => 0, exists => 0, }, 'key' => { default => 1, exists => 0, }, 'cross' => { default => 2, exists => 0, }, ); foreach my $col_name (keys %Columns) { $Columns[$col_name}{exists} = column_exists( \@data, $_[1] || $Columns{$col_name}{default} ) +; die if $Columns{$col_name}{exists} == -1; }

    THAT is self-documenting code!

    Furthermore, any time I see

    do_A; do_B; do_C; do_D; do_A; do_B; do_C; do_E; do_A; do_B; do_C; do_C;
    I was to do something like:
    for (\&do_D, \&do_E, \&do_C) { do_A; do_B; do_C; $_->(); }
    It's cleaner, more maintainable, and more extensible.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      i can't ++ and -- a node at the same time, and you have a high rep, so i just --'ed this one.

      i like your first example of maintainable code. it could be extended further by variablizing the constant values, like

      sub colNOEXISTS () {-1} # and, then, in the for statement... $Columns{$col_name}{exists} == colNOEXISTS and die "your error here: $!";

      what i don't like is your second example. i think it is difficult for a maintainer to understand that you're evaluating code references using a default variable, as in $_->() for \&do_A;.

      secondly, the do_A; do_B; ... example could have been expressions or blocks, but your example limits do_C, do_D, and do_E to only evaluating code blocks. for instance,

      $i=1; $j=2; $k=3; $l = $i++ * $j++ for 1..$k; $i=1; $j=2; $k=3; $m = $j++ * $k++ for 1..$i; $i=1; $j=2; $k=3; $k=3;
      (which is a somewhat contrived example) cannot easily fit into your method.

      my point: zengargoyle suggested a formatting change to improve maintainability, while you suggested a functionality change. the formatting change is easy to implement and maintain, while your suggestion involves a deeper understanding of the underlying code, and the limitations of the construct which you impose upon it. i believe this has the potential to make code more difficult to maintain and extend, and has a higher potential to be misunderstood.

      ~Particle ;Þ

        Heh. Thanx for the vote of confidence. You got a ++ ... :p

        If you don't like $_->(), then use &{$_} instead. *shrugs* Six of one, half-dozen of the other. The point is that people are too afraid of code-references. They're one of the many reasons why Perl is so powerful.

        As for the fact that do_A and do_B could be expressions or blocks ... That is a straw man. If you're doing the same code over and over, it should be a function. Plain and simple. I'm going to be stubborn here, but "The Rule"(tm) should be "Use a function for a piece of code that appears twice unless you have a good reason otherwise". Hence, if you initialize $i three times, there should be a function to initialize $i. What if you want to change what happens there, such as printing a debugging statement every time $i is initialized to 1? If it's broken out to a function, that's easy. If it's not, then it's nigh-unto impossible.

        The point is maintainability, not ease of initial coding. Perl is very good in terms of reducing development time. Part of that is the ability to test out possibilities through one-liners and throw-away scripts. The other is the capability to write extremely maintainable code, in ways that other languages don't allow.

        Remember - development is about 40% writing new stuff and 60% changing stuff that's already there. (I might even be overestimating the former.) If you can't easily change it, it's not reducing your development time. End of story.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://156951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-19 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found