News for hackers. Stuff that matters.
Re:Are dynamic languages going to replace static languages? ... by brightball on Wednesday August 04, 2021 @10:30AM (#28061411)
> To make up for it in a dynamic language, you end up having to reimplement what a type checker would do for you with no guarantee you didn't miss something or make a mistake, with the added cost of e.g. polluting your projects with noisy defensive code that double checks the types of parameters, adding extra tests, and bolting on linting, analysis and IDE tools to help.

This is entirely dependent on the type of application that you're dealing with.

A web application backed by a database, especially CRUD style, acts as little more than a pass through layer. You're getting everything as a string from the browser, storing it in the database with a SQL string, usually transformed by an ORM layer. In that style of application, the types are maintained in the database itself.

Having a language with strong static typing in that middle layer between the browser and the database only serves to add a huge amount of headaches and extra friction, especially when you're not even using each variable that comes through on its way to the database.

The only thing you're doing in that situation is adding an extra set of type checks that you have to keep in sync between the database and the application layer for virtually no benefit.

Re:Are dynamic languages going to replace static languages? ... by duped on Wednesday August 04, 2021 @10:52AM (#28061685)
Types have never given me a headache. They've only prevented me from writing bugs and made classes of tests redundant.

In your example, the database and the application layer must be kept in sync regardless of whether you use static typing or not. The benefit of static typing is that it can be enforced before you push to production or write a test for congruency.

Re:Are dynamic languages going to replace static languages? ... by brightball on Wednesday August 04, 2021 @11:01AM (#28061791)
> kept in sync regardless of whether you use static typing or not

This is handled automatically by a large number of ORM tools in dynamic languages (Ruby, PHP, etc). It's a non-issue.

I've spent significantly more time in my career debugging issues in statically typed languages used as an app server in front of a database than I have in dynamically typed languages.

You're doing some processing work in the language? Calculations, ML, etc? Static typing is the obvious way to go.

If you're just sticking things in a database you're going to end up wasting a lot of people's time (and subsequently, the money that goes to pay them). In 20 years, I've never seen it be a good choice but I've seen a lot of people married to the delusion that it is while other people are getting real work done.

Re:Are dynamic languages going to replace static languages? ... by lolinder on Wednesday August 04, 2021 @12:02PM (#28062605)
> You're getting everything as a string from the browser

This may be one of the differences of requirements that causes disagreement here. I can see how this might work if your app:

* Loads data from the client

* Later displays that data, unchanged, to the client

In that case, treating everything as a string is great, because you never need to use it as anything else.

In my case, I've never actually worked on an app that didn't need to do more than just display the data at some point. In those cases, I don't want to get everything as a string from the browser. My data is often not merely a string, and treating it as such means I have to deal with stupid questions like whether "0" == 0 == "null".

In my work, I have spent many, many hours repairing issues that could have been avoided if the client and server were both statically typed (and on the flip side, in services that are statically typed, rarely run into those same problems). I'm sure that this can be remedied by just "doing it better", but I'd rather operate from a foundation where doing it right is easier than doing it wrong.

Instead of strings, I want my apps to have a statically typed contract between the client and the server that specifies what endpoints are available and the data structures they accept and return. Ideally this is in a shared class/type definition file, but two definitions kept in sync can work as well if they don't share a language.

The common argument against statically typing your network requests at each end is that you're not resilient against unexpected values and/or version differences, but:

a) It's trivial to tell any JSON parser to ignore unknown values, so adding new optional fields to the request is never a problem.

b) If your app ever uses the data received from the client, version differences will eventually break your dynamic code, too. You'll just only be told so at runtime, potentially only after loading the bad data from the db.

If you ever use your app's data for anything other than rendering a template, you have to make breaking changes to your data structures very carefully, and using a strong layer of static types on both client and server helps to ensure that you consider all the breaking changes and either avoid them altogether or somehow mitigate them.

Re:Are dynamic languages going to replace static languages? ... by brightball on Wednesday August 04, 2021 @03:00PM (#28064824)
This is taking it a step further though. Now you're talking about static typing in the client, the server and the database.

I agree that this is a different scenario. If you're able to have the client control the input to ensure only the correct types are passed in there may be some additional value there to keep things more sane.

However, how much of the data that you're dealing with in the client on the server and in the database are you really working with in the server itself? 1% of the variables? 2%?

Generally any heavy work will be handled in either the database itself or a background job just to reduce wait time for the client.

And if that's the case, you're spending the time defining types for the client, server and database, keeping them in sync, having to change them in each place and creating this exercise for the other 98-99% of variables passed in and out of your server just to gain the static benefit for the 1-2% where you're actually working with it in the server on the web request.

It's significantly more work with questionable benefits IMO.

Re:Are dynamic languages going to replace static languages? ... by lolinder on Wednesday August 04, 2021 @07:39PM (#28067860)
I guess that's true if you're dealing with an SPA. If your logic is primarily on the client, then the server doesn't benefit much from types. If your logic is primarily on the server, then your client doesn't. If it's a balance, then both do.