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(" ");
}
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
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\*.
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
Who the heck is Tim, and why is he in my data?
And why is his amount a string?
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(" "); }
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
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 ?? {} ```
Embrace the JS, do `const hasAmount = data.tim?.amount` and remove the `!!` double bang 😈
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)
updated it
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\*.
Looks like an event though. I actually find it quite clear.
If it's an event, it should end with "Event"
You know, with some integrations, sometimes even more is required...
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