News for hackers. Stuff that matters.
Re:The type system is a programmer's best friend by gary17the on Saturday November 05, 2022 @11:56AM (#33482655)
This is, IMvHO, such old news that it feels... weird to still read about it in a year with the prefix of 20.

Every programmer who has ever single-handedly written a 100,000+ LOC software system will tell you the same thing: shift as much responsibility on the compiler as you can and have the compiler check the code you write to any extent technologically possible.

Getting rid of bugs by experiencing, diagnosing and fixing them takes at least ten times more effort than getting rid of bugs by not making them in the first place, through expressing the problem at hand with a strong type system.

When you also consider the never ending necessity to introduce change to an already written software system, thus the necessity to refactor code (in the sense of altering the previously assumed meaning of its idioms), the critical advantage of a strong type system becomes self-evident.

(Yes, Rust 4ev3r! ;))

Re:The type system is a programmer's best friend by c54 on Saturday November 05, 2022 @12:11PM (#33482814)
I agree with this, yet look at some of the extremely salty comments in this thread. People are upset that something might be useful and that they might benefit from learning it or changing their ways.
Re:The type system is a programmer's best friend by suzzer99 on Saturday November 05, 2022 @12:35PM (#33483033)
It's weird to me how scanning the comments all seem to refer to systems with 100k-ish LoC and dozens of contributors.

A big chunk of my job is writing node microservices in AWS Lambda. I do everything I can to avoid shared library code, since past experience tells me there be lots of dragons (mainly in when and how to push or pull lib updates to components). I have a very tiny shared lib that I try to never touch and definitely never introduce breaking changes.

Unit tests are a breeze since I never have to cast objects or worry about generics, etc.

Typescript would slow me down so much and add absolutely no benefit. Maybe I'm misinterpreting though and no one is claiming Typescript would benefit here.

We also have some C# lamdbas and I find writing unit tests for those so much more of a pain - since the shared libs have generics and I'm always casting things. But admittedly I don't know all the tricks.

Re:The type system is a programmer's best friend by hither_shores on Saturday November 05, 2022 @01:08PM (#33483330)
> since the shared libs have generics and I'm always casting things.

This indicates to me that you're trying to write code that isn't correct (not doesn't work, but rather only works because of implicit couplings between components) and/or doing exotic lisp-style metaprogramming.

In the latter case, yeah, C#'s type system isn't powerful enough. Others are (to an extent: arbitrary code execution at compile time is never going to be completely safe).

In the former case ... that should be difficult. Forcing you to be explicit is half the point of a type system.

Re:The type system is a programmer's best friend by noduerme on Saturday November 05, 2022 @01:44PM (#33483719)
Casting is often necessary for parsing inbound data from certain mysql libraries or CSV or JSON depending on how it's written. I would guess that might be what the parent is talking about. That said, if you don't cast or parseFloat or whatever in JS you're going to have a lot of trouble. And if you're doing that, why not do it in Typescript where you'll know that the data you're accessing has been safely cast based on its type.
Re:The type system is a programmer's best friend by xigoi on Saturday November 05, 2022 @04:50PM (#33485549)
> Casting is often necessary for parsing inbound data from certain mysql libraries or CSV or JSON depending on how it's written.

No, that's what sum types are for.

Re:The type system is a programmer's best friend by noduerme on Monday November 07, 2022 @04:14PM (#33512553)
I don't see how they're mutually exclusive. I use union types prior to typeguards to end up with ultimately checked, cast values. Say I have a boolean fetched from JSON as "1" or "0". By the time I expose it to the rest of the code as part of a Record, I want to change it to an actual boolean. At first I'm going to treat the inbound value as a union type, e.g. (String | Number | null | undefined | Error). After I deal with the error cases, I'm going to cast it (or in TS, reinitialize it as a single type, boolean) so that any code looking at the imported value sees it as definitely only a boolean without needing to have lots of different pieces of code run their own checks on its type.
Re:The type system is a programmer's best friend by xigoi on Monday November 07, 2022 @04:18PM (#33512618)
I wouldn't really call that casting, that's just narrowing based on control flow.
Re:The type system is a programmer's best friend by suzzer99 on Saturday November 05, 2022 @08:07PM (#33487298)
I mean casting to get mocks to work in unit tests, not live code.

Probably there's a better way. I'm not a C# expert. But I was a Java dev for 10 years so it's not super unfamiliar.

Re:The type system is a programmer's best friend by marcosdumay on Saturday November 05, 2022 @01:17PM (#33483427)
> Unit tests are a breeze since I never have to cast objects or worry about generics

Generics reduce the amount of things you must care about on your tests.

And you shouldn't cast objects in almost no code ever. Most 100k LoC programs won't need it even once, your microservices should need it proportionally less.

That's the thing. The gains grow superlineraly with the amount of code. They make it just a bit easier to write some trivial 100's LoC programs, and they make it possible at all to have a working 100k LoC system. But if you don't learn them, you won't know where the break-even point is for you.

Re:The type system is a programmer's best friend by gary17the on Saturday November 05, 2022 @01:31PM (#33483582)
> The gains [introduced by a strong type system] grow superlineraly with the amount of code.

That's a good way to put it.

Re:The type system is a programmer's best friend by veidelis on Saturday November 05, 2022 @04:53PM (#33485595)
"And you shouldn't cast objects in almost no code ever." - I have a question about tests. Imagine I want to test a function that operates on quite large application state but not all app state is necessary for that function. Options:

- Define all app state as a snapshot. Problem: snapshot can become stale, so more infra might be necessary to make sure that snapshot is up to date;

- Pass only the necessary state and construct as necessary. Problem: hard to define whole state precisely and ensure that it conforms to runtime state of a healthy app;

- Pass a subset of necessary state for some execution branch and cast the type. Problem: casting may result in test failures during runtime and potentially other issues such as modify-run-fail debug loop;

- Mock return values of functions called within the function being tested and use any combination of "state passing options above".

In a lot of places I use such approach with custom type helpers and transitive types, and passing in only the necessary subset for smaller functions or mocking return values for bigger ones. What do you think? I know that the AppState can be defined as a union of possible states and together with type guards can address those issues better. I just wanted to hear your opinion on how you would address such problems. I hope I explained it well enough.

  export type Fn = (...params: any) => any;
  
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
  
  export type FirstParamType<G> = G extends Fn[]
      ? UnionToIntersection<Parameters<G[number]>[0]>
      : G extends Fn
          ? Parameters<G>[0]
          : never;
  
  export interface AppState {
      first: {
          a: number;
          b: number[];
      };
      second: {
          c: string;
          d: string[];
      }
  }
  
  type DeepPick<A, B extends keyof A, C extends keyof A[B]> = { [BK in B]: Pick<A[B], C> };
  
  function calculateUsingFirstB(state: DeepPick<AppState, "first", "b">): number[] {
      return state.first.b; // some calculation
  }
  
  function calculateUsingSecondC(state: DeepPick<AppState, "second", "c">): string {
      return state.second.c; // another calculation
  }
  
  // function which takes complex state parameter and calculates the result based on results of other functions
  function calculateMore(state: FirstParamType<[typeof calculateUsingFirstB, typeof calculateUsingSecondC]> & DeepPick<AppState, "first", "a">): string | number[] {
      if (state.first.a > 10) {
          return calculateUsingFirstB(state);
      }
      return calculateUsingSecondC(state);
  }
Re:The type system is a programmer's best friend by hither_shores on Sunday November 06, 2022 @06:47PM (#33498985)
> Imagine I want to test a function that operates on quite large application state but not all app state is necessary for that function

If not all app state is necessary for that function, it shouldn't require the whole app state in its arguments.

     type AppState = { databaseConnection: DatabaseConnection, env : "dev" | "prod", apiToken: string, userId: string, ... } & SomeOtherStuff

     const dropTables = (app: Pick<AppState, "databaseConnection"> & {env: "dev"} ) => app.databaseConnection.dropTables()
Conversely, if a function you don't control says it needs the whole app state, believe it.
Re:The type system is a programmer's best friend by icedchai on Saturday November 05, 2022 @02:01PM (#33483944)
I've seen plenty of Lambda code developed in a "copy-and-paste" style, with little to no code sharing, similar to early CGI scripts from 25+ years ago. It makes maintainability incredibly difficult. The more shared code the better, in my opinion.
Re:The type system is a programmer's best friend by suzzer99 on Saturday November 05, 2022 @08:05PM (#33487284)
Yeah if it's really shared code. But if you're just trying to factor out small redundancies that are only implemented a few times and could diverge in the future - then no.

I'm a big fan of WET - write everything twice. Then maybe the 3rd of 4th time worry about creating some new abstraction to share code. It's so much easier to add an abstraction later when it becomes obviously needed - than to remove one when you find out your three things actually need different variations on the lib code.

Re:The type system is a programmer's best friend by tyingq on Saturday November 05, 2022 @12:25PM (#33482950)
Some of the salt might come from experiences using dynamically typed languages that later had some amount of stronger typing added on.

No matter how well that's done, it creates friction somewhere in the process interacting with existing code.

That is, I can agree that an inherently strongly typed language has benefits, while also being skeptical about bolted on additions.

Re:The type system is a programmer's best friend by c54 on Saturday November 05, 2022 @12:33PM (#33483021)
Makes sense. People have been burned. Probably there are lots of people who think about typescript environment setup and source maps when they think about typing, or who think about python's "isinstance(str, foo)". Or who think that it's overly complicated arcane nonsense with weird terminology (lookin at haskell). Or that types specifically refer to borrow checker woes in rust.
Re:The type system is a programmer's best friend by benreesman on Saturday November 05, 2022 @12:32PM (#33483014)
Based on the author’s examples and the JS-ish looking code in the article, maybe PureScript in general and row types in particular, uh, 4ev3r?
Re:The type system is a programmer's best friend by david422 on Saturday November 05, 2022 @12:54PM (#33483213)
> shift as much responsibility on the compiler as you can and have the compiler check the code you write to any extent technologically possible.

I think that people first starting out or people who have never worked on large/new code bases with a diverse range of authors don't seem to appreciate this concept.

Re:The type system is a programmer's best friend by kodah on Saturday November 05, 2022 @12:59PM (#33483258)
My hypothesis is that it's so old to you because that discovery and the spreading revelation was first order to you. To people learning programming today, they end up having to somewhat rewind the timeline and learn everything new at 2x speed.

That's to say, having old conversations with new engineers is a really refreshing exercise and I'd encourage the world to continue doing it.

Re:The type system is a programmer's best friend by highwaylights on Saturday November 05, 2022 @01:01PM (#33483278)
I’d take this further.

I was listening to the John Carmack episode of Lex Fridman from the summer, and he makes a comment about being frustrated that in the Valley there’s an almost religious opposition to IDEs, debuggers, and static analysis.

Some of those tools have only become more powerful over time and I’m perplexed as to the mindset that would make a person averse to automating the drudgiest parts of their job in a career that is almost entirely based around automating things.

Re:The type system is a programmer's best friend by noveltyaccount on Saturday November 05, 2022 @01:15PM (#33483409)
When I graduated college in the '00s I thought Vim was the most amazing thing I'd ever learned. Then a colleague at my first job showed me what happend when you typed . after a variable name in Visual Studio. Code completion, inline documentation...my mind was blown, and I never looked back. When I meet a young chap extolling the benefits of Vim or Emacs or really anything that doesn't have stepped debugging and code competition...well, there are no bonus points for doing things the hard way.
Re:The type system is a programmer's best friend by constantcrying on Saturday November 05, 2022 @01:24PM (#33483502)
Vim is not really an editor. It is a way to edit text, you can use vim in almost any environment. You are also totally wrong about vim or emacs not having code completion, that is just nonsense.

I use (a version of) visual studio, the first thing that I did was installing a vim extension.

Also vim itself already supports almost any of these features with some basic plugins for the completion logic. If I type "." in my vim I get the same thing I would in visual studio. If you are saying "vim is bad because it lacks X IDE feature" you are missing the point.

Re:The type system is a programmer's best friend by Our_Benefactors on Saturday November 05, 2022 @03:57PM (#33485019)
Vim: I can do that too, I swear! Just configure some plugins, can’t tell you what they might be though. But I’m turing complete, and I’m the best!

VScode: Of course I can do autocomplete bud. Here, search my package repo, I’ll tell you which plug-ins are the most popular and handle the entire download and install process for you.

There’s no comparison.

Re:The type system is a programmer's best friend by yellowapple on Saturday November 05, 2022 @04:34PM (#33485377)
Visual Studio: Of course I can autocomplete! Hold my beer while I bring your machine with 16 cores and 64GB of RAM to its knees for multiple minutes ;)
Re:The type system is a programmer's best friend by Our_Benefactors on Saturday November 05, 2022 @04:43PM (#33485475)
This is just false. Does it have a higher memory/cpu footprint than vim? Sure. But VScode is plenty performant.
Re:The type system is a programmer's best friend by yellowapple on Saturday November 05, 2022 @05:04PM (#33485722)
VSCode != Visual Studio
Re:The type system is a programmer's best friend by nmarinov on Saturday November 05, 2022 @06:50PM (#33486771)
Have you tried the latest couple of releases?

