3 ms·
And in Java they're in the the standard library. Which is going to go down better - simply using something official that every java library program will already
by lacampbell 10y ago
And in Java they're in the the standard library. Which is going to go down better - simply using something official that every java library program will already have, or trying to explain to your colleagues why all your methods are returning something defined in this obscure library you added to all your builds?
- platz 10y agothis is a different question than the premise you initially outlined "It's much easier to write robust code in Java - with it's Optional type... than in C#."
- lacampbell 10y agoWhy the pedantry? C# is my day job - I'd love to be able to use options but I would have a hell of a time getting it cleared. Your quote also omitted checked exceptions, which is another reason why it's easier than me to write robust code in Java than in C#.
- platz 10y agoYou seem to be making several arguments at once. My only point is that C# has no problem implementing java's Optional, which some other folks seemed confused about, and there are already libraries that do so. If there is a social/cultural issue too, I was not commenting on that, or commenting on the usefulness of checked exceptions.
- lacampbell 10y agoLook how many people here are advocating for the nullable hack because they don't understand what an option is. With java the discussion could be side stepped entirely, since they added proper support and put it in the standard lib.
- platz 10y agoAgain, you are arguing a different point than than one I was addressing.
- tigershark 10y agoOnly since Java 8. The coalesce operator is in c# since the version 3.5 or 4 I think and c# 6 introduced the null conditional operator that, as I said before, is objectively nicer than the verbose Java optional. Before c# 6 I used this simple extension method: public static TDest IfNotNull<TSource, TDest>(this TSource obj, Func<TSource, TDest> func) { return obj != null ? func(obj) : default(TDest); } Or something similar (writing c# code on the iPad before sleeping is not exactly simple), inspired by some blog post ages ago. Just adding these three line of codes in a static class in the solution will enable you to write something like this: var id = order.IfNotNull(o => o.Customer).IfNotNull(c => c.Id) ?? "INVALID" Still not as good as the null check operator but comparable with the Java optional (arguably better from my point of view)
- lacampbell 10y agoHow is it better? All you've done is re-implemented mapping over an option. Your C# code is this in Java: String id = order.map(o -> o.Customer).map(c -> c.Id).orElse("INVALID"); The Java code is actually shorter and works straight out of the box, so I don't know why you're calling Optionals "verbose".
- vile 10y agoIt's not shorter than out-of-the-box c# 6, which would be var id = order?.Customer?.Id ?? "INVALID"; I think that was the point he was making, that it's always been possible, and now is simple.
- lacampbell 10y agoExcept that nullable types aren't the same thing as option types, so it's not doing the same thing. Nullables are much weaker than options. You can't do this for instance: string? id = "Name"; While you can do stuff like this: int x = 0; int? y = x; Again, F# has nullable types and options, and the fact that no one in F# uses nullable types should tell you something.
- tigershark 10y ago