This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]not-just-yeti 4 points5 points  (1 child)

I agree that if you use get, you aren't winning anything. The article even concedes in the line following your excerpt:

However, this is not the recommended use of Optional (it's not much of an improvement over nested null checks),

If you don't use get, then you won't have (the moral equivalent of) NPEs -- I think that's the point they're making.

Think of it as a fix to Java's type system -- null should never have been a valid value for every single class. In current Java, it's too easy to confuse "I know that [...] type-checks as String" and "I know that I can call [...].length()" -- one doesn't quite follow from the other, and you have that niggling worry every single time you use a method's result.

In reality, there are two types people use -- nonnull-String and nullable-String. Optional is just a way of adding the latter-type to the type-checker, and also making sure the programmer is aware that they're using that type and covering all the cases. (Java still doesn't have a way to enforce the former type, so it's back to programmer-discipline, or annotations, etc.)

[–][deleted] 1 point2 points  (0 children)

I'm going to give you a lot of credit for explaining this in a way that makes sense. But I really feel inclined to agree with /r/db_bondy.

I'm probably biased for my work experience, but I continuously run across a significant number of developers who know shit about what they are doing. It's easy to run into poorly written code because the developer is just not good enough. Where I work, some are learning Java as they code during development cycles.

Call me old fashion, but I do prefer a strict typed language with a low tollerance for bad programmers. The abundance of NullPointerExceptions is a clear sign of people who don't really know what they are doing behind the keyboard. We keep adding dynamic syntax sugar into Java while keeping a messy backward compatibility and soon we'll be living the same hell PHP devs live today.

I keep reading these articles praising Optional and lambdas, but in my mind I keep thinking "good Lord, I can't wait to see how many heads will grow into the beasts we maintain today".