I know javascript uses duck typing and at first I thought this would make polymorphism easy compared to strongly typed languages like C#. But now my functions that take arguments are littered with things like:
if(myObj.hasSomeProperty())
or
if(myObj.hasSomeMethod())
or
if(isNumber(myParam))
etc.
This is really ugly to me. I come from a C# background and I find defined interfaces to be much better.
I'm wondering if I'm incorrectly attempting to apply strategies that are effective in statically typed languages and there's some better way to do this in javascript?
I know I could just not check, but tracking down javascript run time errors can be a nightmare since they don't always happen where the error is actually occurring in the code.
Best Answer
Simple: don't always check for properties and methods.
In Ruby, what you are calling is called "chicken typing". In a dynamically duck-typed, language, you simply trust that the caller passes you a suitable object. It's the caller's job to honor his side of the contract.
You are confusing multiple orthogonal axis of typing here. There are four orthogonal axis of typing:
Since you mentioned C#: it is mostly statically typed, but supports dynamic typing through the type
dynamic
, it is mostly nominally typed, but anonymous types use structural typing, and syntactic patterns (such as LINQ query comprehension syntax) can be argued to be either duck-typed or structurally typed, it is mostly explicitly typed but supports implicit typing for generic type arguments and local variables (although the local variable case is rather strange compared to most other languages, because you cannot just leave the type out, instead you have to give it an explicit pseudo-typevar
, in other words, if you want an implicit type, you have to explicitly say so). Whether or not C# is strongly or weakly typed is a matter of which definition of the two terms you use, however, note that there can be plenty of runtime type errors in C#, especially because of unsafe array covariance.Debugging is not an easy skill to learn. There are, however, techniques to make debugging easier, e.g. the Saff Squeeze is a technique described by Kent Beck which uses tests and refactoring for debugging: