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

Embracing functions with parentheses

by crenz (Priest)
on Mar 16, 2005 at 09:30 UTC ( #439886=perlmeditation: print w/replies, xml ) Need Help??

When writing/formatting code, amongst other things I strive to reach two goals:

  • Write consistently - ie. don't express the same thing in two different ways
  • Write cleanly - don't use more parentheses, braces, brackets, etc. than necessary, but also use enough of them to be clear.

An example for not being consistent in choosing your method names would be

$obj->SetName($value); $obj->set_title($newtitle);

As another example, I'm not a fan of too many parentheses. I usually write one of

print join ' - ', grep { -d } @list; print join(' - ', grep { -d } @list);

rather than

print(join(' - ', grep({-d}, @list)));

Especially for Perl built-in functions, a variety of styles exist as many can be called both with and without parentheses. I find myself often leaving away the parentheses, just to have to bring them back in to solve some precedence problem. And then I am left with a weird mixture of functions that use them and functions that don't. An example of things that just happen because of not strong enough habits:

open(my $fh, '>', $filename); print $fh $message; myownfunction($param1, $param2); close $fh;

Which brings me to my question (sorry it took so long): What do you guys do to have a consistent style for both Perl built-in functions and your own subroutines?

Replies are listed 'Best First'.
Re: Embracing functions with parentheses
by Zaxo (Archbishop) on Mar 16, 2005 at 10:07 UTC

    I tend to use as few parentheses as I can. The reason is that when I see them then I know they mean something. Usually they mean that I am overriding operator precedence, my $foo = ($bar + 1)/2; or I need to terminate the argument list of a function or builtin, print map( {baz $_} @quux), ' results.', $/; The effect is about like the style you describe, and I'm happy with it.

    In general I try to follow perlstyle, but my efforts may show a few quirks.

    After Compline,

Re: Embracing functions with parentheses
by eyepopslikeamosquito (Bishop) on Mar 16, 2005 at 10:31 UTC

    For user-defined functions, I always use parens. This is being kind to the Perl parser, making it easier and less error-prone to switch between "use" (compile-time) and "require" (run-time). I've seen Perl newbies spend hours trying to figure out what is wrong because they didn't use parens when calling their user-defined functions. For example:

    use strict; Fred; sub Fred { print "in function Fred\n" }
    produces the error Bareword "Fred" not allowed while "strict subs" in use at line 2..

    Re style guide, I quite like Abigail's My coding guidelines. If you know of any other good ones, please let me know, because I hope to (eventually) write a tutorial that will include recommendations on Perl style.

Re: Embracing functions with parentheses
by jbrugger (Parson) on Mar 16, 2005 at 09:58 UTC
    Our own functions are easy to recognice due to the fact we have to program conform to a predefined coding standard.
    funcions are named using camelnotation, we use an indentation of 4 spaces(tab) 'private' functions have an _ prefix (i know they do not really exist in perl, but this way we know a function is ment to be private) etc.
    Next we allways use strict and have warnings on, and we code with the idea that readability is more important than a fast and unreadable algorithm, and we try to avoid perlmagic as much as possible.
    package Just_A_Package_Name use strict; sub _privateFunctionName() { my $self = shift; my ($otherItem) = @_; } sub publicFunctionName(){ ... } # So we'd rather write foreach my $item (@array) { # than foreach (@array) {
Re: Embracing functions with parentheses
by dragonchild (Archbishop) on Mar 16, 2005 at 13:19 UTC
    I'm actually somewhat Python-esque in my use of parentheses. I use as few parens as I can get away with, but I organize my code so that indentation plays the part that parens would for reading purposes.

    As for your open/close example, I would write it as so:

    open my($fh), '>', $filename or die "Cannot open '$filename': $!\n"; print $fh $message; close $fh; myownfunction($param1, $param2);

    I don't pay for my whitespace ... do you? :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Embracing functions with parentheses
by Vynce (Friar) on Mar 16, 2005 at 10:04 UTC

    I have often wanted a good single perl style guide -- like the MLA or something, but for perl -- but it just can't be done to enough people's satisfaction.

    I personally tend to end up with lots o' parens, and if needed I break them up on multiple lines. (sometimes! i swear!)

    I think this qestion is one where really, context can matter a lot. do what's right for the situation -- but if you're having to add tons of parens to clarify a precedence issue, consider making it more than one visual line, if not more than one conceptual line. yeah, your code may be a litle faster if you skip a few assignments to temp variables -- but do you care that much?

Re: Embracing functions with parentheses
by Ninthwave (Chaplain) on Mar 16, 2005 at 12:09 UTC

    It is funny you use the open close example because I always use.

    open (FILEHANDLE, '>', $filename); ......... close (FILEHANDLE);

    Becuase I did not like open needing parens but close not. So I used this to visually help see the open and close statements.

    The thing is it has worked for me but I have never known if I am setting myself up for a fall later.

    "No matter where you go, there you are." BB
      Huh? open doesn't need parens.

        I re read my manual and just say duh, need is too strong of a word. Or just the wrong word my bad.

        But I stick by the sytle I use, well because I do.

        "No matter where you go, there you are." BB
Re: Embracing functions with parentheses
by Juerd (Abbot) on Mar 21, 2005 at 20:56 UTC

    What do you guys do to have a consistent style for both Perl built-in functions and your own subroutines?

    Actually really preferring one style over another helps. Your way isn't bad, if that is what you like. Except, of course, that you didn't check the return values for open and close (and print). My style guide explains the way I like my Perl, in case you want to know the details.

    Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://439886]
Approved by BazB
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-01-27 21:39 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (71 votes). Check out past polls.