Last active
September 4, 2024 09:39
-
-
Save ghn/aa0cd2c97e52b291e9349b82add85438 to your computer and use it in GitHub Desktop.
What should be the value returned by a service?
This file contains hidden or 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
# A - only returns success true/false | |
module DocumentService | |
class Create | |
def self.call(a, b) | |
if process_works? | |
true | |
else | |
false | |
end | |
end | |
end | |
end | |
# B - raise exception if fails, return something else (what?) if it works | |
module DocumentService | |
class Create | |
def self.call(a, b) | |
raise NotFoundError if a.nil? | |
a.whatever(b) | |
a | |
end | |
end | |
end | |
# C - returns a tuple [status, result] | |
module DocumentService | |
class Create | |
def self.call(a, b) | |
a.something(b) | |
[:ok, a] | |
rescue Exception => exception | |
[:error, exception] | |
end | |
end | |
end | |
# D - returns nothing but provide methods to extract the result | |
module DocumentService | |
class Create | |
def call(a, b) | |
@result = process(a,b) | |
end | |
def result | |
@result | |
end | |
def success? | |
@result.present? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment