Defining active menu item in Phoenix Framework through Short-circuit Evaluation
Tags: phoenixframework, elixir, css, html
Published: 2023-12-30 01:50:00

---
title: Defining active menu item in Phoenix Framework through Short-circuit Evaluation
published: true
description: Learn how to use lazy evaluation and short-circuit evaluation to define active item in Phoenix Framework (Elixir). 
tags: phoenixframework, elixir, css, html
cover_image: https://images.unsplash.com/photo-1693392132762-6442cb40102f?q=80&w=2673&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D
canonical_url: https://wevtimoteo.github.io/posts/2023-12-29-defining-active-menu-item-in-phoenix-framework.html
published_at: 2023-12-30 01:50 +0000
---

- [Ler versão em pt-BR](https://dev.to/wevtimoteo/definindo-item-ativo-no-menu-no-phoenix-framework-usando-short-circuit-evaluation-ph0)

When developing web applications, marking menu items, tabs, or other parts of the user interface as active is a common need. In the [Phoenix Framework](https://phoenixframework.org/), doing this quickly might lead you to something like this:

```elixir
<.link
  href={~p"/stores/#{@store}/products"}
  class={"tab" <> if @active == :products, do: " tab-active", else: ""}
>
  Products
</.link>
```

In this approach, we use ternary expressions and string concatenation to conditionally define CSS classes. It works, but as conditional logic increases, it can become challenging to read and maintain.

## Introducing Short-circuit Evaluation

In Elixir, **lazy evaluation** and **short-circuit evaluation** are often used interchangeably. However, there's a slight difference. Lazy evaluation is usually associated with more complex data structures, while short-circuit is **more common in boolean expressions**. And that's what we're going to use.

Let's look at some examples using `iex`:

```elixir
list = ["plataformatec", nil, "sourcelevel", false, "dashbit"]
#=> ["plataformatec", nil, "sourcelevel", false, "dashbit"]

# Using short-circuit evaluation on the list
list = ["plataformatec", nil && "sourcelevel", false && "dashbit"]
#=> ["plataformatec", nil, false]
```

In the second definition of the list, we are using a short-circuit operation, where the last value of the expression prevails when the previous one is true; otherwise, the previous value is retained.

In simple terms, it's like telling [Elixir](https://elixir-lang.org/): "stop thinking as soon as you know the answer."

## Applying Short-circuit to the CSS Class List

In a [Phoenix Framework issue #276](https://github.com/phoenixframework/phoenix_html/issues/276#issuecomment-584911356), an approach using lazy evaluation for the class syntax was discussed. [In this comment](https://github.com/phoenixframework/phoenix_html/issues/276#issuecomment-742375950), it was clarified that if the list item is `false` or `nil`, it will be removed. We can apply this solution to our example:

```elixir
<.link
  href={~p"/stores/#{@store}/products"}
  class={["tab", @active == :products && "tab-active"]}
>
  Products
</.link>
```

If `@active` is `:products`, the string `"tab-active"` will be added to the list (resulting in `tab tab-active`), otherwise, it will be omitted (resulting in just `tab`). This approach is cleaner, more readable, and efficient:

1. **Code Conciseness**: The lazy evaluation syntax is more concise, resulting in cleaner code.
2. **Efficiency**: Lazy evaluation halts evaluation as soon as the result is known, avoiding unnecessary evaluations.
3. **Readability**: The class list is easy to understand and maintain, improving code readability.
4. **Ease of Maintenance**: The simplified syntax makes code maintenance easier over time.

When developing applications in Elixir, it's not just about using the functional paradigm, but also about writing concise and easily understandable code. This approach not only simplifies our code but also makes maintenance more straightforward and direct. 😎✨