Last active
July 10, 2020 02:19
-
-
Save storrence88/e8281c73522cf6a46c40515596623e6d to your computer and use it in GitHub Desktop.
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
# Refactoring conditional logic using short-circut evaluation and ruby implicit return | |
# before refactor | |
def straight_flush?(array) | |
if straight?(array) and flush?(array) | |
return true | |
else | |
return false | |
end | |
end | |
# after refactor | |
def straight_flush?(array) | |
straight?(array) && flush?(array) | |
end | |
####################################################################################################################### | |
# Refactoring conditional logic using case statement and ruby range operator for readability | |
# before refactor | |
def calculate_ovulation_risk(day) | |
if day >= 1 && <= 5 | |
'very unlikely' | |
elsif day >= 6 && <= 10 | |
'unlikely' | |
elsif day >= 11 && <= 13 | |
'likely' | |
elsif day >= 14 && <= 16 | |
'very likely' | |
elsif day >= 17 && <= 19 | |
'likely' | |
elsif day >= 20 && <= 25 | |
'unlikely' | |
elsif day >= 26 && <= 28 | |
'very unlikely' | |
end | |
end | |
# after refactor | |
def calculate_ovulation_risk(day) | |
case day | |
when 1..5 | |
'very unlikely' | |
when 6..10 | |
'unlikely' | |
when 11..13 | |
'likely' | |
when 14..16 | |
'very likely' | |
when 17..19 | |
'likely' | |
when 20..25 | |
'unlikely' | |
when 26..28 | |
'very unlikely' | |
else | |
'very unlikely' | |
end | |
end | |
######################################################################################################################### | |
# Refactoring case statement to utilize a hash and reduce lines of code | |
# Before refactor | |
case params[:student_level] | |
when :freshman, :sophomore then | |
student = Student::Underclassman.new(name, birthdate, | |
address, phone) | |
when :junior, :senior then | |
student = Student::Upperclassman.new(name, birthdate, | |
address, phone) | |
when :graduate | |
student = Student::Graduate.new(name, birthdate, | |
address, phone) | |
else | |
student = Student::Unregistered.new(name, birthdate, | |
address, phone) | |
end | |
# halfway there... | |
klass = case params[:student_level] | |
when :freshman, :sophomore then | |
Student::Underclassman | |
when :junior, :senior then | |
Student::Upperclassman | |
when :graduate | |
Student::Graduate | |
else | |
Student::Unregistered | |
end | |
student = klass.new(name, birthdate, address, phone) | |
# boom | |
STUDENT_LEVELS = Hash.new(Student::Unregistered).merge( | |
freshman: Student::Underclassman, | |
sophomore: Student::Underclassman, | |
junior: Student::Upperclassman, | |
senior: Student::Upperclassman, | |
graduate: Student::Graduate | |
) | |
klass = STUDENT_LEVELS[params[:student_level]] | |
student = klass.new(name, birthdate, address, phone) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment