Live data from Hacker News

Fear, trust and JavaScript: When types and functional programming fail

reaktor.com

171–180 of 210 posts

Re: Fear, trust and JavaScript: When types and functional programming fail

#171
post #69

Not sure what this author is saying here. Types have nothing to do with being able to trust other systems and everything to do with the internal formal consistency of the code that you write. It's about being consistent with yourself. If you find yourself writing defensive checks in fulfilled promise handlers because the data you get is sometimes invalid, stop. Get up from your keyboard, go to whoever owns the endpoi…

The spec should be typed. Those other users should not just be giving you typed data, they should be giving you a type signature.

I'm referring to a general situation. You're referring to a specific situation of http apis sharing what I assume is untyped data and the typing is lost during transport. The solution is to develop protocols on top of http where the type signature isn't lost.

Re: Fear, trust and JavaScript: When types and functional programming fail

#172
post #56

Earlier quoted context omitted.

With Bucklescript/Reason you get typesafety, immutability, performance and small codesize: >Now we compare the runtime performance: BuckleScript Immutable Map: 1186ms Facebook Immutable Map: 3415ms We also compare code Size: BuckleScript (Prod mode): 899 Bytes Facebook Immutable: 55.3K Bytes source: https://github.com/BuckleScript/bucklescript/wiki/Why-buckle...

This all sounds great but I cringe a bit at yet another layer of abstraction in an already insane JS tooling ecosystem. It seems like half of the time you’re just using the escape hatch to plain JS to stitch all of this stuff together, no?

Try elm. The elm abstraction is probably the only one where you literally will never ever touch javascript or raw html.

It is the least leaky abstraction I have ever encountered, which is amazing given the fact that it is also blazingly faster than every other framework from React to Vue. Elm is a way higher level abstraction then jsx or Vue yet you don't need to understand the lower level details to produce more efficient applications.

The most leaky abstraction I have ever encountered is probably SQL. Optimizing SQL involves understanding what it compiles into; Elm there is no such understanding required.

Re: Fear, trust and JavaScript: When types and functional programming fail

#173
post #125

Earlier quoted context omitted.

Passing props through a higher-order component using JSX's spread operator instead of the attribute syntax, e.g. `{...{...props, foo, }}` and accessing that prop in the wrapped component. I found it was possible with the attribute syntax. More examples can be found in the issues for `@types/ramda`. In some cases, the types are incorrect. In other cases, TypeScript's type inference is too weak.

From your description, that sounds like the compiler accepting something that isn't correct. I'm more interested in the case(s?) mentioned where the types are correct and the compiler won't accept the code. I will have a quick skim through the @types/ramda issues, thanks.

The compiler was telling me that there was no field foo on props but there clearly was.

Re: Fear, trust and JavaScript: When types and functional programming fail

#175
post #33
post #27

Earlier quoted context omitted.

You try to parse the received data into a predefined data type, if this fails you are forced by the type system to handle the failure scenario, if it parses correctly you now have a 100% valid data structure you can work with in the rest of your codebase, no defensive checks necessary.

How does that work? I thought static typing was only checked when compiling.

Static types can and usually are checked at compile time.

Whether the language runtime also uses type checks while running depends entirely on the language and the runtime.

IIRC, the JVM (to take an example) more or less throws away a lot of the static type information -- which is often too generic to be useful in guiding optimization -- in favor of runtime profiling, and several of its key optimizations involve inserting runtime type checks.

Re: Fear, trust and JavaScript: When types and functional programming fail

#176

Earlier quoted context omitted.

No it doesn't. This may not be a popular opinion for those coming from C or Java, but runtime type checks in JavaScript are usually an anti-pattern. If my function defines a contract that the caller violates by passing in null or undefined, then my function should raise an exception when it tries to access `undefined.xyz` or whatever. Otherwise, embrace the duck! If the function can work with what it was given, it sh…

> If the function can work with what it was given, it should How do you know that it "can work" based purely on the name of the function? > But be pragmatic I am. I consider javascript a last-resort option, only really useful for adding minor enhancements client side. Beyond that, I don't use it.

Documentation and/or a type checker. Jsdocs are both documentation for programmers and can be enforced using tooling like the closure compiler or typescript

JavaScript is no different from most other dynamic languages this way

Re: Fear, trust and JavaScript: When types and functional programming fail

#177

Earlier quoted context omitted.

> If the function can work with what it was given, it should How do you know that it "can work" based purely on the name of the function? > But be pragmatic I am. I consider javascript a last-resort option, only really useful for adding minor enhancements client side. Beyond that, I don't use it.

Documentation and/or a type checker. Jsdocs are both documentation for programmers and can be enforced using tooling like the closure compiler or typescript JavaScript is no different from most other dynamic languages this way

So, 'hope'.

This is exactly why I can't take javascript seriously for anything besides basic UI enhancements to a web page.

Re: Fear, trust and JavaScript: When types and functional programming fail

#178

Earlier quoted context omitted.

Type checked against what, if a variable doesn't have a type? There are contexts in which two values are involves, where we can at least check that the types are matching (or reasonably compatible), like (a + b). JS is a massive failure in that regard, too, and there are dynamic languages that do it far better - e.g. in Python, ("1" + 2) is a runtime type error, not 3. But in the context of this thread, I feel that's…

> if you require types to be declared explicitly, there's no reason to not check them statically. I think some of this thread is suffering from confusing terms so, my entire point is this: There is no native way in javascript to say that the first argument of function foo must be an instance of class Bar (which in JS could be a string or an array or some custom object or even a DOM element - everything is an object a…

Yes, I understand that. My point was that it's not really dynamic typing at that point anymore. It's kinda like C#, where you do have "dynamic", but there are static type annotations that code utilizing "dynamic" can validate against. At that point, I'd say that it's no longer a dynamic language in conventional sense.

Getting back to your issue - it's a pragmatic decision for TS, because enforcing its type system efficiently is virtually impossible on top of the JS runtime type system, when you consider how it has to work. It sounds like simple, obvious check for trivial cases, like if I annotate a function argument to be an number or a string. But what if it's an interface? At that point, the runtime check must ensure that 1) it's an object, 2) it has all the members listed in the interface, and 2) they have the corresponding types. If they are themselves interfaces, this becomes recursive. If they're arrays or maps, then this has to be done for every element. An array of arrays is O(N^2) already!

To check it efficiently, you need to have proper runtime type tags - an array has to know that it's an array of number, not just an array. Then the runtime checker can just query that, like it does in Java when you, say, downcast Object to int[]. But TS has to work on top of JS runtime, so any such runtime type tagging would manifest as visible artifacts in JS; and TS is intentionally designed to be "invisible" from JS interop perspective.

If we want quality strong typing that works properly with cross-language interop for the web, we need to throw away the JS runtimes entirely (it would be a good idea in any case - it's an atrocious object and memory model for anything other than JS itself), and replace them with something saner.

Re: Fear, trust and JavaScript: When types and functional programming fail

#179

> in various cases the types are wrong and the compiler doesn’t care It's disturbing how often I encounter this in TypeScript. Or its inverse: the types are correct and the compiler is wrong. Or a third common problem: the types for a library are incorrect. The unsoundness of TypeScript is not merely theoretical. The compiler is frequently just wrong. For that reason, I am mystified by the amount of enthusiasm for Ty…

With regard to the third, this is a problem specifica with libraries written in JavaScript. Hopefully it'll become less common as more libraries are written in TypeScript and the types are automatically generated from the code.

Re: Fear, trust and JavaScript: When types and functional programming fail

#180

Earlier quoted context omitted.

Documentation and/or a type checker. Jsdocs are both documentation for programmers and can be enforced using tooling like the closure compiler or typescript JavaScript is no different from most other dynamic languages this way

So, 'hope'. This is exactly why I can't take javascript seriously for anything besides basic UI enhancements to a web page.

I’m sorry that I commented on this article. Knew it was a trap. I’ve been writing functional-style for a long time and just wanted to share a bit of what I’ve learned.

Use whatever tools work for you, but also don’t disparage or dismiss other tools because they have different strengths and weaknesses than what you know or appreciate. There is no need for proselytizing; these are just tools after all!

I like JavaScript and TypeScript because they have let me quickly build many great things and share them with people. I also subjectivity find writing in them to be enjoyable. I may always turn to a different language if it is more productive and enjoyable than js/ts for the task at hand.

Post reply on HN