Stacks

Extract a Shared Helper from Duplicated Fortran Code with Atlas in 2026

Updated 7 min read

To extract a shared helper from duplicated Fortran code, Atlas leverages its semantic search capabilities to identify near-identical logic across your project, even when variable names differ. It then guides you through creating a new Fortran module, replacing each duplicate with a call to the new helper, and validating changes with `fpm test (test-drive)` and `fprettify` before committing.

How Atlas Finds Duplicated Fortran Logic

Atlas's `codebase_search` tool efficiently identifies duplicated Fortran logic across your project, even when variable names or minor structural differences exist. Unlike `grep`, which relies on exact text matches, Atlas uses hybrid semantic and keyword retrieval to surface near-duplicate implementations, making it ideal for refactoring tasks in 2026.

Fortran codebases, especially those with a long history, often accumulate duplicated logic where developers have copy-pasted sections and then adapted variable names or minor details. Traditional text-based search tools like `grep` struggle with this, as they require exact string matches. Atlas, however, indexes your Fortran code by AST declarations using tree-sitter and can build its code index with local Ollama embeddings. This allows `codebase_search` to understand the *meaning* of the code, not just its surface form. For instance, if you have the same calculation repeated in `src/module_a.f90` and `src/module_b.f90` but with different dummy argument names, Atlas can still identify them as semantically equivalent. You would ask Atlas to "find the behavior of calculating X" rather than "find this exact line of code." Atlas then presents these potential duplicates, allowing you to `read` each hit and confirm they are genuinely equivalent before proceeding with the refactoring.

Creating a New Fortran Module for Shared Code

Once duplicated Fortran logic is identified, Atlas simplifies the creation of a new shared module to house the extracted helper. Using the `write` tool, Atlas can draft a new Fortran source file, such as `src/shared_helpers.f90`, complete with module declarations and the new procedure, all within a few seconds.

After confirming the semantic equivalence of duplicated Fortran code, the next step is to create a dedicated module for the shared helper. Atlas's `write` tool is designed for this. You can instruct Atlas to "create a new Fortran module named `shared_helpers` in `src/shared_helpers.f90` containing a function `calculate_x` that takes `arg1` and `arg2`." Atlas will draft the necessary Fortran syntax, including `module shared_helpers`, `contains`, and the `function` or `subroutine` definition, along with `implicit none` and `intent` declarations for dummy arguments, adhering to modern Fortran best practices. Before any file is created, Atlas presents a full unified diff for your approval, ensuring you have complete control over the generated code. This process is permission-gated, requiring your explicit `allow` or `ask` before Atlas proceeds, keeping your codebase secure and predictable.

Replacing Duplicates with Helper Calls in Fortran

With the new Fortran helper module in place, Atlas uses `apply_patch` to systematically replace each instance of the duplicated code with a call to the new shared procedure. This process is meticulously managed, generating one reviewable patch per file, ensuring that each swap is independently verifiable and reversible, typically across 3 to 5 files.

The core of the refactoring involves replacing the identified duplicated Fortran code with calls to the newly created shared helper. Atlas's `apply_patch` tool handles this with precision. For each file containing a duplicate, such as `src/module_a.f90` or `src/legacy_routine.f`, Atlas computes a unified diff that shows the removal of the old logic and the insertion of the `use shared_helpers` statement and the call to `shared_helpers%calculate_x(arg1, arg2)`. Each of these changes is presented as a separate, independently reviewable patch. This granular approach is crucial for complex Fortran projects, allowing you to approve or reject changes on a file-by-file basis. After each `apply_patch` operation, Atlas can be instructed to run `fpm test (test-drive)` using its `bash` tool to immediately verify that the change has not introduced regressions. This iterative testing ensures the integrity of your Fortran application throughout the refactoring process.

Ensuring Quality and Safety in Fortran Refactoring

Atlas integrates directly with the Fortran toolchain to ensure every refactoring step maintains code quality and prevents regressions. After each modification, Atlas can run `fpm test (test-drive)` to validate functionality and `fprettify` to enforce formatting standards, providing a robust safety net for your 2026 Fortran projects.

Safety and quality are paramount when refactoring Fortran code. Atlas is designed with multiple layers of verification. Every Atlas tool call, including `write` and `apply_patch`, is permission-gated, requiring your explicit approval. Before writing any changes, Atlas computes and surfaces a unified diff for your review. For Fortran projects, Atlas can be configured to automatically run `fpm test (test-drive)` via its `bash` tool after each `apply_patch` operation. This immediate feedback loop helps catch any regressions introduced by the refactoring. Furthermore, Atlas can run `fprettify` over the changed sources before you approve the final diff, ensuring that the new code adheres to your project's formatting standards. Atlas also reads `git` branches, status, and diffs, and can stage and create commits on your behalf, or snapshot file changes as `git` patches for easy rollback, providing comprehensive version control integration for your Fortran development workflow. Finally, a `grep` for any surviving copies of the original logic ensures a clean refactoring.

Step by step

  1. 01Ask Atlas to find duplicated Fortran logic: Initiate Atlas and use `codebase_search` to describe the behavior of the logic you suspect is duplicated. For example, "find the Fortran code that calculates the checksum for a data block."
  2. 02Review and confirm duplicates: Use Atlas's `read` tool to examine each search result. Confirm that the identified Fortran code snippets, potentially across files like `src/data_processor.f90` and `src/legacy_utils.f`, are semantically equivalent despite differing variable names.
  3. 03Create the shared Fortran helper module: Instruct Atlas's `write` tool to create a new Fortran module, for instance, `src/checksum_module.f90`, containing the extracted helper procedure. Review the full diff presented by Atlas and approve its creation.
  4. 04Replace duplicates with helper calls: For each identified duplicate, use Atlas's `apply_patch` tool to replace the original Fortran code with a `use` statement and a call to the new helper, like `use checksum_module; call checksum_module%calculate_checksum(...)`.
  5. 05Run Fortran tests after each swap: After each `apply_patch` operation, use Atlas's `bash` tool to execute `fpm test (test-drive)` to ensure no regressions have been introduced in your Fortran application.
  6. 06Format changed Fortran sources: Before finalizing, ask Atlas to run `fprettify` over the modified Fortran source files to maintain consistent code style.
  7. 07Verify no surviving copies: Conclude the refactoring by using Atlas's `grep` tool to search for any remaining instances of the original duplicated Fortran logic, ensuring a complete cleanup.

Frequently asked questions

How does Atlas find duplicated Fortran code if variable names are different?
Atlas uses hybrid semantic and keyword retrieval, indexing Fortran code by AST declarations with tree-sitter and local Ollama embeddings. This allows `codebase_search` to identify semantically similar logic even when variable names or minor structural details differ, which `grep` cannot do.
Can Atlas help me move a `COMMON` block into a modern Fortran module?
Yes, Atlas can assist with modernizing legacy Fortran. You can ask Atlas to "move a common block into a module" using its `write` and `apply_patch` tools, helping you transition to explicit interfaces and `intent` declarations.
How does Atlas ensure my Fortran tests pass during refactoring?
Atlas integrates with your Fortran toolchain. After each `apply_patch` operation, Atlas can run `fpm test (test-drive)` via its `bash` tool, allowing you to iterate on `gfortran` diagnostics and immediately catch any regressions.
What if Atlas generates Fortran code that doesn't match my style guide?
Atlas allows you to run `fprettify` over changed Fortran sources before you approve the diff. This ensures that any code generated or modified by Atlas adheres to your project's formatting standards.
Is it safe to let Atlas modify my Fortran codebase?
Yes, Atlas prioritizes safety. Every tool call is permission-gated, and Atlas drafts a plan in a read-only agent. It computes a unified diff for every file edit and surfaces it for your approval before writing. Additionally, it snapshots changes as `git` patches for easy rollback.
Can Atlas handle Fortran modules with explicit interfaces and `intent` declarations?
Absolutely. Atlas is designed for modern Fortran, reading your modules, explicit interfaces, and `intent` declarations on every dummy argument to understand your codebase context accurately.
How does Atlas help me review the changes it makes to my Fortran files?
Atlas provides a unified diff for every file edit and surfaces it for approval. When replacing duplicated code, `apply_patch` creates one reviewable `git` patch per file, allowing you to independently review and revert each specific change to your Fortran source.

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 Fortran: fpm.toml, Explicit Interfaces, and fprettify in 2026

Atlas is a terminal-native AI coding agent for Fortran in 2026. It reads modules, explicit interfaces, and intent declarations, runs fpm test behind a prompt, and runs fprettify.

Diagnose a Hanging or Long-Running Fortran Command with Atlas in 2026

Fortran developers in 2026 can use Atlas to diagnose hanging or slow fpm builds and scripts. Identify if commands are blocked on input or genuinely slow, and get them unstuck efficiently.

Refactor a Legacy Fortran Module with Atlas in 2026

Modernize Fortran code in 2026. Use Atlas to refactor legacy modules, move COMMON blocks, and ensure no regressions with fpm test and fprettify.

Trace a runtime bug from a stack trace in Fortran with Atlas in 2026

Pinpoint Fortran runtime bugs from production stack traces using Atlas in 2026. Leverage fpm and gfortran diagnostics to quickly identify and fix issues without a debugger attached.

Run the Fortran Test Suite and Triage Failures with Atlas in 2026

In 2026, Fortran developers use Atlas to run fpm test suites, triage failures into distinct root causes, and track fixes. Streamline your Fortran debugging workflow.

Onboard to an Unfamiliar Fortran Codebase in 2026 with Atlas

Quickly build a working mental model of any Fortran repository using Atlas. Leverage semantic search, `fpm` package layouts, and `fprettify` formatting for rapid understanding and safe code exploration in 2026.

Locate where a behavior is implemented in Fortran with Atlas in 2026

Fortran developers in 2026 can use Atlas to pinpoint exact code implementations. Leverage semantic search, grep, and LSP tools to navigate modules, interfaces, and fpm projects, ensuring precise code discovery and

Browse this resource hub