Implicit try in Elixir

Article autor
August 18, 2023
Implicit try in Elixir
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

In the world of Elixir programming, there are numerous features and syntactic constructs that contribute to the language's elegance and expressiveness. One such hidden gem is the concept of "implicit try".

If you've been working with Elixir for a while, you might have encountered references to this concept, especially when dealing with errors and exception handling. In this exploration, we'll delve deeper into the implicit try mechanism, understand its purpose, and see how it can enhance the readability of your Elixir code.

Unveiling the Mystery

Imagine you're working on a project, and you stumble upon an intriguing error message from Credo, the widely-used static code analysis tool for Elixir. This message highlights the preference for using an implicit try instead of an explicit one, nudging you towards cleaner and more idiomatic code. Let's take a look at what this means in practice:

Code Readability

┃ [R] ↘ Prefer using an implicit `try` rather than explicit `try`.

The context is a function like the following:

defp value_to_integer(value) do
  try do
    String.to_integer(value)
  catch
    _ -> nil
  end
end

But what exactly is this implicit try concept? It turns out that this lesser-known feature was documented by none other than Jose Valim in 2016. In his contribution, he introduced a syntactic sugar that enhances the way error handling is expressed in Elixir code.

Demystifying Implicit Try

Implicit try is essentially a syntactic transformation that allows you to define a catch block after the function body, all without explicitly needing to use the try keyword. The primary motivation behind this feature is to improve code readability and reduce unnecessary verbosity. Here's a simplified example to illustrate how it works:

defp value_to_integer(value) do
  String.to_integer(value)
catch
  _ -> nil
end

By adopting this style, the code becomes more concise and flows naturally. It aligns with Elixir's philosophy of promoting clean and elegant code that is a joy to read and maintain.

Going Beyond the Basics

The benefits of implicit try don't stop at error handling. In fact, this style can be extended to other clauses such as rescue and after, enhancing the overall structure and clarity of your codebase. This further reinforces the idea that Elixir is not just a programming language but a platform for crafting beautiful and efficient software. Happy coding!

Work with a team that keeps learning and building better software every day.

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

Skip file changes tracking in git

So, you’re changing this one file for local development purposes only. Maybe it’s config, maybe some source file, but one thing is certain - you don’t want those changes to be committed. And what’s worse, .gitignore doesn’t work.

Treating warnings as errors in Elixir's mix compile

Warnings in Elixir are usually an important sign of a problem in the codebase. There is an easy way to make them gone.

How to contain a fixed positioned element

It's easy to contain absolute positioned elements. Things get a little trickier when you want to contain a fixed positioned element without changing its stylings.