DEV Community

loading...

OCaml for Business

Yawar Amin
Programming languages enthusiast. Author of Learn Type Driven Development: https://www.packtpub.com/application-development/learn-type-driven-development
・3 min read

OCaml is a battle-tested programming language with an emphasis on correctness and efficiency. In domains where both are important, it can really shine:

  • Simple but powerful type system helps to model data and logic correctly and catch errors quickly
  • Safe by default but pragmatically allows you to bypass safety checks when efficiency is critical

Background and capabilities

OCaml is an old language (current implementation is about as old as Java, with ancestry going back to the '70s). It offers a pragmatic mix of programming styles:

  • Functional and modular: this is the default–enables writing clean, efficient code
  • Object-oriented: if you need inheritance and dynamic dispatch, they're available. If you just need some objects quickly a la JavaScript, those are available too
  • Imperative: if you really just need a while loop and some mutation, OCaml has those too–it's just that developers don't need them that often with OCaml's idiomatic functional style

OCaml can have a wide reach throughout the tech stack. It can compile to minimal native executable apps and is easy to deploy on servers and end-user machines. It has GUI libraries that enable desktop apps, and mobile GUIs are in development. It can compile to efficient JavaScript and run on browsers and Node.js. There's also a Jupyter Notebook available for doing data science.

Who's using it?

  • Facebook: Messenger web frontend, Ads, various code safety and analysis tools
  • Bloomberg: derivatives analysis platform
  • Docker: virtualization internals, Docker for Mac and Windows
  • Jane Street Capital: core trading platform
  • SAP Bangalore: internal tooling
  • Elastic (i.e. the ElasticSearch vendor): data security tooling
  • Many others

Why should businesses choose OCaml?

OCaml streamlines the process of development and forces many code quality issues to be handled sooner rather than later. Concretely:

  • Fast, Go-like build speed helps iterate and ship quickly
  • Type system catches many classes of bugs; people regularly experience 'if it compiles it works'
  • Module system forces clean, layered code organization
  • Warnings system forces code cleanliness (i.e. removing deprecated and dead code)

OCaml ships out of the box with much of the code quality tooling that other languages need to add on linters and static analyzers for. This helps to reduce maintenance burden of codebases over time.

Although powerful, OCaml by design discourages complex (what developers call 'magical') code, instead favouring simple, explicit code. Developers can get surprisingly far with just types and functions. And OCaml's 'secret sauce'–its simple yet powerful module system–lets developers enforce strong contracts between system components. What in other languages may require microservices with service protocols–will in OCaml require only a module in the app.

OCaml is also very stable. Its maintainers take backward compatibility very seriously and work on large-scale improvements over the time scale of years to ensure everyone has an upgrade path. Old OCaml codebases usually compile with very few changes.

Why not OCaml?

It's a niche language with a smaller (but growing) community and ecosystem. Developers in the team may need to invest a little more time to be more 'in tune' with the community, ecosystem and packages, and general best practices. Fortunately, the community is welcoming and tight-knit, with a lot of knowledge sharing happening regularly on the forums.

OCaml also can run only one thread at a time, much like Node.js/Python/Ruby. (This is to enable an efficient, low-latency garbage collector.) However, its support for system threads as well as asynchronous promises is excellent, and many use cases simply don't need or can easily work around multiple parallel threads.

Hiring concerns?

It really depends on your filtering process for candidates. There is a large pool of programmers who would be excited to program in OCaml. They are probably not located where you are though, unless you're somewhere like New York or San Francisco. If you hire remote you will have a large pool of skilled candidates.

OCaml for you

Ultimately the decision rests on many factors, but OCaml offers some quite attractive outcomes:

  • Getting rid of bugs like 'null pointer exception' crashing your service at 3 am
  • Apps that can be iterated on quickly and safely
  • Performant apps that don't guzzle system resources
  • A clean, understandable, and maintainable codebase over time

If you're interested, here's where you can learn more about OCaml and what it can do:

Discussion (1)

Collapse
epsi profile image
E.R. Nurwijayadi

Good article. Thank you for posting.

I'm a beginner in OCaml. To help more beginner, I have made a working example of concurrency using actor pattern with source code in github.

🕷 epsi.bitbucket.io/lambda/2020/11/0...

First the receiver, using mutable reference.

Receiver

Then sender. And finally run receiver in separate thread.

Sender

I hope this could help other who seeks for other case example.

🙏🏽

Thank you for posting with general introduction..