Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Coding style

by nyamned (Sexton)
on Jul 13, 2010 at 09:41 UTC ( #849214=perlquestion: print w/replies, xml ) Need Help??

nyamned has asked for the wisdom of the Perl Monks concerning the following question:

here are two almost identical pieces of catalyst code. I'm not sure which way preferred/more readable. Any comments, suggestions?
sub edit : Chained('object') : PathPart('edit') : Args(0) : FormConfig +('ticker/edit.yml') { my ( $self, $c ) = @_; $c->stash->{form}->model->default_values( $c->stash->{object} ); if ( $c->stash->{form}->submitted_and_valid ) { $c->stash->{form}->model->update( $c->stash->{object} ); $c->response->redirect( $c->uri_for( $c->controller('Ticker')- +>action_for('index') ) ); $c->detach; } $c->stash( template => 'ticker/edit.tt2' ); }

sub edit : Chained('object') : PathPart('edit') : Args(0) : FormConfig +('ticker/edit.yml') { my ( $self, $c ) = @_; my $form = $c->stash->{form}; my $ticker = $c->stash->{object}; $form->model->default_values($ticker); if ( $form->submitted_and_valid ) { $form->model->update($ticker); $c->response->redirect( $c->uri_for( $c->controller('Ticker')- +>action_for('index') ) ); $c->detach; } $c->stash( template => 'ticker/edit.tt2' ); }

Replies are listed 'Best First'.
Re: Coding style
by ahmad (Hermit) on Jul 13, 2010 at 10:46 UTC

    For me, The second code is more readable

    But it's just a matter of choice, choose whatever you're more comfortable with.

Re: Coding style
by biohisham (Priest) on Jul 13, 2010 at 12:28 UTC
    The second piece of code looks more structured, visual-friendly and self-explaining. Reading only the few first lines makes it clear that there's a form and a ticker and a story that goes on around these...The first code on the other hand makes it mentally exhaustive since you need to incrementally follow the infix arrow to the next and collectively decide on the overall synergy all these pointing arrows will possess, hence, maintaining such a code by someone else can invite wrath upon the one who originally wrote it..

    At the end, it all boils down to preferences, if you're working in a team, there will be an agreed upon style and method to code design and its documentation. If you're solo then it is you who calls the shots on the style choice but meanwhile keep reading others' code and inspecting others' styles for you to be flexible enough to adjust to style modifications...

    Best of luck :)

    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
Re: Coding style
by roboticus (Chancellor) on Jul 13, 2010 at 10:51 UTC


    I've never used Catalyst, but I find the second one much more readable than the first.


      But is it reasonable to add extra lines of "useless" code to such small subroutine, just to make it more readable?

        IMHO, it certainly is - maintainability is an important part of good coding, and readable, well commented, well laid out code is a big part of that.

        In 6 months time could you (or someone else) look at the first example and quickly see what was going on? Or if you get an error, quickly see where it was coming from?

        Just my 2 cents.

        Just a something something...

        When I need to speed up code I go looking for nested hash references as in your example. It's my believe the cost of creating a new variable is small(er) compared to the cost of looking up the value of a nested reference. If the same value is referenced more than once, it's worth creating a variable to skip as much dereferencing as you can.

        The fact that it's more readable is just a bonus.

        Unless it causes a [significant ] performance hit you should always go with the more readable version. It's depressing that you even had to ask a question with such an obvious answer!

        Elda Taluta; Sarks Sark; Ark Arks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://849214]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2020-05-28 11:28 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (165 votes). Check out past polls.