Naming convention of @FunctionalInterface


Naming convention of @FunctionalInterface



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 is also nouns expressing an activities e.g. decoration.apply(o).


@FunctionalInterface


decorate.apply(o)


decorator.apply(o)


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?


Function


Operator


Consumer


Supplier





Class names are nouns, method and function names are verbs - if you see something having noun name in spec - it means somewhere there's a class or an interface having such name. Whenever you have an object - it's identified by nouns, whenever you have a behavior - it's identified by verbs.
– dmitryro
Jul 1 at 13:48





1 Answer
1



@FunctionalInterface can mark only types (classes, interfaces, annotations, enums).


@FunctionalInterface



To name a type you should use a noun, while for a method you need a verb.



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 should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.


run();
runFast();
getBackground();



Naming Conventions, Oracle



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.


"Decoration"


"Decorator"



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.


Decoration



If I saw Decorator or Painter, I would be expecting methods like decorate() or paint() because that's what they can/should do.


Decorator


Painter


decorate()


paint()



Compare suppliance/supplier -> get() and consumption/consumer -> accept(t).


suppliance/supplier -> get()


consumption/consumer -> accept(t)





thanks, the last part helped me to decide which pattern to follow for now (+1), but I will wait some time before accepting the answer (waitting for another answers if any).
– matoni
Jul 3 at 15:06






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?