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)