I used to have that issue 5 years ago but since then it opens under 5 seconds and loads the solutions I need for not much more. Currently using it on a cheapish recent windows laptop but even on a 8gb x220 it works fine, just loads for 30 sec in the beginning and then it's smooth.

Granted I'm only opening .net solutions with under 100 projects each but anything above that is unnecessarily more difficult to navigate with just vim.

And in my experience VSCode is many times slower than Visual Studio on the same projects. None of them are as fast as barebones sublime or vim on an m1 mac but when I load the latter with plugins it's not a huge difference.

Re:The type system is a programmer's best friend by constantcrying on Saturday November 05, 2022 @04:05PM (#33485093)
Why do you even reply to a post that you didn't bother to read? VScode is a neovim frontend.

>There’s no comparison.

Read the first line of my post. There literally is no comparison, because there is a category error.

Re:The type system is a programmer's best friend by Our_Benefactors on Saturday November 05, 2022 @04:13PM (#33485169)
> Why do you even reply to a post that you didn't bother to read? VScode is a neovim frontend.

I did read your post Mr. snark.

> There literally is no comparison, because there is a category error.

Pure pedantry. VScode takes far less effort to get a high quality feature set when compared to vim. That’s the only point of debate that matters for most people.

Re:The type system is a programmer's best friend by za3faran on Saturday November 05, 2022 @04:44PM (#33485489)
I think OP wanted to make a categorical difference between vim-the-editor vs vim-motions. The latter of which are supported in all major IDEs as plugins/extensions.
Re:The type system is a programmer's best friend by aidos on Saturday November 05, 2022 @08:34PM (#33487465)
They’re different experiences for users with different priorities.

Your priority is evidently ease of configuration, VScode is definitely easier out of the box - there is no comparison.

My priority is hackabilty for streamlined editing and code navigation. Here neovim run circles around vscode.

Neither is “right”, just different preferences. Note that I’m not here to tell you my editor is better, I personally enjoy using it more, and that’s enough for me.

Re:The type system is a programmer's best friend by throwaway675309 on Sunday November 06, 2022 @01:40AM (#33489482)
"My priority is hackabilty for streamlined editing and code navigation. Here neovim run circles around vscode."

I hear this line a lot from hard-core vim users, but it's always talked about in the general. I'd like to hear a specific use case and exact scenario where a common workflow is faster in neovim over a more full featured IDE like JetBrains or Visual Studio.

Re:The type system is a programmer's best friend by aidos on Sunday November 06, 2022 @05:21AM (#33491077)
Little mini example in terms of LSP behaviour is fixing this bug:

https://github.com/microsoft/TypeScript/issues/37816

I edit the results of gotoDefinition on the way through so I always jump to the one I want.

Re:The type system is a programmer's best friend by tsimionescu on Saturday November 05, 2022 @01:23PM (#33483490)
Note that Emacs had these features and more for Lisp since about the 1990s or even earlier, while being free.

Now, for C or Java or most other popular languages, you're absolutely right.

Re:The type system is a programmer's best friend by pcthrowaway on Saturday November 05, 2022 @01:29PM (#33483554)
For the people responding to you who are saying you can get all the same things in vim, they're right of course, but a lot of this modern functionality is now built on top of the Language Server Protocol[1], which is an open standard created by microsoft for VS Code.

Kudos on the people who have ported this to Vim[2], but I suspect the support for LSP features will still be better in VS Code

[1] https://microsoft.github.io/language-server-protocol/

[2] https://github.com/prabirshrestha/vim-lsp

Re:The type system is a programmer's best friend by jrumbut on Saturday November 05, 2022 @02:01PM (#33483941)
Code completion, search, cross referencing, and all sorts of other features in vim, emacs, and all kinds of other editors (including Visual Studio) predate LSP by decades.

LSP is cool though, an advancement certainly, but it is not a completely new thing.

https://en.m.wikipedia.org/wiki/Ctags

Re:The type system is a programmer's best friend by jcelerier on Saturday November 05, 2022 @08:08PM (#33487301)
If you think ctags or rtags are even remotely comparable to what an actual IDE brings you you have absolutely never used more than 10% of what an IDE with semantic understanding of your code can do
Re:The type system is a programmer's best friend by jrumbut on Monday November 07, 2022 @05:21PM (#33513395)
The post I replied to brought up a very narrow set of features that had been available in vim for a long time.
Re:The type system is a programmer's best friend by jcelerier on Monday November 07, 2022 @07:04PM (#33514334)
it brings up "Code completion, search, cross referencing, and all sorts of other feature" and again, those work much less well in ctags / rtags than with a proper IDE
Re:The type system is a programmer's best friend by abraxas on Saturday November 05, 2022 @03:16PM (#33484664)
Kind of a perpendicular discussion but it amazes me that with this language server thing hipsters turned what was a very mature and proven pattern (a plugin architecture) into a distributed software problem. My god talk about doing shit the hard way...
Re:The type system is a programmer's best friend by quicklime on Saturday November 05, 2022 @04:15PM (#33485177)
The plugin architectures that I've seen require plugins to be written in the same language as the IDE. For example, Eclipse plugins need to be written in Java.

Language servers run in a separate process to the editor, and they communicate over JSON-RPC. The nice thing about this is that the language server can be written in any programming language - which usually ends up being the language of the code being edited, rather than the language that the editor was written in.

This makes it a lot easier for language servers to be written and maintained by the people who maintain the compilers for those languages, e.g. gopls is written by the Go developers, clangd is part of LLVM.

Re:The type system is a programmer's best friend by morelisp on Saturday November 05, 2022 @06:45PM (#33486734)
LSPs turn a M*N problem into an (ideally) M+N problem. You can't do that with any existing single editor's plugin architecture, basically by definition.

Ctags is a better analogy, and LSP has fairly obvious advantages compared to it.

Re:The type system is a programmer's best friend by maxbond on Saturday November 05, 2022 @01:24PM (#33483498)
Just so you know, you can do those things in vim now (I do), and the whippersnappers may well be.
Re:The type system is a programmer's best friend by sidlls on Saturday November 05, 2022 @01:29PM (#33483552)
emacs has plenty of plug-ins that turn it into an IDE. For people who are already comfortable with it, these tools are great. I’m more productive in it than with “modern” tools. For newcomers the learning curve is going to be steeper. I’d recommend learning at least either vim or emacs (or perhaps other text-based editors with similar features, if they exist) though, as it does provide versatility for oneself. Those GUI IDEs aren’t available in every environment where one might want to code or debug.
Re:The type system is a programmer's best friend by rleigh on Sunday November 06, 2022 @06:38AM (#33491522)
> I’m more productive in it than with “modern” tools.

Are you objectively more productive, or do you perceive yourself to be more productive?

I was an Emacs diehard for nearly two decades. Then I got introduced to CLion. In retrospect, Emacs was an inferior tool.

While it's certainly true that Emacs can be configured and extended to do all sorts of interesting things, I do suspect many of its users are deluding themselves when it comes to the sheer breadth and depth of functionality offered by a modern IDE. It also doesn't help that its maintainers are living several decades in the past, and haven't kept up with what the "competition" are doing.

I'm certainly not implying that Emacs is a poor editor. Very few IDEs have text editing capabilities on par with that. But its integration with other tools doesn't hold a candle to today's IDEs.

Re:The type system is a programmer's best friend by czx4f4bd on Saturday November 05, 2022 @01:22PM (#33483470)
Both Vim and Emacs have plugins for intelligent code completion and viewing inline documentation. I personally prefer to use VS Code or Jetbrains IDEs with Vim emulation, but I've seen setups for both Vim and Emacs that basically made them into full-fledged IDEs.
Re:The type system is a programmer's best friend by SAI_Peregrinus on Saturday November 05, 2022 @04:44PM (#33485482)
Full-fledged Plugin-based development environments. An IDE is an Integrated Development Environment; it comes with the features necessary for efficient development built-in.
Re:The type system is a programmer's best friend by _qua on Saturday November 05, 2022 @02:30PM (#33484234)
When I was a kid learning to code, programming books recommended stridently against even using syntax highlighting. It's funny how helpful things get rejected by people who "did it the hard way."
Re:The type system is a programmer's best friend by jlarocco on Saturday November 05, 2022 @03:35PM (#33484798)
> When I meet a young chap extolling the benefits of Vim or Emacs or really anything that doesn't have stepped debugging and code competition...well, there are no bonus points for doing things the hard way.

I can't speak for Vim, but Emacs has stepped debugging, code completion, etc.

Part of the reason I use Emacs is that I get those features and (and a ton more) with the same light weight interface across multiple languages and platforms. At the same time, it's usually very easy to make Emacs work with third-party tools. At work, for example, I spend most of my time in Emacs, but can easily switch to the MSVC++ debugger when I need it. It's the best of everything.

Meanwhile, I get to listen to my coworkers celebrate new features in VSCode that I've used in Emacs for years...

There are no bonus points for learning a new IDE for every project, either.

Re:The type system is a programmer's best friend by throwaway2037 on Sunday November 06, 2022 @02:45AM (#33490185)
You wrote: <<There are no bonus points for learning a new IDE for every project, either.>>

I'm confused. The point of uber-IDEs (IntelliJ, Eclipse, NetBeans, VSCode): You only need to learn one IDE. Each language is just another plug-in.

Re:The type system is a programmer's best friend by titzer on Saturday November 05, 2022 @03:45PM (#33484884)
Wait until you learn about these 2002 technologies called "extract method", "inline method", "encapsulate fields", "extract interface", "extract local", "inline local", "rename", plus about 200 code inspections...
Re:The type system is a programmer's best friend by christophilus on Saturday November 05, 2022 @03:56PM (#33485004)
I remember using Visual Studio in college (back in 1999, 2000) and coding circles around folks who were using text editors.

These days, I use Neovim + LSP for a pretty decent approximation of an IDE-- it's quite good. Still not as good as Visual Studio + C#, but I'm on Linux now, and not writing C# anymore, and I definitely prefer an open-source, general purpose, light-weight, customizable editor.

Re:The type system is a programmer's best friend by yellowapple on Saturday November 05, 2022 @04:32PM (#33485351)
Emacs (with e.g. company) can do much of the same, without my laptop making a fighter jet sound quiet in comparison :)
Re:The type system is a programmer's best friend by outworlder on Saturday November 05, 2022 @06:08PM (#33486411)
> When I meet a young chap extolling the benefits of Vim or Emacs or really anything that doesn't have stepped debugging and code competition...well, there are no bonus points for doing things the hard way.

Why do you think they are doing things "the hard way"? Emacs can have all the things you have described. And it can do that for languages that are not part of the .NET Framework, they just have to have a language server implementation.

The problem with "IDEs" was placing all your eggs into a single basket. You are on Visual Studio and then you need to do some Java(or Scala, or whatever). And now you have to get another IDE. Some tried to be one IDE to rule them all (Eclipse, Netbeans), didn't work all that well.

You need a good editor - most IDEs don't have one. You need integration with your language of choice. You need compilers and linters and a bunch of other things. Better to glue components that do one thing and do it well, than having one IDE trying to do everything. And those components can fell just as integrated.

Re:The type system is a programmer's best friend by closeparen on Saturday November 05, 2022 @06:41PM (#33486701)
>Why do you think they are doing things "the hard way"? Emacs can have all the things you have described

In the first few years of my career I would try every few months to get these things actually installed and working in emacs. It was definitely the hard way.

>The problem with "IDEs" was placing all your eggs into a single basket

Eclipse is a uniquely terrible piece of software and I understand why it might polarize people against IDEs for life. The JetBrains products are pretty good and, importantly, modular and consistent enough across languages that I don't mind.

Re:The type system is a programmer's best friend by aidos on Saturday November 05, 2022 @08:41PM (#33487508)
I believe they’re talking about a different “hard way” in that they’re suggesting you can’t have those features in vim / eMacs etc and have to do something else instead.

You’re not wrong, configuring these editors is 100% the hard way (that doesn’t stop me from doing it though).

Re:The type system is a programmer's best friend by MrJohz on Saturday November 05, 2022 @06:39PM (#33486679)
IntelliJ and VSCode have both worked pretty well with anything I've thrown at them, I'd consider them both fairly successful as IDEs "to rule them all". Obviously they're both very different ends of the IDE spectrum, but they've both had intellisense, debugging, type revealing, and refactoring features for all the mainstream languages I've tried with them. VSCode particularly tends to integrate well with LSPs.
Re:The type system is a programmer's best friend by throwaway2037 on Sunday November 06, 2022 @03:02AM (#33490273)
You wrote: <<You need a good editor - most IDEs don't have one.>> Can you name any?
Re:The type system is a programmer's best friend by aidos on Saturday November 05, 2022 @08:39PM (#33487495)
Everything is pretty much just language servers behind the scenes these days anyway. Neovim supports those natively, so those young chaps aren’t talking about your vim of yesteryear.
Re:The type system is a programmer's best friend by flippinburgers on Sunday November 06, 2022 @03:08AM (#33490311)
I am older than you and use neovim. It has code completion thank you very much.
Re:The type system is a programmer's best friend by Viliam1234 on Sunday November 06, 2022 @08:19AM (#33492275)
> there are no bonus points for doing things the hard way

Perhaps not from the technical perspective, but there are social advantages if you are a member of a clique that does things the hard way and considers it a sign of competence. ("Any idiot can write a code that compiles using autocomplete and syntax highlighting, but it takes a true master to achieve the same result using a decades-old Linux equivalent of Notepad.") They don't seem to understand that the ancient masters did things the hard way not as a pointless exercise, but simply because the easy way was not available back then.

Re:The type system is a programmer's best friend by bluGill on Saturday November 05, 2022 @01:10PM (#33483355)
I'm adverse to debuggers as i've more than once caught myself following a rabbit hole of steping through code instead of thinking.

IDEs have some use, and static analysis has proven to catch the same mistake over and over, but only as the authors of those tools have discovered that false positives cannot be allowed ever, once there is a false positive the tools is worthless.

Re:The type system is a programmer's best friend by icedchai on Saturday November 05, 2022 @01:56PM (#33483877)
I find debuggers more valuable with dynamically typed languages, especially Python. It's handy to be able to drop a `breakpoint()` in the middle of a script when you have no idea what a function is actually returning. The happens more often than you might think.
Re:The type system is a programmer's best friend by bornfreddy on Saturday November 05, 2022 @05:52PM (#33486242)
Of course, when this happens the bug you are hunting is not your only problem. You really should clean up the code to make it clear what is being returned from the function.
Re:The type system is a programmer's best friend by astrobe_ on Saturday November 05, 2022 @02:38PM (#33484317)
> I'm adverse to debuggers as i've more than once caught myself following a rabbit hole of steping through code instead of thinking.

Yes. I was kind of forced to think and do printf debugging at the beginning of my career, after having used before that (as a hobbyist) quite good asm debuggers.

Maybe that's just me, but I also was, I believe, a bit over-reliant on the debugger - I would just compile, run, see what happen, and launch the debugger if something didn't work.

Nowadays I could use sometimes a debugger - but the system I work with is sort of soft real-time so stopping at a breakpoint of even slight changes in timings can change the context - in some cases even printf debugging could make a bug vanish.

If nothing else, debugging without a debugger is a good exercise in logically thinking - and it can save time too.

Re:The type system is a programmer's best friend by Gibbon1 on Saturday November 05, 2022 @05:34PM (#33486067)
I have a code base that is a mix of hard, soft, and static real time. I have a command line interface built in to it and a lot debug logging that can be re-enabled.

I've also spent some effort into making it tolerate being interrupted. And there is also the good old technique of inserting break points while the code is running.

Re:The type system is a programmer's best friend by titzer on Saturday November 05, 2022 @03:48PM (#33484918)
Yes yes, indeed. The ability to look at what's happening step by step really hamstrings my imagination. Sorry, no. A debugger is a microscope! It will help you find problems faster and will seed your imagination by filling in what is really going on. It's an augmentation, like any tool. Or do you prefer to stare into space blindfolded?
Re:The type system is a programmer's best friend by morelisp on Saturday November 05, 2022 @04:15PM (#33485179)
> A debugger is a microscope! ... Or do you prefer to stare into space blindfolded?

Perhaps illustrating the original point, microscopes aren't used to stare into space. A debugger is a microscope but the most pernicious bugs don't benefit from such a thing.

Re:The type system is a programmer's best friend by acchow on Saturday November 05, 2022 @03:15PM (#33484659)
This must be strictly an old school Valley problem. Up in the city, IDEs are standard. How would you even write Scala at Twitter without an IDE?
Re:The type system is a programmer's best friend by outworlder on Saturday November 05, 2022 @06:10PM (#33486427)
Re:The type system is a programmer's best friend by acchow on Sunday November 06, 2022 @03:04PM (#33496476)
This is an LSP backend for Emacs. In what way is this setup not an IDE?
Re:The type system is a programmer's best friend by closeparen on Saturday November 05, 2022 @06:44PM (#33486725)
There's a pendulum swing back and forth between local compilation being supported or not... currently settling onto VSCode remote, with Jetbrains Gateway trying to catch up in usability.
Re:The type system is a programmer's best friend by posharma on Saturday November 05, 2022 @03:37PM (#33484822)
Oh man! why did you bring up IDEs. Now we'll argue ad nauseam about emacs vs vi vs vim vs etc.
Re:The type system is a programmer's best friend by pjmlp on Saturday November 05, 2022 @03:43PM (#33484874)
Yeah, the same culture worships using their powerful computers in 2022, the same way I was using Xenix in 1993.

Talk about progress.

Re:The type system is a programmer's best friend by emodendroket on Saturday November 05, 2022 @01:17PM (#33483428)
I don’t disagree that it makes things much more pleasant, but I started doing this around 2013, which, while old, was still a year beginning with 20, and consensus was trending the opposite way and people were bullish about stuff like Ruby. The pendulum has really swung in the other direction.
Re:The type system is a programmer's best friend by noobermin on Saturday November 05, 2022 @01:25PM (#33483518)
No, there really is no new insight OP or anyone else has.

At the end of the day, the only thing that matters is writing something that works and works well. Hackers go through too many moodswings to be worth paying attention to when they start telling you how you should code.

Re:The type system is a programmer's best friend by zbentley on Saturday November 05, 2022 @02:16PM (#33484089)
> Hackers go through too many moodswings

True! But computer scientists (who are sometimes also hackers, sometimes not) apply research methods to existing codebases and the practice of coding, and have repeatedly presented findings that indicate that, mood swings/fads/hype cycles aside, some techniques really do deliver better software quicker. The VPRI STEPS work is an interesting example of this.

That's not to say that every CS methodology paper should be taken as gospel; we have problems just like other disciplines, sometimes more. But it's a far cry from post-hoc rationalization and hacker mood swings.

Re:The type system is a programmer's best friend by emodendroket on Saturday November 05, 2022 @02:45PM (#33484373)
Doesn't really follow that because you ended up with a successful product that means it was the best way you could have possibly done it. I don't feel like I'd learned everything I know today the first time I delivered a successful product, and I doubt I know everything I'll know in the future either.
Re:The type system is a programmer's best friend by waprin on Saturday November 05, 2022 @03:00PM (#33484518)
I noticed this too and I have a simple explanation.

Ruby and Python overtook Java and C++ in the early 2010s in _spite_ of their lack of a good typing system, not because of it. On the whole, they are much more productive languages.

Now we're seeing languages that have Ruby / Python productivity but also have much better ways of static typing such as Typescript and Swift. And the Ruby / Python community is more open to static types as well.

The problems of ~2010 Java and C++ were mistakenly pinned on static types and the framing of "static vs dynamic languages" was always a red herring. Java and C++ were just crappy languages (at least in 2010, not sure about modern incarnations).

It really is a shame that Swift is so confined to the iOS world because it's such a great example of how you can have a language that feels like a scripting language but with much more advanced type safety.

Re:The type system is a programmer's best friend by grumpyprole on Saturday November 05, 2022 @06:14PM (#33486471)
> [Swift] it's such a great example of how you can have a language that feels like a scripting language but with much more advanced type safety.

There are great examples far older than Swift, for example ML that dates back to the 1970s, or OCaml that's as old as Java.

