top | item 43728395

(no title)

aomix | 10 months ago

I saw someone describe python as “stressful” for this reason and I couldn’t agree more. It’s difficult to have confidence in any change I make or review. I need to sit down and manually exercise codepaths because I don’t get the guarantees I crave from the language or tooling. While with the small amount of Rust code I’ve written lately I could yolo changes into production with no stress.

discuss

order

pnathan|10 months ago

Agreed. I had to work in a larger Python codebase after spending a few years with Go and Rust and the drop in logical confidence around the language was remarkable.

I have, roughly, sworn off dynamic languages at this point. Although I have dreams of implementing a firm typed system over Common Lisp.

shermantanktop|10 months ago

Same, though my trauma was Ruby. Those Rubyists who were apparently born with the language spec in their heads can do amazing things, but I am a mere mortal who needs to be told I wrote bad code right when I wrote it, not told at 2am on a production server.

felipeccastro|10 months ago

I’m assuming that Python code base didn’t have thorough type hints. What if it had? Would Go still feel safer? I know these aren’t checked in runtime, but Python type system seems more thorough than Go’s, so shouldn’t a Python code base fully typed be even safer than Go? If so, why not?

(I know Python type checks aren’t mandatory, but for this question assume that the type checker is running in CI)

-__---____-ZXyw|10 months ago

Firm like:

  https://coalton-lang.github.io/20211010-introducing-coalton/

?

jlarocco|10 months ago

> I need to sit down and manually exercise codepaths

Isn't that exactly what unit tests are for?

pansa2|10 months ago

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.

mkehrt|10 months ago

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

airstrike|10 months ago

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

je42|10 months ago

When using dynamic languages, either minimize code dependencies / function calls and complexity or ensure high test coverage.

d0mine|10 months ago

Do you believe that Rust's type system is as flexible, powerful, and easy to maintain as unit tests in Python?

MrJohz|10 months ago

One of the big advantages of Rust's type system is that, if you decide you want to change something (add a parameter, give a type a lifetime, rewrite an entire module), you can just do that, and then follow the errors until they're all gone. Often, once the types have been fixed again, the result will work first time when you try and run it.

In this regard, Rust (and other languages where lots of data invariants can be encoded in the type system) is very flexible and easy to maintain indeed, because you can easily make changes, even in very old or poorly-maintained code, without having to worry about the consequences. Moreover, rather than writing all the unit tests yourself, it's as if the compiler is writing the unit tests for you.

In fairness, you can't encode everything in the type system, so you still need unit tests in top of that, but in my experience you can get away with far fewer. In general, I would say that Rust's type system, when combined with unit tests, is far more flexible, powerful, and easy to maintain than dynamic Python with only unit tests.

12_throw_away|10 months ago

I write and test a lot of both rust and python, so I can say quite confidently:

1. Of course a type system is not as "flexible" as arbitrary test code.

2. Compiler-enforced type safety is many orders of magnitude easier to maintain than the equivalent unit tests

3. Defining rigorously enforced invariants with a type system is far, far more powerful than hoping you remembered to test all the important cases.

airstrike|10 months ago

No, it's more flexible, more powerful, and easier to maintain than unit tests in Python.

bormaj|10 months ago

If using python with type annotations, linters like ruff and mypy do a great job at identifying issues. It's no substitute for tests and nor will it give you the same guarantees that rust will at compile time. But I think it improves the base quality of the code.

mabster|10 months ago

The thing I find annoying with MyPt is trying to tell it I'm doing variable shadowing.

E.g. X is a list of strings Translate X to a list of indices Translate X back to a list of strings.

In that paragraph the input and output types are the same, but not complains about the second line.

I always have to introduce a variable with a new name.