r/webdev • • May 18 '25

Discussion I wonder why some devs hate server side javascript

I personally love it. Using javascript on both the server and client sides is a great opportunity IMO. From what I’ve seen, express or fastify is enough for many projects. But some developers call server side javascript a "tragedy." Why is that?

193 Upvotes

254 comments sorted by

View all comments

Show parent comment

2

u/c-digs May 19 '25

Yes, its important to not just do as Whatever in these cases, but use something that will actually parse it into the correct thing at injestion.

You're missing the point: I don't have to do this in typed languages; this is extra work in JS and it is inherently unsafe in JS beacuse JSON.parse doesn't actually care.

So now you have to add a schema object using, for example, Zod or class-validator. Is that fun? Would you rather not just write your types and be done with it? That's how it works in Java and C# and I have the option then of customizing serialization if I want to or need to but I don't have to do extra work with my domain models to ensure correctness.

Typia is the only lib that somewhat brings that experience in TS.

3

u/thekwoka May 19 '25

You're missing the point: I don't have to do this in typed languages

Well, yes you do.

You don't just magically know what the type returned by parsing a JSON string is.

You have to parse it and specify some kind of parser or structure that it is parsing that string into.

You do have to do it.

The difference is that a very strict typing system will REQUIRE it, while TS/JS will let you get away with it.

Though you can kind of get that level of enforcement by having those methods return unknown instead of any and not allowing casting the type as "any" or as a specific type, without going through a type parsing step.

Yes, it's not as ergonomic, for sure.

But let's focus on that real issue, and not on the false claims that you don't have to do that in other languages.

2

u/c-digs May 20 '25 edited May 20 '25

You don't just magically know what the type returned by parsing a JSON string is.

You have to parse it and specify some kind of parser or structure that it is parsing that string into.

You do have to do it.

It's just your domain type in C#.

Working example: https://dotnetfiddle.net/IpwNIf

using System.Text.Json;

record Person(string FirstName, string LastName);

var person = JsonSerializer.Deserialize<Person>(json);

// ✅ 
Console.WriteLine(person.FirstName);

See how I just use my model? See how I don't have to do any additional work like defining a schema? This is going to fail:

using System.Text.Json;

var json = "{'FirstName': 1, 'LastName': true}";

// ❌ Fails because 1 and `true` cannot be assigned to `string`
var person = JsonSerializer.Deserialize<Person>(json)

See how I didn't have to define a rule here like firstName: z.string()? Because my model itself defines the type constraints and these do not disappear when I build the code.

1 and true cannot fit into string so it fails automatically. This works because the type metadata doesn't get erased at compile time like it does when it goes from TS -> JS. All type metadata is lost in that process so it is necessary to have a schema now represent that lost type or write the schema in the first place instead of the type.

// This type information no longer exists at runtime
export type Person = {
  firstName: string,
  lastName: string
}

// So you need a schema
const PersonSchema = z.object({
  firstName: z.string(),
  lastName: z.string()
})

// In C#, Java, I only need types; I don't need
// secondary schemas.  
// "But you can just z.infer<type PersonSchema>!!"
// No, I want to write types, not Zod and Zod
// is slooooooooooow at runtime

Only AOT solutions like Typia emulate the "ideal" experience in JS by inlining the JS validation while allowing "pure" TypeScript at dev time.

But let's focus on that real issue, and not on the false claims that you don't have to do that in other languages.

It is pretty clear you really, really do not know what you are talking about.

4

u/thekwoka May 20 '25

See how I don't have to do any additional work like defining a schema?

What?

record Person(string FirstName, string LastName);

You defined the schema right there.

It is pretty clear you really, really do not know what you are talking about.

Bruh, you literally said typed languages have type metadata at runtime.

That's only Dynamically typed languages (which includes JavaScript).

Rust is statically typed and there is no type metadata available at runtime.

I agree that JS does not have a native way to just parse directly into a specified struct, and TS doesn't provide that either.

It's nice than other languages have it built in, or have available macros that can do it nicely (like Rust does)

But the whole way you describe these things is just plain wild.

and Zod is slooooooooooow at runtime

There are other choices, but mainly yes, doing validation is slower at runtime than not validating. Which is also something your C# is doing. It's parsing and validating.

You have like a half decent point and then you wrap it up in a lot of strange and just plain false tertiary claims.

1

u/efari_ May 21 '25

👏 hear hear. some people really think they understand. but few really do. (*insert bell curve meme.jpg)

1

u/ub3rh4x0rz May 19 '25

JSON.parse is like unmarshalling to map[string]any in golang. Yes, typescript parsers require more than just providing the type, so you invert it and infer the type of the parser. There's a performance hit with zod, sure.

1

u/Sensi1093 May 19 '25

Its like parsing to `any` in go - top-level strings, numbers, etc exist too :)

2

u/ub3rh4x0rz May 19 '25

Yes, you're right, but while I've never seen real go code unmarshal json to any, I've seen map[string]any plenty, so it seemed like a better example in this context