For the last while, I've been writing some pretty complex Object Oriented modules, where the class gathers up objects of different types, and stuffs them into itself.

Instead of using the object like this:

$obj->{other_obj}->method;

I've taken to writing accessors so I can use them like this:

$obj->other_obj()->method;

However, using the bareword method names works all the same:

$obj->other_obj->method;

My question is, is which way do you prefer? I like the reduction of noise (the parens in this case) as much as possible, but on the other hand, it makes it really, really easy to see what is happening when the parens are in place. To further, I may not be the only one whoever looks at my code, so I'm trying to come up with my own best practices syntax muscle memory for doing things like this.

Here's a full blown example that demonstrates this (while using only chained calls from a single class, not with an object within one of the class methods; the principles are the same):

use warnings; use strict; package Blah; { sub new { return bless {}, $_[0]; }; sub one { $_[0]->{num}++; return $_[0]; }; sub say { print "$_[0]->{num}\n"; }; } package main; { my $obj = Blah->new; $obj->one()->say; # or $obj->one->say; }

Given a complex module with chained method calls that could go four+ wide, is eliminating the parens noise something you'd still get at a glance, or would you leave the empty parens in, for clarification?


In reply to To parens or not parens in chained method calls by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.