3 ms·
You'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 except
by bcheung 11y ago
You'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?