Re:Null Object Pattern in Swift
by panic on Wednesday November 11, 2015 @08:39PM
(#10550760)
Why not use optional chaining? https://developer.apple.com/library/ios/documentation/Swift/...
[ Parent
| on HN ]
I now see that my examples are oversimplified. I also don’t think that the Null Object pattern is the ultimate solution for handling nulls. I just found it useful in certain cases.
What I wanted to show here among other things and why optional chaining would not help is if we wanted to have a special case handling on failure. Optional chaining fails silently. So if we wanted to log an error or to terminate the app every time the request fails, we'd have to do it on every function call.
Re:Null Object Pattern in Swift
by andrew_mason1 on Wednesday November 11, 2015 @11:40PM
(#10551338)
I fail to see how optional chaining, Maybe monads (or Optionals, depending on your persuasion), or the Null Object pattern are fundamentally any different from each other.
In languages with good type-checking and optional / maybe types (like Swift, Scala, Rust, Haskell, …), the null object pattern seems less useful, because the compiler makes sure you unwrap things in the right places. But the null object pattern seems really useful in languages like Ruby and Python, where attempting to use optionals tends to just replace null object errors with errors from forgetting to unwrap the value.
Well, the Null Object Pattern is clearly the worst of the bunch, since it requires a whole lot of extra code to set up. It also introduces maintenance issues since you need to keep adding methods to it. Basically, it's a horrid idea to use in Swift.