Alternate names: MutableConstantField, MutableMethodReturnType
Where possible, prefer referring to some types by a subtype which conveys more
information. For example: ImmutableList
conveys more information (the
immutability of the type) than List
, List
conveys more
information than Collection
, and ListMultimap
conveys more information than
Multimap
.
This is consistent with Effective Java 3rd Edition §64, which says to refer to objects by their interfaces.
ArrayList
. They should be treated as interfaces in every
important sense of the word.Multimap
was designed as a superinterface for ListMultimap
and SetMultimap
which should rarely be referred to directly.That is, prefer this:
ImmutableList<String> getCountries() {
return ImmutableList.of("Denmark", "Norway", "Sweden");
}
to this:
List<String> getCountries() {
return ImmutableList.of("Denmark", "Norway", "Sweden");
}
TIP: Using the immutable type for the method return type allows Error Prone to
prevent accidental attempts to modify the collection at compile-time (see
DoNotCall
).
Suppress false positives by adding the suppression annotation @SuppressWarnings("PreferredInterfaceType")
to the enclosing element.