Last active
June 10, 2019 19:00
-
-
Save hnordt/7bb2af621d4f2c3f10ec2060ece11b14 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
// Good | |
let { parameters, customParameters } = connection; | |
let firstName = customParameters.get("FirstName"); | |
let lastName = customParameters.get("LastName"); | |
// Lets say we now need to pass eventId. | |
// One could easily add it inside caller as it | |
// already has multiple props, and things are a bit | |
// implicit. | |
send({ | |
type: "INCOMING", | |
caller: { | |
name: | |
firstName && lastName | |
? `${lastName}, ${firstName}` | |
: firstName || lastName || null, | |
email: customParameters.get("From") | |
} | |
}); | |
// Better | |
let { parameters, customParameters } = connection; | |
function createCaller() { | |
let firstName = customParameters.get("FirstName"); | |
let lastName = customParameters.get("LastName"); | |
let email = customParameters.get("From"); | |
let name = | |
firstName && lastName | |
? `${lastName}, ${firstName}` | |
: firstName || lastName || null; | |
return { name, email }; | |
} | |
// As we are explicitly calling createCaller(), | |
// it's easy to understand that eventId shouldn't be part | |
// of createCaller(). | |
send({ type: "INCOMING", caller: createCaller() }); | |
// Notes | |
// Of course this problem could be easily catched | |
// by some typing system, but what I want is to | |
// make code easier to read. I prefer to reduce bugs | |
// by writing explicit code, which may result in more lines, | |
// instead of introducing a potential bug, so then another | |
// tool might catch it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment