Created
April 3, 2012 15:54
-
-
Save SimplGy/2293129 to your computer and use it in GitHub Desktop.
Comparison of two ways to check for bad data in a method
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
//------------------ Option A: Use if else to avoid execution of bad data | |
// panelData = { | |
// title: "a title for the panel (optional)" | |
// subtitle: "subtitle for the panel (optional)" | |
// contents: "panel contents (required)" | |
// } | |
_addPanel = function (panelData) { | |
var $panel | |
, panelPos | |
; | |
// Verify the integrity of the panelData | |
if (!panelData || !panelData.contents) { | |
log.error(_me + ': can\'t add a panel without a data object that has .contents'); | |
} else { | |
// Add the panel to the dom and track it in memory | |
$panel = $(tmpl(panelTemplate, panelData)); | |
$panel.css('width', _panelWidth); | |
_configureRandomPanel($panel.find('.panel')); | |
_panels.push($panel); | |
_$panelBox.append($panel); | |
// Record the panel position in memory | |
panelPos = _calculatePanelPos(_panels.length - 1); | |
_panelPositions.push(panelPos); | |
} | |
}; | |
//------------------ Option B: Use return to avoid execution for bad data | |
// panelData = { | |
// title: "a title for the panel (optional)" | |
// subtitle: "subtitle for the panel (optional)" | |
// contents: "panel contents (required)" | |
// } | |
_addPanel = function (panelData) { | |
var $panel | |
, panelPos | |
; | |
// Verify the integrity of the panelData | |
if (!panelData || !panelData.contents) { | |
log.error(_me + ': can\'t add a panel without a data object that has .contents'); | |
return; | |
} | |
// Add the panel to the dom and track it in memory | |
$panel = $(tmpl(panelTemplate, panelData)); | |
$panel.css('width', _panelWidth); | |
_configureRandomPanel($panel.find('.panel')); | |
_panels.push($panel); | |
_$panelBox.append($panel); | |
// Record the panel position in memory | |
panelPos = _calculatePanelPos(_panels.length - 1); | |
_panelPositions.push(panelPos); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am looking for opinions on whether it's acceptable to do an error check in an if and return out of it. I recall the traditional CS wisdom is to only return at the bottom of the method, but it seems cleaner to exit on error in line.