This topic is for the [Null Object - Part Two] exercise in the [Refactoring] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Null Object - Part Two]: https://exercises.upcase.com/exercises/null-object-part-two
[Refactoring]: Refactoring Code | Code Refactoring Online Tutorial by thoughtbot
When I initially ran rake
I was receiving this error:
/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.0.2/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now
.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:644:in `block in expect_with': uninitialized constant RSpec::Expectations::MultipleExpectationsNotMetError (NameError)
To fix this I updated RSpec by running $ bundle update rspec
I then was getting two initial failing specs. To fix this:
- In
spec/user_spec.rb
I changedtrue
totruthy
andfalse
tofalsey
as follows:expect(user.has_mentoring?).to be_truthy
expect(user.has_mentoring?).to be_falsey
After that all tests were green.
Hey harry!
Iâve been wondering how to technically solve the problem of creating new Null object instances within the following code setting:
def subscription
@subscription || NoPlan.new
end
What is best practice to prevent creating a new NoPlan-instance everytime querying subscription
?
The point is to return an instance that conforms to @subscriptionâs api so that the consumer doesnât need to know they belong to different classes. so Iâd say you donât need to worry about returning NoPlan instances everytime in that particular snippet.
Sure, I was only wondering about memory-consumption. Do I have to be careful in a real-world setting, like a heavy-traffic-web-App with lots of requests spawning new null object instances or is that - in practice - a minor problem compared to other rails-memory-issues?
I donât think itâs a problem at all. In such scenario, you will have other types of problems such as slow response times and issues on other parts of your stack such as databases (your sql database, redis, etc.)
If you think about it, an object oriented language itâs all about objects, so you shouldnât need to worry about it.