http://www.perlmonks.org?node_id=994002

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

Hi all,

what is the common sense about calling methods without explicit arguments?

  1. $object->method
  2. $object->method()
  3. $object->method( )

Best regards
McA

Replies are listed 'Best First'.
Re: Handling braces with methods without arguments
by tobyink (Canon) on Sep 17, 2012 at 11:04 UTC

    It makes no difference to perl, and little difference to readability, but I generally prefer to use the first form.

    I might use the second form if I were calling a method which is documented as taking a list of things, but I want to make it look clear that I'm specifically providing it the empty list. e.g.:

    $town_crier->announce_statements( "The King is dead!", "Long live the king!", ); ...; $town_crier->announce_statements();

    But again it makes no difference to perl.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Handling braces with methods without arguments
by rjt (Curate) on Sep 17, 2012 at 11:33 UTC

    As others before me have mentioned, all three options are valid syntax, but you probably already knew that. So, it comes down to matters of style. Where style is concerned, perlstyle is the go-to document.

    perlstyle doesn't specify this case. However, a couple of quotes hint that parenthesis might be slightly preferred:

    Function and method names seem to work best as all lowercase. E.g., $obj->as_string() .

    This example uses parens.

    Note that function names are considered more readable with parentheses after their name, that is function() .

    This quote comes from the Pod doc section, where functions are referred to in English (or whatever) text, so it doesn't exactly refer to the code itself.

    The bottom line, the golden rule with any stylistic choice, is be consistent. If you (or someone else) starts coding with or without parenthesis, keep doing it.

Re: Handling braces with methods without arguments
by BrowserUk (Patriarch) on Sep 17, 2012 at 10:33 UTC

    Every method receives at least one argument: itself.

    So an 'argumentless' method is akin to a function that takes only one argument. Like close, flush, tell etc. for us on files.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Thank you for your correction. I just inserted the word 'explicit' to my question.
        Thank you for your correction. I just inserted the word 'explicit' to my question.

        That implies that you still consider this an open question?

        I ask, because I thought I answered your question even in its modified form.

        Some commands take arguments: $you->doTheShopping( shoppingList ) requires the list.

        (I'm aware that "retail therapy" can and often does rely entirely on impulse buying; but that's not 'the shopping' :)

        Some commands don't need arguments: $you->comeHomeNow (Well, you can and often do try arguing, but where did it ever get you :)

        Whilst some of that is humour, the point is serious.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

        Having just seen tobyink's answer, maybe I completely misread your question? If so, I apologise.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Re: Handling braces with methods without arguments
by Jenda (Abbot) on Sep 17, 2012 at 12:18 UTC

    Perl doesn't care, but in some well known languages the form without the braces would not call the method, but rather return a reference to the method (JavaScript and C# come to mind, not sure about Java ... though in C# it's a bit more complicated than that ...). Mainly to prevent confusion for people using those languages, I'd recommend using the second syntax. I don't see any reason to include the space within the braces.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      This is a nice argument which didn't come to my mind. Thank you. You forget python in the list.

      Best regards
      McA

Re: Handling braces with methods without arguments
by greengaroo (Hermit) on Sep 17, 2012 at 13:48 UTC

    I always use the second syntax: $object->method(). The reason is simple. Since I use Moose and MooseX to create my classes, I want to differentiate a call to a common method: $object->method() and a call to an attribute "getter": $object->attribute.

    Moose automatically creates a getter-setter method for each read-write attributes. The same method can be call to "set": $object->attribute('new value') or to "get": $object->attribute.

    Now let's say the attribute is an object itself, and I want to call one of its own methods: $object->attribute->method() but if I call a method that returns an object based on an argument, I would do: $object->method('arg')->method() or $object->method()->method().

    Of course this is just me... I like to have a certain standardization of my syntax because I feel it helps me read my code and I hope it would help others too! It's easier to debug when you know what the programmer intended to do in the first place.

    But remember, there is always more then one way to do it! My advice is find the way that suits you the best and be consistent with it!

    There are no stupid questions, but there are a lot of inquisitive idiots.