Created
March 2, 2019 17:56
-
-
Save nabil-nuvolo/91c5c4a6cbe3ad306d9e5603f28a02ce to your computer and use it in GitHub Desktop.
Some concepts to consider when writing code
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
// Original & refactored with comments noting changes & reasoning | |
// Original | |
function validateSearchRequest(parent_space_id, event_start, event_end) { | |
var isValid = true; | |
var message = ''; | |
if (!this.moment(event_start).isValid()) { | |
isValid = false; | |
message = 'Invalid start datetime.'; | |
} | |
else if (!this.moment(event_end).isValid()) { | |
isValid = false; | |
message = 'Invalid end datetime.'; | |
} | |
else if (this.moment(event_start).isAfter(this.moment(event_end))) { | |
isValid = false; | |
message = 'The end date must not be before the start date.'; | |
} | |
var result = { | |
'isValid': isValid, | |
'message': message | |
}; | |
return result; | |
}, | |
// Refactored | |
function validateSearchRequest(parent_space_id, event_start, event_end) { | |
// Consider constructing the entire return payload at the start so next dev | |
// can reason about the shape of the return from the start | |
var returnResult = { | |
isValid: false, // Consider starting off with a default object that is invalid, validate at the end | |
message: "" | |
}; | |
// Move logic for validation into individual variables, keep boolean logic in if | |
// blocks simpler for readability | |
var valid_start = this.moment(event_start).isValid(); | |
if (!valid_start) { | |
// Since result is false to begin with, we just need to add a message | |
returnResult.message = 'Invalid start datetime.'; | |
// Returning if validation fails means rest of function won't even be considered | |
// Returning early also helps the next dev reading the code more easily reason | |
// about the logical flow. If else's tend to open new paths of code execution | |
// which can be difficult to reason about for the next dev (which might be yourself | |
// in the future). In this simple case, it seems trivial but as the logic becomes more | |
// complex, following this pattern will immensly improve code readability | |
return returnResult; | |
} | |
var valid_end = this.moment(event_end).isValid() | |
if (!valid_end) { | |
returnResult.message = 'Invalid end datetime.'; | |
return returnResult; | |
} | |
var end_is_after_start = this.moment(event_end).isAfter(this.moment(event_start)); | |
if (!end_is_after_start) { | |
returnResult.message = 'The end date must not be before the start date.'; | |
return returnResult; | |
} | |
// If we make it past all the guard blocks -- we can validate the response and return | |
// successfully | |
returnResult.isValid = true; | |
return returnResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment