You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2019/08/20 03:28:00 UTC

[jira] [Commented] (GROOVY-9230) Parentheses: omit or not?

    [ https://issues.apache.org/jira/browse/GROOVY-9230?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16910962#comment-16910962 ] 

Paul King commented on GROOVY-9230:
-----------------------------------

We could certainly say more about style but I am not sure that we would want to endorse a particular style always. I agree with you that sometimes having less options to think about can be a good thing but I am not sure that the language has to be the enforcer of style when we have style tools like CodeNarc.

My recommendation would be to create some additional CodeNarc rules and suggest to projects that they consistently apply one of the rules. There are rules like:
 [http://codenarc.sourceforge.net/codenarc-rules-unnecessary.html#UnnecessaryParenthesesForMethodCallWithClosure]


 We could create some more rules (I actually thought I would find one already but didn't spot it), e.g. an {{UnnecessaryParentheses}} rule and a {{MissingParentheses}} rule. Hopefully with the rule being smart enough to know about some cases where the parentheses are required and of course, CodeNarc has various mechanisms for turning off the rules in particular cases.

In general, Groovy would encourage the first of these but if Gradle are now trying to have build scripts that can easily be switched between Kotlin and Groovy, then they could recommend the latter style. If folks aren't planning to switch to Kotlin, they could stick to the more Groovy idiomatic style.

Similarly, some projects might want to be able to switch easily between Groovy and Java. For them, they might adopt a non-idiomatic style.

Also, there are some forms of code where adding the parentheses could be argued leads to simplified changes. E.g. for builder code (which is a lot of Gradle) and if I make lots of changes, e.g. adding or removing {{transitive}} or {{exclude}} child properties, e.g. changing:
{code:java}
compile "org.apache.ivy:ivy:$ivyVersion"
{code}
to this:
{code:java}
compile("org.apache.ivy:ivy:$ivyVersion") {
    transitive = false
}
{code}
then maybe having the parentheses already there helps just a little. My build scripts don't change often enough for me to justify the extra parentheses noise but I'd regard this as a case of "_beauty is in the eye of the beholder_" and someone who has become familiar with parentheses everywhere might have a different preference to me.

From the wording in the "Introduction to JVM Languages" book you referenced, I don't think it really recommends always adding parentheses, I think it is more stating "_use care_" - and if you are familiar with using parentheses and aren't familiar with the rules for when they are required and when they can be left out, you could argue it is being more careful to keep them in. The book certainly has a few places with non-idiomatic code, so I am not sure it is the best reference to go by.

> Parentheses: omit or not?
> -------------------------
>
>                 Key: GROOVY-9230
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9230
>             Project: Groovy
>          Issue Type: Documentation
>          Components: syntax
>            Reporter: rkrisztian
>            Priority: Major
>
> The Groovy Style Guide mentions that parentheses can be omitted (not always, I know). And that's it. I think the primary purpose of a style guide is to define a good set of recommendations for coding practices, rather than list all the available options. (Because that's like good old Perl: you can write the same thing in so many ways, since the syntax is so lenient, which then practically doomed the language.) So the style guide that is supposed to give us answers, is still leaving us with questions right now.
> I would like to know what you recommend: should we omit parentheses or not? I am really puzzled, because:
>  * IntelliJ IDEA can suggest auto-removal of unnecessary parentheses.
>  * I think Gradle started with not using parentheses but now it mixes up styles: [https://github.com/gradle/gradle/issues/10307]
>  * The Internet kinda started making up their own style guides, and it is not the first time I see that they actually recommend the parentheses for readability: [https://books.google.hu/books?id=7Hc5DwAAQBAJ&lpg=PA347&ots=I5irALxRRu&dq=groovy%20code%20style%20parentheses&hl=hu&pg=PA347#v=onepage&q=groovy%20code%20style%20parentheses&f=false]
>  * Whereas, for DSLs, the target audience may not know programming so well, so the parentheses are not recommended: [https://books.google.hu/books?id=RNtOCwAAQBAJ&lpg=PA16&ots=xEOKCWi0xo&dq=groovy%20code%20style%20parentheses&hl=hu&pg=PA16#v=onepage&q=groovy%20code%20style%20parentheses&f=false]
> But this still puzzles me.
>  * Are missing parentheses really that hard to read? If someone with no programming skills can read statements without them better, than how come the opposite is true for an advanced programmer?
>  * Should the no-parentheses style be suggested only for DSLs? That would include Gradle too, right?
> Thanks in advance.



--
This message was sent by Atlassian Jira
(v8.3.2#803003)