Reasoning behind no space between Generic brackets in Java
Asked Answered
O

3

6

Looking at GenericWhitespaceCheck in Checkstyle documentation,

Left angle bracket (<):

  • should be preceded with whitespace only in generic methods definitions.
  • should not be preceded with whitespace when it is precede method name or following type name.
  • should not be followed with whitespace in all cases.

Right angle bracket (>):

  • should not be preceded with whitespace in all cases.
  • should be followed with whitespace in almost all cases, except diamond operators and when preceding method name.

I am not sure I fully understand the reasoning behind why < should not be followed by a space and why > should not be preceded by one.

In other words, why is Map<String> the convention over Map < String >?

Is this only because as the number of parameters and depth increases it, the without spaces version is more readable.

Like, Map<String, List<String>> is more readable than, Map < String, List < String > >?

Also, as a general question, are there some repository or guides which explain reasons behind Checkstyle conventions?

Outrageous answered 27/12, 2019 at 17:17 Comment(10)
It's the same as parenthesis. (a, b, c(d)), <a, b, c<d>>. And you don't just squish everything together. You can put spaces after your commas, instead of <a,b,c<d>>.Voluble
The reasoning is basically, "because that's how it is". This is what the greater community accepted as a convention; it's a balance between zero whitespace and excessive whitespace. Also, it should be Map<String, List<String>> (notice the space after the comma).Extravagate
It's also similar to writing plain old English: space after punctuation like comma, period, question mark, and exclamation mark. Space before opening braces and after closing ones. People are used to this style, and it makes it easier to read as when everything is squeezed together.Neiman
@Slaw, corrected it.Outrageous
Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. This is the standard that was assumed and accepted by people. If you wish to, you can disable the feature and add spaces. no one is stopping you and it wouldn't break any functionality. But your peers may not like it :)Donavon
@SunilDabburi I understand, my question was to understand if there is some reason behind this standard. Like for e.g in C its is a standard to do if(null == x) instead of if(x == null) to avoid mistaken assignment by doing, if(x = null). Wanted to know if something similar exists here too.Outrageous
I don't think so. I think it's merely cosmetic that's comfortable for most of the people. It is a style.Donavon
Java does not care about whitespace most of the time. You could put a 10,000+ line class all on one line, and remove all spaces not strictly needed to delineate, for instance, variable types from variable names, and it would still compile. The reason we add whitespace (including new-line characters) is to make the code readable. Readability is paramount.Extravagate
@Slaw, the C reference was an example that sometimes there is a reasoning behind conventions that get lost with time. I agree there might be nothing here, which is what the question was about, to know if there is one.Outrageous
My comment was meant to imply there's no behavior-based reason like with the C example you gave. In this case, whether there's whitespace or not does not change how the code compiles or behaves.Extravagate
C
4

Although I have no evidence or research to base my theory on, I'd reason as follows:

Cohesion

A (kind of language-philosophical) rationale could be:

The parametrization of types (generic's major role) such as in Map<String, Object> belongs to the type-name the same like parentheses and parameters belong to the method-name. So adding parameter to a signature should follow a consistent spacing-rule: no space around parametrizing brackets (neither in generic-type's parameter definition, nor in method's parameter definition).

Thus the angle brackets are coherently defining the "type signature" and should stay as close to the type as possible (semantic and spatial), which means no space should untie this relation.

Readability

From the (Clean Code) perspective there is a clear benefit for avoiding spaces:

Spaces around angle brackets rather make them misread or misinterpreted as logical comparison operators.

Cease answered 27/12, 2019 at 19:44 Comment(0)
B
5

The introduction to an early tutorial on generics (from 2004) says (emphasis mine):

This tutorial is aimed at introducing you to generics. You may be familiar with similar constructs from other languages, most notably C++ templates. If so, you’ll soon see that there are both similarities and important differences. If you are not familiar with look-a-alike constructs from elsewhere, all the better; you can start afresh, without unlearning any misconceptions.

This is acknowledging that Java generics look like C++ templates. C++ templates also conventionally omit the space after the opening <, and before the closing >.

The conventions around Java generics will follow from the ways they were written in early tutorials.

Beane answered 27/12, 2019 at 19:5 Comment(0)
C
4

Although I have no evidence or research to base my theory on, I'd reason as follows:

Cohesion

A (kind of language-philosophical) rationale could be:

The parametrization of types (generic's major role) such as in Map<String, Object> belongs to the type-name the same like parentheses and parameters belong to the method-name. So adding parameter to a signature should follow a consistent spacing-rule: no space around parametrizing brackets (neither in generic-type's parameter definition, nor in method's parameter definition).

Thus the angle brackets are coherently defining the "type signature" and should stay as close to the type as possible (semantic and spatial), which means no space should untie this relation.

Readability

From the (Clean Code) perspective there is a clear benefit for avoiding spaces:

Spaces around angle brackets rather make them misread or misinterpreted as logical comparison operators.

Cease answered 27/12, 2019 at 19:44 Comment(0)
T
0

Most coding styles have a limit of characters per line or column length. Reducing white space make shorter lines that are easier to read.

For example, the Google code style for Java has a column limit of 100 characters.

Coding styles depend on the community behind the language, so I would recommend checking their standards.

Transience answered 27/12, 2019 at 17:33 Comment(1)
This isn't the reason. If it were, whitespace would be omitted in other places, such as after a comma, or after the semicolon in a for loop declaration.Beane

© 2022 - 2025 — McMap. All rights reserved.