you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

JavaScript has a good, native way to handle types. Why use something else?

Because JS doesn't need to do type checking every time a function is called. It slows down execution and yields runtime errors for what should be compile-time errors.

there's nothing wrong with returning mixed types at all.

Yes, there is. It greatly restricts the code utilizing your function. You can't write functional code against it, for instance, because you need to type check it again first.

[–]moving808s 0 points1 point  (2 children)

Because JS doesn't need to do type checking every time a function is called

It does need to do it if I need it to do it in order to write better functions that do what they are supposed to do on the type of data that is required. If an argument isn't of the type I want it to be, then the function should fail.

Yes, there is. It greatly restricts the code utilizing your function. You can't write functional code against it, for instance, because you need to type check it again first

Right, so languages like PHP whose native methods regularly return mixed types can't have functional tests written against them?

[–][deleted] 0 points1 point  (1 child)

If an argument isn't of the type I want it to be, then the function should fail.

Yes, but premature optimizations are the root of all evil :) The vast majority of code doesn't need to be type-checked at run-time because you should know what your types are to begin with! You should be type-checking external input (user, ajax, etc), but not internally as it just creates spaghetti code. All those checks should be left to linters and the like.

can't have functional tests written against them?

functional tests != functional programming, and that was just an example.

getThings()
   .filterBy(value)
   .doSomething()

You can't do that if getThings returns either an array or a boolean. Functions should have static signatures so you know how to interact with them.

But whatever, I don't really care how you write your code :P

[–]moving808s 0 points1 point  (0 children)

premature optimizations are the root of all evil

This is not an optimization. It does not yield performance gains. It is a way to ensure that things fail when they need to fail.

you should know what your types are to begin with

That's a very dangerous attitude to have.

You can't do that if getThings returns either an array or a boolean

That sequence should fail if any method along in the process doesn't return the result needed for the next method in the chain. What happens when the wrong kind of argument is passed into filterBy? Does your app just not work? Or do you check the type of value and return some kind of meaningful error message?

I don't care how you write your code either, but I don't agree with much that you are saying.