Created
November 11, 2014 00:16
-
-
Save mondalaci/c300eaaf8500f2c4f7d5 to your computer and use it in GitHub Desktop.
Refactoring example
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
// If separated storage data is available then extract it. | |
var hasStoredData = false; | |
if (project.userData.publishWebsite) { | |
var storedData = project.userData.publishWebsite; | |
if (storedData[locale]) { | |
hasStoredData = true; | |
scope.servingDomainPrefix = storedData[locale].servingDomainPrefix; | |
scope.servingDomainPostfix = storedData[locale].servingDomainPostfix; | |
} | |
} | |
// If separate storage data isn't available then split the validated serving domain. | |
if (!hasStoredData) { | |
var explodedDomain = util.explodeDomain(validatedServingDomain); | |
scope.servingDomainPrefix = explodedDomain.pre; | |
scope.servingDomainPostfix = explodedDomain.post; | |
} | |
// Step 1: Extract project.userData.publishWebsite[locale] as storedDataLocale | |
// If separated storage data is available then extract it. | |
var hasStoredData = false; | |
if (project.userData.publishWebsite) { | |
var storedDataLocale = project.userData.publishWebsite[locale]; | |
if (storedDataLocale) { | |
hasStoredData = true; | |
scope.servingDomainPrefix = storedDataLocale.servingDomainPrefix; | |
scope.servingDomainPostfix = storedDataLocale.servingDomainPostfix; | |
} | |
} | |
// If separate storage data isn't available then split the validated serving domain. | |
if (!hasStoredData) { | |
var explodedDomain = util.explodeDomain(validatedServingDomain); | |
scope.servingDomainPrefix = explodedDomain.pre; | |
scope.servingDomainPostfix = explodedDomain.post; | |
} | |
// Step 2: Extract storedDataLocale into the outer scope and brach based on it. | |
var storedDataLocale = (project.userData.publishWebsite || {})[locale]; | |
if (storedDataLocale) { | |
// If separated storage data is available then extract it. | |
scope.servingDomainPrefix = storedDataLocale.servingDomainPrefix; | |
scope.servingDomainPostfix = storedDataLocale.servingDomainPostfix; | |
} else { | |
// If separate storage data isn't available then split the validated serving domain. | |
var explodedDomain = util.explodeDomain(validatedServingDomain); | |
scope.servingDomainPrefix = explodedDomain.pre; | |
scope.servingDomainPostfix = explodedDomain.post; | |
} | |
// Step 3: Use the ternary operator to flatten block structure. | |
var storedDataLocale = (project.userData.publishWebsite || {})[locale]; | |
var explodedDomain = util.explodeDomain(validatedServingDomain); | |
scope.servingDomainPrefix = storedDataLocale ? storedDataLocale.servingDomainPrefix : explodedDomain.pre; | |
scope.servingDomainPostfix = storedDataLocale ? storedDataLocale.servingDomainPostfix : explodedDomain.post; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment