avatarEric Elliott

Summary

The article discusses the limited impact of static types, as provided by TypeScript, on reducing overall bug density in software development, despite their popularity and the belief that they significantly decrease bugs.

Abstract

The article titled "The Shocking Secret About Static Types" explores the widespread belief that static types, particularly in TypeScript, are essential for reducing bug density in large-scale web applications. While static types do offer some benefits, such as enabling powerful developer tools and catching a subset of bugs, the article questions their effectiveness in significantly lowering the overall number of bugs. It references studies that suggest the impact of static typing on bug density is minimal and emphasizes that type correctness does not guarantee program correctness. The author, Eric Elliott, argues that Test Driven Development (TDD), especially the test-first methodology, has been proven to be much more effective at reducing bug density, with studies showing a 40%-90% reduction in bugs. Elliott suggests that while static types have their merits, developers should not rely on them as a primary means of bug prevention. Instead, he recommends TDD for bug reduction and static types for their contributions to code intelligence tools.

Opinions

  • The author, Eric Elliott, likes TypeScript and its features but is critical of the belief that it solves the problem of bug density in large-scale web applications.
  • Static types are seen as providing a false sense of security regarding bug prevention, as they do not significantly reduce overall bug density.
  • Empirical evidence suggests that the benefits of static types in reducing bugs are quite small, contrary to popular marketing rhetoric.
  • The author emphasizes that type correctness is not equivalent to program correctness, and many bugs cannot be caught by type checking alone.
  • Eric Elliott advocates for Test Driven Development (TDD), particularly the test-first approach, as a more effective method for reducing bug density, backed by various studies.
  • While acknowledging the cool features of static types, the author warns about potential issues with TypeScript's future compatibility with evolving ECMAScript specifications.
  • The article suggests that the choice to use TypeScript should be based on its actual benefits, such as developer tooling, rather than its overstated ability to catch bugs.
  • Eric Elliott predicts that TypeScript may not be able to maintain its position as a superset of JavaScript due to potential divergence from the JavaScript specification over time.
  • The author concludes that for those looking to reduce bugs, TDD is the recommended approach, while static types are best used for their code intelligence benefits.

The Shocking Secret About Static Types

Google Trends: Angular (Red) / TypeScript (Blue)

The popularity of TypeScript has really exploded recently. I like TypeScript, and I like static types. It has some great features. It’s structural rather than nominal, meaning that it works basically like automatic duck typing rather than name or identity based type checking. That is a good fit for dynamic languages like JavaScript.

What I don’t like is that a lot of people think that TypeScript solves a problem that it doesn’t actually solve — at least, not to any significant degree.

The famous marketing rhetoric is “large-scale web applications are hard without static types”. It’s true that static types power some really great developer tools such as jump to definition and automatic refactoring, and those tools feel like they make us more productive. (And may actually make us more productive, we need more study on the real benefits of static types).

But many people believe that static types help you reduce the number of bugs that get into the application. It’s undeniably true that static types catch a significant subclass of bugs, but is it really true that static types reduce over-all bug density?

Static Types Give You a False Sense of Security

A couple of recent studies used the rich data available on GitHub to put the bug density question to the test, and the results are… well… not that hot, actually:

“Whilst not conclusive, the lack of evidence in the charts that more advanced type languages are going to save us from writing bugs is very disturbing.” — Daniel Lebrero, “The Broken Promise of Static Typing”

Static types fared slightly better in a more formal study: “A Large Scale Study of Programming Languages and Code Quality in Github” from Baishakhi Ray, Daryl Posnett, Vladimir Filkov, Premkumar T Devanbu, from UC Davis:

“The data indicates functional languages are better than procedural languages; it suggests that strong typing is better than weak typing; that static typing is better than dynamic; and that managed memory usage is better than unmanaged.”

Now that’s more like it! Except:

“While these relationships are statistically significant, the effects are quite small.” [Emphasis added.]

In fact, I’m not aware of any empirical evidence that static types have a strong impact on bug density.

So in spite of the cool developer tooling they enable, static types don’t actually help reduce over-all bug density by very much. Why?

Type correctness does not guarantee program correctness.

If you haven’t exercised the code, you really have no idea whether or not it works. Sure, you can know whether or not a variable has been defined, or whether or not a function is passing an array instead of an object, but as it turns out:

  • There are a lot of other ways to express bugs in your programs type checking won’t catch, and…
  • A lot of other ways to catch type related bugs.

So What Really Does Reduce Bug Density?

Test Driven Development (TDD). Specifically, test-first methodology.

TDD to the rescue.

Some good studies have been conducted on the effects of TDD. There are several good meta studies, and the famous study from Microsoft, IBM, and Springer. The Springer study showed 40% — 90% reduction in bug density relative to similar projects that did not use the test-first practice. Many other studies have found similar results, comparing test-first to test-after, and no tests at all, with impressive reductions in bug density for test-first (which fares significantly better than test-after) mostly falling in the range of 40% — 80%.

In other words, TDD can effectively cut your shipping bug density in half, and there’s plenty of evidence to back up that claim.

Should You Give TypeScript a Try?

You still may want to use static types, but if you’re going to opt into static types, do it because of the cool benefits they really do provide, and not because of their supposed bug-catching abilities.

I was impressed enough with TypeScript that it’s the primary influence on rtype: a type notation designed to complement standard JavaScript. Why didn’t I just use TypeScript?

I worry about building up a large codebase using TypeScript, only to have the ECMAScript spec introduce conflicting keywords and type features such as `interface` and `implements`.

The advantage of using the TypeScript compiler is that even if that happens, you can just compile away the differences. The disadvantage is that you’re not really writing standard JavaScript anymore. I predict that TypeScript won’t be able to maintain its position as a superset of JavaScript. I believe it will inevitably diverge over the years.

And that’s my bigger concern, because I teach JavaScript, and I want what I teach to apply to a very broad number of developers using various frameworks and developer tools.

You probably don’t have that concern, but your codebase still needs to be easy for other developers to learn and contribute to, and you as an individual need to be able to adapt if you join a team that doesn’t use TypeScript.

Right now, even though it’s growing rapidly, TypeScript has a tiny user base compared to the whole JavaScript language at large, or even jQuery, but it’s on an impressive trajectory. It’s already dominating the compile-to-JavaScript alternatives by a healthy margin.

Some tools are worth the tradeoffs. It’s not right for me right now, but maybe you’ll like it. If you haven’t experienced Microsoft’s Code IDE paired with TypeScript, it’s worth kicking the tires. You might fall in love.

Conclusion

When it comes to bug reduction, I think it’s fair to say:

Static types are overrated.

But when it comes to other features, static types are still cool, and may still be worth using.

Bottom line:

You want to reduce bugs? Use TDD. You want useful code intelligence tools? Use static types.

Want to sit shotgun with me while I build real applications using TDD?

Learn JavaScript with Eric Elliott

Eric Elliott is the author of “Programming JavaScript Applications” (O’Reilly), and “Learn JavaScript with Eric Elliott”. He has contributed to software experiences for Adobe Systems, Zumba Fitness, The Wall Street Journal, ESPN, BBC, and top recording artists including Usher, Frank Ocean, Metallica, and many more.

He spends most of his time in the San Francisco Bay Area with the most beautiful woman in the world.

JavaScript
Programming
Recommended from ReadMedium