Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 constant function naming convention [closed]

Tags:

java

java-8

I know about the Oracle Java naming conventions and I've read similar SO questions (like Java constant variable, naming convention) but what naming should I use for constant functions?

E.g. if I have the functional interface

public interface StringDecider {
  public boolean decide(String str);
}

and now I'm using it to define a constant function. Should the naming be 'upper snake case', like

public final static StringDecider STRING_NOT_EMPTY = (str) -> (str!=null && !str.isEmpty());
STRING_NOT_EMPTY.decide("Example");

or camel case

public final static StringDecider stringNotEmpty = (str) -> (str!=null && !str.isEmpty());
stringNotEmpty.decide("Example");
like image 622
Kris Avatar asked Nov 07 '16 17:11

Kris


People also ask

What is the naming convention for constant in Java?

Convention: Use Uppercase with Underscore One important naming convention for constants in Java is to use all uppercase letters with the underscore character separating the words.

What is the convention for naming a constant?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

Which of the following are true of Java naming conventions?

Naming Conventions of the Different Identifiers It should start with the uppercase letter. It should be a noun such as Color, Button, System, Thread, etc. Use appropriate words, instead of acronyms. It should start with the uppercase letter.

What is the best way to define constants in Java?

To make any variable a constant, we must use 'static' and 'final' modifiers in the following manner: Syntax to assign a constant value in java: static final datatype identifier_name = constant; The static modifier causes the variable to be available without an instance of it's defining class being loaded.


1 Answers

In Java, there is no such thing as a "constant function": A lambda constructs an object which has a single method, and, like all other objects, it can be assigned to a reference — including a final reference. At the level of your reference StringDecider STRING_NOT_EMPTY, there is no distinction between an object which has been created using a lambda and one created through any other means. Therefore, the term "constant" doesn't have a different meaning depending on what constructs were used to define and construct the constant object.

In fact, beyond their definition in code, there is no discernible difference between a lambda function object and e.g. an anonymous class object:

public final static StringDecider STRING_NOT_EMPTY_LAMBDA = str -> (str != null && !str.isEmpty());

public final static StringDecider STRING_NOT_EMPTY_ANON = new StringDecider() {

    @Override
    public boolean decide(final String str) {
        return str != null && !str.isEmpty();
    }

};

For all intents and purposes, STRING_NOT_EMPTY_LAMBDA and STRING_NOT_EMPTY_ANON have an equivalent function — they were simply defined using different syntax, which may or may not be implemented differently.

Conclusion

At the level of referencing, there is no distinction of whether an object was defined using a lambda or not. Therefore, for constants created using a lambda, you probably should use the same conventions you use for constants created in other ways for consistency's sake:

public final static int ANSWER_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING = 42;

public final static StringDecider STRING_NOT_EMPTY = str -> (str != null && !str.isEmpty());

If, for whatever reason, you and/or your team want to discern between the two, feel free to do so... but make sure to stay consistent.


In fact, lambdas and anonymous classes need not be implemented in the same way. However, their usage is identical once they are created (there are few differences such as in variable scope, but, once the object is created and assigned to StringDecider STRING_NOT_EMPTY, these differences are no longer relevant).

like image 80
errantlinguist Avatar answered Oct 28 '22 02:10

errantlinguist