Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Convention for Null Objects in Rails?

For rails applications: does there exist a naming convention for naming your null objects that are mapped to model objects?

Example:

#app/models/blog.rb
class Blog < ActiveRecord::Base
end

#app/models/null_blog.rb
class NullBlog # Null Objects are POROs, correct?
  def title
    "No title"
  end
end

# so to implement it I can do this:
blogs = ids.map{|id| Blog.find_by(id: id) || NullBlog.new}
# which allows me to do this and it works, even if some of those ids did not find a blog
blogs.each{|blog| blog.title}

Is NullBlog conventional?

Neither this article by Avdi Grimm nor the Nothing is Something presentation by Sandi Metz mentioned any null object naming convention.

like image 408
Neil Avatar asked Oct 11 '15 08:10

Neil


1 Answers

There is no mainstream convention as of now. Likely because the Null Object Pattern itself is not seeing widespread use yet. The only reference to a naming guideline that I have come across comes from the ThoughBot Style Guide, which uses it as an example of a naming rule:

Prefer naming classes after domain concepts rather than patterns they implement (e.g. Guest vs NullUser, CachedRequest vs RequestDecorator).

In your case, it would be a matter of finding the "domain language" for a "null blog." The best method I've found for finding these terms is to simply talk to domain users about the topic, and note down any nouns they use. This approach was outlined in the book Object Thinking.

like image 181
Drenmi Avatar answered Oct 25 '22 11:10

Drenmi