Re:The type system is a programmer's best friend by emodendroket on Sunday November 06, 2022 @01:41AM (#33489882)
This seems compelling. I do think Java has done a lot to mitigate the tedium of writing it in the meantime but my acquaintance with it is pretty casual.
Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @01:29PM (#33483557)
But there's a paradox.

Why does 100,000 lines of code of python tend to be safer and more manageable then 100,000 lines of C++ despite the fact that python has no type checker and C++ has a relatively advanced type checker?

Why do startups choose a python web stack over a C++ web stack?

I don't think it's "self-evident." I think there's something more nuanced going on here. Hear me out. I think type systems are GREAT. I think python type hints and typescripts are the way forward. HOWEVER, the paradox is real.

Think about it this way. If you have errors in your program, does it matter that much if those errors are caught during runtime or compile time? An error in compile time is caught sooner rather then later but either way it's caught. YOU are protected regardless.

So basically compile time type checking just makes some of the errors get caught earlier which is a slight benefit but not a KEY differentiator. I mean we all run our code and test it anyways despite whether the system is typed or not so the programmer usually finds most of these errors anyways.

So what was it that makes python easier to use then C++?

Traceability and determinism. Errors are easily reproduced, languages that always display the same symptoms from certain errors and in turn deliver error messages that are clear and are readable. These are really the key factors. C++ on top of non-deterministic segfaults, astonishingly even has compile time messages that can confuse users even further.

Re:The type system is a programmer's best friend by tikhonj on Saturday November 05, 2022 @01:56PM (#33483879)
There is no "paradox". C++ is dangerous because of memory management and awful semantics (undefined behavior/etc), both of which are orthogonal to static typing.

It's a bit like saying that there's a paradox: everyone says that flying is safer than driving, but experimental test pilots die at a much higher rate than school bus drivers!

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @04:24PM (#33485269)
Paradoxes don't exist in reality. It's a figure of speech based on something that was perceived as a paradox. This much is obvious.

Much of the fervor around dynamically typed languages in the past was driven largely by the dichotomy between c++ and other dynamically typed languages.

Nowadays it's more obvious what the differentiator was. But the point im making here is that type checking is NOT the key differentiator here.

Re:The type system is a programmer's best friend by gary17the on Saturday November 05, 2022 @02:19PM (#33484113)
> So basically compile time type checking just makes some of the errors get caught earlier which is a slight benefit but not a KEY differentiator.

Unfortunately, I have to completely disagree here, at least based on my experience. Shifting software error detection from runtime to compile time is absolutely paramount and, in the long run, worth any additional effort required to take advantage of a strong type system.

Firstly, writing unit tests that examine all the possible combinations and edge cases of software component input and state is... an art that requires enormous effort. (If you don't believe me, talk to the SQLite guys and gals, whose codebase is 5% product code and 95% unit test code.)

Secondly, writing automated UI tests that examine all the possible combinations and edge cases of UI event processing and UI state is... next to impossible. (If you don't believe me, talk to all the iOS XCUI guys and gals who had to invent entire dedicated Functional Reactive paradigms such as Combine and SwiftUI. ;) J/K)

Thirdly, I don't even want to get into the topic of writing tests for detecting advanced software problems such as memory corruption or multi-threaded race conditions. Almost nobody really seems to know how to write those truly effectively.

> So what was it that makes python easier to use then C++?

The Garbage Collector, which is side-stepping all the possible memory management problems possible with careless C++. However, a GC programming language probably cannot be the tool of choice for all the possible problem domains (e.g., resource-constrained environments such as embedded and serverless; high-performance environments such as operating systems, database internals, financial trading systems, etc.)

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @04:31PM (#33485340)
Your argument makes no sense. I say the type checker is not the key differentiator then you say for python the key differentiator is the garbage collector.

So that makes your statement contradictory. You think type checkers are important but you think python works because of garbage collection.

Either way I'm not talking about the implementation of the language. I'm talking about the user interface. Why is one user interface better than the other?

I bet you if c++ has sane error messages and was able to deliver the exact location of seg faults nobody would be complaining about it as much. (There's an implementation cost to this but I am not talking about this)

Even an ugly ass language like golang is loved simply because the user interface is straight forward. You don't get non deterministic errors or unclear messages.

Re:The type system is a programmer's best friend by za3faran on Saturday November 05, 2022 @05:15PM (#33485856)
GC was one of the most important and relevant features (if not the most important) that allowed Java to penetrate, and eventually dominate the space where C++ used to be relevant in terms of middleware/business type applications. This detail matters a lot in this discussion. Then once that is taken as a given, you can compare different GC enabled languages based on other factors, such as type safety (or lack thereof in the case of python).
Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @07:59PM (#33487252)
If it does matter to the conversation then it's evidence supporting my point. I'm saying type checking isn't a key differentiator between something like JS/ruby/python vs. C++. You're implying the GC is the key differentiator.

If you're saying that you CAN'T compare the python to C++ because of the GC then I disagree. GC only stops memory leaks. That is not the most frequent error that happens with C++. Clearly if you just subtract memory leak issues from C++ there's still a usability issue with just C++.

Re:The type system is a programmer's best friend by za3faran on Saturday November 05, 2022 @09:40PM (#33487921)
GC is not just for memory leaks, but memory safety in general. It also enables several paradigms that are extremely difficult to get right without memory safety.

In order to have a proper comparison, you should control for variables that are irrelevant to the experiment. In this case, you want to look at the effect of typing, so you should control for GC. Which is why you should compare python to other GC'd static languages, but not to static non-GC'd languages.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @11:55PM (#33488759)
>GC is not just for memory leaks, but memory safety in general.

No this is not true. Memory safety and memory leaks are different concepts. You can trigger a memory leak without violating memory safety. In fact a memory leak is not really an error recognized by an interpreter or a compiler or a GC. It is a logic error. A memory leak is only a leak because you interpret it as a leak. Otherwise the code is literally doing what you told it to do. It's similar to a logic error. I mean think about it, the interpreter can't know whether you purposefully allocated 1gb of memory or whether you accidentally allocated it.

Memory safety on the other hand is protection against violation of certain runtime protocols. The interpreter or runtime knows something went wrong and immediately crashes the program. It is a provable violation of rules and it is actually not open to interpretation like the memory leak was.

See python: https://docs.python.org/3/library/gc.html. You can literally disable the GC (during runtime) and the only other additional crash error that becomes more frequent is OOM. The GC literally just does reference counting and generational garbage collection... that's it.

I can tell you what makes python MORE memory safe then C++. It's just an additional runtime checks that are not zero cost.

  x = [1,2]
  print(x[2])
The above triggers an immediate exception that names the type of error (out of bounds) and the exact line that triggered it. This error will occur regardless of whether or not you disabled the GC. It happens because every index access to a list also checks against a stored length. If you're above that length it raises an exception. It's not zero cost but it's more safe.

For C++:

   int x[] = {1,2};
   std::cout<<x[2]<<std::endl;
This triggers nothing. It will run even though index 2 is beyond the bounds of the array. There is no runtime check because to do so would make the array data structure not zero cost. This is what happens during buffer overflows. It's one of the things that makes C++ a huge security problem.

Let's look at the type issue.

    def head(input_list: List[int]) -> Optional[int]:
        return input_list[0] if len(input_list) > 0 else None

    x: int = head(2)
--------------------

    #include <optional>
    #include <vector>
    std::Optional<int> head(const std::vector<int>& input_list){
         return input_list.length() > 0 ? input_list[0] : std::nullopt; 
    }
    
    int main(){
       auto x = head(2)
       return 0;
    }

Both pieces of code are identical. Python is type annotated for readability (not type checked). But both literally produce the same error messages. Both will tell you there's a type error. It's just python happens at runtime and C++ happens at compile time. C++ has a slight edge in the fact that the error is caught as a static check. But this is only a SLIGHT advantage. Hopefully this example will allow you to see what I'm talking about. A minority of bugs are exclusively caught with type checking because runtime still catches a huge portion of the same bugs... and in general this is why overall C++ is still MUCH worse in terms of usability then python despite type checking.
Re:The type system is a programmer's best friend by za3faran on Sunday November 06, 2022 @07:24PM (#33499377)
I don't think anyone is arguing that C++ is more difficult to use than Python, and much less safe. The question is how does python stack up to Java or C#? As you can see in this thread and many other discussions on this forum and elsewhere, people with experience working on larger systems will tell you that it doesn't.
Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @11:29PM (#33501151)
If you had jobs in both stacks as I have you'll see that the differences are trivial. Python can get just as complex as either c# and java.

Those other people your copying your argument from likely only had jobs doing Java or C# and they did some python scripts on the side and came to their conclusions like that. I have extensive experience for production work in both and I can assure you my conclusions are much more nuanced.

Python and java stack up pretty similarly in my experience. There's no hard red flags that make either language a nightmare to use when compared to the other. People panic about runtime errors, but like I said those errors happen anyway.

Python does however have a slight edge in the fact that it promotes a more humane style of coding by not enforcing the oop style. Java programmers on the otherhand are herded into doing oop so you have all kinds of service objects with dependency injection and mutating state everywhere. So what happens is in Java you tend to get more complex code, while python code can be more straightforward as long as the programmer doesn't migrate their oop design patterns over to python.

That's the difference between the two in my personal experience. You're mostly likely thinking about types. My experience is that those types are not that important, but either way, modern python with external type checkers actually has a type system that is more powerful then Java or C#. So in modern times there is no argument. Python wins.

But prior to that new python type system my personal anecdotal experience is more relevant and accurate then other people's given my background in both Java and python And C++. Types aren't that important period. They are certainly better then no types but any practical contribution to safety is minimal.

Re:The type system is a programmer's best friend by gary17the on Saturday November 05, 2022 @05:24PM (#33485971)
No contradiction, really, it's just that we are talking about two different programming goals: I emphasize the goal of producing well-behaved software (especially when it comes to large software systems), while you emphasize the goal of producing software in an easier (more productive) manner. For my goal, a strong type system is a key differentiator. For your goal, a garbage collector is a key differentiator. The discussion probably comes to down to the question of whether garbage-collected, weakly-typed Python is as "bug-prone" as memory-managed, strongly-typed C++. I have no significant experience with Python, so I cannot answer authoritatively, but I suspect your assumption that "100,000 lines of code of python tend to be safer and more manageable then 100,000 lines of C++" might be wrong. In a large codebase, there will probably be many more dynamic-typing error opportunities (after all, the correct type has to be used for every operation, every function call, every calculation, every concatenation, etc.) than memory-management error opportunities (the correct alloc/dealloc/size has to be used for every pointer to a memory chunk; but only if C++ smart pointers are not used).
Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @11:56PM (#33488770)
>but I suspect your assumption that "100,000 lines of code of python tend to be safer and more manageable then 100,000 lines of C++" might be wrong.

I can give you my anecdotal experience on this aka "authoritative" in your words. I am a really really really good python engineer with over a decade of experience. For C++ I have 4 years of experience, I would say I'm just ok with it.

Python is indeed safer then C++. Basically when you check for type errors at runtime, you actually easily hit all reasonable use cases pretty quickly. This is why unit testing works in reality even though your only testing a fraction of the domain.

Sure this isn't a static proof but in Practical terms static type checking is only minimally better then run-time type checking. You can only see this once you have extensive experience with both languages and you see how trivial type errors are. Practicality of technologies isn't a property you can mathematically derive, it's something you get a feel for once you've programmed enough in the relevant technologies. It helps you answer the question of "How often and how easy do type errors occur uncaught by tests?" Not that much more often and not hard at all to debug.

The thing that is actually making C++ less usable are the errors outside of type checking. The memory leaks, the segfaults, etc. The GC basically makes memory leaks nearly impossible and python doesn't have segfaults period. What python does is fail fast and hard once you write something outside of memory bounds. Basically it has extra run time checks that aren't zero cost that make it much much more safe.

All of this being said, I am talking about type-less python above... when I write python, I am in actuality a type Nazi. I extensively use all available python type hints including building powerful compositional sum types to a far more creative extent then you can with C++. I am extremely familiar with types and python types. I have a very detailed viewpoint from both sides of the spectrum from both languages. That's why I feel I'm qualified to say this.

>No contradiction, really, it's just that we are talking about two different programming goals: I emphasize the goal of producing well-behaved software (especially when it comes to large software systems), while you emphasize the goal of producing software in an easier (more productive) manner.

I'm actually partly saying both. Python is both easier and more well-behaved and more safe. The "well-behaved" aspect has a causal relationship to "easier". It makes sense if you think about it. Python behaves as expected more so then C++.

Literally I repeat: Python (even without types) is categorically safer then C++. I have a total of 14 years of experience in both. I would say that's enough to form a realistic picture.

Re:The type system is a programmer's best friend by throwaway2037 on Sunday November 06, 2022 @03:22AM (#33490394)
"financial trading systems" This is a myth. Many financial trading systems are written in C# and Java. Don't be distracted by the 1% of hedge funds with lousy funding that need nanosecond reactions to make money. If you have good funding, product diversity matters more than speed.

Otherwise, your post is excellent. Lots of good points. SQLite is something that EADS/ESA/NASA/JAXA would write for a aeroplane / jet fighter / satellite / rocket.

Re:The type system is a programmer's best friend by gary17the on Sunday November 06, 2022 @01:50PM (#33495783)
Thanks, glad you found the post useful.

I'm sure C# and Java make excellent programming languages for many if not most financial applications, but I meant that in the context of high-volume Enterprise Application Integration (EAI). Basically financial message transformation, explosion, summarization, audit, etc. across multiple financial institutions. The volume of messages to be processed was quite considerable, so nobody even thought about taking the risk of switching from battle-tested C++ to anything else.

Re:The type system is a programmer's best friend by throwaway2037 on Monday November 07, 2022 @09:17AM (#33506027)
I am sure your use case was incredibly specific. For insane performance requirements plus enterprise software that is not greenfield, basically everything is C++.

No trolling. Have you ever seen the high-frequency Java stuff from Peter Lawrey's Higher Frequency Ltd.? It is insanely fast. Also, LMAX Disruptor (Java) data structure (ring buffer) is also legendary. I have seen it ported to C++. That said, you can beat all of this with C++, given enough time and resources!

Re:The type system is a programmer's best friend by deltasevennine on Monday November 07, 2022 @10:06AM (#33506726)
Another thing you're not addressing here is basically Type checking solves none of the problems you describe. You claim it's extraordinarily hard to write tests for UI and for memory corruption. And that's your argument for type checkers? It's next to impossible to type check UI and memory corruption. So your argument has no point here.

SQlite is written in C. It has type checking. Yet people still write unit tests for it. Why? Because type checking is mostly practically inconsequential. All your points don't prove anything. It proves my point.

All the problems you talk about can be solved with more advanced proof based checkers. These systems can literally proof check your entire program to be fully in spec precompile time. It goes far beyond just types. Agda, Idris, Coq, and Microsofts lean have facilities to prove your programs to be fully correct 100% of the time. They exist. But they're not popular. And there's a reason for that.

You say it's paramount to move error detection to compile time. I say, this problem is ALREADY solved, but remains unused because these methods aren't PRACTICAL.

Re:The type system is a programmer's best friend by gary17the on Tuesday November 08, 2022 @08:52AM (#33518907)
> It's next to impossible to type check UI

Incorrect. Have a look at the Swift OpenCombine library. Multiple Publishers of a particular type that emits a single boolean value (e.g., an "Agree to Terms" UI checkmark and an "Agree to Privacy Policy" UI checkmark) are combined at compile-time to be transformed into a single Publisher of a type that emits only a single boolean value (e.g., the enabled/disabled state of a "Submit" button). Effectively, it is not possible to even compile an app that incorrectly ignores one of the "Agree" checkmarks before enabling/disabling the "Submit" button.

> It's next to impossible to type check (...) memory corruption

Incorrect. Have a look at the Rust standard library. Sharing data across multiple treads requires passing a multi-threaded Mutex type; attempting to share data through a single-threaded Rc (reference-counted) type will not compile. Once the Mutex type is passed, each thread can only access the memory the Mutex type represents by acquiring another type, a MutexGuard, through locking. Effectively, it is not possible to even compile a program that incorrectly ignores multi-threading or incorrectly accesses memory in a race condition with other threads thus possibly corrupting that memory. Moreover, it is also not possible for a thread not to properly release a lock once the MutexGuard type goes out of scope.

> All the problems you talk about can be solved with more advanced proof based checkers.

Unlikely. Without feeding strong type information that describes your problem domain into a checker, the checker cannot reason about your code and figure out possible errors. A strong type system is a "language" for a programmer to communicate with his or her checker.

> You say it's paramount to move error detection to compile time. I say, this problem is ALREADY solved, but remains unused because these methods aren't PRACTICAL.

> [the C language] has type checking. Yet people still write unit tests for it. Why? Because type checking is mostly practically inconsequential.

Please do not hold it against me if I do not continue commenting here - you must be from a different, parallel Universe. (How is Elvis doin' on your end? ;) J/K)

Re:The type system is a programmer's best friend by deltasevennine on Tuesday November 08, 2022 @09:39AM (#33519437)
>Incorrect. Have a look at the Swift OpenCombine library. Multiple Publishers of a particular type that emits a single boolean value

First off types can't emmit values. Types don't exist at run time. They're simply meta info for the compiler to run checks. Thus they can't emmit anything. Second if you're talking about something that emmits a value then it involves logic that doesn't have to do with UI. A UI is not about logic, it is simply a presentation given to the user, all logic is handled by things that AREN'T UI based.

UI would be like html and css. Can you type check html and css make sure the hackernews UI is correct? There is no definition of correctness in UI thus it can't be type checked. The example you're talking about is actually type checking the logic UNDERNEATH the UI.

>Effectively, it is not possible to even compile a program that incorrectly ignores multi-threading or incorrectly accesses memory in a race condition with other threads thus possibly corrupting that memory. Moreover, it is also not possible for a thread not to properly release a lock once the MutexGuard type goes out of scope.

This is different. It's not type checking memory corruption. It's preventing certain race conditions by restricting your code such that you can't create a race condition. There's a subtle difference here. You can violate Rusts constraints in C++ yet still have correct code. Type checking memory corruption would involve code that actually HAS a memory corruption, and some checker proving it has a memory violation. My statement still stands Memory corruption cannot be type checked.

Think about it. A memory corruption is an error because we interpret to be an error. Logically it's not an error. The code is doing what you told it to do. You can't check for an error that's interpreted.

At best you can only restrict your code such that ownership lives in a single thread and a single function always which is what rust does. This has a cost such that implementing doubly linked lists are a hugely over complicated in rust: https://news.ycombinator.com/item?id=16442743. Safety at the cost of highly restricting the expressiveness of the language.

>Unlikely. Without feeding strong type information that describes your problem domain into a checker, the checker cannot reason about your code and figure out possible errors. A strong type system is a "language" for a programmer to communicate with his or her checker.

No no, you're literally ignorant about this. There's a whole industry out there of automated proof checking of code via type theory and type systems and there's technology that enables this. It's just not mainstream. It's more obscure then haskell but it's very real.

It's only unlikely to you because you're completely ignorant about type theory. You're unaware of how "complex" that "language" can get. Dependent types is one example of how that "type language" can actually "type check" your entire program to be not just type correct but logically correct. Lean, Idris, Coq, Agda, literally are technologies that enable proof checking at the type level. It's not unlikely at all. it's reality.

>Please do not hold it against me if I do not continue commenting here - you must be from a different, parallel Universe. (How is Elvis doin' on your end? ;) J/K)

Wow. This is just fucking rude. I'm sorry but you're ignorant and you're about to have a rude awakening about just how ignorant you are. Take a look at this: https://www.ben-sherman.net/posts/2014-09-20-quicksort-in-id...

It's quick sort implemented in a language called idris. The implementation is long because not only is it just quick sort, the programmer is utilizing the type system to PROVE that quick sort actually does what it's suppose to do (sort ordinal values).

I'd appreciate an apology if you had any gall. But you likely won't "continue commenting here". Wow just wow. I am holding it against you. I didn't realize how stupid and rude people can actually be.

Re:The type system is a programmer's best friend by gary17the on Tuesday November 08, 2022 @10:36AM (#33520111)
> First off types can't emmit values. Types don't exist at run time.

Incorrect. Please have a look at OpenCombine sources, or, say, at the Typestate Pattern in Rust:

http://cliffle.com/blog/rust-typestate/

"Typestates are a technique for moving properties of state (the dynamic information a program is processing) into the type level (the static world that the compiler can check ahead-of-time)."

> you're completely ignorant about type theory. (...) This is just fucking rude. (...) I didn't realize how stupid and rude people can actually be.

Yes, of course, naturally, you must be right, how blind could I have been?

> I'd appreciate an apology if you had any gall.

Sure, sorry about my little previous joke[1], meant no factual offense. The very best of luck to you as a programmer and a wonderful human being with great sense of humor.

[1] "Topper: I thought I saw Elvis. Block: Let it go, Topper. The King is gone. Let's head for home." ("Hot Shots!", 1991)

Re:The type system is a programmer's best friend by deltasevennine on Wednesday November 09, 2022 @12:31AM (#33527824)
>Sure, sorry about my little previous joke[1], meant no factual offense. The very best of luck to you as a programmer and a wonderfully polite human being with a great sense of humor.

Jokes are supposed to be funny. Not offensive. Your intent was offense under the guise of humor. Common tactic. Anyone serious doesn't take well to the other party being sarcastic or joking, you know this, yet you still play games. It's a typical strategy to win the crowd by making someone overly serious look like a fool. But there is no crowd here, nobody is laughing. Just me and you.

So your real intent is just to piss me off given that you know nobody is here to laugh at your stupid joke. Your just a vile human being. Go ahead crack more jokes. Be more sarcastic, it just shows off your character. We're done.

Re:The type system is a programmer's best friend by gary17the on Wednesday November 09, 2022 @01:44AM (#33528224)
> Your intent was offense (...) you still play games (...) a typical strategy to win the crowd (...) But there is no crowd here (...) your real intent is just to piss me off (...) Your just a vile human being (...) it just shows off your character

I assure you that I am not joking when I say the following: you are beginning to act in a disturbing manner at this point, please consider speaking to a mental health professional.

Again, sorry to have caused you discomfort with my little joke and best of luck to you.

Re:The type system is a programmer's best friend by deltasevennine on Wednesday November 09, 2022 @08:11AM (#33531054)
Bro. If someone was truly disturbing and you truly wanted to help them wouldn't walk up to them and tell them to speak to a mental health professional. Telling them that is even more offensive. We both know this.

You're not joking. You're just being an even bigger ass, but now instead of jokes, you're feigning concern. It's stupid.

There's subtle motivations behind everything. A genuine apology comes without insulting the other party. Clearly you didn't do that here, and clearly you and everyone else knows what a genuine apology should NOT look like: "go get help with your mental problems, I'm really sorry."

It shows just what kind of person you are. It's not me who's disturbing... it's you, the person behind a mask.

Also clearly my words are from a place of anger and seriousness not mental issues. Mental problems are a very grave issue and it's a far bigger problem and the symptoms are far more extreme then what's happening here. But you know this. And you're trying to falsely re-frame the situation by disgustingly using mental issues as some kind of tool to discredit the other party. It's just vile.

I don't wish you the best of luck. I think someone like you doesn't deserve it.

Re:The type system is a programmer's best friend by icedchai on Saturday November 05, 2022 @02:20PM (#33484125)
I think it is simpler than that: C++ is an incredibly complex and verbose language. Most of web development is working with strings, and C++ kinda sucks there. There is also a compilation/build step, so overall productivity is lower. Python is "easier" all the way around (we'll ignore the dependency management/packaging debates.)

It depends on how you define "safer." Run-time errors with Python happen frequently in large programs due to poor type checking all the time. Often internal code is not well documented (or documented incorrectly) so you may get back a surprise under certain conditions. Unless you've have very strict tooling, like mypy, very high test coverage, etc. there is less determinism with Python.

Also, this may come as a surprise, but many people do not run or test their code. I've seen Python code committed that was copy-pasta'd from elsewhere and has missing imports, for example. Generally this is in some unhappy path that handles an error condition, which was obviously never tested or run.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @04:42PM (#33485466)
I know it happens "all the time" but these runtime errors happen fast and quick. You catch most of these issues while testing your program.

Statistically more errors are caught by python runtime then an equivalent type checked c++ program simply because the python user interface fails hard and fast with a clear error message. C++ on the other doesn't do this at all. The symptoms of the error are often not related to the cause. Python is safer then C++. And this dichotomy causes insight to emerge. Why did python beat c++?

In this case the type checker is irrelevant. Python is better because of clear and deterministic errors and hard and fast failures. If this is exemplary of the dichotomy between c++ and python and if type checkers are irrelevant in this dichotomy it points to the possibility that type checking isn't truly what makes a language easier to use and safer.

The current paradigm is rust and Haskell are great because of type checking. This is an illusion. I initially thought this was well.

Imagine a type checker that worked like c++. Non deterministic errors and obscure error messages. Sure your program can't compile but you are suffering from much of the same problems, it's just everything is moved to compile time.

It's not about type checking. It's all about traceability. This is the key.

>there is less determinism with Python

You don't understand the meaning of the word determinism. Python is almost 100 percent deterministic. The same program run anywhere with an error will produce the same error message at the same location all the time. That is determinism. Type checking and unit testing does not correlate with this at all.

This is not the case with c++.

Re:The type system is a programmer's best friend by icedchai on Saturday November 05, 2022 @05:23PM (#33485959)
I think it's better to catch errors sooner than later. This is where type checking helps. I've seen plenty of Python code that takes a poorly named argument (say "data").. is it a dict? list? something from a third party library like boto3? If it's a dict, what's in the dict? What if someone suddenly starts passing in 'None' values for the dict? Does the function still work? Almost nobody documents this stuff. Unless you read the code, you have no idea. "Determinism" of code is determined based on inputs. Type checking helps constrain those inputs.

As for C++ "non-determinism": If you write buggy code that overwrites memory, then of course you're going to get segfaults. This isn't C++'s fault.

I've seen plenty of code in all languages (including Python) that appears to exhibit chaotic run time behavior. At a previous company, we had apps that Python would bloat to gigabytes in size and eventually OOM. Is this "non-determinism"? No, it's buggy code or dependencies.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @08:09PM (#33487304)
>I think it's better to catch errors sooner than later. This is where type checking helps.

Agreed. It is better. But it's not that much better. That's why python is able to beat out C++ by leagues in terms of usability and ease of debugging and safety. This is my entire point. That type checking is not the deal breaker hear. Type checking is just some extra seasoning on top of good fundamentals, but it is NOT fundamental in itself.

>As for C++ "non-determinism": If you write buggy code that overwrites memory, then of course you're going to get segfaults. This isn't C++'s fault.

This doesn't happen in python. You can't segfault in python. Nobody is at "fault" but in terms of safety python is safer.

This language of "who's fault it is" is the wrong angle. There is nothing at "fault" here. There is only what is and what isn't.

Also my point was that when you write outside of memory bounds, anything could happen. You can even NOT get a segfault. That's the problem with what makes C++ so not user friendly.

>I've seen plenty of code in all languages (including Python) that appears to exhibit chaotic run time behavior. At a previous company, we had apps that Python would bloat to gigabytes in size and eventually OOM. Is this "non-determinism"? No, it's buggy code or dependencies.

This is literally one of the few things that are non-deterministic in python or dynamic languages. Memory leaks. But these are Very very very hard to trigger in python. But another thing you should realize is that this error has nothing to do with type checking. Type checking is completely orthogonal to this type of error.

>I think it's better to catch errors sooner than later. This is where type checking helps. I've seen plenty of Python code that takes a poorly named argument (say "data").. is it a dict? list? something from a third party library like boto3? If it's a dict, what's in the dict? What if someone suddenly starts passing in 'None' values for the dict? Does the function still work? Almost nobody documents this stuff. Unless you read the code, you have no idea. "Determinism" of code is determined based on inputs. Type checking helps constrain those inputs.

When you get a lot of experience, you realize that "sooner" rather then "later" is better but not that much. Again the paradox reels it's head here. Python forwards ALL type errors to "later" while C++ makes all type errors happen sooner and Python is STILL FAR EASIER to program in. This is evidence for the fact that type checking does not improve things by too much. Other aspects of programming have FAR more weight on the language.

Re:The type system is a programmer's best friend by icedchai on Saturday November 05, 2022 @10:17PM (#33488163)
Well, we do agree on something! I too much prefer programming in Python over C++. I honestly hope I never have to touch C++ code again. It's been about 5 years.

I try to add typing in Python where it makes sense (especially external interfaces), mostly as documentation, but am not overly zealous about them like some others I know. Mostly I look at them as better comments.

Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @07:13AM (#33491768)
>I try to add typing in Python where it makes sense (especially external interfaces), mostly as documentation, but am not overly zealous about them like some others I know. Mostly I look at them as better comments.

See you don't type everything because it doesn't improve things from a practical standpoint. You view it as better comments rather then additional type safety. You leave holes in your program where certain random parts aren't type checked. It's like if only half of C++ was type checked, one would think that it'd be a nightmare to program in given that we can't assume type correctness everywhere in the code. but this is not the case.

Your practical usage of types actually proves my point. You don't type everything. You have type holes everywhere and things still function just fine.

I type everything for that extra 1% in safety. But I'm not biased. I know 1% isn't a practical number. I do it partly out of habit from my days programming in haskell.

Re:The type system is a programmer's best friend by mamcx on Saturday November 05, 2022 @02:48PM (#33484410)
> and C++ has a relatively advanced type checker?

But why it NEEDS that?

Because C++ is FAR MORE DANGEROUS.

And worse language, in so many aspects, that you need everything to tame it.

In contrast, other langs like python have the luxury of see what C/C++ do wrong and improve over it.

Just having a `String` type, for example, is a massive boost.

So for them, the type system already have improved the experience!

---

So this is key: Langs like python have a type system (and that includes the whole space from syntax to ergonomics - like `for i in x`, to semantics) and the impact of adding a "static type system checker analysis" is reduced thank to that.

And considering that if you benchmark for a "static type system checker analysis" is what C++/C#/Java (at the start?) is then the value is not much.

Is only when you go for ML type systems where the value of a static checker become much more profitable.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @04:47PM (#33485517)
Hindley mindler allows for flexibility in your types and this high abstraction and usability in code. The full abstraction of categories allows for beautiful and efficient use of logic and code but it's not safety per se.

Simple type systems can also offer equivalent safety with less flexibility. What make Haskell seem more safe is more the functional part combined with type safety. Functional programming eliminates out of order errors where imperative procedure were done in the wrong order.

Re:The type system is a programmer's best friend by jjav on Saturday November 05, 2022 @04:06PM (#33485103)
> If you have errors in your program, does it matter that much if those errors are caught during runtime or compile time?

Of course it matters. If an error can be caught by the compiler, it will never get to production. Big win.

With typeless languages like python the code will get to production unless you have 100% perfect test coverage (corollary: nobody has 100% perfect test coverage) and then some unexpected moment it'll blow up there causing an outage.

This happens with metronomic regularity at my current startup (python codebase), at least once a month. It is so frustrating that in this day and age we are still making such basic mistakes when superior technology exists and the benefits are well understood.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @04:53PM (#33485592)
That's fine. A type checker won't catch everything. Run time errors happen regardless. I find it unlikely that all the errors your code base is experiencing is the result of type errors.

Something like c++. You get a runtime errors. You have no idea where it lives or what caused it.

Your python code base delivers an error but a patch should trivial because python tells you what happened. Over time these errors should become much less.

Re:The type system is a programmer's best friend by jjav on Saturday November 05, 2022 @05:06PM (#33485742)
> A type checker won't catch everything.

That's a strawman, nobody has claimed a statically typed language will catch all possible errors.

It will however catch an important category of common errors at compile-time, thus preventing them from reaching production and blowing up there. Other types of logic error of course exist, in all languages.

> Something like c++. You get a runtime errors. You have no idea where it lives or what caused it.

I don't know what this means? You seem to be suggesting that code in a statically typed language cannot be debugged? Clearly that's not true. Debugging is in fact usually easier because you can rule out the type errors that can't happen.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @07:59PM (#33487250)
>I don't know what this means? You seem to be suggesting that code in a statically typed language cannot be debugged?

You don't know what it means probably because you don't have experience with C++. These types of errors are littered throughout C++. What you think I'm suggesting here was invented by your own imagination. I am suggesting no such thing.

You talk about strawmen? Literally what you said can be viewed as an aspect of deception at it's finest. I literally in no way suggested what you accused me of suggesting. Accusatory language is offensive. Just attack the argument... don't use words like "strawman" to accuse people of being deliberately manipulative here. We both believe what we're saying, no need to accuse someone of an ulterior agenda when ZERO motive for one exists.

What I am suggesting here is that there is an EXAMPLE of a statically typed language that is FAR less safe and FAR harder to debug then a dynamically typed language (C++ and python). This EXAMPLE can function as evidence for the fact that static type checking is not a key differentiator for safety or ease of use or ease of debugging.

>Debugging is in fact usually easier because you can rule out the type errors that can't happen.

You don't get it. Type errors that happen at runtime or compile time contain the same error message. You get the same information. Therefore you rule out the same thing. Type checking is only doing extra checking in the sense that it checks code that doesn't execute while runtime checks code that does execute.

Python was programmed with sane error messages and runtime checks that immediately fail the program and gives you relevant logic about where the error occurred. This is the key differentiator that allows it to beat out a language like C++ which has none of this. C++ does have static type checking but it does little to make it better then python in terms of safety and ease of use.

Re:The type system is a programmer's best friend by jjav on Saturday November 05, 2022 @09:58PM (#33488051)
> You don't know what it means probably because you don't have experience with C++.

I started developing in C++ in 1992, so I have a few years with it. I've never run into the problems you seem to be experiencing.

> Type errors that happen at runtime or compile time contain the same error message.

Yes. But for the runtime error to occur, you need to trigger it by passing the wrong object. Unless you have a test case for every possible wrong object in every possible call sequence (approximately nobody has such thorough test coverage) then you have untested combinations and some day someone will modify some seemingly unrelated code in a way that ends up calling some distant function with the wrong object and now you have a production outage to deal with.

If you had been catching these during compile time, like a static type system allows, that can never happen.

Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @12:37AM (#33489034)
>Yes. But for the runtime error to occur, you need to trigger it by passing the wrong object. Unless you have a test case for every possible wrong object in every possible call sequence (approximately nobody has such thorough test coverage)

And I'm saying from a practical standpoint manual tests and unit tests PRACTICALLY cover most of what you need.

Think about it. The domain of the addition function is huge. Almost infinite. Thus from a probabilistic standpoint why would you write unit tests with one or two numbers? it makes no sense as the your only testing a probability of 2 out of infinite of the domain. But that probability is flawed because it is in direct conflict with our behavior and intuition. Unit tests are an industry standard because it works.

This is what I'm saying. Yes static checks catch more. But not that much more. Unit tests and manual tests cover the "practical" (keyword) majority of what you need to ensure correctness without going for an all out proof.

Re:The type system is a programmer's best friend by jjav on Sunday November 06, 2022 @01:57AM (#33489602)
I don't understand why you're talking about statistical sampling. Aside from random functions, functions are deterministic, unit testing isn't about random sampling. That's not the problem here.

Problem is you have a python function that takes, say, 5 arguments. The first one is supposed to be an object representing json data so that's how it is used in the implementation. You may have some unit tests passing a few of those json objects. Great.

Next month some code elsewhere changes and that function ends up getting called with a string containing json instead, so now it blows up in production, you have an outage until someone fixed it. Not great. You might think maybe you were so careful that you actually earlier had unit tests passing a string instead, so maybe it could've been caught before causing an outage. But unlikely.

Following month some code elsewhere ends up pulling a different json library which produces subtly incompatible json objects and one of those gets passed in, again blowing up in production. You definitely didn't have unit tests for this one because two months ago when the code was written you had never heard of this incompatible json library. Another outage, CEO is getting angry.

And this is one of the 5 arguments, same applies for all of them so there is exponential complexity in attempting to cover every scenario with unit tests. So you can't.

Had this been written in a statically typed language, none of this can ever happen. It's the wrong object, it won't compile, no outage, happy CEO.

This isn't a theoretical example, it's happening in our service very regularly. It was a huge mistake to use python for production code but it's too expensive to change now, at least for now.

Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @06:52AM (#33491615)
> I don't understand why you're talking about statistical sampling. Aside from random functions, functions are deterministic, unit testing isn't about random sampling. That's not the problem here.

Completely and utterly incorrect. You are not understanding. Your preconceived notion that unit testing has nothing to do with random sampling is WRONG. Unit Testing IS Random sampling.

If you want 100% coverage on your unit tests you need to test EVERY POSSIBILITY. You don't. Because every possibility is too much. Instead you test a few possibilities. How you select those few possibilities is "random." You sample a few random possibilities OUT OF a domain. Unit Testing IS random sampling. They are one in the same. That random sample says something about the entire population of possible inputs.

>Next month some code elsewhere changes and that function ends up getting called with a string containing json instead, so now it blows up in production, you have an outage until someone fixed it. Not great. You might think maybe you were so careful that you actually earlier had unit tests passing a string instead, so maybe it could've been caught before causing an outage. But unlikely.

Rare. In theory what you write is true. In practice people are careful not to do this; and unit tests mostly prevent this. I can prove it to you. Entire web stacks are written in python without types. That means most of those unit tests were successful. Random Sampling statistically covers most of what you need.

If it blows up production the fix for python happens in minutes. A seg fault in C++, well that won't happen in minutes. Even locating the offending line, let alone the fix could take days.

>Following month some code elsewhere ends up pulling a different json library which produces subtly incompatible json objects and one of those gets passed in, again blowing up in production. You definitely didn't have unit tests for this one because two months ago when the code was written you had never heard of this incompatible json library. Another outage, CEO is getting angry.

Yeah except first off in practice most people tend to not be so stupid as to do this, additionally unit tests will catch this. How do I know? Because companies like yelp have had typeless python as webstacks for years and years and years and this mostly works. C++ isn't used because it's mostly a bigger nightmare.

There are plenty of companies for years and years have functioned very successfully using python without types. To say that those companies are all wrong is a mistake. Your company is likely doing something wrong... python functions just fine with or without types.

>And this is one of the 5 arguments, same applies for all of them so there is exponential complexity in attempting to cover every scenario with unit tests. So you can't.

I think you should think very carefully about what I said. You're not understanding it. Unit testing Works. You know this. It's used in industry, there's a reason why WE use it. But your logic here is implying something false.

You're implying that because of exponential complexity it's useless to write unit tests. Because you are only covering a fraction of possible inputs (aka domain). But then this doesn't make sense because we both know unit testing works to an extent.

What you're not getting is WHY it works. It works because it's a statistical sample of all possible inputs. It's like taking a statistical sample of the population of people. A small sample of people says something about the ENTIRE population of people. Just like how a small amount of unit tests Says something about the correctness of the entire population of Possible inputs.

>This isn't a theoretical example, it's happening in our service very regularly. It was a huge mistake to use python for production code but it's too expensive to change now, at least for now.

The problem here is there are practical examples of python in production that do work. Entire frameworks have been written in python. Django. You look at your company but blindly ignore the rest of the industry. Explain why this is so popular if it doesn't work: https://www.djangoproject.com/ It literally makes no sense.

Also if you're so in love with types you can actually use python with type annotations and an external type checker like mypy. These types can be added to your code base without changing your code. Python types with an external checker are actually more powerful then C++ types. It will give you equivalent type safety (with greater flexibility then C++) to a static language if you choose to go this route. I believe both yelp and Instagram decided to do add type annotations and type checking to their code and CI pipeline to grab the additional 10% of safety you get from types.

But do note, both of those companies handled production python JUST FINE before python type annotations. You'd do well do analyze why your company has so many problems and why yelp and instagram supported a typeless python stack just fine.

Re:The type system is a programmer's best friend by jejones3141 on Saturday November 05, 2022 @04:48PM (#33485528)
> If you have errors in your program, does it matter that much if those errors are caught during runtime or compile time?

The passengers on the fly-by-wire jet running the program might well say that it matters.

Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @10:14AM (#33493460)
Well there's an irony to your statement. Those programmers who write embedded systems (I'm one of them) tend to use C++. C++ lacks memory safety and has segfaults, python doesn't. They literally used the most unsafe programming language ever that literally doesn't even alert you to errors either at compile time or runtime.

C++ is chosen for speed. Not for safety. The amount of run-time and compile time checks C++ skips is astronomical. The passengers may think it matters, but the programmers of those systems by NOT using a program that does compile time or run time checks are saying it doesn't matter.

Re:The type system is a programmer's best friend by xigoi on Saturday November 05, 2022 @04:54PM (#33485603)
> Why does 100,000 lines of code of python tend to be safer and more manageable then 100,000 lines of C++ despite the fact that python has no type checker and C++ has a relatively advanced type checker?

Because C++ sucks, but static types are not to blame for that.

Re:The type system is a programmer's best friend by deltasevennine on Saturday November 05, 2022 @08:11PM (#33487326)
My point here is that static types didn't do much to improve C++. We should be focusing on what made C++ bad. The things that made C++ bad and the fixes for those things are what makes python Good.

I'm saying type checking is not one of those things.

Re:The type system is a programmer's best friend by xigoi on Sunday November 06, 2022 @04:00AM (#33490596)
Well, I personally find Python much more pleasant to code in when using type annotations and MyPy. Have you tried that?
Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @07:01AM (#33491690)
Of course. I'm a python guru. I know the python type annotation inside and out. I'm a type nazi when it comes to writing python.

That's why I know exactly what I'm talking about. I can unbiasedly say that from a practical standpoint the type checker simply let's you run and the "python" application less, and the "mypy" application more.

Example:

   def addOne(x: int) -> int:
       return x + 1

   addOne(None)
The above... if you run the interpreter on it, you get a type error. Pretty convenient, you can't add one to None.

But if you want to add type checking you run mypy on it. You get the SAME type error if you run mypy. They are effectively the same thing. One error happens at runtime the other happens at before runtime. No practical difference. Your manual testing and unit testing should give you practically the amount of safety and coverage you need.

Keyword here is "practically." yes type checking covers more. But in practice not much more.

Re:The type system is a programmer's best friend by xigoi on Sunday November 06, 2022 @07:29AM (#33491864)
> No practical difference.

For a simple example like this, no. But consider this:

   def add_one(x: int) -> int:
       return x + 1

   data = load_huge_database()
   expensive_computation(data)
   add_one(None)
MyPy will show you the error instantly. Python, on the other hand…
Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @09:57AM (#33493271)
Sure but the time delta is inconsequential. Why? because you're going to run that program anyway. You're going to at the very least manually test it to see if it works. The error will be caught. You spend delta T time to run the program. Either you catch the error after delta T or at the beginning of delta T. Either way you spent delta T time.

Additionally something like your example code looks like data science work as nobody loads huge databases into memory like that. Usually web developers will stream such data or preload it for efficiency. You'll never do this kind of thing in a server loop.

I admit it is slightly better to have type checking here. But my point still stands. I talk about practical examples where code usually executes instantly. You came up with a specialized example here where code blocks for what you imply to be hours. I mean it has to be hours for that time delta to matter, otherwise minutes of extra execution time is hardly an argument for type checking.

Let's be real, you cherry picked this example. It's not a practical example unfortunately. Most code executes instantaneously from the human perspective. Blocking code to the point where you can't practically run a test is very rare.

Data scientists, mind you, from the one I've seen, they don't use types typically with their little test scripts and model building that they do. They're the ones most likely to write that type of code. It goes to show that type checking gives them relatively little improvement over their workflow.

One other possibility is that expensive_computation() can live in a worker processing jobs off a queue. A possible but not the most common use case. Again for this, likely the end to end or your manual testing procedures will test loading a very small dataset which will in turn make the computation fast. Typical engineering practices and common sense lead you to uncover the error WITHOUT type checking being involved.

To prove your point you need to give me a scenario where the programmer won't ever run his code. And this scenario has to be quite common for it to be a practical scenario as that's my thesis. Practicality is a keyword here: Types are not "practically" that much better.

Re:The type system is a programmer's best friend by za3faran on Saturday November 05, 2022 @05:07PM (#33485759)
You shouldn't compare a Python web stack with a C++ web stack, as C++ and Python target very different use cases.

You can compare however with a Java or C# web stack, both of which offer a superior developer experience, as well as a superior production experience (monitoring, performance, package management, etc.).

Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @09:43AM (#33493113)
The comparison isn't happening because everyone is getting caught up in some language war.

I compare C++ and python because I'm trying to make a statement about types. How types are actually not as important as people think.

C++ and python are good examples because python is safer and easier to use then C++ yet python doesn't have type checking.

Re:The type system is a programmer's best friend by throwaway2037 on Sunday November 06, 2022 @03:17AM (#33490360)
I would not use C++ in your comparison. Try with C# or Java. Not even close. They will crush in developer productivity and maintenance over Python, Ruby, Perl, JavaScript.
Re:The type system is a programmer's best friend by deltasevennine on Sunday November 06, 2022 @07:21AM (#33491811)
First off python now has types (you can place type annotations on the interpreter and run an external type checker) and javascript people use typescript. In terms of type safety i would argue python and javascript are now EQUAL to C# and Java.

Developer productivity in these scripting languages is also even higher. Simply because of how much faster they are to program in with the code then run/test loop. Java and C# can have loong compile times. Python and typescript are typically much much more quicker. With the additional type safety python and typescript are actually categorically higher in developer productivity then C# or Java.

But that's besides my point. Let's assume we aren't using modern conventions and javascript and python are typeless. My point is that whether or not C# or java crushes python and javascript over maintenance it doesn't win because of type checking.

Re:The type system is a programmer's best friend by throwaway2037 on Monday November 07, 2022 @07:22AM (#33504893)
You wrote: <<Java and C# can have loong compile times.>> Yes, for initial build. After, it is only incremental. I have worked on three 1M+ line Java projects in my career. All of them could do initial compile with top spec desktop PC in less than 5 mins. Incremental builds were just a few seconds. If your incremental build in Java or C# isn't a few seconds, then your build is broken. Example: Apache Maven multi-module builds are notoriously slow. Most projects don't really need modules, but someone years ago thought it was a good idea. Removing modules can improve compile time by 5x. I have seen it with my own eyes.
Re:The type system is a programmer's best friend by deltasevennine on Monday November 07, 2022 @09:43AM (#33506368)
><<Java and C# can have loong compile times.>> Yes, for initial build. After, it is only incremental.

I work with C++ currently. Even the incremental build is too slow. Also eventually you have to clear the cache for various reasons including debugging, a new library, etc, etc/

1M line python is 0s compilation time. You hit the runtime section instantaneously.

Go was created with fast compilation times to get around this problem. I would say in terms of compilation, go basically is the closest in terms of the python experience.

Basically when things are fast enough "5x compilation time" isn't even thought about because things are too fast to matter anyway. Go hits this area as well as python (given no compilation)

Re:The type system is a programmer's best friend by acchow on Saturday November 05, 2022 @03:14PM (#33484649)
You say “prefix 20” but there was this weird trend in the early-mid 2000’s where Ruby evangelists really believed that TDD is just as good as static types - even better because you’re forced to test actual business logic! And they even managed to convince masses of programmers that this is true!

Glad that’s over.

Re:The type system is a programmer's best friend by tialaramex on Saturday November 05, 2022 @04:59PM (#33485664)
> (Yes, Rust 4ev3r! ;))

Rust has a perfectly nice type system by modern standards, but it's nowhere close to showing you just how deep the rabbit hole goes when it comes to avoiding bugs at runtime by having stronger type systems.

For example suppose my Rust function takes a slice of clowns (named unimaginatively "clowns") and also a usize integer k. Can we write clowns[k] ? Rust says sure, it will emit a runtime bounds check to confirm that k is inside the bounds of the slice. If there are sixteen clowns, and we ask for k = 20, this Rust code will panic at runtime.

But we can do better, if we are willing to pay for it. Dependent Types. In a language with dependent types and enough inference our type inference system will conclude that k can be 20 here, thus clowns must be a slice of at least 21 clowns, but this slice has only sixteen clowns - type error during compilation, either k or clowns are wrong.

Now, for cases where bounds checking would be the reasonable thing to do, Dependent Types just result in you writing bounds checks, ie in this case checking k < 16, and so it's possible you will just end up doing more work to result in a program that still just says, at runtime, "Nope, not enough clowns" or whatever like in Rust. The type system will require you to write correct bounds checks, but the Rust bounds checks are auto-generated, so they're correct too.

But in cases where bounds checks were not the only sensible approach, or maybe you didn't even realise a bounds check would be emitted because you assumed it was statically correct - this can catch some bugs at compile time which would otherwise survive into a running program, "Shifting left" is I believe the usual phrase to describe this improvement.

If you thought the function is obviously correct, "Of course there are more than k clowns" but it isn't, the type error may cause you to take that extra moment to think about it. "Wait, why can there be fewer clowns than... oh, I didn't mean clowns here, this should say circus_performers. I'm not even using the right slice!".

Re:The type system is a programmer's best friend by creata on Saturday November 05, 2022 @05:17PM (#33485872)
I don't know about this. Even some of the people who use dependently typed proof assistants seem to doubt that they should be used much in the programming part (as opposed to the proving part). Also, some of the examples you give might be addressed well enough by Rust's const generics.

https://xenaproject.wordpress.com/2020/07/05/division-by-zer...

https://www.cs.ox.ac.uk/ralf.hinze/WG2.8/26/slides/xavier.pd...

Re:The type system is a programmer's best friend by elcritch on Sunday November 06, 2022 @03:53PM (#33496995)
> Rust has a perfectly nice type system by modern standards

I disagree -- Rust's type system is pretty weak and very limiting compared to other modern languages like Typescript, Nim, Zig, or even C++. That's without even without getting into dependent type languages.

There are so many basic patterns which Rust's type system can't handle, especially when it comes to compile time types. For example, one recent thing I ran into was trying to downcast a dyn Trait into another more specific dyn Trait. It's just not possible, at least on stable. Instead you have to do ugly work like getters that return sub-traits wrapped in optionals. Ick, the visitor pattern was easier.

Rust's type system philosophically takes a very strict "closed system" approach too. Meaning one can only really write code that's valid for every known instance. This limits entirely valid and useful subset of programs to programmers using libraries or language features. I'm not talking about runtime dynamism, but compile time dynanism. C++ with concepts provides a much more powerful and adaptive type system.

Programming in Rust frustrates me that I can't do any of the compile time checks that I've become accustomed to doing in other languages. It makes doing them at runtime difficult as well.

> but it's nowhere close to showing you just how deep the rabbit hole goes when it comes to avoiding bugs at runtime by having stronger type systems.

Totally agree on that! Though that's missing how powerful compile time programming in general can be even without the dependent types. Personally I'm excited to see how C++ Concepts can evolve. Though I'm not sure how much that starts overlapping with Dependent Types.

Re:The type system is a programmer's best friend by tialaramex on Sunday November 06, 2022 @07:45PM (#33499558)
> Personally I'm excited to see how C++ Concepts can evolve.

So, C++ 20 Concepts is basically what Bjarne Stroustrup proposed for a future version of his C++ language in the early 2000s. Several people proposed and WG21 accepted, a far more capable feature set for Concepts, this is often referred to as C++ 0x Concepts, since it was accepted for C++ 0x, the standard that would eventually (after years of delays) become C++ 11.

Bjarne wrote a paper arguing that this more powerful feature set was unnecessary and perhaps unworkable, and WG21 wound up removing Concepts from C++ 11 entirely (and the people behind it mostly got the message and ceased working on C++ altogether). A decade later, something that's close to Bjarne's original proposal became a C++ 20 feature.

C++ 0x Concepts was similar to Rust's Trait system in many ways. Particularly notable features of C++ 0x Concepts you might recognise in Rust's Traits:

1. Third parties can implement a C++ 0x Concept for some type which was not originally conceived with this Concept in mind, they just write the implementation and it works.

2. C++ 0x Concepts must be explicitly implemented they're not just a syntactic requirement that could be satisfied by happenstance in a type which is not in fact suitable.

3. As a result of 1 & 2, the C++ 0x Concepts have Semantics which in C++ 20 Concepts are confined to the idea of "modelling" a Concept or else IFNDR.

Re:The type system is a programmer's best friend by elcritch on Sunday November 06, 2022 @09:12PM (#33500229)
To be fair I've mostly used concepts in Nim, not C++ x0 concepts. However, your comparisons of C++ concepts to Rust traits seems to be lacking a lot of details or is plain inaccurate. It also misses the flexibility of C++ concepts.

> C++ 0x Concepts was similar to Rust's Trait system in many ways. Particularly notable features of C++ 0x Concepts you might recognise in Rust's traits:

Perhaps at the loosest level of comparison around only defining limitations on possible types. However, C++ x0 concepts enable much more powerful combinations of logic to specify if a template fulfills a concept. In Nim the concept can be any arbitrary boolean statement.

This blog has some good examples of C++ concepts: https://www.sandordargo.com/blog/2021/03/10/write-your-own-c...

It's trivial to specify that a C++ concept can be either a float or an integer:

    template <typename Base, typename Exponent>
    concept HasPower = (std::integral<Exponent> || std::floating_point<Exponent>) && requires (Base base, Exponent exponent) { 
        base.power(exponent);
    };
That's not possible AFAICT with Rust's traits.

> 1. Third parties can implement a C++ 0x Concept for some type which was not originally conceived with this Concept in mind, they just write the implementation and it works.

That's true for C++ concepts, but not entirely true for Rust traits. You can only implement a Rust trait if you own the type or own the trait. If you use two third party libraries, you cannot implement a trait from one for a type from the other. At best you can wrap the type in a new struct, and reimplement the parent's traits.

https://stackoverflow.com/questions/25413201/how-do-i-implem...

> 2. C++ 0x Concepts must be explicitly implemented they're not just a syntactic requirement that could be satisfied by happenstance in a type which is not in fact suitable.

That doesn't appear to match with C++ resources like: https://en.cppreference.com/w/cpp/language/constraints

Everything in Rust traits requires them to be encoded into existing traits. C++ concepts let you define rules for arbitrary combinations of types. So you can create functions that take two independent types and define a constraint on those types.

Re:The type system is a programmer's best friend by tialaramex on Monday November 07, 2022 @07:56PM (#33514694)
Unfortunately you don't seem to have understood much of what I wrote.

The most crucial thing to understand is that C++ 0x Concepts were a significantly more powerful feature than the C++ 20 Concepts you got. Even though I emphasised this, you seem to have muddled them together to produce what you're calling "C++ x0 concepts" in several places, which is not actually a thing.

> That's not possible AFAICT with Rust's traits.

It is of course possible to write a Rust trait for something as useless as "any numeric type regardless of what kind", that's what the Num crate's Num trait is. Because Rust's traits have semantics, useless traits reveal themselves - you can't do much with something whose only decisive property is that it's numeric in some way.

Num also defines some traits for numeric properties that are way more useful, like the additive and multiplicative identities (Zero and One). A type can be numeric without having zero (NonZeroU32 is a trivial Rust example from the standard library) so expressing that you mean specifically a type with additive identity is useful in a way that merely "numeric" largely is not.

The "HasPower" example is revealing though, lots of people's toy Concepts are like this. They just dictate a morsel of syntax. C++ 20 Concepts are indeed suitable for this, but so is nothing whatsoever, because of C++ template "magic".

Why C++ 20 Concepts at all then? Your C++ compiler's diagnostics with nothing whatsoever are terrible because Substitution Failure Is Not An Error. Bjarne's simple "Concepts" can hide this somewhat - the diagnostics you get for a Concept failure are more digestible.

> That's true for C++ concepts, but not entirely true for Rust traits.

No, it would be true for C++ 0x Concepts but those never existed beyond a draft document. It does work for Rust traits as you say but you can't do it for C++ 20 Concepts.

> That doesn't appear to match with C++ resources like: https://en.cppreference.com/w/cpp/language/constraints

Once again you're confused, that document is about C++ 20 Concepts, which exist, but I was describing C++ 0x Concepts, which are much closer to the capabilities of Rust's Traits and were never implemented.

> Everything in Rust traits requires them to be encoded into existing traits.

What you've written is a tautology. So I can only guess what insight you thought you had here.

Maybe you're imagining it's not possible to do obvious stuff like say that a type T must implement both trait A and trait B (a conjunction, signified in C++ Concepts with &&)? I assure you things do that all the time in Rust. foo<T: A + B + C, S: C + D>(p1: T, p2: S) is a function which takes two parameters p1 and p2, the type of p1 must implement traits A, B and C, while the type of p2 must implement traits C and D. For such complicated trait bounds idiomatic Rust would use the where keyword, but it's not mandatory, just easier to read.

In Rust you can't write the disjunctive bounds C++ 20 Concepts can express as || because it's not yet clear (and might never become clear) how to do so in a sound way. C++ doesn't care, none of the rest of the language is sound anyway, so it's too late to worry.

Re:The type system is a programmer's best friend by elcritch on Tuesday November 08, 2022 @01:54AM (#33516573)
> The most crucial thing to understand is that C++ 0x Concepts were a significantly more powerful feature than the C++ 20 Concepts you got.

This makes a bit more sense than what you originally wrote.

Though regardless if C++ 0x concepts were more powerful, C++ 20 concepts as they exist today are strictly more powerful than Rust traits. You admit that yourself when you say that Rust traits cannot model disjunctive bounds. They certainly can't do arbitrary boolean predicates or negation.

That means that Rust's trait system cannot implement compile time type checking rules that C++ 20 concepts can today. You cannot encode entire sets of logic in the type system that you can in encode in C++ (or other) languages.

Features like disjunctive logic may not be able to be proved "sound" for things like borrow checking but that's not the point or intent. The point is that you can use arbitrary boolean predicates in inventive ways. Hence my original comment about "seeing where C++ concepts go".

> Even though I emphasised this, you seem to have muddled them together to produce what you're calling "C++ x0 concepts" in several places, which is not actually a thing.

True, my terminology got muddled. Trying to follow your terminology and (odd) backstory about C++ 0x concepts was confusing.

You are incorrect about how C++ 20 concepts work and that makes it more confusing.

There are lots of resources showing that C++ concepts are implicit and don't need to be explicitly instantiated. Take this Rust comparison: https://mcla.ug/blog/cpp20-concepts-are-not-like-rust-traits...

While Rust traits force a "closed" system (to be imprecise) that's easier to prove soundness on upfront, that doesn't make the type system more powerful. It may make it more useful in some people's view. That's a pretty big distinction.

> No, it would be true for C++ 0x Concepts but those never existed beyond a draft document. It does work for Rust traits as you say but you can't do it for C++ 20 Concepts.

Err, no that's incorrect as you can easily check in any of the references I gave. C++ concepts as they exist allow you to call concepts if they fulfill the concept.

In Rust you must own either the trait or the type in order to implement said trait for that type. This is a widely known and deliberate limitation of the Rust trait system. It has some benefits, but is also leads to significant "trait bloat".

The "orphan rule" doesn't exist in C++ 20 concepts. It's an intentional Rust design decision: https://rust-lang.github.io/chalk/book/clauses/coherence.htm...

> It is of course possible to write a Rust trait for something as useless as "any numeric type regardless of what kind", that's what the Num crate's Num trait is. Because Rust's traits have semantics, useless traits reveal themselves - you can't do much with something whose only decisive property is that it's numeric in some way.

I don't really follow what you're trying to say here.

In contrast, I do find it very useful to define default algorithms for any numeric type that matches. It's a core part of C++ numerical libraries.

However, I get that this would be fairly pointless in Rust because you can't do much useful with it without things like generics specializations being stable.

> The "HasPower" example is revealing though, lots of people's toy Concepts are like this. They just dictate a morsel of syntax. C++ 20 Concepts are indeed suitable for this, but so is nothing whatsoever, because of C++ template "magic".

This makes no sense. A "morsel of syntax" or alluding to "C++ template magic" make no sense.

Granted C++ template's are amazing powerful, and amazingly difficult to debug.

C++ 20 concepts provide a useful and flexible way to describe compile time type restrictions, while not limiting C++ templates to the purely adjunctive subset of type logic able to be described in Rust's trait system.

> > Everything in Rust traits requires them to be encoded into existing traits. > What you've written is a tautology. So I can only guess what insight you thought you had here.

It is, I was being lazy but the point I'm reaching for is that using Rust's trait system requires the traits you want to target to already exist and to be implemented for the types in question. Moreover, creating some type-based logic rules using the Rust trait system, requires that logic to effectively already be encoded into the traits (as some combination of adjunctive properties).

This is why you end up with incompatible HAL libraries for various STM32 models, among others. In my opinion it's a very limiting part of the ecosystem.

Re:The type system is a programmer's best friend by tialaramex on Tuesday November 08, 2022 @07:10AM (#33518261)
> This makes a bit more sense than what you originally wrote.

It re-states what I originally wrote, I'm glad you find it clearer now although since this is a matter of history it was all there for you to read if you cared. I don't think I have time to say everything two or three times until you "get" it.

> Features like disjunctive logic may not be able to be proved "sound" for things like borrow checking but that's not the point or intent.

The soundness problem is, unfortunately, fundamental. It's not about the borrow checker. C++ doesn't care whether your program has any logical meaning at all, so long as it is syntactically OK in these cases - of course in such case its meaning is unknown, but the standard explicitly tells compilers not to worry about that, the important thing is that the gibberish compiled, a C++ programmer can congratulate themselves on another successful project.

Maybe I need to expand the abbreviation I used, IFNDR: Ill-Formed, No Diagnostic Required. This is what the standard says to wave away such problems, not only with concepts but throughout the language. "Ill-formed" means this isn't actually a C++ program and so the standard does not define what it means, but "No Diagnostic Required" means the compiler needn't give an error or warning, it just presses on anyway.

[ You might imagine surely they could give a diagnostic, but actually they can't because of Rice's theorem. For a sufficiently powerful programming language you have to pick: 1. Your compiler sometimes gets "stuck" forever trying to decide whether a program is valid. 2. Your compiler reports errors in some otherwise valid programs. 3. Your compiler reports no errors in some invalid programs. Rust chose (2) and C++ chose (3) ]

> C++ concepts as they exist allow you to call concepts if they fulfill the concept.

Once again you've got turned around. The question isn't whether you can call concepts but whether anybody else can implement the concepts, and you simply can't do that. C++ 0x Concepts had "concept maps" to fix this, in Rust obviously the traits are explicitly implemented, but C++ 20 Concepts doesn't have an equivalent.

> Granted C++ template's are amazing powerful, and amazingly difficult to debug.

They're copy-paste. A slight improvement on C pre-processor macros. I suppose it's in the name, "templates" like a mail merge system. It's childishly simple like the cups and balls trick. The resulting mess does indeed produce unintelligible error messages and is also unsound in both obvious and surprising ways.

> In contrast, I do find it very useful to define default algorithms for any numeric type that matches. It's a core part of C++ numerical libraries.

Useful here meaning only you get better error messages than from SFINAE?

> using Rust's trait system requires the traits you want to target to already exist and to be implemented for the types in question.

I think it obviously follows that you can't use things which don't exist.

Re:The type system is a programmer's best friend by drpixie on Saturday November 05, 2022 @07:12PM (#33486946)
We (the industry) are still so quick to disregard the benefits of strict typing.

"Back in the day..." I worked on a collection of vital (to the company) infrastructure apps written in Borland Turbo (object) Pascal. Strong static type checking was enforced by the language. Good type design and strict type checking meant that it was normal that when a program compiled, it was bug free!

Much as I enjoy the flexibility of python, I know that every refactor or significant change means that there are now execution paths that have not been exercised - the burden of comprehensive testing is enormous, far outweighing the convenience of dynamic typing.

Re:The type system is a programmer's best friend by kaba0 on Sunday November 06, 2022 @09:18AM (#33492880)
While I also agree that static typing is the easiest, statically decidable way to significantly increase program correctness, I’m not sure we can do significantly better with it then we currently do. Most interesting properties are not expressible even with dependent types, and those are very hard to prove, making their advantages non-no-brainers.

What I’m trying to say is that we should be open about another concept, for example contract-based programs (clojure’s spec for example), because they might have better properties.