white-label icon indicating copy to clipboard operation
white-label copied to clipboard

Use default parameters instead of thernary operator

Open fumegalli opened this issue 2 years ago • 1 comments

Hi,

While using this repository as an example to build my app, I saw an opportunity to contribute cleaning the code. Default parameters are often cleaner than shorting circuiting (or thernary operator, in this case).

Be aware that if you use default parameters, your function will only provide default values for undefined arguments. Other "falsy" values such as '', "", false, null, 0, and NaN, will not be replaced by a default value.

So, if this is a problem in this case, please, let me know.

Bad: https://github.com/stemmlerjs/white-label/blob/503ee491192a3eff026704f4de9a7477daebb630/src/core/infra/BaseController.ts#L34-L36

Good:

public clientError (message = 'Unauthorized') {
    return BaseController.jsonResponse(this.res, 400, message);
}

Reference: https://github.com/ryanmcdermott/clean-code-javascript#use-default-parameters-instead-of-short-circuiting-or-conditionals

fumegalli avatar Dec 09 '22 00:12 fumegalli

As an aside, code 400 is Bad Request, not Unauthorized so that should be corrected as well

AntennaeVY avatar Dec 19 '22 03:12 AntennaeVY