Skip to content

Instantly share code, notes, and snippets.

@mondalaci
Created November 11, 2014 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mondalaci/c300eaaf8500f2c4f7d5 to your computer and use it in GitHub Desktop.
Save mondalaci/c300eaaf8500f2c4f7d5 to your computer and use it in GitHub Desktop.
Refactoring example
// 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