Java – Using a “strong” type system in the real world, say, for large-scale web-apps

haskelljavarustscalatype-systems

I know this is a very broad, ambiguous, and possibly philosophical question. To an extent, that the most important keyword in the question – "strong" type system – itself, is ill-defined. So, let me try to explain what I mean.

Overall context for the question

We've been building a very large scale web-app in Ruby on Rails and we've been generally happy about our stack. When we want to, we can ship stuff really fast – something that works for 90% of the "business" case, without worrying too much about 10% edge cases. On the other hand, with the help of code-reviews & test-coverage, we can be slow & deliberate and make sure we cover all bases – again, only in situations that merit such closer scrutiny & safety.

However, as the team grows, I've started getting uncomfortable with the lack of a "safety net" baked right into our stack.

We recently started doing some native Android development on Java. And I was (pleasantly) reminded of the safety provided by a compiled/static/strongly-typed language.

  • Mis-spelled variables, wrong data types, incorrect function invocations, and a host of trivial errors are caught by your IDE itself. All because the IDE can hook into the compiler and verify certain aspects of program "correctness".
  • Need to change a function signature? Easy. The compiler+IDE can help you spot ALL call sites.
  • Need to ensure that certain exceptions are always handled? Checked exceptions to your rescue.

Now, while these safety features have their advantages, I'm well aware of their disadvantages as well. More so, in the world of "boilerplate heavy" Java. Therefore, instead of Java, I've started looking at the slew of modern, "strongly typed" languages that people have started working on these days. For example: Scala, Rust, Haskell, etc. What interests me most is the power of their type systems and static/compile-time checks.

Now, the question

How do I use these powerful type systems and static/compile-time features in larger applications?

For example, how would I move beyond the standard "hello world" kind of introductions to these powerful features? One that uses a a rich type system to model a business-domain problem? Does the type system help, or hinder, when you're in the 30,000 LOC+ zone? What happens to the safety net provided by these type systems (and compile-time checks) when your system interacts with the weakly typed outside world, eg. via JSON or XML APIs, various data stores, user input, etc.

Best Answer

I'll give a short answer due to my lack of time at the moment, but I'm currently working on two big projects (> 100.000 LOC in Haskell) - flowbox.io and luna-lang.org. We use Haskell for all the parts, including the backend, compiler of our programming language and even the webGL based GUI. I have to admit that the strong type system and the "dependent type"-like machinery can guide you and save you from burden and hassle known from other languages. We use the types very extensively and everything that could be checked in compile time, is done so. In fact, during the last 3 years of development, we never, ever encountered any runtime error or stack overflow (and this is something really incredible). The only errors are obvious logic errors made by programmers. A lot of people tell, that if something compiles in Haskell, it just works and you should be pretty sure it will not blow in your face some day. This is true for most of the situations and when you know the language well and know what to avoid (like unimplemented typeclass methods), you'll be safe and gain big profits from the type system.

Answering the first part of the question: You can learn about these powerful type system features reading some great blogs, like:

In fact, there are a lot of other nice blogs out there (like planet Haskell). Anyway, the best method to really understand the advanced type systems is to develop a useful open source library. We (at Flowbox & New Byte Order) are releasing a lot of libraries (you can find them at Hackage), so if you don't have an idea what to develop, you can always involve into our projects – just email me whenever you want (mail available at luna-lang.org).

Related Topic