method call clarity

geoff on 2005-08-23T14:34:24

ok, so every shop has style guidelines, and some of those style preferences differ from my own, and I'm perfectly fine with that - if the team wants 4 spaces but I'm a two-spacer, or if they don't care that they have some lines that are 172 characters, fine. but this one just doesn't make any sense to me:



all method/function calls end in () for 'clarity'



now, for function calls like this

  my $foo = bar();


I think it makes sense, since the definition of bar() might get shuffled in larger codebases so just using the bareword might fail to compile someday.



for method calls, however, I'm completely unconvinced. I don't see how

  my $foo = $obj->bar;


is any less clear than

  my $foo = $obj->bar();


but perl has lots of little easter eggs that even the best of us don't know, so I ask: in this case can

  $obj->bar


ever mean anything different than

  $obj->bar()


? and I don't mean for cases where you need to pass in arguments :) and I don't mean cases where () is required and tricky, like

  my $foo = $cv->();


I mean, in all seriousness, exactly how does () add clarity to a method call? what else could you possibly expect ->bar to represent?



maybe it's the difference between

  $obj->bar


and

  $obj->{bar}


? if so, I don't buy it :)


Other Languages

shiflett on 2005-08-23T14:41:23

Maybe they are taking other languages into consideration. For example, to someone with a PHP background, this looks like a property:

my $foo = $obj->bar;

This looks like a method:

my $foo = $obj->bar();

I think the parentheses universally indicate a function or method, so that might be what they mean by clarity.

Re:Other Languages

geoff on 2005-08-23T15:02:39

yeah, like I care about other languages...

nice of you to notice the php to perl migration strategy we've been covertly implementing, though ;)

Re:Other Languages

shiflett on 2005-08-23T15:42:26

I'll see your PHP to Perl migration strategy and raise you a Perl to PHP migration strategy:

http://search.cpan.org/dist/PHP-Interpreter/

:-)

just easier to read

perrin on 2005-08-23T15:00:09

Sorry Geoff, I'm in total agreement with this style guideline. I find it much easier to scan the code when the method calls end in (). I find it especially good in cases where they are chained.

Re:just easier to read

geoff on 2005-08-23T15:41:46

ugh... it's chaining where I find them the most superfluous
  my $val = Foo->new->attribute->format;
just feels much more idiomatic to me than
  my $val = Foo->new()->attribute()->format();
but ok, you're a reasonable guy, so I'll give in :)

Re:just easier to read

perrin on 2005-08-23T15:53:31

Imagine a mixed-case like this:

my $val = Foo->new->{attribute}->format->{id};

It can get confusing quickly, especially when people do that horrible "return yourself" method chaining that SOAP-Lite uses. I know I'm in the minority on this though. I just don't think there's any advantage to leaving them off, and it makes your code less consistent.

What about :lvalue?

jk2addict on 2005-08-23T16:09:29

A sub is a property is a sub. What about when using the :lvalue attributes; as evil as they are?

sub value : lvalue {
    my $this = shift;
    $this->{VALUE};
};

Would you put the parens here:

my $value = $self->name();

but not here:

$self->name = 'Chris';

or leave it

$self->name() = 'Chris';

Re:What about :lvalue?

Dom2 on 2005-08-23T19:19:22

Using :lvalue is bad because you lose encapsulation.

-Dom

Re:What about :lvalue?

jk2addict on 2005-08-23T21:38:59

Hell I'd never use it. I hate.

However, I have seen 3rd party modules that use it internally, so calling those 3rd party functions will fall under the formatting policy above.

Re:

Aristotle on 2005-08-26T18:43:30

Unless you return a tied object that validates the value it gets assigned. Which is obviously a huge overhead.

The only rationale for consistent guidelines is...

rafael on 2005-08-23T17:29:54

...make maintainance easier.

In this case, that may make the code more greppable. So you don't have "->foobar" results when you grep for "->foo" for example. (Note that I and my shop both disagree with this particular guideline :)

One of the nice things about -method()...

Alias on 2006-02-03T05:23:19

Is that at least that style item can be automated by perltidy.

Oh wait, I'm dreaming about the PPI automanipulatification of Perl::Style (or perhaps you'd call it Perl::MyWay)...

In any case I REALLY need to get around to getting that written. :/