Last active
December 14, 2015 15:58
-
-
Save user24/5111233 to your computer and use it in GitHub Desktop.
Code Clarity by hoisting logic into nicely named variables
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
// Succinct, does not create strictly unnecessary variables | |
// but not easy to read | |
if((a.notes && a.notes.trim() != "") && (b.notes && b.notes.trim() != "")) { | |
skip = true; | |
console.log('too many notes - skipping'); | |
} | |
// Verbose, creates two vars which are not really needed | |
// but it's instantly clear what the 'if' is doing | |
var aHasNotes = (a.notes && a.notes.trim() != ""); | |
var bHasNotes = (b.notes && b.notes.trim() != ""); | |
if(aHasNotes && bHasNotes) { | |
skip = true; | |
console.log('too many notes - skipping'); | |
} |
I would probably move the conditions into a method of my object.
MyObject.prototype.hasNotes = function() {
return this.notes && this.notes.trim() != "";
};
So then my implimentation is simpler to understand.
if (a.hasNotes() && b.hasNotes()) {
I can then re-use this logic elsewhere in the codebase.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the conditions are parallel, I usually use newlines to accent that: