Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Are there any downsides related to using Maybe or Optional monads instead of nulls?

Parent

Are there any downsides related to using Maybe or Optional monads instead of nulls?

+8
−0

I have recently stumbled across the Maybe (or Optional) modal usage in .NET Code:

Based on everything I read, there are multiple advantages on relying Maybe instead of nulls:

I have also studied the implementation and usage of Maybe monad usage and all the code seems more "fluent" since there is no need for null checks (null propagation might help with this though).

What makes me wonder is the fact that I have never seen this being used in enterprise projects I have worked on and not being mentioned by any of my colleagues. Are there any downsides in switching to using Maybe instead of the regular nulls?

The only thing that comes into my mind is the need to ensure conversions at the service boundaries (e.g. the client might like nulls instead of complex objects) and conversions when using ORMs (databases still work with NULLs or similar). However, these are part of the "infrastructure" code which you write only once per service.

History

0 comment threads

Post
+4
−0

This is mostly an addition to r~~'s answer which I mostly agree with. This elaborates on the "non-idiomatic" part a bit.

Modern Java code doesn't have a problem using Optional. Why? Because Optional is part of the standard library and has been since Java 8 (which is when it would have made most sense to add such a type, though it would have been technically possible [and Guava did so] in Java 5).

For C#, which doesn't (yet) include an analogous type in the standard library, it feels strange to pull in a library just for this one simple type, and many libraries including it also include and push a much more opinionated and "functional" set of code. The alternative is to make your own type which is easy but perhaps too easy. It feels like something that should be in the standard library (because it should), and it also has a high risk of being incompatible with a future standard library version. (For example, Guava's Optional is not compatible with Java 8's Optional.)

All this said, there is absolutely nothing particularly "functional" about Option/Maybe/Optional. It's simply a container that can contain at most one element. These types are obviously popular in (typed) functional languages, though mostly because most of those don't have any analogue to null. Similarly, there is absolutely nothing "OO" about null. In fact, it violates various pillars of object-oriented design. Using Optional makes more sense from an OOD perspective than using null does. I will admit that a concise syntax for lambdas helps, but 1) Smalltalk which is often considered an exemplar of OOP has always had "blocks" which are roughly a concise lambda syntax, and 2) the lambda ship has already sailed since C# 2.0 and Java 8.

So my main disagreement with r~~ is I see no need to "go all in" in a "high functional" style to use Optional. Guava and Java 8 and most of their users certainly didn't. These were certainly influenced by ideas popular in functional programming (FP), but none present themselves as "functional" or "go all in". And, again, many "functional" things added, e.g. the immutable collections of Guava, are in no way specific to FP nor contradict OOP. Indeed, again, good OOD should lead you toward many of these, e.g. an ImmutableList<A> is (conceptually but truly) covariant in A, while a List<A> is invariant. (C# has the hideous array covariance because people expect this kind of covariance because they often think of arrays as values, but mutable arrays lack this covariance. If C# separated read-only (i.e. immutable) and read-write array types, this hack would not have been necessary.)

History

1 comment thread

Java has one big problem with Optional: Optional values are also nullable. So you have null, Optional... (2 comments)
Java has one big problem with Optional: Optional values are also nullable. So you have null, Optional...
user253751‭ wrote over 1 year ago

Java has one big problem with Optional: Optional values are also nullable. So you have null, Optional.empty(), and Optional.of(whatever). Instead you could write @Nullable Whatever and simply have null and whatever. And this would work even if the compiler doesn't understand the @Nullable annotation.

Derek Elkins‭ wrote over 1 year ago

This is not a problem with Optional. It's a problem with null. You could just as well say that Java has a problem with Integer: there are the integers but also null. There are many well-known ways in which null doesn't accomplish the same thing as Optional and is worse, e.g. nested Optionals and being able to use methods such as someOpt.equals(someOtherOpt). In fact, being not equal to null is one of the reasons to use Optional. For example, if looking up a value in a collection returns an Optional<T>, then you can distinguish between Optional.of(null) and Optional.empty(). Related to this, and from a more software engineering perspective, null doesn't communicate intent. If I get a NullPointerException deep in some code, was that a valid value that I failed to handle or was that the detritus of some broken code elsewhere? In my opinion, it's best to write your code in such a way that a null value is always indicative of a bug.