r/badcode • sadistic • Apr 22 '21

js ternary operators at their maximum potential

Post image
960 Upvotes

73 comments sorted by

View all comments

Show parent comments

1

u/recycle4science Apr 23 '21 edited Apr 23 '21

It's the closest thing the frontend has got, and this is a javascript conversation.

Edit: to be fair, javascript runs on servers too. Substitute Haskell for typescript in my previous comment if you like.

1

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Apr 23 '21

It's the closest thing the frontend has got, and this is a javascript conversation.

OCaml or Reason (an alternate syntax on top of OCaml) can be compiled to JS, and there's also ReScript which is a fork of Reason specifically focused on JS compilation. They all use OCaml's type system, and F# (which is similar) can compile to JS as well.

It's odd that you claim TypeScript is the "closest thing" to type safety that frontend has when these and other options exist.

1

u/recycle4science Apr 24 '21

Ah well that's my bad, I didn't know! I think my point still stands that many legacy apps are written in shitty ad hoc javascript and can't just be re written in a type safe language. But! Good to know there are better alternatives available :)

(Okay and yes technically legacy apps can probably be gradually transitioned. But you get what I'm saying I'm sure.)

2

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Apr 24 '21

I think my point still stands that many legacy apps are written in shitty ad hoc javascript and can't just be re written in a type safe language.

You're right, it does. I wasn't trying to suggest otherwise, just pointing out that there are other options than TS with good type systems out there, and they can be really nice to use.

I'm personally a fan of OCaml's syntax (which is more Pascal/algol-like) and ridiculously fast compile times (someone comparing compile times, with 15ms for OCaml vs 1400+ for TS), but Reason or ReScript specifically try to wrap its features in a JS-like look-and-feel which gives them a familiarity for people interested in moving from JS to something nicer. Plus with OCaml and Reason you can use the same language on the backend to make native code instead of having to run JS on the server. (ReScript is subtly different but you could do it on front and Reason on back for a similar effect.)

It's worth checking out the options, because being able to define types for your data and have the remaining code practically write itself by pattern matching on the data is so damn good it's hard to go back to something else. :)

1

u/recycle4science Apr 24 '21

Yeah I've learned a bit about Haskell and F# (didn't realize the latter could compile into js); our VP is really into types. One day I'll get to work on a project that isn't php and jquery!