News for hackers. Stuff that matters.
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @01:52PM (#13474293)
It's much easier to write robust code in Java - with it's Optional type, and checked exceptions - than in C#.

You probably would never write this, and your colleagues would never write this. That reasoning is good to apply in many situations. I in fact do this all the time; it’s a huge part of my research agenda. But you have to be careful where you apply it. Soundness is a security guarantee, not a usability concern. What matters is if someone can write this, because then that someone can get around the security measures that people have placed their confidence in and do whatever they would like to do. In the case of soundness, it’s the malicious programmer you should be worried, not just you and your friends.

I really hate this attitude. Programming languages should be designed to be safe and strict by default, but with easy outs. I utterly reject the notion that we should design languages first and foremost to protect us from people can't program that well or aren't careful when they write code.

Do we always have to appeal to the lowest common denominator? Empower the developers that care rather than trying to turn this into an unskilled profession.

Re:Java Is Unsound: The Industry Perspective by dsp1234 on Tuesday January 24, 2017 @02:06PM (#13474427)
with it's Optional type,

C# has Nullable<T> (aka T?)[0]. I'm not a Java expert, but they appear to be the same (represent a value or a null value, allow you to inspect for the presence of a value or null).

ex:

  int? x = null;
  int y = x ?? 0;
  int z = x.HasValue ? x.Value : 0;
  if(x.HasValue && x.Value == 42){}
C# definitely doesn't have checked exceptions.

edit:

For reference types, as compared to value types, c# has the null coalescing operator

  List<string> y = someList() ?? new List<string>;
and the null conditional operator

  int? length = customers?.Length; // null if customers is null
which are defintely not Optional, but are what I use.

[0] - https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx

Re:Java Is Unsound: The Industry Perspective by sremani on Tuesday January 24, 2017 @02:20PM (#13474554)
Yes, Nullable was introduced in 2005 with C# 2.0 if memory serves me right.

Checked Exceptions was a design choice, http://www.artima.com/intv/handcuffs.html

Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @05:10PM (#13476164)
Nullables work at a pinch, but they are a poor mans option type.

http://fsharpforfunandprofit.com/posts/the-option-type/

Scroll down to "Option vs. Null vs. Nullable" for an explanation of the differences.

Re:Java Is Unsound: The Industry Perspective by jpfed on Tuesday January 24, 2017 @02:23PM (#13474585)
I'm unfamiliar with Java's Optional type, but the .NET type Nullable can only be parameterized over value types. It is there to add the concept of null to value types, not allow you to more precisely characterize the nullability of reference types.
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @02:48PM (#13474812)
Optional in Java is just a library. If you want to to use Optional in C#, there is no reason to believe that that is not possible due to some issue that makes it only available to Java. https://github.com/louthy/language-ext/tree/master/LanguageE...
Re:Java Is Unsound: The Industry Perspective by jpfed on Tuesday January 24, 2017 @03:14PM (#13475033)
I didn't imply it was desired or wasn't possible...? Another reply to my comment clarified that Optional also only applies to value types, which makes it analogous to Nullable. There's no reason to port it (as opposed to any other implementation of an Optional type) over.

What is really necessary is an equivalent for reference types.

struct Maybe<T> where T : class {

  private bool _isInitialized;
  private T _data;

  public Maybe(T data) {
    _isInitialized = (data != null);
    _data = data;
  }

  // ...etc
}
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @03:23PM (#13475106)
> Optional also only applies to value types

no - Java's Optional applies to reference types.

Re:Java Is Unsound: The Industry Perspective by dsp1234 on Tuesday January 24, 2017 @03:36PM (#13475226)
Yes, I was misreading the documentation. The Optional class itself is to be considered a value type, and reference equality on Optional objects shouldn't be done.
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @05:06PM (#13476115)
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?
Re:Java Is Unsound: The Industry Perspective by tigershark on Tuesday January 24, 2017 @06:35PM (#13476862)
Only 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)
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @07:00PM (#13477050)
How 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".
Re:Java Is Unsound: The Industry Perspective by tigershark on Wednesday January 25, 2017 @04:04AM (#13479891)
The null check operator is much more concise than my pre-c# 6 solution. Moreover you can apply both my solution and the null check operator to any type, while for Java you have to specify that a field is optional (increasing the verbosity and moving the responsibility to who write the class) and you can't do it for types that you don't own. So for these types the only way is to create a wrapper type just to add the optional fields, and this brings the amount of boilerplate code through the roof. So, just to sum it up, contrary to what you say the c# solution works out of the box, is much shorter than the Java alternative and works for every type. Honestly I can't really see why Java solution is superior for you.
Re:Java Is Unsound: The Industry Perspective by vile on Tuesday January 24, 2017 @10:38PM (#13478249)
It'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.

Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @11:37PM (#13478569)
Except 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.
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @05:18PM (#13476244)
this 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#."
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @09:11PM (#13477827)
Why 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#.

Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @09:21PM (#13477872)
You 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.
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @11:16PM (#13478461)
Look 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.
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @11:22PM (#13478489)
Again, you are arguing a different point than than one I was addressing.
Re:Java Is Unsound: The Industry Perspective by dsp1234 on Tuesday January 24, 2017 @02:52PM (#13474845)
edit: this is wrong, I was misreading the documentation, which states that the Optional class is a value type, not the contents of the Optional class.

According the documentation, Optional is only valid with value types[0]:

"This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided."

Again, I have no experience, so if this is wrong, then additional corrective references would be awesome.

[0] - https://docs.oracle.com/javase/8/docs/api/java/util/Optional...

Re:Java Is Unsound: The Industry Perspective by solidsnack9000 on Wednesday January 25, 2017 @03:28AM (#13479733)
> I really hate this attitude. Programming languages should be designed to be safe and strict by default, but with easy outs. I utterly reject the notion that we should design languages first and foremost to protect us from people can't program that well or aren't careful when they write code.

I don't think the author has a beef with easy outs, but with accidental outs. Specifically with regards to the JVM, too, there are some practical security ramifications, with regards to running plugins.

Re:Java Is Unsound: The Industry Perspective by tigershark on Tuesday January 24, 2017 @05:56PM (#13476561)
Thanks god c# doesn't have checked exceptions, they are just an enormous pain, especially when heavily using actions and functions, and most of the time they are completely ignored, rendering them completely useless. The option type in Java is extremely verbose and doesn't guarantee that you won't have null pointers. In c# you can use the null conditional operator and the coalesce operator that at least are much less verbose and more elegant. The best solution is obviously to use a functional language like f# where you can't have null pointers.
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @07:14PM (#13477146)
> The best solution is obviously to use a functional language like f# where you can't have null pointers.

You absolutely can:

    Microsoft (R) F# Interactive version 14.0.23413.0
    Copyright (c) Microsoft Corporation. All Rights Reserved.

    For help type #help;;

    > let s:string = null ;;

    val s : string = null
Re:Java Is Unsound: The Industry Perspective by tigershark on Wednesday January 25, 2017 @04:11AM (#13479922)
String is a .net type, f# types cannot reference null unless you explicitly add some attribute if I recall correctly.
Re:Java Is Unsound: The Industry Perspective by pswenson on Tuesday January 24, 2017 @03:33PM (#13475191)
checked exceptions? They lead to very un-robust code like try{...}catch(Exception e){//ignore or log, continue along like everything is grand!} or (much better) every method declaring "throws Exception"
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @05:03PM (#13476089)
Sure, it may lead to that kind of code if you have mediocre programmers who don't care about robustness - absolutely. But in C# it's much harder to keep track of all the exceptions that might be thrown.

Now, if a programming language has error aware return types - using algebraic data types or multiple dispatch - that's a better solution than both. But in the absence of that, give me checked exceptions over unchecked ones any day.

Re:Java Is Unsound: The Industry Perspective by orf on Tuesday January 24, 2017 @03:28PM (#13475155)
We don't always have to appeal to the lowest common denominator, but languages that do appeal to more people. Hence popularity.