News for hackers. Stuff that matters.
Re:The type system is a programmer's best friend by PainfullyNormal on Saturday November 05, 2022 @10:42AM (#33481919)
Articles like this bug me. You've given me a list of why types are awesome. Great. Now, tell me what the tradeoff is. Nothing is free in engineering. To get something, you have to give up something else. Even grug[0] understands this.

[0]: https://grugbrain.dev/#grug-on-type-systems

Re:The type system is a programmer's best friend by arwhatever on Saturday November 05, 2022 @11:22AM (#33482316)
Understanding existing code is a big benefit of static types as well.

I’m sure one could argue that member names should obviate the need for type annotations.

There’s also the distinct possibility that my preceding ~15 years of statically-typed software development have affected how I think about software development in some way. (wink)

But I am finding type annotations internet useful while working on a huge application that is about 2 years into adding a gradual typing system, enough so that I usually take time whenever I enter a new code area to add annotations to everything, just to understand what’s going on.

My perception is that I invest time to build understanding of the types, and then document what I’ve learned in the form of these type annotations so that future maintainers then gain a quicker understanding without having to do the initial research.

At least my non-statistically-significantly-sized team agrees.

Re:The type system is a programmer's best friend by Izkata on Saturday November 05, 2022 @01:32PM (#33483597)
> Understanding existing code is a big benefit of static types as well.

At a syntax level perhaps, but not necessarily at a semantic level. This won't apply to everyone, but I've noticed that the more types are relied on, the less my co-workers really understand the code. They're relying on the compiler so much they don't slow down and think through the changes they're making. In one extreme case I saw a guess-change-compile workflow that relied entirely on the compiler doing the work for them.

Re:The type system is a programmer's best friend by bigyikes on Saturday November 05, 2022 @10:50AM (#33482001)
grug miss big brain benefit for types. Grug says the main benefit is auto completion, I think the real benefit is to making code changes.

If I update a type, the compiler will tell me every single location where I need to make a corresponding code change. For grug: change type give red squiggle, make change code good

Also, grug makes a good point about the temptations of generics, but I think they’re exaggerating the impact to the speed of development.

Re:The type system is a programmer's best friend by floppydisc on Saturday November 05, 2022 @11:34AM (#33482437)
I usually run into issues at the boundaries in the system.

Usually moving from primitives into complex types does not account for serialization and deserialization between db and the client. This can be very annoying to work with in something like C#.

Usually it ends up resulting in alot more types and a lot more mapping between types.

However this has its own benefits, but is very boilerplate-y and is sluggish to work with when your domain changes.

Luckily, for C#, https://github.com/SteveDunn/Vogen now exists thanks to source code generators which soothes some of the issues.

Re:The type system is a programmer's best friend by SaltyBackendGuy on Saturday November 05, 2022 @10:52AM (#33482018)
> big brain type system shaman often say type correctness main point type system, but grug note some big brain type system shaman not often ship code. grug suppose code never shipped is correct, in some sense, but not really what grug mean when say correct

I forgot about this, thanks for the morning laugh. No such thing as a free lunch.

Re:The type system is a programmer's best friend by marcus_cemes on Saturday November 05, 2022 @11:15AM (#33482251)
This is brilliant. Just made my day
Re:The type system is a programmer's best friend by hardwaregeek on Saturday November 05, 2022 @11:45AM (#33482556)
Sure there are tradeoffs, but I disagree that it’s always so balanced. When people moved from assembly to high level languages presumably there were tradeoffs but in retrospect it’s a pretty clear cut choice. I’m not saying typed languages are as big of a shift as high level languages but it’s possible they are the unequivocal right choice.
Re:The type system is a programmer's best friend by c7b on Saturday November 05, 2022 @12:22PM (#33482923)
Does that also hold for people doing data science in Jupyter notebooks? They're also programmers, arguably.

At this point in the trajectory of software engineering, it's fair to assume that most of the low-hanging fruits have been picked, and solutions that are unequivocally better would have to bring something fundamentally new to the table (which types aren't at all). Most solutions will be picking a particular point on a trade-off isocurve.

Apart from that, it's always fair to ask someone who's strongly proposing something what the downsides are.

Re:The type system is a programmer's best friend by jolux on Saturday November 05, 2022 @12:46PM (#33483149)
> Does that also hold for people doing data science in Jupyter notebooks? They're also programmers, arguably.

Yeah they’re definitely programmers, but anyone who’s had to maintain and deploy what a data scientist came up with in a notebook will question whether they should really be using types.

Re:The type system is a programmer's best friend by hardwaregeek on Saturday November 05, 2022 @06:47PM (#33486751)
Why are you sure that all of the low hanging fruits have been picked? The origins of software development are pretty much still within living memory. We’re still extremely new to programming. I wouldn’t be surprised if the field looks completely different in 50 years.

As for types, I’m not saying they’re flawless. I’m pointing out that in the transition from assembly to high level languages there were flaws and criticisms that came out. But looking back fifty years, were these flaws and criticisms genuine tradeoffs that kept assembly as a reasonable option for most developers? No, they were not. Now we look back on programmers who insisted on writing assembly as oddities, as niche figures. I cannot say if this will be the case for types, but I won’t rule it out.

Re:The type system is a programmer's best friend by tinyspacewizard on Saturday November 05, 2022 @11:46AM (#33482564)
If you are happy to use "obj", "System.Object", "Any" etc. in any place where the type-system breaks down then the downsides are very few.
Re:The type system is a programmer's best friend by dkarl on Saturday November 05, 2022 @11:52AM (#33482618)
I don't think every article has to "teach the controversy." This is an article for programmers who don't know the upsides of types.

What's more, for a programmer who doesn't get the value of types, the major downsides are already apparent, at least at a basic level. Doesn't this make my code more verbose? Doesn't this get super confusing sometimes?

It's an article design to help certain programmers learn a particular thing, not an article meant to satisfy more experienced programmers' desire to see all sides of an argument acknowledged.

Re:The type system is a programmer's best friend by PainfullyNormal on Saturday November 05, 2022 @11:55AM (#33482643)
> What's more, for a programmer who doesn't get the value of types, the major downsides are already apparent, at least at a basic level.

How could the downsides possibly be apparent if the upsides are so mysterious they need an article to spell them out?

> It's an article design to help certain programmers learn a particular thing

Have they actually learned that particular thing if they don't know the tradeoffs they're making? I would argue they haven't. You need to know what you're getting and what you're giving up before you can decide whether something is worth using at all. There are too many articles hyping the upsides of technology X, but nobody asking what the downsides are.

Re:The type system is a programmer's best friend by howenterprisey on Saturday November 05, 2022 @12:11PM (#33482820)
I don't know what to tell you. The downsides are apparent. There is no logic theorem that says the upsides and downsides need to be equally as apparent.

The trade-off is obvious: you gain confidence about your program but you need to Learn More Stuff. Nobody's talking about the downsides of type systems except to the extent that they're worth talking about: see the comments here every time someone compares the type systems of Python and Rust.

Re:The type system is a programmer's best friend by capableweb on Saturday November 05, 2022 @12:27PM (#33482977)
> The trade-off is obvious: you gain confidence about your program but you need to Learn More Stuff

This is why engineering/software articles in general (this one included) needs to bring up tradeoffs more often. No, "learning more stuff" is not a downside or a tradeoff, it's just a fact of learning anything.

That you introduce more coupling is a tradeoff. That the program (sometimes) gets harder to change is a tradeoff. That is becomes easier to write large, messy programs because programmers feel more safe in the future to refactor, is a tradeoff. Trying to fix each one of those tradeoffs also come with their own tradeoffs, and so on.

These are "apparent" for me, when talking about languages using static types vs dynamic languages, but it is not apparent for everyone. So when bringing up these "obvious" upsides, also bring up the "obvious" downsides, as it seems quite a lot of people don't see it as "obvious" as we do.

Re:The type system is a programmer's best friend by jolux on Saturday November 05, 2022 @12:43PM (#33483108)
> That you introduce more coupling is a tradeoff.

That’s not inherent in static types.

Re:The type system is a programmer's best friend by hither_shores on Saturday November 05, 2022 @01:17PM (#33483432)
> That you introduce more coupling is a tradeoff. That the program (sometimes) gets harder to change is a tradeoff.

You don't introduce more coupling, you document the coupling that already exists. If your program is hard to change with types, it would be hard to change without types - but easier to change incorrectly.

> That is becomes easier to write large, messy programs because programmers feel more safe in the future to refactor, is a tradeoff

Sure, I guess this is true in principle - but you could say the same about IDEs, or version control, or grep. The effect size is small.

Re:The type system is a programmer's best friend by germanjoey on Saturday November 05, 2022 @03:56PM (#33485006)
> You don't introduce more coupling, you don't the coupling that already exists.

This is true at the code level. But at the system-design level, this documentation is the extra coupling.

I feel like it's important to understand this. I agree with the original commenter; in engineering, nothing is truly free. In many cases, this extra coupling helps keep a system strong and stable, like extra nails holding planks of wood together. In other cases, you may find that part of a system's spec actually missed the mark and now needs to be ripped up and redone. That extra coupling might now work against you!

Again, that doesn't mean that it wasn't worth having it. It is just important to understand tradeoffs in engineering.

Re:The type system is a programmer's best friend by jjav on Saturday November 05, 2022 @04:11PM (#33485149)
> That you introduce more coupling is a tradeoff.

No, the coupling (i.e. assumptions about what type this thing can be) is implicitly there in a typeless language. If you pass in the wrong thing it'll blow up. But it'll happen in production.

The coupling is always there, it's just a matter whether you make it explicit (this allowing errors to be caught early) or you pretend it's not there and let things crash in production.

Re:The type system is a programmer's best friend by kaashif on Saturday November 05, 2022 @01:19PM (#33483452)
> The trade-off is obvious: you gain confidence about your program but you need to Learn More Stuff.

You still do need to know that stuff if you have no types in your language, but the compiler won't help you.

Re:The type system is a programmer's best friend by saagarjha on Saturday November 05, 2022 @12:07PM (#33482778)
How can the downsides of having to wear a seatbelt possibly be apparent if the upsides are so mysterious they need an article to spell them out? People have a quick aversion to things all the time. Sometimes the actual benefits need to be carefully explained. (“You are statically likely to be in a car crash. Wearing a seatbelt multiplies your chance of living through it.”)
Re:The type system is a programmer's best friend by lapcat on Saturday November 05, 2022 @03:06PM (#33484578)
I think almost everyone understands the benefits of both types and seat belts. The fact that a seat belt keeps you restrained during a crash is pretty intuitively obvious. The idiots who don't wear seat belts either (1) believe they'll beat the statistics, and thus no statistical argument will convince them or (2) value their "freedom" a lot more than they value their own lives.

In any case, though, wearing a seat belt or not is a choice one can make independently of all other factors. The cars come with seat belts, it's the same car either way. You click or not, nothing else changes about the car.

With types, however, that's not how it works. The tradeoff is... you may have to change your entire programming language, change your IDE, change your frameworks, rewrite existing code, etc. It's not analogous to seat belts at all. Do programmers want the compiler to catch mistakes? Of course they do, in an ideal world. Why wouldn't they? But there are a lot of tradeoffs here that don't exist in the case of seat belts.

Re:The type system is a programmer's best friend by saagarjha on Saturday November 05, 2022 @09:50PM (#33488003)
Replace it with helmets if you want a more controversial option.
Re:The type system is a programmer's best friend by lapcat on Saturday November 05, 2022 @10:12PM (#33488140)
How do helmets change the argument?

Like seat belts, you can choose to wear or not wear a helmet independent of all other factors. The motorcycle or bicycle is exactly the same regardless of whether you're wearing a helmet. Also, everyone understands the benefits of a helmet. The benefits don't make everyone wear a helmet, but everyone is clear about why there are helmets.

Re:The type system is a programmer's best friend by saagarjha on Saturday November 05, 2022 @11:26PM (#33488588)
It does, wearing helmets means you need to carry them around and store them, and it can ruin your hairstyle. People who can balance the tradeoffs make mostly reasonable decisions ("I write large programs with the support of a type system and eschew it for small scripts", "I will probably be OK without a helmet just biking slowly between two buildings at work") but some people will never accept the upsides as being worth it.
Re:The type system is a programmer's best friend by lapcat on Saturday November 05, 2022 @11:51PM (#33488739)
> some people will never accept the upsides as being worth it.

