TypeScript
Avoid ternary operators and use defaults

by Benny Neugebauer

private showError() {
  const detail =
    MAXIMUM_ACCOUNTS === 1
      ? getText('wrapperAddAccountErrorMessageSingular')
      : getText('wrapperAddAccountErrorMessagePlural');
  const message =
    MAXIMUM_ACCOUNTS === 1
      ? getText('wrapperAddAccountErrorTitleSingular')
      : getText('wrapperAddAccountErrorTitlePlural');

  dialog.showMessageBox({
    detail,
    message,
    type: 'warning',
  });
}

private showError() {
  let detail = getText('wrapperAddAccountErrorMessagePlural');
  let message = getText('wrapperAddAccountErrorTitlePlural');

  if (MAXIMUM_ACCOUNTS === 1) {
    detail = getText('wrapperAddAccountErrorMessageSingular');
    message = getText('wrapperAddAccountErrorTitleSingular');
  }
  
  dialog.showMessageBox({
    detail,
    message,
    type: 'warning',
  });
}

Yellow corner:


const detail = getText(MAXIMUM_ACCOUNTS === 1 ? 'wrapperAddAccountErrorMessageSingular' : 'wrapperAddAccountErrorMessagePlural')

Florian Keller

If the string in getText would be shorter, I'd vote for red corner. In this case I'll vote for blue.

Florian Keller

+1 for yellow. This fight is ridiculously trying to favor the blue corner. I've never seen anyone manage to make a ternary expression longer than the without it before. Also (and this is a major problem), these are not equivalent snippets. The blue side will call getText() 4 times if MAXIMUM_ACCOUNTS === 1. Lastly, this is an overly long example script for what is essentially a one (or two) liner.

krowe-hsc

Sign in to comment!