By convention, types should be named by nouns, that's ok. However, @FunctionalInterface
is a special type - object type that acts like function/method. In case of function I feel like it's more natural to choose a verb e.g. decorate.apply(o)
instead of noun decorator.apply(o)
. And there are also nouns expressing an activities e.g. decoration.apply(o)
.
If I look at java.util.function package all the functions are named by nouns: Function
, Operator
, Consumer
, Supplier
, ... Should I prefer nouns, verbs or nouns expressing verbs?
UPDATE
Sometimes, we can get little help with factory method returning an object/function:
// toList factory returning Collector instance
list.stream().filter(...).collect(toList());
@FunctionalInterface
can mark only types (classes, interfaces, annotations, enums).
To name a type you should use a noun, while for a method you need a verb.
Classes
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
class Raster; class ImageSprite;
Methods
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
run(); runFast(); getBackground();
Here's an example of how I would define a functional interface.
@FunctionalInterface
interface Decorator {
void decorate();
}
In code, it looks readable.
Decorator decorator = () -> {};
decorator.decorate();
My thoughts on "decorator vs decoration":
"Decoration"
is more about describing the process/result, while "Decorator"
is a mechanism/tool/means to start that process/to obtain that result.
If I saw the Decoration
class in code, I would think about a collection of decoration stuff (like ornaments, paints, wallpapers) rather than a person who actually decorate.
If I saw Decorator
or Painter
, I would be expecting methods like decorate()
or paint()
because that's what they can/should do.
Compare suppliance/supplier -> get()
and consumption/consumer -> accept(t)
.