The Short Answer

Four spaces is the standard in most programming communities, particularly Python and languages derived from C-style formatting traditions. Two spaces is common in documentation, README files, and some JavaScript codebases. The "right" answer depends entirely on your project's existing conventions and the style guide you're following. If you're asking this because you started a new project and no one told you the rules, you're not alone. I inherited a codebase once where the frontend developer used two spaces, the backend developer used four, and one of the senior engineers was committed to tabs. Merging branches required a pre-commit hook that I wrote specifically to convert everything to four-space indents before allowing a push. That took me about three hours to set up properly, and it prevented maybe two dozen merge conflicts per week going forward. The real issue isn't the number itself—it's consistency across the entire codebase. Python is the outlier that forces your hand. PEP 8 mandates four spaces and makes it a language requirement because Python doesn't use braces to delimit blocks. Get the indent wrong and your code literally won't run. In JavaScript, TypeScript, Go, and most other compiled or interpreted languages, indentation is stylistic. The compiler ignores it. That freedom is what creates the mess.

I ran into a specific edge case last year while reviewing a pull request for a JSON schema validation library. The author had used two spaces throughout the configuration file and four spaces inside the handler functions. It was subtle—hardly noticeable unless you were looking at the raw diff. What made it worse was that the project's CI pipeline had a linter configured to enforce four spaces, but the configuration file was excluded from the linter rules. The tests passed. The code worked. But anyone reading the source after a fresh clone would see two different indent styles and have no idea which was intentional. I added the config directory to the linter scope and changed all instances to four spaces. The fix took twenty minutes and eliminated a class of confusion that would have persisted indefinitely.

What Different Communities Actually Use

Google's style guides across languages consistently recommend four spaces. Microsoft's Cand Java conventions also call for four. The Linux kernel project uses tabs, which is unusual but consistent with its history. Facebook's JavaScript style guide from several years ago specified two spaces, which influenced a lot of React codebases in the wild. Ruby on Rails defaults to two spaces in many templates. PHP projects tend toward four spaces but the ecosystem is fragmented. The pattern is clear: four spaces dominates systems programming and languages where readability under version control matters most. Two spaces survives in web development where screen real estate is at a premium and developers are accustomed to nesting deep with JSX or template syntax. Neither approach is technically wrong. The penalty comes when you switch between projects without adjusting your editor settings.

Get the Full Details

How Many Spaces Are In A Hanging Indent? – EJZV
How Many Spaces Are In A Hanging Indent? – EJZV

Configuring Your Editor Correctly

The most important thing you can do is configure your text editor or IDE to insert spaces instead of tabs and to apply the correct width automatically. VS Code handles this well through workspace settings and .editorconfig files. Open the Settings.json for your project and add "editor.tabSize" set to your chosen indent width and "editor.insertSpaces" set to true. Any developer who opens the project with VS Code will inherit those settings immediately without needing to know the convention by heart. For larger teams, an .editorconfig file at the repository root is the standard approach. It's a plain text file that any modern editor supports and it enforces indentation rules at the project level regardless of which IDE each developer uses. Here's what a minimal config looks like: *.py means all Python files use four spaces. Root tells the parser where the config begins. These rules cascade downward, so a subdirectory can override them if necessary without breaking the overall structure.

Common Pitfalls Beginners Miss

The biggest mistake I see is assuming that visual alignment in the editor equals correct indentation. Many editors show soft tabs as spaces even when you've configured them as actual tab characters. You look at the code, it appears uniformly spaced, and you assume the project is consistent. It might not be. Run a search for the tab character (chr(9) in most languages) across your codebase to verify. If you find any tabs in a spaces-only project, you've found your inconsistency. Fixing a large codebase this way usually takes between thirty minutes and two hours depending on size and how deeply tabs are embedded in the source. Another issue is copy-pasting code between projects with different conventions. Stack Overflow answers, tutorial snippets, and copied functions often carry their original indentation with them. If you paste four-space code into a two-space project, the linter will flag it, but the real damage is visual—your PR reviewers will see a block of code that appears misaligned even though the indentation is technically correct within its own context. The workaround is to re-indent the pasted block before committing. Most editors have a "reformat selection" command. In VS Code it's Shift+Alt+F. In JetBrains IDEs it's Ctrl+Alt+L. Learn the shortcut for your editor and use it every time you paste code from outside your project. Documentation files present a separate challenge. Markdown, AsciiDoc, and reStructuredText all handle indentation differently than source code. In Markdown, four leading spaces create a code block. Two leading spaces do nothing special. If you're writing a README and your examples appear as indented paragraphs instead of formatted code, your readers will interpret the documentation incorrectly. Always test your markdown files by rendering them in the actual viewer your project uses before pushing.

When Tabs Are Actually Preferred

Tabs aren't dead. They make sense when developers have different screen sizes and window configurations. A tab character renders as however wide the viewer's tab stop is set to, which means the code adjusts to the reader's environment. Four spaces look cramped on a narrow terminal and loose on a wide monitor. This is why the Linux kernel, Git itself, and some older Unix toolsstick with tabs—it's a deliberate choice to prioritize readability across varying display configurations. If your team works primarily in terminal environments with fixed-width fonts and varied terminal widths, tabs may genuinely be the better choice. But you need to be honest about the tradeoff: newcomers joining your project will need to configure their editors correctly, and any automated tooling that converts tabs to spaces or vice versa needs to be part of your build pipeline. I worked on a project where the build script converted tabs to four spaces before testing and converted them back before packaging. It added roughly forty seconds to the build time and introduced a bug once when the converter silently failed on a specially formatted multiline comment. The team kept tabs because they valued the flexibility, but the cost was real and ongoing.

Indenting In Word How To Replace All Tab Spaces In A Word Document
Indenting In Word How To Replace All Tab Spaces In A Word Document

What Happens When You Get It Wrong

Wrong indentation in Python raises an IndentationError at runtime and stops execution immediately. In statically typed languages like Go, the compiler won't complain about indent width but gofmt will reformat your code every time you save it if it deviates from the standard. That automatic reformatting can surprise developers who are used to their code staying exactly as typed. You'll see unsolicited whitespace changes in your diffs and might waste time investigating whether a reviewer intentionally modified formatting. In JavaScript projects without a linter, inconsistent indentation is nearly invisible until someone opens the file in an editor with a different tab width setting. What looked like a clean two-space indent to one person might render as misaligned to another, making code review more difficult than it needs to be. The problem compounds in monorepos where different services or packages may have been bootstrapped at different times with different defaults.

A Practical Decision Framework

Start a new project and check what the dominant convention is in your language's standard library and most popular frameworks. Python and Go ship with four-space formatting. JavaScript and TypeScript ecosystems lean toward two in many community projects. Match the majority and you'll spend less time fighting style disagreements than trying to convert an existing codebase. If your team is split, pick four spaces. It's the safer default because it prevents visual ambiguity at deeper nesting levels and it aligns with the largest number of established style guides. Two spaces works fine for shallow nesting but becomes hard to read once you're three or four levels deep, which happens frequently in React components, nested API handlers, and complex conditional logic. Document the decision in your CONTRIBUTING.md or README. Write one sentence stating the indent size and the editor configuration to use. This eliminates the question entirely for anyone who joins later and saves reviewers from having to mention it in every PR that gets opened.