Created
October 30, 2014 16:26
-
-
Save JoshCheek/87a13eea70ef01701228 to your computer and use it in GitHub Desktop.
Refactoring an if statement for a student
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
# original | |
def shoot | |
if cranky? || laying? | |
"NO!" | |
else | |
@tired += 1 | |
"Twang!!!" | |
end | |
end | |
# ternary -- as requested | |
def shoot | |
cranky? || laying? ? "NO!" : (@tired += 1; "Twang!!!") | |
end | |
# guard clause -- what I'd actually do | |
def shoot | |
return "NO!" if cranky? || laying? | |
@tired += 1 | |
"Twang!!!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment