0

Let say I have a JavaScript function and I want to use it as number to calculate.

However, I could either pass in string number (then convert to integer) or integer directly. Which one is good practice, considering that I'm writing plugging for other users to use and they have no knowledge of implementation detail (of course I could read function signature to see which one to pass in).

Thanks.

2 Answers 2

3

Handling all conversions within the function body allows for greater flexibility. A function is supposed to do stuff for you, not require you to do stuff.

Converting from string to integer isn't difficult, and you could throw an exception if the string passed is unparseable to prevent silent failure.

Sign up to request clarification or add additional context in comments.

Comments

2

Use whatever is apropriate for the situation. If you are writing a function to add two numbers, expect an input of a number not a string. You can always choose to deal with the situation where someone passes a string however to prevent failure.

Documentation is the key to 3rd party plugins. This will let your users know what they should be passing in, what functions are available and how to use them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.