DEV Community

Discussion on: Self-Documenting Function Calls

Collapse
pclundaahl profile image
Patrick Charles-Lundaahl • Edited on

Something I tend to do on personal projects: when I'm writing functions (or constructors) that takes more than a couple of arguments, I tend to bundle all of the arguments into a single object. This way, I force users to explicitly state the name of the arguments they're passing in.

Granted, this works a lot better in TypeScript, where the compiler yells at you if you don't supply required args.

Also, bravo: I've never seen that approach before. I'm not sure I like it, but it's damned cool that you can do that!

Collapse
bytebodger profile image
Adam Nathaniel Davis Author

Yeah, I was originally intending to include object-wrapped arguments in this post. But then I really thought that I wanted to make that its own future post, cuz it can be really useful - but there are at least some potential downsides.

I wanted to write this particular post from the perspective of how to call a function in this manner when you either don't have access to write/define/clarify the function itself, or when it's just not practical for you to do so.

Also, there are many built-in functions, or core-library functions, that you basically have no option except to call them as-is. So, in those cases, there are still ways to call them such that future readers can clearly see what's being passed in.

Collapse
pclundaahl profile image
Patrick Charles-Lundaahl

Ah! That makes a lot of sense. Thanks!