eph baum dot dev

← Back to blog

WTF is Idiomatic

Published on 10/27/2023 09:50 PM by Eph Baum

Featured Image

In a recent post I used the word “idiomatic” more than once and had to make sure I was using right. Then, someone asked me what the hell it meant so I figured others might also wonder what it means and figured I’d write another post about it because content, right?

In the context of programming, “idiomatic” refers to code that follows the practices, patterns, and conventions of a particular language. Writing idiomatic code usually results in more readable, efficient, and maintainable code. It aligns with the language’s philosophy and leverages its unique features.

On the other hand, “non-idiomatic” code can often resemble “writing Language A in the style of Language B.” For example, using throw and catch for control flow in Elixir could be considered non-idiomatic because Elixir, being a functional programming language, prefers other constructs like pattern matching, guard clauses, case, and cond for handling various conditions and branching logic.

Idiomatic Elixir Examples:

Pattern Matching:

def calculate_area({:circle, radius}) do
  :math.pi() * radius * radius
end

Guard Clauses:

def is_even?(n) when rem(n, 2) == 0, do: true
def is_even?(_), do: false

Non-Idiomatic Elixir Examples:

Throw/Catch for Control Flow:

try do
  if some_condition do
    throw(:abort)
  end
catch
  :throw, :abort -> :error
end

Imperative Style Loops:

# Using recursion to mimic a 'for' loop, instead of using Enum.map/2, Enum.reduce/2, etc.

By adhering to idiomatic practices, you embrace the strengths and paradigms of the language, making it easier for others who are familiar with the language to understand and contribute to your code.

I hope this helps clarify the terms “idiomatic” and “non-idiomatic” in the context of Elixir or any programming language.

Written by Eph Baum

← Back to blog
  • 50 Stars - Puzzle Solver (of Little Renown)

    50 Stars - Puzzle Solver (of Little Renown)

    Join Eph Baum as they recount their journey through the Advent of Code 2023. For the first time, Eph completes all puzzles, leveraging resources like GPT-4 and Code Llama. Despite the challenges and time constraints, Eph not only stays on top of the puzzles but also lands on the top 1,000 leaderboard. Dive into this post to explore the role of generative AIs in problem-solving and the joy of coding puzzles. - GitHub Co-pilot

  • Don't Trust AI - An Advent of Code Tale

    Don't Trust AI - An Advent of Code Tale

    Join Eph Baum in 'Don't Trust AI - An Advent of Code Tale' as they navigate the Advent of Code 2023. Despite the December rush, Eph is determined to complete all puzzles. This post shares an intriguing incident where an AI-generated code line proves less than helpful. Eph's journey underscores the importance of verifying AI suggestions, especially when optimizing code. Dive in to explore the challenges and triumphs of coding puzzles, and the role of AI in this process. - GitHub CoPilot

  • Condoning Another Pi Day

    Condoning Another Pi Day

    Placeholder description for imported post from Ghost Blog

  • ANSI Terminal Colors

    ANSI Terminal Colors

    Placeholder description for imported post from Ghost Blog

  • WTF is Idiomatic

    WTF is Idiomatic

    Placeholder description for imported post from Ghost Blog

  • From Early Return in OOP to Control Flow in Elixir - A Transition Guide

    From Early Return in OOP to Control Flow in Elixir - A Transition Guide

    Placeholder description for imported post from Ghost Blog