Created
July 14, 2020 13:50
-
-
Save antonvolkoff/33f8ffc5b627c5f037946ed23ed92f86 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
// Let's say we have a product that validates emails. | |
// Person visits our websites gives us a list of emails and we validate that those emails are real. | |
// Our value is "Great customer experience". | |
// We have two engineers in our company and they both inline with our values. | |
// However they disagree on qualities that our code should have. | |
// Person "A" believes that we should optimize for maintenability. | |
// Person "B" is sure that we should optimize for change. | |
// They both send to implement an email validator. | |
// Code optimized for maintenability | |
// Let's say that "maintenability" means the following for person "A": | |
// - I know what is happening in my application | |
// - Application should handle error states explicitly | |
const validateTotal = Metric.counter("validate_total"); | |
function validate(emails) { | |
validateTotal.observe(1); | |
const validateEmail = (email) => { | |
email.valid = ( | |
validateAddress(email) && checkDNS(email) && checkSMTPVRFY(email) | |
); | |
return email; | |
}; | |
return emails.map(validateEmail); | |
} | |
// Code optimized for change | |
// Let's say that "change" means the following for person "A": | |
// - It is easy to plug-in new behaviour | |
// - No need for configuration as it is easier to change code than maintain configuration. | |
function validate(emails, validators = [validateAddress, checkDNS, checkSMTPVRFY]) { | |
const validateWith = (validators) => (emails, email) => { | |
email.valid = validators.reduce((r, v) => r && v(email), true); | |
return email; | |
}; | |
return emails.map(validateWith(validators)); | |
} | |
/// Both will be called the same: | |
let vaidatedEmails = validate([ | |
{ address: "[email protected]", valid: false }, | |
{ address: "[email protected]", valid: false }, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment