Your IDE will probably warn you if you fail to use SNAKE_CASE_CAPS like any other static final variable.
Nothing I could find from Oracle suggests that you should treat a lambda differently from any other type, in this respect.
I failed to find any examples in the JDK source. I found some examples in Guava that use SNAKE_CASE_CAPS.
However teams and individuals can and do invent local conventions (for example, some projects use snake_case_lower_case_for_test_methods_in_jUnit()), and if you feel it's useful, you could adopt a local convention in your case.
I personally feel that:
myStream.map(MY_FUNCTION) feels a bit ugly.
- declaring lambdas at the top of the file, among the other variables, feels a bit ugly.
I feel static final functions should be "sort-of peers" to methods, capitalised and placed accordingly, so it feels more natural to me to have something like:
private List<String> asStrings(List<Foo> foos) {
return foos.stream().map(fooToString).collect(toList());
}
private static final Function<Foo,String> fooToString = f ->
"Foo: " + foo.id;
... than to declare the function at the top of the file as FOO_TO_STRING. We usually write private methods below the public methods that use them. It seems reasonable to me to write private functions below the public methods that use them, too.
But note, there's a lot of "feels" in my justification here, which may or may not be enough reason.
I note that in the Oracle Java source, the standard code sidesteps this issue, by not using static variables when it could. For example, in Function.java:
static <T> Function<T, T> identity() {
return t -> t;
}
... returns an inline new Function object, rather than reusing a shared Function object stored in a class or object variable.
Note also that you can declare ordinary static methods and use them as functions, with the slight overhead of using this or the class name with :::
private static String slimify(String s) {
return "Slim says: " + s;
}
...
out = stream.map(this::slimify).collect(Collectors.toList());
(I don't currently get a compiler warning about using this in a static context)
final static Predicate<String> STRING_NOT_EMPTYversusfinal static Predicate<String> stringNotEmpty. This underlines that it's a question inspired by Java 8's addition of lambdas.static finalvariable. I failed to find any examples in the JDK source. I found some examples in Guava that use SNAKE_CASE_CAPS. I myself feel that a local convention in which you do different would be OK, becausemyStream.map(MY_FUNCTION)feels ugly. I feel static final functions should be sort-of peers to methods, and capitalised accordingly. But that's just me.STRING_NOT_EMPTY.decide("Example"), but that’s because it makes no sense to create a function object via lambda expression, just to call directly a method on it. The natural way is to write a methodstatic boolean isNotEmpty(String str) { … }and callisNotEmpty("Example"). Then, if you need a function object, e.g. of typeStringDeciderjust useContainingType::isNotEmpty. No sane programmer would callString.CASE_INSENSITIVE_ORDER .compare(s1, s2); the natural way is using an ordinary method, i.e.s1.compareToIgnoreCase(s2)…ALL_CAPS_THING.method(foo);feels weird to write at all. Not if you've usedenums in earnest. Or pre-compiled regex patterns.