SimonT
SimonT

Reputation: 2369

To put space before method signature or not?

I would like to know what the overall recommendation is for whitespace between a method's name and its parameters.

That is, the general preference between the following two lines:

public static void main (String[] args) {} // (We'll ignore the question of spaces between the `String` and `[]` .)
public static void main(String[] args) {}

I recently have begun to feel like the former is the better one, especially considering that everything else in a method declaration (e.g. the throws Exception(s) section) is also space-separated.

Upvotes: 7

Views: 7099

Answers (2)

supersam654
supersam654

Reputation: 3244

As chris mentioned in the comments, the Official Java Code Conventions specifically states:

Note that a blank space should not be used between a method name and its opening parenthesis. This helps to distinguish keywords from method calls.

As you questionably considered in your question, methods are different on purpose.

Upvotes: 9

user719662
user719662

Reputation:

2018 update: since almost all IDEs allow easy invocation/declaration lookup, the main gains of switching the convention here are moot; after 3 years, I've switched back to "no space after method name" rule just because most style guides in most languages use it... making an exception to existing code styles is IMVHO viable when the gains outweigh the "WTF factor" of the change; in this case, with up-to-date tooling, there are no actual gains, so I'd personally recomend against the alternative proposed below.


I beg to differ with the interpretation of 1999's unmaintained White Space Java conventions. It only says that the space shouldn't be used to help distinguish keywords from method calls. Thus, there ain't no official rules as to whether use the space in contexts where method call can't appear (and thus where no such help is needed) - as such, the rule obviously does apply to invocative context (where calls can appear and where it helps) and doesn't apply to declarative context (where calls can't appear and where it would serve no purpose). Even more - because the conventions state that the whitespace use should help in distinguishing the usage context, using space on declaration actually keeps with the spirit of the rule - it actually allows you to distinguish method invocation from method declaration, even when using simple text search (just search for the method name followed by space).

After switching to it, distinguishing invocations from the declaration got easier. It also highlighted the fact that the parentheses after the name ain't the invocative ones - and that their syntax is different from the call syntax (i.e. type declarations are needed before variable names etc.), as you noticed already.

tl;dr you can use

void method () { } // declaration
void method2 () { // declaration
    method(); // invocation
}

to be able to quickly do searches on declarations/invocations only and to satisfy the convention at the same time.

Note that all the official Java code, as well as most of the code styles in the wild, doesn't use the space in the declarations.

Upvotes: 2

Related Questions