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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |