Stacks

Extract a shared helper from duplicated code in Elixir with Atlas in 2026

Updated 7 min read

Atlas empowers Elixir developers in 2026 to efficiently extract shared helpers from duplicated code, leveraging its semantic search to identify near-identical logic that traditional `grep` might miss. This process integrates direct with the Elixir toolchain, allowing you to create new modules, replace code, and verify changes using `ExUnit via mix test` and `mix format` directly from your terminal.

How Atlas finds duplicated Elixir logic across files

Atlas streamlines extracting shared helpers in Elixir by leveraging its semantic search capabilities. In 2026, developers can use `codebase_search` to identify near-duplicate logic across 5 or more files, even when variable names differ, a task `grep` often misses. This ensures you target genuine semantic duplication, not just textual matches.

Identifying duplicated logic in an Elixir codebase is the first critical step in refactoring. Atlas's `codebase_search` tool goes beyond simple text matching by using hybrid semantic and keyword retrieval fused by reciprocal rank fusion. This means it understands the *meaning* of your Elixir code, not just the exact characters. For instance, if you have a function `calculate_discount(price, quantity)` in `lib/my_app/orders.ex` and a semantically identical `compute_rebate(cost, count)` in `lib/my_app/invoices.ex`, `codebase_search` can surface both. It indexes code by AST declarations using tree-sitter, providing a deeper understanding of Elixir's `defmodule`, `def`, and `fn` constructs. You initiate the search by describing the behavior you're looking for, rather than specific variable names or syntax. Atlas can build its code index with local Ollama embeddings, keeping your proprietary Elixir code off third-party servers. After the search, you'll `read` each hit and confirm the copies are genuinely equivalent before proceeding to collapse them.

Creating a new shared Elixir module with Atlas

Once duplicated Elixir logic is identified, creating a new shared module is straightforward with Atlas. The `write` tool allows you to draft a new `.ex` file, such as `lib/my_app/helpers/calculations.ex`, complete with its `defmodule` and helper functions, ensuring a clean abstraction for your refactoring efforts in 2026.

After confirming the duplicated logic, the next step is to create a dedicated Elixir module to house the shared helper. Atlas's `write` tool facilitates this by allowing you to specify the new file path and its initial content. For example, you might instruct Atlas to `write lib/my_app/helpers/calculations.ex` with a `defmodule MyApp.Helpers.Calculations do ... end` structure. Before any file is created, Atlas computes a unified diff for the proposed changes and surfaces it for your approval. This permission-gated process ensures you have full control over the new module's structure and location within your `mix` project or OTP application. You can review the full diff in the permission prompt, ensuring the new module adheres to your team's Elixir coding standards and `mix format` conventions. This step is crucial for establishing a single source of truth for the extracted logic, ready to be called from various parts of your application.

Replacing duplicated Elixir code with helper calls

Replacing each instance of duplicated Elixir code with a call to the new shared helper is a precise operation handled by Atlas's `apply_patch` tool. This tool generates one reviewable patch per file, ensuring that each swap, like changing a 10-line block to a single `MyApp.Helpers.Calculations.calculate(...)` call, is independently verifiable and reversible.

With the shared Elixir helper module in place, the next task is to replace all instances of the original duplicated code with calls to the new helper function. Atlas's `apply_patch` tool is designed for this exact scenario. Instead of a single, monolithic change, `apply_patch` creates a separate, unified diff for each file where a replacement occurs. For example, if `lib/my_app/orders.ex` and `lib/my_app/invoices.ex` both contained the duplicated logic, Atlas would generate two distinct patches. This granular approach means each swap, such as replacing a complex `case` statement with `alias MyApp.Helpers.Calculations; Calculations.calculate(data)`, is independently reviewable and revertible. Before applying any patch, Atlas presents the diff for your approval, adhering to its permission-gated tool call policy. This allows you to confirm that the new helper call correctly integrates with the existing Elixir code, including proper `alias` or `import` statements, and that the original logic is fully removed.

Verifying Elixir refactoring with `mix test` and `mix format`

After each code replacement in Elixir, it's paramount to verify the changes immediately. Atlas integrates directly with your Elixir toolchain, allowing you to run your test suite using `atlas bash "mix test"` after every `apply_patch` operation, ensuring that the refactoring hasn't introduced any regressions in your 2026 codebase.

Maintaining code quality and correctness during refactoring is non-negotiable in Elixir development. Atlas facilitates this by allowing you to execute arbitrary shell commands using its `bash` tool. After each `apply_patch` operation that replaces duplicated code with a helper call, you can immediately run `atlas bash "mix test"` to execute your `ExUnit` test suite. This ensures that the change in that specific file has not broken any existing functionality. If tests pass, you can proceed to the next file. If they fail, you can easily roll back the last patch using Atlas's git patch snapshots, which allow edits to be diffed and rolled back. Furthermore, Atlas can be instructed to run `atlas bash "mix format"` to ensure that all newly created or modified Elixir files adhere to your project's formatting standards, maintaining consistency across your `mix` project. The final step involves using `atlas grep` to search for any surviving copies of the original duplicated logic, providing a definitive check that the refactoring is complete.

Step by step

  1. 01Describe the duplicated Elixir logic: Ask Atlas to `codebase_search` for the behavior of the duplicated logic, not the exact code, to surface near-duplicate implementations that `grep` would miss across your `mix` project.
  2. 02Confirm semantic equivalence: `read` each hit surfaced by `codebase_search` and confirm the Elixir code copies are genuinely equivalent in meaning before collapsing them into a shared helper.
  3. 03Create the shared Elixir helper module: Use `atlas write lib/my_app/shared_helpers.ex` to create the new module, defining the helper function(s). Review the full diff in the permission prompt before the file is created, ensuring it aligns with `mix format` standards.
  4. 04Replace duplicates with helper calls: For each identified duplicate, use `atlas apply_patch` to replace the Elixir code block with a call to the new shared helper, such as `MyApp.SharedHelpers.my_function(args)`. Atlas will generate one file-specific patch for review.
  5. 05Run Elixir tests: After each `apply_patch` operation, execute your test suite with `atlas bash "mix test"` to ensure no regressions have been introduced in your Elixir application.
  6. 06Format the code: Optionally, run `atlas bash "mix format"` to ensure all modified Elixir files adhere to the project's formatting guidelines.
  7. 07Verify completion: Finish by using `atlas grep "original_logic_keyword"` to search for any surviving copies of the original duplicated Elixir logic, confirming the refactoring is complete.

Frequently asked questions

How does Atlas find duplicated Elixir code that `grep` misses?
Atlas uses hybrid semantic and keyword retrieval, fused by reciprocal rank fusion, to understand the *meaning* of your Elixir code. It indexes by AST declarations using tree-sitter, allowing it to identify semantically identical logic even when variable names or minor syntax differ, which `grep` cannot do.
Can Atlas help me create a new `.ex` file for my shared helper?
Yes, Atlas's `write` tool allows you to create new Elixir files, such as `lib/my_app/utils.ex`, with specified content. Before creation, Atlas presents a unified diff for your approval, ensuring you control the new module's structure and location within your `mix` project.
How does Atlas ensure my Elixir tests still pass after refactoring?
After each `apply_patch` operation, you can use `atlas bash "mix test"` to run your `ExUnit` test suite. This immediate feedback loop ensures that each individual change to your Elixir codebase is verified for correctness before proceeding.
What if I need to revert a change made by Atlas in my Elixir project?
Atlas snapshots file changes as git patches, allowing edits to be diffed and rolled back. Every `apply_patch` operation is permission-gated and creates a reviewable patch, making it easy to revert specific changes if needed.
Does Atlas integrate with Elixir's `mix format`?
Yes, you can use `atlas bash "mix format"` at any point to ensure that all modified or newly created Elixir files adhere to your project's formatting standards, maintaining code consistency across your `mix` project.
Can Atlas work with my local Elixir development environment?
Absolutely. Atlas is terminal-native and works directly within your local Elixir project. It can build its code index with local Ollama embeddings, keeping your code off third-party servers, and reads your `mix.exs` and git status.
How does Atlas handle reviewing changes before applying them to my Elixir files?
Every Atlas tool call, including `write` and `apply_patch`, is permission-gated. Atlas drafts a plan, computes a unified diff for every file edit, and surfaces it for your approval before writing any changes to your Elixir codebase.

Try Atlas in your terminal

The terminal-native AI coding agent. Free core, single binary.

Install Atlas

Related guides

Extract a Shared Helper from Duplicated Code with Atlas (2026 Workflow)

How to extract a shared helper from duplicated code with Atlas in 2026: codebase_search finds the copies by meaning, write creates the module, apply_patch swaps each call.

Atlas for Elixir in 2026

Adopt Atlas, the terminal-native AI coding agent, for Elixir development in 2026. Enhance productivity with deep code understanding, safety features, and direct integration into mix projects and OTP applications.

Plan a Multi-File Elixir Change Before Editing with Atlas (2026)

Plan a multi-file Elixir change before editing with Atlas in 2026. Plan mode denies all edit tools, so you research contexts and GenServers and design the change first.

Audit an Elixir Repo with Parallel Subagents in 2026 using Atlas

Sweep your Elixir repository for specific problems without blowing your context window. Atlas uses parallel subagents, ExUnit, Mix, and Hex to audit large Elixir codebases efficiently.

Automate GitHub Issue and Pull Request Triage in Elixir with Atlas in 2026

Automate GitHub issue and pull request triage in Elixir projects using Atlas. Configure workflows to safely respond to events, ensuring only trusted users trigger actions and reviewing all changes.

Review a Pull Request in Elixir with Atlas in 2026

Streamline your Elixir pull request reviews in 2026 with Atlas. Leverage AI to fetch diffs, analyze full files, check mix.exs dependencies, and run ExUnit via mix test for comprehensive bug detection.

Refactor a legacy module in Elixir with Atlas in 2026

Refactor Elixir modules safely in 2026 with Atlas, the terminal-native AI coding agent. Pin behavior with ExUnit via mix test, map callsites with LSP, and apply changes with atomic patches.

Write Unit Tests for Untested Code in Elixir with Atlas in 2026

Learn how Atlas helps Elixir developers in 2026 add robust unit tests to untested modules, matching existing repo conventions using ExUnit via mix test.

Browse this resource hub