Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

The following code works but I am wondering if it can be improved both readability and performance.
I am using jquery and underscore.
Any suggestions, comments or feedbacks will be appreciate. thanks.

var getThemeName = function (theme_name, transparency) {
    if (theme_name === 'light') {
        if (transparency) {
            return translator.get('messages:theme.info.light_with_transparency', {
                percentage: transparency
            });
        }
        return translator.get('messages:theme.info.light');
    } else if (theme_name === 'dark') {
        if (transparency) {
            return translator.get('messages:theme.info.dark_with_transparency', {
                percentage: transparency
            });
        }
        return translator.get('messages:theme.info.dark');
    }
};

var THEME_ITEMS = {
    themeLight: getThemeName('light'),
    themeLightTransparency75: getThemeName('light', 75),
    themeLightTransparency50: getThemeName('light', 50),
    themeLightTransparency25: getThemeName('light', 25),
    themeDark: getThemeName('dark'),
    themeDarkTransparency75: getThemeName('dark', 75),
    themeDarkTransparency50: getThemeName('dark', 50),
    themeDarkTransparency25: getThemeName('dark', 25)
};
share|improve this question

1 Answer

If you keep the naming consistent, you could probably do something like:

function getThemeName(themeName, transparency) {
  var name = "messages:theme.info." + themeName;
  if( transparency ) {
    name += "_with_transparency";
    return translator.get(name, { percentage: transparency });
  } else {
    return translator.get(name);
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.