Align Variable Declarations in Source Code

Align variable declarations, assignments, and type annotations in source code for improved readability.

Code Formatting

Detailed Explanation

Aligning Code Declarations

Many style guides recommend aligning related variable declarations, struct fields, or constant definitions so that the types, names, and values form neat columns. While some code formatters handle this automatically, many do not — especially for ad-hoc alignment of related lines.

Before (Go struct)

Name string
Age int
Email string
IsActive bool
CreatedAt time.Time

After

Name      string
Age       int
Email     string
IsActive  bool
CreatedAt time.Time

Before (JavaScript constants)

const API_URL = "https://api.example.com";
const TIMEOUT = 5000;
const MAX_RETRIES = 3;
const DEBUG = false;

After (aligned on =)

const API_URL     = "https://api.example.com";
const TIMEOUT     = 5000;
const MAX_RETRIES = 3;
const DEBUG       = false;

Technique

  1. For type-annotated languages (Go, Rust, C), use Spaces (2+) as the delimiter to split on the gap between the name and the type.
  2. For assignment alignment (JavaScript, Python), use Equals = as the delimiter.
  3. Keep the output delimiter as Same as Input so the code remains syntactically valid.
  4. Left-align both columns.

When to Align Code

Code alignment is a stylistic choice. It improves scanability for groups of related declarations but can create noisy diffs when a new, longer name is added (because every line's padding changes). Use alignment for stable, rarely-changing blocks like constant definitions, struct fields, and configuration maps.

Use Case

A Go developer wants to align struct field names and types in a large struct definition before committing the code.

Try It — Text Column Aligner

Open full tool