How is this any different from the selt belt case? "(1) believe they'll beat the statistics, and thus no statistical argument will convince them or (2) value their "freedom" a lot more than they value their own lives"

Would you really expect an article "The helmet is a biker's best friend" to convince them?

Everyone knows that a helmet helps prevent head injuries. That doesn't need to be explained. Whether the tradeoff of messing up your hair or whatever is worth it is up to the individual to decide, but there's nothing complex about the decision that needs an academic discussion.

Re:The type system is a programmer's best friend by dkarl on Saturday November 05, 2022 @12:10PM (#33482798)
They aren't going to learn that in a day or a week or a month. Maybe a year if they're extremely bright or are in a perfect environment for figuring it out, but most people take years. This article is a few minutes out of that hypothetical best-case year. If it gives them food for thought for a week, it'll be time better spent than 99% of what they could read, certainly better than if they read a comprehensive article that went 98% over their heads and got them hung up on things that they weren't yet able to experience and understand.

Besides, they aren't going print this article out and take it to a cave in the mountains to learn about type systems for a year. They're going to read other stuff along the way.

Re:The type system is a programmer's best friend by BiteCode_dev on Saturday November 05, 2022 @11:56AM (#33482652)
I must be grug. Just forgot I wrote because dumb brain.
Re:The type system is a programmer's best friend by ironSkillet on Saturday November 05, 2022 @12:09PM (#33482795)
That grugbrain post was so funny and enjoyable to read, thanks for sharing.
Re:The type system is a programmer's best friend by rowanG077 on Saturday November 05, 2022 @12:13PM (#33482835)
Of course there are thing that are free. Assembly is free over machine code which is free over punch cards.
Re:The type system is a programmer's best friend by thefourthchime on Saturday November 05, 2022 @12:39PM (#33483059)
How have I not seen Grug before, wonderful! Thank you!
Re:The type system is a programmer's best friend by maxbond on Saturday November 05, 2022 @01:26PM (#33483522)
Ever so slightly longer compile times. It's pretty close to a free lunch.
Re:The type system is a programmer's best friend by closeparen on Saturday November 05, 2022 @06:52PM (#33486783)
People using dynamic languages deeply, especially library and framework authors, regularly write abstract/generic code for which a suitable type declaration would be mind-bendingly difficult in a very sophisticated type system and impossible in a weak one. You can argue that this is ill-advised! But static typing with normally-powered type systems leads to more voluminous and more purpose-specific code. Very powerful type systems are possible, but treated as academic and too difficult to use in the real world.

A way this often gets worked around is code generation. Anywhere you have or reach for codegen in a static language, you probably could have used a plain old function in a dynamic language.

Re:The type system is a programmer's best friend by maxbond on Saturday November 05, 2022 @07:50PM (#33487186)
Thank you for the thoughtful response; this is something I had failed to consider (since, as you anticipated, I try to avoid creating deep or highly dynamic abstractions), and I do regularly throw in the towel on complex or deeply nested types (either by just omitting them out using a "close enough" type in Python, or by using trait objects in Rust - looking at you, `Map<Chain<RangeInclusive<...>>>`).
Re:The type system is a programmer's best friend by throwaway2037 on Sunday November 06, 2022 @03:36AM (#33490477)
Can you provide a concete example? If you do, I am sure Rust/C++/C/Java/C# programmers can show you how to implement in their language.
Re:The type system is a programmer's best friend by maxbond on Sunday November 06, 2022 @08:19PM (#33499829)
Not GP, but for instance, Python web frameworks are oriented around decorators, and Rust web frameworks are oriented around macros. The syntax looks pretty similar, but like GP says, in dynamic languages you're using a (higher order) function, and in static languages you're using code generation for the same purpose.
Re:The type system is a programmer's best friend by throwaway2037 on Tuesday November 08, 2022 @09:12AM (#33519123)
I am confused. "Code gen" is a fuzzy concept once you have a virtual machine because you can do it at runtime. This is how mocking works in Java and C#. What you describe already exists in Java and C# -- decorators for web frameworks. I would not describe Java nor C# as dynamic. I describe them as "stricter" (types) and Python as "weaker" (types). To quote Norman Ramsey: "Every time I see a question about "strong" or "weak" typing, I kill a kitten." :-)
Re:The type system is a programmer's best friend by LEDThereBeLight on Monday November 07, 2022 @10:49AM (#33507347)
I think this is a little disingenuous. It’s not that the type system makes highly abstract/generic code difficult, it’s more that the specific ways people are used to writing that type of code in dynamically typed languages doesn’t lend itself well to adding type annotations. But I think you’d be hard pressed to find many places where Haskell programmers, for example, haven’t found a different way to express whatever the Python code is achieving while also allowing for type annotations.
Re:The type system is a programmer's best friend by AnimalMuppet on Saturday November 05, 2022 @01:33PM (#33483606)
The tradeoff is that I have to explicitly say (and know) what type I'm dealing with at every point in the program.

But I'm not sure that's much of a tradeoff. If I don't know what type this thing is, how do I know what operations I can safely do on it? How do I know that I can make it do what I'm trying to do? Or will it blow up at runtime when I do that?

I consider "I coded it, it's done, but it might blow up at runtime" to be highly unprofessional. "We covered that with unit tests" is theoretically OK, if you've got 100% test coverage. But you don't, and you never will.

Having to say everywhere what the type is gets tedious. Autocomplete (and "auto", for those languages that have it) help a bit here, but only a bit.

Re:The type system is a programmer's best friend by pca006132 on Saturday November 05, 2022 @02:12PM (#33484045)
Most modern languages can do type inference and doesn't require explicit type annotation for variables. Hack, even C++ gots auto! For declarations, I think it does make sense to ask for the annotation because it can also serve as documentation. Have you ever tried scala, rust, haskell or typescript?
Re:The type system is a programmer's best friend by zbentley on Saturday November 05, 2022 @02:35PM (#33484285)
> Nothing is free in engineering. To get something, you have to give up something

I think this is a dangerous position to take to extremes/as an axiom.

Not talking about type systems at all here. The assumption that, given two tools/techniques for accomplishing the same goal, there are always equivalent tradeoffs simply isn't true.

Some tools are better than others.

That statement usually provokes misinterpretation. It should not be taken to mean:

- That some tools are always better than others, in every context. There are situations in 2022 where COBOL is the best choice for new code, and other situations where rewrite-it-in-Rust is the best choice. Problems occur when "tradeoffs of tool A (even if we don't know what they are yet) make it equivalent to tool B" is a core tenet of decision making.

- That some tools always have been and (therefore)/or always will be better than others. Context, expectations, tool capabilities, and available programmer talent pools all change massively over time.

- That one tool is better than all the others. Plenty of times there are multiple ways to deliver optimal-given-constraints outcomes, and it comes down to a matter of taste or "just pick something, anything, and let us get to work".

Chasing hype and cargo culting leads to poor outcomes; "we should build our two-core app on Kubernetes/write our 2TPS app in Rust" are often justified with "because it's the future" or "because the cool kids are doing it". That's a major bummer.

But the opposite extreme is just as bad: assuming that all choices are fundamentally a wash because "to get something, you have to give up something else" is just as methodologically irresponsible as following the hype cycle. This kind of bad decisionmaking can lead to dependence on obsolete (unsupported/insecure) tools, difficulty hiring, and, at worst, a culture of "don't talk about Python to me; if you can't freehand it in C you just need to get gud" gatekeeping sadism.

Everything has tradeoffs. That doesn't mean they're equivalent.

Re:The type system is a programmer's best friend by hbrn on Sunday November 06, 2022 @05:42PM (#33498283)
Noone ever claimed they are equivalent.

