Search code examples
javalambdacoding-stylenaming-conventionsnaming

What is the convention for variable names in lambda expressions when the variable is not used?


I'm trying to find a name similar to what i,j are for loops, or x,y is for coordinates etc.

I have a code like this:

DbSetup.setupCommon(x -> HibernateHelper.addResource(SpecificEntityHelper.HIBERNATE_RESOURCE, schemaName));

In this case x is not a required variable name and I found it is used in few places in code base of our project, so probably is Of course in this specific case I can not use a static reference to the function HibernateHelper::addResource and it sounds like there is no other way to not to have a name of the variable at all.


Solution

  • Currently, Java syntax doesn't provide a way to have no identifier at all if the argument is to be ignored.

    A single _ may serve that purpose in the future, but as of Java 16 _ is just a keyword that is "reserved for possible future use in parameter declarations."; see JLS 3.9.)

    It is inadvisable to use $ because all identifiers that contain $ are reserved for use by source code generators or for legacy purposes; see JLS 3.8

    Also, there isn't an established conventional name for a dummy argument to a lambda expression.


    My advice would be to just use a single letter identifier; e.g. x. A lambda expression will typically be small enough that you can easily see that (say) x is not used in the expression.

    Alternatively, you could pick a name like unused or dummy or ignore to flag your intent to not use the value.