Created
December 16, 2014 17:16
-
-
Save andremedeiros/bbf423220d63de017721 to your computer and use it in GitHub Desktop.
Style poll
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
# Option 1 | |
info = if publication | |
"Title: #{ publication.title } (ID: #{ publication.id })" | |
else | |
'N/A' | |
end | |
# Option 2 | |
info = case publication | |
when Publication then puts "Title: #{ publication.title } (ID: #{ publication.id })" | |
when nil then puts 'N/A' | |
end |
one funky style i've seen:
info = pubication
? "Title: #{ publication.title } (ID: #{ publication.id })"
: "N/A"
I kinda like how the operators align it but it takes a while to parse in your head if you are not used it. Also I think ruby syntax won't let you do it.
for option 2 I also find the indentation a bit weird. Maybe if the when
statements where indented or even aligned with the case
.
def publication_caption(publication)
return 'N/A' if publication.nil?
"Title: #{ publication.title } (ID: #{ publication.id })"
end
info = pubication_caption(publication)
Actually long term in such cases I would use the null object pattern (see https://github.com/avdi/naught) and define #to_s on both NullPublication and Publication.
From your options however, the first one seems a lot more readable, if the only case you need to address is NIlClass.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how about a third option...
info = publication ? "Title: #{ publication.title } (ID: #{ publication.id })" : "N/A"