Are Default Params in JavaScript bad? (Snack pack #4)

Not if it’s still passed

Take this example:

I wrote this function well over two years ago. It just pops up a notification if the user makes a CRUD call. (Create, Read, Update Delete). The function only takes two defined parameters a messag…


This content originally appeared on DEV Community and was authored by Blake Campbell

Not if it's still passed

Take this example:
carbon

I wrote this function well over two years ago. It just pops up a notification if the user makes a CRUD call. (Create, Read, Update Delete). The function only takes two defined parameters a message and a notification.

The notification parameter is a boolean.

notification = true // notify user by a pop-up
notification = false // inline-notification by form button, or in a defined space

Looking back at this function, it could be much better.

  • Instead of a notification boolean, it can be notification style as a string.
  • Call with the second parameter everywhere

Back to the question: Default Params in JS bad?

I think so if I'm not explicitly calling with all the params of a function. It's okay to have a fallback, but don't depend on it!

Why?

  • It's leaves technical debt.
  • How is another developer supposed to know what parameters are passed if they only see a partial picture?
  • My boolean parameter is bad in this case.

How I refactored this function:

carbon (2)

My snack pack reads are intended for a quick read without any fluff or BS.

Feedback is always welcome.


This content originally appeared on DEV Community and was authored by Blake Campbell


Print Share Comment Cite Upload Translate Updates
APA

Blake Campbell | Sciencx (2021-04-11T01:52:58+00:00) Are Default Params in JavaScript bad? (Snack pack #4). Retrieved from https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/

MLA
" » Are Default Params in JavaScript bad? (Snack pack #4)." Blake Campbell | Sciencx - Sunday April 11, 2021, https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/
HARVARD
Blake Campbell | Sciencx Sunday April 11, 2021 » Are Default Params in JavaScript bad? (Snack pack #4)., viewed ,<https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/>
VANCOUVER
Blake Campbell | Sciencx - » Are Default Params in JavaScript bad? (Snack pack #4). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/
CHICAGO
" » Are Default Params in JavaScript bad? (Snack pack #4)." Blake Campbell | Sciencx - Accessed . https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/
IEEE
" » Are Default Params in JavaScript bad? (Snack pack #4)." Blake Campbell | Sciencx [Online]. Available: https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/. [Accessed: ]
rf:citation
» Are Default Params in JavaScript bad? (Snack pack #4) | Blake Campbell | Sciencx | https://www.scien.cx/2021/04/11/are-default-params-in-javascript-bad-snack-pack-4/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.