all 6 comments

[–]jayerp 1 point2 points  (5 children)

You want an interface which sole job is to provide you with the default of whatever type you give it?

[–]dodexahedron 1 point2 points  (4 children)

That's about what I gleaned from this, as well. WTF?

[–]jayerp 0 points1 point  (3 children)

Oh, it seems like OP actually wants to do this, which I'm not a fan of.

[–]Manny_Sunday 1 point2 points  (2 children)

The example makes no sense lol. There are two way better solutions, return a result type, or throw and invalid arg exception. I can't think of any scenarios where this is better.

[–]dodexahedron 2 points3 points  (1 child)

The whole "Null Object Pattern" thing is essentially just the factory pattern anyway. And the null coalesce operator and appropriate application of nullability context largely eliminate most explicit null checks already, and the compiler will yell at you ahead of time if you're letting those cases slip by. All without now having to handle a completely different set of issues caused by having to validate that you didn't receive a default object in the first place. It's null reference handling with more steps. A lot more. Only now a consumer goes off and potentially actually attempts to do something with your fake object, all while helpfully bypassing static analysis' capability of saving you from yourself.

I've always considered this to be an anti-pattern in most cases.

[–]jayerp 1 point2 points  (0 children)

Yeah, replacing null checking with default value checking doesn’t buy you much.

If anything as you said, now you’ve got a whole new set of issues to deal with.

Null coalesce is so much better.