Last active
August 29, 2015 14:09
-
-
Save sajoku/fcfe1965bf011982f3d0 to your computer and use it in GitHub Desktop.
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
#Developer code: | |
def add | |
1 + 1 | |
end | |
# After another developer comments on the code with | |
# "1 is a magic number!" | |
def add | |
a + b | |
end | |
# After another developer comments on the code with | |
# "It's not clear what a and b are!" | |
class Adder | |
A = 1 | |
B = 2 | |
def add | |
first_letter_of_the_alphabet + second_letter_of_the_alphabet | |
end | |
def first_letter_of_the_alphabet | |
A | |
end | |
def second_letter_of_the_alphabet | |
B | |
end | |
end | |
# SO CLEAN CODE, MUCH CLEAR, VERY EFFICIENT! | |
# After showing this to a colleague! | |
# Seperation of concerns, the implementation of add shouldn't know about how to actually ADD the two numbers | |
DEFAULT_ADDITION_FUNCTION = :+ | |
def add(function = DEFAULT_ADDITION_FUNCTION) | |
first_letter_of_the_alphabet.send(function, second_letter_of_the_alphabet) | |
end | |
😄
This code directly returns the result. It's better to do something like 'lazy calculation': the actual calculation should be executed, when the result is used.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Customer request, add 3 to 1 and 2.