Skip to content

Instantly share code, notes, and snippets.

@cmstead
Created January 13, 2019 02:41
Show Gist options
  • Save cmstead/63c33175bdaec9321eb69069f88990cc to your computer and use it in GitHub Desktop.
Save cmstead/63c33175bdaec9321eb69069f88990cc to your computer and use it in GitHub Desktop.
Eliminating conditional blocks with value selection and assignment
function async saveDataOrDefault(todoId, messageRecord) {
if(messageRecord !== null) {
await dbModel.saveData(todoId, messageRecord);
} else {
await dbModel.saveData(todoId, { message: null });
}
}
function getMessageRecordOrDefault(messageRecord) {
return messageRecord === null ? { message: null } : messageRecord;
}
function async saveDataOrDefault(todoId, messageRecord) {
const cleanMessageRecord = getMessageRecordOrDefault(messageRecord);
await dbModel.saveData(todoId, cleanMessageRecord);
}
@martypdx
Copy link

const save = (todoId, record = { message: null }) => dbModel.saveData(todoId, record);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment