Re:Null Object Pattern in Swift
by eofster on Thursday November 12, 2015 @04:43AM
(#10552158)
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.
Re:Null Object Pattern in Swift
by sanderjd on Thursday November 12, 2015 @04:29PM
(#10556277)
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.
Re:Null Object Pattern in Swift
by Camillo on Thursday November 12, 2015 @02:49AM
(#10551838)
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.
Re:Null Object Pattern in Swift
by adamwk on Wednesday November 11, 2015 @08:55PM
(#10550806)
So now, instead of knowing why my access request failed, I just know that it did fail somehow, somewhere. The Null Object Pattern is barely better than runtime null pointer exception; instead of throwing an exception I'm tricking my client into thinking everything is fine.
Re:Null Object Pattern in Swift
by ashark on Wednesday November 11, 2015 @08:54PM
(#10550805)
Every time I read about monads I feel stupid, but it's not always for the same reason. Sometimes it's because I don't understand it. Other times it's because I'm pretty sure I do understand it but I can't see what the big deal is.
Re:Null Object Pattern in Swift
by cballard on Wednesday November 11, 2015 @09:24PM
(#10550897)
A monad in Swift terms is essentially just a container type with an initializer that lets you put something in it, and implements flatMap. In Haskell, the equivalents are return and >>=. You can do more with it in Haskell because Monad is essentially a protocol, so you can rely on monadic types having those functions defined in terms of them.
Array and Optional are examples. RAC's SignalProducer is... maybe one? Maybe three?
Re:Null Object Pattern in Swift
by runT1ME on Wednesday November 11, 2015 @09:19PM
(#10550882)
Ah. The big deal is some languages can abstract over the idea of an arbitrary monad. You are correct though, most monads are not a big deal and fall into the "oh neat that's useful" category, and not the "omfg" one.
The abstracting over them is pretty mind blowing though.
Re:Null Object Pattern in Swift
by arcticbull on Wednesday November 11, 2015 @09:23PM
(#10550895)
Agreed completely. The null object with inheritance is particularly difficult to deal with because you're just asking to forget to nerf something dangerous in the superclass leaving you with partially functional objects. Optional<T> is the right way to handle this.
Re:Null Object Pattern in Swift
by danappelxx on Wednesday November 11, 2015 @08:59PM
(#10550824)
I agree. Perhaps it makes the code look cleaner on the outside, but ultimately it makes it harder to maintain. Also, optionals have loads of features (ex: chaining) to make your code much cleaner, though the author doesn't go into that.
It's a neat idea but I don't think it's as good as optionals.
Re:Null Object Pattern in Swift
by jheriko on Wednesday November 11, 2015 @09:14PM
(#10550868)
i can see why this might be useful in reducing the amount of code typed, but it makes things harder to read and debug by moving the null check somewhere hidden.
i guess its something you could get used to though...
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.
Array and Optional are examples. RAC's SignalProducer is... maybe one? Maybe three?
That's really all there is.
The abstracting over them is pretty mind blowing though.
There's some saying about Java "design patterns" that can just be replaced with closures. There should probably be another for for ADTs.
It's a neat idea but I don't think it's as good as optionals.
Why would you ever want to maintain such code when the language already solves all that for you with optional chaining and flatMap?
i guess its something you could get used to though...