Skip to content

Instantly share code, notes, and snippets.

@sajoku
Last active August 29, 2015 14:09
Show Gist options
  • Save sajoku/fcfe1965bf011982f3d0 to your computer and use it in GitHub Desktop.
Save sajoku/fcfe1965bf011982f3d0 to your computer and use it in GitHub Desktop.
#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
@bulters
Copy link

bulters commented Nov 13, 2014

Customer request, add 3 to 1 and 2.

@stephankaag
Copy link

😄

@ewoutquax
Copy link

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