• Optional<Comment> (unregistered)

    return comments.stream().findFirst(Comment::isFrist);

    I don't agree that a lambda is less readable than a Java method reference, actually. A lambda clearly shows you the parameters, a method reference requires you to be familiar with the method to understand it. Though in this case neither is hard to read.

  • Naomi (unregistered)

    That would be comments.stream().min(Comment::postDate), but if your data structure forces you to do an O(N) operation just to find the oldest, it's time to refactor. If comments is a List you would just do comments.isEmpty() ? Optional.empty() : Optional.of(comments.get(0)).

    And to nitpick the article, Collection#stream() returns a Stream. A StreamBuilder is a pseudo-collection that you can add values to before streaming it.

    (Interestingly enough, lambdas are sugar for method references, not the other way around - when you use a lambda, the compiler generates a method and takes a reference to it. You can see this most easily in a debugging session, or in a stack trace. The more you know!)

  • Hal (unregistered) in reply to Optional<Comment>

    I agree lambda vs method reference each have merits here. Honestly it comes down to an aesthetic choice IMHO. I would probably do whatever is most consistent with the existing code base.

  • podlesh (unregistered)

    The Optional deconstruct-construct command is basically just the good old "return booleanValue==true" for 21st century.

  • (nodebb)

    I get the feeling that this is the result of a method that originally returned a CachedSession rather than an Optional of a CachedSession. In my mind the way this played out was:

    1. return theSession.get()
    2. "oh hey the linter says I need to check isPresent() before calling get()"
    3. if theSession.isPresent() return theSession.get() else return null
    4. during code review the reviewer says "hey let's make just return an optional so that the caller knows it might be null"
    5. "oh it works now how do I make this return an optional without breaking my code"
    6. we get today's WTF
  • Jezor (unregistered)

    A lambda clearly shows you the parameters, a method reference requires you to be familiar with the method to understand it.

    Parameters are just an extra name you need to keep track of. If the method is named properly, you should be able to understand it as is.

  • ravs (unregistered)

    This is the modern, fancy equivalent of the traditional

    Boolean bSucceeded = doSomething(); if(bSucceeded == true) return true; else return false;

  • (nodebb)
    Comment held for moderation.
  • Sole Purpose of Visit (unregistered) in reply to Jezor

    Or, you could just think of the "named method" as a "lamdba" and hoist the relevant parameters in from the current scope. I admit, you might have issues with Java native types (unboxed), but only if you want them to be in/out variables, which is not necessarily a good idea to begin with. The issue of "well-named functions" (such as, say, FactoryFactoryFactory) disappears if your lambda is declared locally, which of course it will be.

    I'll accept that an external (descriptively named) function is a good idea if that function is called from all over the place. In which case I would use the moral equivalent of () => WellNamedFunction(). Why Java felt the need to introduce the Class::Function syntax is beyond me, except that it sort of looks like a C++ method pointer, which might make programmers transitioning from C++ to Java happier.

    (On another note, I love optionals, or maybes, call them what you will. Unlike with nulls, it takes actual effort to ignore the absence of a result. One good thing about today's WTF is that, ugly as it is, it doesn't ignore the null result.

  • Alex (unregistered)

    Aren't Java optionals completely pointless since they can be null anyways?

  • (nodebb) in reply to Alex

    Any method that returns a null Optional is highly broken. Fix it.

  • Krzysztof Rudowicz (unregistered)
    Comment held for moderation.
  • Naomi (unregistered) in reply to Alex

    You're correct that the compiler doesn't stop you from assigning null to an Optional value, and yes, that's suboptimal. But saying that that makes them pointless is overstating it. Even a well-written, not at all WTF-y library may have reason to return null from a Widget-valued function. The same is not true if the function returns Optional<Widget>s. So, it's still legitimate to assume they aren't null unless you have to interact with deeply WTF-y code.

  • gnasher729 (unregistered)
    Comment held for moderation.

Leave a comment on “Checking Your Options”

Log In or post as a guest

Replying to comment #:

« Return to Article