Well, it would be cooler if the compiler could check those contracts at compile-time and issue some helpful warnings, but at runtime they are still valuable because the code will fail sooner rather than later. Plus you can probably disable them completely, should you experience problems with performance in production.
They also serve as documentation for other developers, documentation that you're forced to keep in sync. This documentation is not about the actual business logic, that ends up being laid out in tests, but rather about interface specifications and invariants.
So there you have it - testing serves a different purpose.
Clojure has a variable called "*assert*" just for this
purpose: set it to "false" for production to
turn off all these type checks. def increment(n):
assert isinstance(n, int) or isinstance(n, float)
result = n + 1
assert isinstance(result, type(n))
return result
So as long as we can agree on that much, then we can agree that there is a value to being judicious about where you should and shouldn't use type assertions.By the way I'm a fan of static typing as well, although I see value in dynamic languages too. And I definitely acknowledge the limitations of unit testing, although from a practical point of view, a comprehensive set of unit and functional tests is usually robust enough. And, of course, type systems don't make any guarantees against logic errors. :)
On your example, you're of course right. I'm also not a fan of checking the actual type in a dynamic language, since it defeats the purpose of it being dynamic. I like assertions that are more useful than that, like:
def sqrt(x):
assert x >= 0, "only defined for positive numbers"
last_guess = x / 2.0
while True:
guess = (last_guess + x / last_guess) / 2
if abs(guess - last_guess) < .000001:
return guess
last_guess = guess
Now clearly this helps, since it aids in readability (this function is defined for positive numbers only) and if you call it with a negative number, it will loop forever.But that said, :pre/:post w/ unit tests seems pretty powerful. You can assign the invariants to the functions themselves and have a better / more robust set of assertions in your unit tests.
Yes one thing you might use :pre/:post for is type checking, but it can do more than just that.
Indent code by at least 4 spaces. (Just like in Markdown in e.g. Stack Overflow.)
like
this
and indent some moreIf you're dead set on adding type declarations, perhaps you'd be better off with Haskell or something?
- its hard to safely refacture without static types (and the help of the IDE that often comes with it)
- dynamic languages must compensate the missing type checking support from the compiler with additional unit tests, negating the productivity gains
Sometimes it would be nice to have both worlds in the same language, but the way Clojure does it does't appeal to me at all.
The idea: add static types to Clojure without compromising on the dynamism / flexibility / convenience
(defn persist-this-item [item]
org.joda.time.DateTime) So this function needs to be given some kind of map, and it needs to have 2 keys, :item-name and :item-type, and both of these need to be strings, and if :created-at is already set, then it needs to be a Joda DateTime.It's not accurate to say that Clojure has no types. It just gives you some flexibility about how strict you want to be.
I find that Clojure strikes a perfect balance: its flexibility with types allows me to easily integrate a lot of 3rd party tools without much work, but when I need to I can be as strict as a I like with types.
Edit to add: wow, the code is ugly on Hacker News. Funny that this site provides no tools for posting code snippets, given the subject.