• (nodebb)

    Ternaries, of course, are "not readable", "they're confusing", they're "playing code golf", or "The Daily WTF has written so many articles about ternaries being bad!".

    And we won't mention that the "functional" version contains a ternary, I guess.

  • Hanzito (unregistered) in reply to Steve_The_Cynic

    That's why I thought. Are we missing something, or is it really that bad?

  • Java Apologist (unregistered)

    If you're gonna use Apache StringUtils, it's even simpler: this.taxCode = StringUtils.trimToNull(taxCode);

    I would personally skip the 3rd-party library and write a "better" functional version: this.taxCode = Optional.ofNullable(taxCode) .filter(string -> !string.isBlank()) .orElse(null);

    (Though I would question the use of empty strings as sentinels in the first place.)

    As usual, ternaries are fine and Java Streams/Optionals are (clunky but) fine.

  • MaxiTB (unregistered)

    Java lambdas still look awful to someone like me used to sleek C# and dare I even say C++.

  • (author) in reply to MaxiTB

    I like how C++ lambdas let you declare specifically what you want to enclose in the lambda. I still find the syntax a little awkward for whatever reason, but that's a cool feature.

  • IA (unregistered)

    There was no "ternary or optional" battle in the last example because the very same ternary was already there, he just removed the optional part.

  • (nodebb) in reply to Remy Porter

    I like how C++ lambdas let you declare specifically

    I like how they force you to declare specifically ...

    Anything that makes it hard to avoid explaining what you are doing and what exactly you want to do with it is good.

  • (nodebb)

    If you abuse an abuse, is that a good thing?

  • (nodebb) in reply to Remy Porter

    I'd like it more if you could pass the result as a callback without having to wrap it up in std::function

  • (nodebb)

    This is another example of buzzword thinking: "functional programming is cool, therefore we use functional programming". The result is as usual very dysfunctional

  • DaveD (unregistered) in reply to Mr. TA

    functional programming...dysfunctional. Good one Mr. TA :)

  • Дмитрий (unregistered)

    Some people just love Rube Goldberg machines

  • Alan (unregistered)
    Comment held for moderation.
  • löchlein deluxe (unregistered)

    Got to love the passive-aggressiveness of .orElse, though.

  • Nick (unregistered)

    Optional abuse is far preferable to mandatory abuse…

  • Bittle Tobby Lables (unregistered)

    Optional? Optional? What is this "optional" malarkey?

    That which is not compulsory is forbidden.

  • (nodebb)

    Recently migrated from C# to Java. I'm shocked at how few functional constructs exist in Java compared to C#. C# is basically caught up with F# in functional aspects. One example is that Java (pretty sure through Java 18) has Function<> but no Action<> like C# so you can't use a void method. The Optional construct mentioned above seems like a first pass at true Optional types C# has had for years. And having to add/reference a utility function for null or empty strings; C# has had String.IsNullOrEmpty forever.

  • (nodebb) in reply to bradwood

    One example is that Java (pretty sure through Java 18) has Function<> but no Action<> like C# so you can't use a void method.

    It's called Consumer<> or Runnable, depending on whether you pass an argument.

  • anonymous (unregistered)

    As an Android dev where Java 8 is standard and "some devices in common use still only support Java 7", the "many versions ago" line made me feel outdated.

  • Bonehead (unregistered)
    Comment held for moderation.
  • Desiree (unregistered)
    Comment held for moderation.

Leave a comment on “Optional Ternaround”

Log In or post as a guest

Replying to comment #:

« Return to Article