This is not a problem with overloading. This is not a question of performance vs security. This is the person that originally wrote Buffer writing a poor API.
For one thing, it doesn't make sense for every type except Number to result in getting enough memory to hold that thing. Why are you assuming that I'm never going to just want memory to store a number? Writing an API like this in JS where the caller is responsible for handling type issues is fundamentally stupid. When you write an API in JS you do your own, internal typechecking and any other input verification necessary and then you handle the errors yourself. This is just a basic part of writing something that's self-contained. Even in a typed language you still have scenarios where you have to do this - E.g. What do I do when you pass me an array and there are no items in the array but at least one is required for this function?
OP - Your title sucks but it's a good issue to know about. So, thanks?
It has become an issue of performance vs security vs backwards compatability. Sorry, my title was already long and I couldn't fit that last one in. They can zero out the buffer (performance) or they can leave it as is (security) or they can break a lot of code.
Writing an API like this in JS where the caller is responsible for handling type issues is fundamentally stupid. When you write an API in JS you do your own internal typechecking.
They are doing their own internal typechecking. That's the problem. They detect the type and then act on the appropriate behavior. The problem is that in Javascript any type can be plugged into a function so people form the expectation that they can put anything in it and get similar behavior. To expect anything else is to ask the caller to do the type checking. One function name, one general behavior should be the rule. Typechecking should be for consolidating behavior not diverging it. If you need an additional behavior that should be an additional function. It's a constructor so in this case then need some generators and discourage people from using the constructor in the docs.
3 comments
1 u/ForgotMyName 04 Feb 2016 17:12
This is not a problem with overloading. This is not a question of performance vs security. This is the person that originally wrote Buffer writing a poor API.
For one thing, it doesn't make sense for every type except Number to result in getting enough memory to hold that thing. Why are you assuming that I'm never going to just want memory to store a number? Writing an API like this in JS where the caller is responsible for handling type issues is fundamentally stupid. When you write an API in JS you do your own, internal typechecking and any other input verification necessary and then you handle the errors yourself. This is just a basic part of writing something that's self-contained. Even in a typed language you still have scenarios where you have to do this - E.g. What do I do when you pass me an array and there are no items in the array but at least one is required for this function?
OP - Your title sucks but it's a good issue to know about. So, thanks?
0 u/luckyguy [OP] 07 Feb 2016 00:21
It has become an issue of performance vs security vs backwards compatability. Sorry, my title was already long and I couldn't fit that last one in. They can zero out the buffer (performance) or they can leave it as is (security) or they can break a lot of code.
They are doing their own internal typechecking. That's the problem. They detect the type and then act on the appropriate behavior. The problem is that in Javascript any type can be plugged into a function so people form the expectation that they can put anything in it and get similar behavior. To expect anything else is to ask the caller to do the type checking. One function name, one general behavior should be the rule. Typechecking should be for consolidating behavior not diverging it. If you need an additional behavior that should be an additional function. It's a constructor so in this case then need some generators and discourage people from using the constructor in the docs.
0 u/J_Darnley 04 Feb 2016 12:21
Aw. :( I thought javascript was a magical safe language that would never do anything wrong.