Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created October 30, 2014 16:26
Show Gist options
  • Save JoshCheek/87a13eea70ef01701228 to your computer and use it in GitHub Desktop.
Save JoshCheek/87a13eea70ef01701228 to your computer and use it in GitHub Desktop.
Refactoring an if statement for a student
# 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