Remix.run Logo
jlarocco 4 days ago

> I need to sit down and manually exercise codepaths

Isn't that exactly what unit tests are for?

pansa2 4 days ago | parent | next [-]

Yeah, that's a common argument for dynamic typing. You're writing tests anyway (right?), and those will catch type errors and many other kinds of error. Why bother with a separate level of checking just for type errors?

I personally believe it's a valid argument (others will disagree). IMO the main benefit of static types isn't for correctness (nor performance) - it's to force programmers to write a minimal level of documentation, and to support IDE features such as autocomplete and red underlines. Hence the popularity of Python type hints and TypeScript, which provide these features but don't fully prove correctness nor provide any performance benefit.

4 days ago | parent [-]
[deleted]
mkehrt 4 days ago | parent | prev | next [-]

Fortunately my compiler writes a large number of unit tests for me, that run at compile time! I call it the "typechecker".

airstrike 4 days ago | parent | prev [-]

Except now you're writing and maintaining twice the amount of code instead of relying on the compiler and/or type checker to help you catch those errors

jlarocco 4 days ago | parent [-]

Sorry, but I don't agree that static typing is a replacement for unit tests. I can see static languages having fewer unit tests, but it's not going to eliminate them.

alternatex 4 days ago | parent | next [-]

Static typing is a replacement for unit tests aimed to catch type bugs. Also, no one has 100% code coverage so might as we get some guarantees for granted.

I understand that statically typed code doesn't mean bug-less code, but I always find it odd that dynamic language enthusiasts feel like they need to stretch the benefits. Dynamic languages are great for many things. Strictness is not one of them and that's fine.

esafak 4 days ago | parent [-]

It's obviously not a replacement. You can get the types right but the values wrong due to faulty logic.

MrJohz 4 days ago | parent [-]

Sure, but only some of the unit tests are about values. Others, at least in my experience, are any handing various aspects of the implied types of values, and those can largely be removed.

Moreover, a good type system can also force the values to be right by enforcing certain invariants directly in the type system. For example, let's say I have a function `deleteProjectAs(user, project)` that deletes a project, but it only works if the user is an admin, otherwise it throws an error. I can write a bunch of tests that validate that this function checks that the user is an admin in all sorts of different cases, but with a type system I can write something like `deleteProjectAsUser(user: Admin, project: Project)`, and this guarantees at the type level that no non-Admin user can delete a project.

The point here is not that types can replace all tests, but that well-designed types can get rid of a lot of them.

esafak 4 days ago | parent [-]

I agree. My post was unnecessary; I did not realize the response was talking specifically about type bugs.

pdimitar 3 days ago | parent | prev [-]

You're tearing down a straw man. Your parent poster did not say it eliminates tests. He said static typing mean less tests. He didn't say static typing is a replacement for tests either.

Why are you doing double misrepresentation?

More honest discussion tactics, please.