- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
And we won't mention that the "functional" version contains a ternary, I guess.
Admin
That's why I thought. Are we missing something, or is it really that bad?
Admin
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.
Admin
Java lambdas still look awful to someone like me used to sleek C# and dare I even say C++.
Admin
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.
Admin
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.
Admin
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.
Admin
If you abuse an abuse, is that a good thing?
Admin
I'd like it more if you could pass the result as a callback without having to wrap it up in std::function
Admin
This is another example of buzzword thinking: "functional programming is cool, therefore we use functional programming". The result is as usual very dysfunctional
Admin
functional programming...dysfunctional. Good one Mr. TA :)
Admin
Some people just love Rube Goldberg machines
Admin
Got to love the passive-aggressiveness of .orElse, though.
Admin
Optional abuse is far preferable to mandatory abuse…
Admin
Optional? Optional? What is this "optional" malarkey?
That which is not compulsory is forbidden.
Admin
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.
Admin
It's called
Consumer<>
orRunnable
, depending on whether you pass an argument.Admin
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.