3 ms·
Here is what is wrong with Option. option.ifPresent(x -> System.out.println(x)); So instead of just checking to see if it is NULL you want me to create an ins
by bcheung 11y ago
Here is what is wrong with Option.
option.ifPresent(x -> System.out.println(x));
So instead of just checking to see if it is NULL you want me to create an instance of a specialized class that holds my variable that has a method that acts like an "if" statement that I need to pass an anonymous function to that receives the value I already have?
Why not just do:
x && System.out.println(x)
or:
System.out.println(x) if x
Or if you want to skip over the rest of the logic if it is null:
return unless x
System.out.println(x)
I don't see why I need to introduce a new type system and complicate things.
For default values you want me to create an instance of specialized class that holds my variable and has a method that allows me to get my value or return a default value?
option.orElseGet(5)
So instead of a memory lookup I now need the overhead of an entire function call?
Why not just do:
x ||= 5
Or better yet, just put it in your function declaration as a default value:
myFunc = (x=5, y, z) -> ...
The one benefit I see is type safety but it is extremely rare for experienced programmers working in dynamic languages to have bugs related to type.
You are layering on abstractions and forcing programmers to go through hoops just to get at their data. It is more complicated and increasing the cognitive load.
There's also the fact that you are going through functions and classes instead of just memory accesses. This makes code less performant as well.
- dragonwriter 11y ago> The one benefit I see is type safety but it is certainly not easier Type safety is one benefit, but composability is an important benefit (as, you know, is extensively discussed in the source article.)
- bcheung 11y agoComposability comes from functions, macros, and inheritance. What does that have to do with types? "In fact, composibility is really the fundamental issue behind many of these problems. For example, the Store API returning nil for non-existant values was not composable with storing nil for non-existant phone numbers." That's only because you invented some abstraction that got in your way in the first place. NULL is a perfectly acceptable value in dynamic languages and even in most document store databases.
- paulddraper 11y agoThis is not contrived. (1) A cache returns something or NULL. It's generic; i.e. implementation doesn't care what it is storing: integers, strings, etc. (2) A particular value of interest may be NULL or non-NULL. But now I can no longer use my generic cache and my values of interest together. A very real example of this is Java's Map interface. It's completely up to implementation on how they handle null, making interpreting null results difficult. While often you use NULL and everything works, you're still limited to a certain set of circumstances. You can't pick up thing A and thing B and just use them together. Hence non-composable.
- bcheung 11y agoYou're conflating the meaning of NULL in that case. That's not a problem of NULL, you're using NULL to represent multiple different things. You can use exceptions to handle that scenario. cache = {} getFromCache = (key) -> return cache[key] if cache[key]? throw "not found" doSomethingWithValue = (key) -> try value = getFromCache(key) catch value = getValueFromNonCache(key) return _doSomethingWith(value)
- dragonwriter 11y agoYou can, but resorting to exceptions for what may not be an exceptional circumstance is a workaround for not being able to think of a way to communicate results correctly through the return value. Which illustrates the need here as effectively as the ambiguous use of null in the article.
- bcheung 11y agoYou don't like exceptions so your solution is creating an instance of a specialized class that holds my variable that has a method that acts like an "if" statement that I need to pass an anonymous function to that receives the value I already have?
- justthistime_ 11y agoI think your failure to use Option correctly cannot be blamed on Option alone, given that many people use it successfully.
- bcheung 11y agoI think your failure to use NULL correctly cannot be blamed on NULL alone, given that many people use it successfully.
- justthistime_ 11y agoThe whole problem with NULL is that practically no one can use it correctly.
- paulddraper 11y ago> it is extremely rare for experienced programmers working in dynamic languages to have bugs related to type That's a good one. I really needed that ;)