Last active
April 19, 2017 15:15
-
-
Save ntijoh-daniel-berg/eb32ff953c84aab54ffbdeff094684b6 to your computer and use it in GitHub Desktop.
if-else-end kontra if-end
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
#med överflödig else (sämre): | |
def binary_search(list:,search_value:) | |
lower_bound = 0 | |
upper_bound = list.length - 1 | |
if list[lower_bound] > search_value list[upper_bound] < search_value | |
return false | |
#om ovanstående villkor varit sant, skulle följande else-sats aldrig nås. | |
#Alltså är den onödig (och koden blir svårare att förstå). | |
else | |
while lower_bound <= upper_bound | |
## massa kod här | |
end | |
return false | |
end | |
end | |
#utan överflödig else (bättre): | |
def binary_search(list:,search_value:) | |
lower_bound = 0 | |
upper_bound = list.length - 1 | |
if list[lower_bound] > search_value || list[upper_bound] < search_value | |
return false | |
end | |
while lower_bound <= upper_bound | |
# massa kod här | |
end | |
return false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment