News for hackers. Stuff that matters.
Re:Announcing TypeScript 2.0 by johnfn on Thursday September 22, 2016 @03:29PM (#12559556)
Statically typed languages have a few large advantages over dynamic languages. These include:

- Refactoring. Refactoring a large JavaScript codebase (> 100k loc) is nearly impossible and at best incredibly painful, error prone and time consuming. With static types, it becomes a trivial and straightforward process.

- Your code is now more discoverable. You can easily find definitons, and discovering all methods and properties on an object is as simple as triggering an autocomplete pop up.

- Code is now more readable. Objects are never opaque. You never have to dig through multiple calls just to see what the shape of an object is. It's all right there.

For me, the killer app is refactoring. Writing huge amounts of JS is very painful to me because I refactor almost constantly as I add more code. Most dynamic languages have this problem. Static typing changes that completely.

Re:Announcing TypeScript 2.0 by jeremiep on Thursday September 22, 2016 @03:49PM (#12559747)
I don't get this. Most of the known refactorings can't be easily implemented by IDEs so you're left with the trivial ones like renaming or moving things.

Static types won't help you refactor a monster class into a structure of arrays. It won't refactor your callback hell into coroutines. It won't refactor your "one-at-a-time" algorithm into batches. Yet these all yield more agility and performance.

The thing is, I usually have so much less code when throwing static types out the window that refactorings become a non-issue. I get away with basic Emacs macros and unix tools.

Re:Announcing TypeScript 2.0 by coldtea on Thursday September 22, 2016 @03:55PM (#12559806)
>I don't get this. Most of the known refactorings can't be easily implemented by IDEs so you're left with the trivial ones like renaming or moving things.

First, the trivial ones are great and useful refactorings too -- and still difficult in dynamic type systems.

Second, static types also benefit the more difficult refactorings that you still have to do manually, by giving you compiler errors and warnings when you botched them at least at the type level. With dynamic languages those need to be done both 100% manual and with no aid.

Third, static languages are a superset of dynamic typed languages, so they can do everything the latter can do, while the inverse is not true.

You can have a "var" or "variant" etc type in a dynamic language that lets you code exactly like in a dynamic typed language where you want it.

(In fact dynamic typed languages are just static type languages with a single type: https://existentialtype.wordpress.com/2011/03/19/dynamic-lan... ).

Re:Announcing TypeScript 2.0 by jeremiep on Thursday September 22, 2016 @04:02PM (#12559869)
I'd rather have tests than types. Unless your type-system is sound; most of them are very, very far from it.

You can specify the domain of values with types, but not their ranges. Side-effects fall through unaffected by types as well.

I've done plenty of refactorings in huge statically typed codebases; types are a very, very superficial help there. Everything compiles fine but now you're passing 20 to a function whose range is 1-10. Or you're now generating the wrong filenames.

To me the overhead of using types outweighs the advantages they bring to the table. If I can help it I'd even choose to have a spec I can enforce at runtime using custom predicates than some vague predefined types with weak semantics or unit tests I have to maintain manually.

Re:Announcing TypeScript 2.0 by coldtea on Thursday September 22, 2016 @06:44PM (#12560963)
>I'd rather have tests than types. Unless your type-system is sound; most of them are very, very far from it.

As I wrote, static type systems are a superset of dynamic type systems. So they can have both.

Besides, static types are, among other things, automated tests for the trivial stuff -- which the computer automatically tests/ensures them instead of a human writing them as busywork (and potentially getting them wrong).

The "sound" type system requirement doesn't make sense either. A non sound type system can still catch the majority of parameter/variable misuses automatically. With dynamic type systems you don't even have that, so essentially what you wrote means "I want it all or nothing". Cool when it comes to personal ambition, doesn't make sense when what you get still protects you from all kinds of errors and helps you refactor and self-document your code.

>I've done plenty of refactorings in huge statically typed codebases; types are a very, very superficial help there. Everything compiles fine but now you're passing 20 to a function whose range is 1-10. Or you're now generating the wrong filenames.

That would matter if static types somehow prevented unit/functional testing, which they do not.

And, depending on the language, the help of static types can be far from superficial. Even in Ada, back in the 80s, you could define the 1-10 range as a constrain on the type/input parameter.

Yeah, they don't catch all problems -- but that's good too, else we would not have jobs.

Re:Announcing TypeScript 2.0 by jeremiep on Friday September 23, 2016 @10:38AM (#12564925)
> but that's good too, else we would not have jobs.

Given how mediocre the average software out there is, I'd much much rather have an industry focus on actual engineering. Its a lot more profitable to find new problems to solve rather than continuously fix your own mess.

I used to write very well documented statically typed code, and then watching people without domain knowledge butcher the hell out of it after I left.

At the end of the day knowing what you're doing is priceless. Just like having guard rails is no excuse to not learn how to drive.

Alls I'm saying is that types are very overrated when it comes to actual robustness and correctness. You can get it without types and you can miss it with types.

Re:Announcing TypeScript 2.0 by lloyd-christmas on Thursday September 22, 2016 @04:18PM (#12560004)
> I'd rather have tests than types.

They aren't mutually exclusive. I'd rather have both.

Re:Announcing TypeScript 2.0 by jeremiep on Thursday September 22, 2016 @04:42PM (#12560208)
Oh I know, but my opinion and experience in half a dozen statically typed languages over a decade is that most type systems add much more overhead to development than agility.

I do want types when building native software, because these sit directly on the CPU and have to match the hardware. Other than that I feel they at best give you the illusion of robustness - unless the type-system is actually sound but very, very few are.

Re:Announcing TypeScript 2.0 by wvenable on Thursday September 22, 2016 @05:01PM (#12560343)
Type inference makes all the difference in my opinion. Once you have even simple (C# level) type inference than the overhead of static typing is so low there isn't much reason to use dynamic typing.
Re:Announcing TypeScript 2.0 by jeremiep on Friday September 23, 2016 @10:22AM (#12564815)
Yes I agree type inference is wonderful! But when the underlying type-system is just C# I feel like I'm given a half-assed solution.

What good is having a int type if you can't limit its range?

I'm not going to maintain BOTH static types and tests. The ROI is way too low on that and won't even come close to guarantee robustness.

Assertions over composable predicates gives me much more out of the time invested to write it. They are right there in the code at runtime, not separate like unit tests or in a different phase like static types.

Re:Announcing TypeScript 2.0 by wvenable on Friday September 23, 2016 @11:23AM (#12565277)
> What good is having a int type if you can't limit its range?

Well then you don't have to test what happens when someone passes a string, an object, or a boolean. Most dynamic languages will silently convert many of these values into an int and produce incorrect results.

> I'm not going to maintain BOTH static types and tests.

You have to maintain documentation and you have to maintain tests and you have to maintain assertions for the implied types of your functions anyway so you might as well make them explicit in the code.

Re:Announcing TypeScript 2.0 by jeremiep on Friday September 23, 2016 @12:06PM (#12565610)
> Well then you don't have to test what happens when someone passes a string, an object, or a boolean.

That's useless, I still need to test the value's range which implicitly checks the type.

> You have to maintain documentation and you have to maintain tests and you have to maintain assertions

Not if I can generate most of it from the specification data.

Re:Announcing TypeScript 2.0 by wvenable on Monday September 26, 2016 @06:53PM (#12586311)
> What good is having a int type if you can't limit its range?

Most types in the program aren't ints anyway; they're interfaces. If I want to have a range-limited value, I could just create a new type for it.

Re:Announcing TypeScript 2.0 by coldtea on Thursday September 22, 2016 @06:50PM (#12560997)
>Oh I know, but my opinion and experience in half a dozen statically typed languages over a decade is that most type systems add much more overhead to development than agility.

In what sense?

With type inference you don't have to manually specify them.

With a variant type, you don't have to confirm to a specific type, but can accept all, if you want it.

And you still need to conform to an interface in a dynamic language, if your function is x.indexOf(x) you still need to make sure to use it on two strings, if it's a math function on some numbers (and depending on the function, on a range), etc (1). So you are not even spared that -- instead, you just lose the help from the compiler when it comes to it.

(1) You could e.g. let a numeric type string autocoerce of course, but auto-coercion can also happen in static languages, is orthogonal to static vs dynamic typing, and is also generally to be avoided).

Re:Announcing TypeScript 2.0 by lloyd-christmas on Thursday September 22, 2016 @06:58PM (#12561039)
> most type systems add much more overhead to development than agility

I think this is true to a certain extent, but with a language like JS, there is a lot more composition of generics than there is inheritance. I see:

  add5toStuff<T extends {stuff: number}>(item: T): T;
drastically more than:

  class X {}
  class Y extends X {}
  class Z extends Y {}
The latter is always a nightmare of fighting the compiler. The former is a breeze to deal with. Your type casting will be limited almost completely to IO.
Re:Announcing TypeScript 2.0 by jeremiep on Friday September 23, 2016 @10:03AM (#12564670)
Both styles still encourage a lot of mutability and complexity throughout the codebase.

The vast majority of typed web codebases I've seen were exercises in Java Enterprise development of various degrees. Generics or not the time spent typing everything is not matched by the advantages of static typing.

You'd get more robustness by managing complexity and state; I've spent far more time hunting down bugs that passed through the type-system and tests than fixing typos in dynamically typed languages.

Re:Announcing TypeScript 2.0 by lloyd-christmas on Friday September 23, 2016 @01:08PM (#12566184)
> Both styles still encourage a lot of mutability and complexity throughout the codebase.

> You'd get more robustness by managing complexity and state;

The top function has a return value of T. That's not encouraging mutability. It's only mutable because JS pass by reference, which is irrelevant to static/dynamic. If you use the return value instead of the passed value, it's "effectively" immutable and is a great way of managing state.

Re:Announcing TypeScript 2.0 by wvenable on Thursday September 22, 2016 @05:03PM (#12560352)
If I do heavy enough refactoring where even the tests break then static typing is wonderful. You literally know exactly what needs to be fixed because it won't compile (and usually you know well in advance of that in the IDE).

Heck then I can even fix the statically typed tests that might be broken.

Re:Announcing TypeScript 2.0 by Too on Friday September 23, 2016 @12:41PM (#12565937)
Overhead? What overhead? Overhead is having to keep the implicit type in my brain instead of having it written on the screen right in front of me.
Re:Announcing TypeScript 2.0 by copperx on Thursday September 22, 2016 @04:26PM (#12560068)
> Or you're now generating the wrong filenames.

Having a no-overhead Filename type would make this impossible.

Re:Announcing TypeScript 2.0 by jeremiep on Thursday September 22, 2016 @04:38PM (#12560170)
How so? That type would be a glorified string at best.

Types can't check the ranges of their values in most languages. And definitely can't do so at runtime.

Re:Announcing TypeScript 2.0 by rpeden on Thursday September 22, 2016 @06:06PM (#12560729)
I can see it working in a statically types language with macros - maybe Scala or Template Haskell? I'm not 100% sure if their metaprogramming is capable of this. Ideally, you could define a type like FileNameString to be a string that has to match a certain regex. Though if you wanted to use the new type throughout your program, I suppose you'd need a String -> FileNameString function somewhere to handle instance of file names generated at runtime.

I agree that the type systems in most mainstream languages fall somewhere short of awesome. I was putting together a small web app in Elm last week, and it was nice to work with a type system that felt like it was helping me design and write better code. That, plus it has the most helpful errors message I've ever seen from a compiler.

Re:Announcing TypeScript 2.0 by Too on Friday September 23, 2016 @12:45PM (#12565971)
Actually it's quite common. For example in .net you have a Uri class and a DirectoryInfo class which are basically just wrappers on top of strings with some validation and helper functions.
Re:Announcing TypeScript 2.0 by johnfn on Thursday September 22, 2016 @04:55PM (#12560302)
> I usually have so much less code when throwing static types out the window that refactorings become a non-issue.

This is almost certainly our difference. The utility of static typing is greatly diminished when the codebase is not very large.

> Static types won't help you refactor a monster class into a structure of arrays.

Sure they will. I mean, sure, it won't be a one-click magic refactor, but that was never the benefit anyways. One strong advantage of static typings is that now you'll know exactly when all your call sites break, so that finding and fixing them will be much more straightforward.

Re:Announcing TypeScript 2.0 by hota_mazi on Thursday September 22, 2016 @04:52PM (#12560269)
> I don't get this. Most of the known refactorings can't be easily implemented by IDEs so you're left with the trivial ones like renaming or moving things.

These alone are already huge. Think about it for a while, keeping a code base healthy as it goes through evolutions involves a lot of renaming and moving things around.

But there are so many more refactorings that are tedious or even dangerous to do manually. For example, switching the parameters of a method, lifting a method in a base class, extracting an interface out of a class and using that interface everywhere in your code base instead of the concrete class, etc...

Spend some time reading the extremely long list of refactorings offered by IDEA and Eclipse, it will open your eyes.

Re:Announcing TypeScript 2.0 by duaneb on Thursday September 22, 2016 @05:11PM (#12560402)
Renaming and moving is still more than you can get with a dynamic language.
Re:Announcing TypeScript 2.0 by bsimpson on Thursday September 22, 2016 @03:55PM (#12559794)
Or in a GUI: Apple-Shift-F

Find: <old name>

Replace: <new name>

…continue working.

Re:Announcing TypeScript 2.0 by SiVal on Thursday September 22, 2016 @04:37PM (#12560167)
That crosses namespace boundaries and risks hitting embedded parts of names, which means that you can't simply replace all as a general practice. You can inspect each one before replacing and be your own type checker or just go ahead and blast them all blindly. With a little thought, blind replace-all will usually work with small code written very recently by you but will become increasingly error-prone with more code, more developers, over more time.

You can, of course, try using case-sensitivity and whole-word search modifiers while trying to maintain naming conventions to keep replace-all working a little longer, but that's just an incomplete and buggy attempt at creating your own watered-down type system, not proving that types aren't useful.

Re:Announcing TypeScript 2.0 by shados on Thursday September 22, 2016 @04:16PM (#12559978)
Won't work super hot if you have 2 functions with the same name in 2 different modules and you want to rename one and all its usages but not the other.

It's quite impressive what VisualStudio + Resharper + plugins or IntelliJ with a few plugins can do in their respective first class languages.

Re:Announcing TypeScript 2.0 by jeremiep on Thursday September 22, 2016 @04:29PM (#12560094)
I used to like ReSharper but quickly uninstalled it from the large amount of false positives it reported.

Back when I did C# last year I was doing 2D skeletal animations in Unity3D and pretty much wrote the most non-idiomatic C# code ever. Everything was bitfields, structures of arrays and integer indices into contiguous memory buffers rather than heap references.

The tooling was completely useless there. You can't optimize for cache misses and branch predictions without throwing all that nice typed goodness out the window.

As much as types, tests and whatnot can help you, at the end of the day it is impossible to do engineering without understanding what you're doing, regardless of what is there to do heavylifting for you.

Re:Announcing TypeScript 2.0 by westoncb on Thursday September 22, 2016 @04:21PM (#12560027)
This doesn't actually work. Let's say the variable you'd like to rename is 'label,' and you also have variables named 'labelOne' and 'labelTwo'—see the problem?
Re:Announcing TypeScript 2.0 by jshen on Thursday September 22, 2016 @03:41PM (#12559689)
I just want to point out there is very little emporical data on this front which makes almost every statement along the lines of "static is better because ..." or "dynamic is better because ..." to be pure opinion.
Re:Announcing TypeScript 2.0 by stcredzero on Thursday September 22, 2016 @05:25PM (#12560499)
Statically typed languages have a few large advantages over dynamic languages

Fallacies! I'd have said "lies" but I realize you just have limited information. See below.

    - Refactoring. Refactoring a large JavaScript codebase (> 100k loc) is nearly impossible
Really? Refactoring large codebases in VisualWorks Smalltalk was a joy! Hyper nimble. If you stuck to canned refactorings, you had 100% confidence. For years, we were talking about how we could refactor an order of magnitude faster than other environments, and this is still true.

    - Your code is now more discoverable. You can easily find definitons, and discovering all methods and properties
Discovery in Smalltalk was much superior and way more nimble. Combining senders/implementors searches, syntactic pattern matching, and runtime discovery was powerful and joyful to use.

    - Code is now more readable. Objects are never opaque. You never have to dig through multiple calls 
I don't know what you're getting at here. No environment has been more transparent to work in than Smalltalk. You don't just get to read a spec of the object -- you actually get to see it and poke and prod the live version of it to verify your understanding and test your theories. (I understand you can do this in languages like C++ -- it's what I use in my day job -- but it's literally an order of magnitude nimbler in Smalltalk.)

What you have to say about dynamic languages perhaps has to do with somewhat ad-hoc language design combined with somewhat ad-hoc design of the environment and distribution/execution platform. (Javascript/Web browsers?) It certainly doesn't have to be true for dynamic environments per se.

Re:Announcing TypeScript 2.0 by johnfn on Thursday September 22, 2016 @08:00PM (#12561360)
Recall that we're talking in the context of TypeScript vs JavaScript. :P I'm not saying that it's impossible for dynamic languages to be able to refactor, but the truth in the industry is that your choice is probably between JavaScript and TypeScript, not JavaScript and VisualWorks Smalltalk. ;)
Re:Announcing TypeScript 2.0 by qwertyuiop924 on Thursday September 22, 2016 @09:12PM (#12561639)
Well, amber does exist...

But yeah, probably not.

Re:Announcing TypeScript 2.0 by Roboprog on Sunday September 25, 2016 @01:07PM (#12576178)
> more readable

Well, you have very detailed trees, anyway. But the forest is often quite obscured: http://stackoverflow.com/questions/39672359/make-all-identif...

Yeah, I know I'm complaining about one of the worst offenders, rather than TypeScript, but "Welcome to My Nightmare", as it were.