The issue is that people from one side will always downplay the tradeoffs (or pretend they don't exist, like the current author). This exactly how hype and cargo culting happens.

Re:The type system is a programmer's best friend by samatman on Saturday November 05, 2022 @02:47PM (#33484391)
I found this blog post on HN some time this year, and refer to it frequently:

https://hirrolot.github.io/posts/why-static-languages-suffer...

The key thing the author identifies is the two languages problem. Static types are a second program about the program, and that's great when the second program is simple declarations that keep you from passing one struct when another is expected.

But a flexible language needs more than that, and generics end up either Turing Complete or bad in some other way.

I'll be mulling this one over in years to come.

Re:The type system is a programmer's best friend by plasticeagle on Saturday November 05, 2022 @04:13PM (#33485165)
For a while in the codebase I was working on, we had a set of distinct types for different units. You know, a type for meters, another for centimetres, etc etc. We had types for radians, types for degrees.

We had conversion functions between them, and type inference when you performed certain operations.

The result was a disaster. Not an enormous disaster, but enough of a problem to rip the entire thing out and replace it with plain double-prevision floating points, and sensible variable names, everywhere.

Why was this? It was a combination of there being nothing sensible to infer when you, say, multiply an angle and a distance (which happens when you're doing algebra), and everything in the whole codebase needing to be aware of these types.

The downside of all these brilliant ideas is dependency. If you define an "EmailAddress" type, as the article suggests, you've got to write the code for it somewhere. Now all your projects are dependent on this library, with all the pain and anguish that versioning and linking/including/whatever the library brings.

Before, you depended on nothing but the String type, which is very likely built into your language. When all your code needed to do was pull that email address out of some persistent store (say), and send to some other piece of code, your dependency list was just the Persistence library. But with your fancy EmailAddress type, your dependencies are now much worse.

Keep things as simple as they can be. An EmailAddress type is not useful.

Re:The type system is a programmer's best friend by _dain_ on Saturday November 05, 2022 @04:54PM (#33485600)
>The result was a disaster.

wtf? metres and centimetres are not different types! they are just different ways of writing same type: Length. radians and degrees are just ways of writing a dimensionless Angle quantity. you made the absolutely elementary mistake of conflating a physical quantity with the unit used to measure it, of course it was a disaster.

>nothing sensible to infer when you, say, multiply an angle and a distance

angles are dimensionless so they should just be a distinct type of float. there is literally no problem here.

Re:The type system is a programmer's best friend by sixstringtheory on Saturday November 05, 2022 @05:06PM (#33485743)
I can see how it could cause problems if they weren’t using the type system correctly.

    typedef float cm;
    typedef float meter:
    cm a = 1;
    meter b = 1;
    if (a == b) {
        // launch rockets
    }
I’ve done something similar (not including rockets, don’t worry!) in Swift with its typealias feature. Thankfully there is a way to actually force compiler errors in such situations with something like https://github.com/pointfreeco/swift-tagged
Re:The type system is a programmer's best friend by LEDThereBeLight on Monday November 07, 2022 @11:01AM (#33507560)
You might not have seen this pattern before, but annotating values with units as types is a legitimate approach. There’s a whole chapter about it in the book Software Design for Flexibility by Gerald Sussmann, the author of Structure and Interpretation of Computer Programs, which is linked on here pretty often. It has to be done in the right way, though, in a language that’s expressive enough to support it.
Re:The type system is a programmer's best friend by sixstringtheory on Saturday November 05, 2022 @05:01PM (#33485694)
I would rather have dependency problems-which are able to be automated with sufficient tooling-than working in a codebase written by someone that thought you could just use floats and strings for everything in an extremely overloaded fashion. Doubly so if they don’t believe in documenting all the separate use cases and just keep all that knowledge in their head.
Re:The type system is a programmer's best friend by za3faran on Saturday November 05, 2022 @05:20PM (#33485911)
Didn't NASA lose a space orbiter due to mixing up units in the code? A properly written library should not have the issues you're mentioning.
Re:The type system is a programmer's best friend by broadwaylamb on Sunday November 06, 2022 @08:04AM (#33492148)
I think there's a misconception that types are meant to tag things according to the programmer's mental taxonomy. They're not. Types should be based on significant distinctions in how your particular program treats and processes the data. For instance, you don't need an EmailAddress type because your program doesn't do anything special with the knowledge that this string is actually an email address. It just treats it like another string. It takes some judgment to determine this, but I consider that part of the learning curve in using types rather than an inherent tradeoff from the tool itself.