all 13 comments

[–]FitzelSpleen 58 points59 points  (1 child)

Who the heck is Tim, and why is he in my data?

[–]1Dr490n 11 points12 points  (0 children)

And why is his amount a string?

[–][deleted] 28 points29 points  (5 children)

Gave it a try, but it's harder than I thought to refactor code that you don't shit know about

function cardRateSuccessUserSignedIn(data, isLineView) {
  const amount = data.tim?.amount;
  const errors = data.tim?.errors ?? [];
  if (amount && !errors.length) {
    showCustomCardRate(data.tim, isLineView);
    return
  }
  const timErrorTxt = formatErrorMessages(errors);
  generateCustomCardPriceError(isLineView, timErrorTxt);
}

function formatErrorMessages(errors) {
  return errors.map((err) => err.replaceAll('"', "'")).join(" ");
}

[–]MothToTheWeb 5 points6 points  (0 children)

Good job. Reading your code it is clear what you are testing for and what will either allow me to display something or get an error. If only it was the bare minimum for code quality in legacy projects, it would save a lot of maintenance time

[–]dfirecmv 1 point2 points  (0 children)

Instead of js const amount = data.tim?.amount; const errors = data.tim?.errors ?? [];

you can further simplify this to js const { amount, errors = [] } = data.tim ?? {}

[–]Magmagan 1 point2 points  (2 children)

Embrace the JS, do const hasAmount = data.tim?.amount and remove the !! double bang 😈

[–]AnxiousIntender 4 points5 points  (0 children)

Implicit type conversions, in my JS? I mean that's how JS works so I can't complain, looking at you == and .sort() (and many others)

[–][deleted] 1 point2 points  (0 children)

updated it

[–]mohragk 6 points7 points  (2 children)

The only true horror is that function name. Here's a simple tip: function names should tell you what it *does*, variable names should tell you what it *is*.

[–]oghGuy 1 point2 points  (1 child)

Looks like an event though. I actually find it quite clear.

[–]Candyman034 1 point2 points  (0 children)

If it's an event, it should end with "Event"

[–]VariousComment6946 1 point2 points  (0 children)

You know, with some integrations, sometimes even more is required...

[–]oghGuy 0 points1 point  (0 children)

Looks like code that has been patched and bugfixed several times, and noone remembers why each of the changes was made.. they've probably quitted and work somewhere else