DEV Community

Cover image for How do you handle improper function arguments?
George
George

Posted on

1 1

How do you handle improper function arguments?

I've recently been thinking about how to improve my code patterns to give my work a more consistent and easy to understand feel.

Working functionally means that every function must return a response. If a function was to receive incorrect arguments, then how should it respond?

Returning false for any anomalous behaviour (invalid input/error catch) and handling that in the parent is the best solution I have come up with.

How do you handle this scenario?

Top comments (2)

Collapse
 
avalander profile image
Avalander

I find that Either and Maybe (also known as Optional) are good ways of handling functions that can return a successful result or a failure. I tend to avoid throwing errors in Javascript because then both programmatic and application errors fall in the same bag and if I want to handle some errors in different places than others I need to catch and rethrow, polluting the error stack.

A while ago I wrote an article about handling errors with Either and Maybe, you might find it interesting.

Collapse
 
haidv profile image
HaiDV

There should be a validate function to validate your initial input and if the input is invalid, the system should throw a meaningful error for end-user, like BadRequestError. That's my idea