Sometimes the null-object pattern is a good pattern: for example, in views, when you need to show default text when an object is missing.
But the rest of the time, it is a bad pattern, or an anti-pattern.
I think that pushing the null-object pattern into ActiveRecord is a mistake. It is not an active-record concern; it is not a domain-model concern; it is a view concern only, a “last-mile” concern if you will.
This pattern sometimes appears to solve a problem of code which could be made more concise, if only ________.
But when actually applied consistently and automatically throughout a codebase at the domain-model layer, it will create more problems than it solves, and the problems it creates will be worse than the problems that it solves.
The following visceral analogy seems to me to apply: it is like solving the problem of a stubbed toe by hacking off your whole foot.
You can of course use null-objects explicitly in your views, without pushing them into ActiveRecord. And there are many other solutions too.
I disagree strongly with the assertion that null object is a view
concern only. Null objects are useful anytime you want to provide
default behavior in the absence of a specific result. Views tend to
have lots of these cases, but there are plenty of other places they
can to be useful as well, including in your models and in your domain.
I confess, I'm having trouble thinking of an example where I'd rather
receive nil from sending a message to which I expect a response than a
null object or false.
For what it's worth, if ActiveRecord introduced a null object option
for belongs_to and has_one associations, I would always use it.
The OP was asking for feedback, I believe they’ve gotten it. If they want to discuss it further they can make a PR, and the discussion can be moved there. Thanks to all who participated, as a reminder there are 478 open issues and 172 active and open pull requests that could use similar attention: https://github.com/rails/rails/issues?state=open.
I guess we have some from the Core team here, so either in a pull request I think we will not accept, so please don’t open it. As you pointed we have a lot of open pull requests and we are trying to no leave feature proposal / requests there and we always ask to move the discussion to here.
I have the same opinion of Aaron. We don’t need support for Null object in Rails since you can do with plain Ruby.
And thank you to remember that there are a lot of open issues, Richard. You are helping a lot.
Like Rafael and Aaron said, they think it does not belong in rails.
Although I disagree, I appreciate the feedback and definitely happy I took the time to ask for it here before opening a pull and taking more attention from the core team for something that was not going to get accepted anyway.