Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasoning behind no space between Generic brackets in Java

Looking at GenericWhitespaceCheck in Checkstyle docs,

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 is there some repository/guides which explains reasons behind Checkstyle conventions ?

like image 935
Adwait Kumar Avatar asked Dec 27 '19 17:12

Adwait Kumar


People also ask

What do triangle brackets mean Java?

Angle Bracket in Java is used to define Generics. It means that the angle bracket takes a generic type, say T, in the definition and any class as a parameter during the calling. The idea is to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes, and interfaces.

How does a generic method is different from generic type?

From the point of view of reflection, the difference between a generic type and an ordinary type is that a generic type has associated with it a set of type parameters (if it is a generic type definition) or type arguments (if it is a constructed type). A generic method differs from an ordinary method in the same way.

How does generics work in Java?

A generic type is a class or interface that is parameterized over types, meaning that a type can be assigned by performing generic type invocation, which will replace the generic type with the assigned concrete type.

Do generics prevent type cast errors?

Implementing generics into your code can greatly improve its overall quality by preventing unprecedented runtime errors involving data types and typecasting.


2 Answers

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.

like image 114
Andy Turner Avatar answered Sep 20 '22 10:09

Andy Turner


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 parameterization 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 angel-brackets are coherently defining the "type signature" and should stay as close to the type as possible (semantical 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 angel-brackets rather make them mis-read or mis-interpreted as logical comparison operators.

like image 20
hc_dev Avatar answered Sep 20 '22 10:09

hc_dev