Last active
February 8, 2016 11:23
-
-
Save the-undefined/fd50c0a9cf8e838da15f to your computer and use it in GitHub Desktop.
NullObject VS GuardClause (http://joejames.io/post/an-alternative-for-guard-clauses-that-do-type-checking)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of a guard clause performing a nil check | |
class BlahSpeaker | |
has_one :audience | |
def speak | |
return if audience.nil? | |
audience.address('blah ' * 100) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of `NullObject` pattern | |
class BlahSpeaker | |
has_one :audience | |
NullAudience = Class.new do | |
def address(words); end | |
end | |
def speak | |
(audience || NullAudience.new).address('blah ' * 100) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment