Skip to content

Why Programmers Use Underscores in Variable Names

Last updated: March 16, 2026

This guide explains how underscores are used in programming for naming conventions like _private, __dunder, and snake_case. Read on to understand when and why developers use underscores in code.

What is this?

Why do programmers use underscores? Guide to _private, __dunder, snake_case & more with Python & JavaScript examples. Free — no signup.

Who needs it?

Developers, designers, content creators, and anyone who needs to quickly generate text, codes, or identifiers.

Bottom line

100% free, runs entirely in your browser — no signup, no data sent to any server.

How to Use the Underscore Conventions Tool

Underscore Conventions Features and Options

About the Free Online Underscore Conventions

Quick examples:

🛡️

Single Underscore Prefix

Protected/internal use variable. Convention for 'internal use only'.

PythonCommon in: Python, JavaScript (convention), Ruby
Example:
_private_variable
🔐

Double Underscore Prefix

Triggers name mangling in Python. Prevents accidental overriding.

PythonCommon in: Python
Example:
__private_mangled

Dunder Methods

Special/magic methods in Python. Called automatically by the language.

PythonCommon in: Python
Example:
__init__
⚠️

Trailing Underscore

Avoids naming conflicts with keywords or built-ins.

PythonCommon in: Python, C++, Rust
Example:
class_
🐍

snake_case Naming

Standard naming convention with underscores between words.

PythonCommon in: Python, Ruby, PHP, Rust
Example:
user_profile_picture
C

UPPER_SNAKE_CASE

Constants in uppercase with underscores between words.

MultipleCommon in: Python, JavaScript, Java, C++, Go
Example:
MAX_RETRY_COUNT

Python Underscore Conventions

`_variable`

protected/internal

`__private`

name mangling

`__init__`

dunder methods

`variable_`

avoid keywords

snake_case

standard

CONSTANT_NAME

constants

When to Use Each Underscore Type

🛡️

Single Underscore Prefix (`_variable`)

Use when: Variable/method is for internal use within module/class. Other developers should avoid using it directly. Python won't import it with `from module import *`.

🔐

Double Underscore Prefix (`__private`)

Use when: You want to prevent accidental overriding in subclasses (Python only). Not for true privacy — use `#private` in JavaScript or `private` keyword in Java.

Dunder Methods (`__init__`)

Use when: Implementing Python's special methods. Don't create your own dunder methods unless you're extending Python's behavior (like creating a new container type).

🐍

snake_case (`variable_name`)

Use when: Writing Python, Ruby, or Rust code. Follows language conventions. More readable than camelCase for long, descriptive names.

The 5 Types of Underscore Usage in Programming

1. Single underscore prefix (`_variable`) — Conventionally indicates 'protected' or 'internal use only' variables. In Python, names starting with `_` aren't imported by `from module import *`. It's a soft warning to other developers: "This is for internal implementation, don't touch it directly."

2. Double underscore prefix (`__private`) — Triggers Python's name mangling. The variable is renamed to `_ClassName__private` to prevent accidental overriding in subclasses. Not true privacy (can still be accessed with the mangled name), but prevents naming collisions in inheritance hierarchies.

3. Double underscore surround (`__init__`) — 'Dunder' (double underscore) methods in Python. These are special methods that Python calls automatically in specific contexts. `__init__` initializes objects, `__str__` provides string representation, `__len__` enables `len()` support, etc.

4. Trailing underscore (`class_`, `type_`) — Used to avoid naming conflicts with Python keywords or built-in functions. When you need a variable named 'class' (a keyword), use `class_` instead. Also used for avoiding shadowing: `list_` instead of `list` (which would shadow the built-in `list`).

5. Snake_case (`user_profile_picture`) — The standard naming convention in Python for variables, functions, and methods. Uses underscores between words for readability. Much clearer than camelCase for longer, descriptive names common in Python codebases.

Language-Specific Underscore Naming Conventions

Python
  • `_variable` — protected/internal
  • `__private` — name mangling
  • `__init__` — dunder methods
  • `variable_` — avoid keywords
  • snake_case — standard naming
JavaScript
  • `CONSTANT_NAME` — constants
  • `_private` — convention only
  • `#private` — true private (ES2022+)
  • camelCase — standard naming
  • No dunder methods
C++
  • `m_variable` — member variables
  • `_variable` — avoid (reserved)
  • `g_variable` — global variables
  • camelCase or snake_case
  • Leading underscore reserved
Java
  • `mVariable` — member variables
  • `CONSTANT_NAME` — constants
  • `_variable` — uncommon
  • camelCase — standard naming
  • `private` keyword for privacy
Ruby
  • `@variable` — instance variable
  • `@@variable` — class variable
  • `$variable` — global variable
  • `_variable` — unused param
  • snake_case — standard naming
PHP
  • `$_variable` — superglobals
  • `$variable_name` — snake_case
  • `CONSTANT_NAME` — constants
  • `__METHOD__` — magic constants
  • Mixed conventions historically

Real-World Examples of Underscore Usage

Python class with all underscore types:

class UserAccount:
    def __init__(self, username, class_):  # trailing underscore avoids 'class' keyword
        self.username = username
        self._protected_data = {}          # single underscore: internal use
        self.__private_key = hash(username) # double underscore: name mangling
        self.class_type = class_           # trailing underscore in variable name
    
    def __str__(self):                     # dunder method
        return f"User: {self.username}"
    
    def get_public_info(self):             # snake_case method name
        return {"username": self.username}

JavaScript with modern and legacy conventions:

// Modern JavaScript (ES2022+)
class UserAccount {
    #privateBalance = 0;          // True private field
    _legacyPrivate = 0;           // Convention-only private
    PUBLIC_CONSTANT = "USER";     // Constant in UPPER_SNAKE_CASE
    
    constructor(username) {
        this.username = username; // Public property (camelCase)
    }
    
    // Public method (camelCase)
    getPublicInfo() {
        return {
            username: this.username,
            constant: this.PUBLIC_CONSTANT
        };
    }
}

Best Practices for Using Underscores

Follow the language's dominant convention.Python uses snake_case and `_private`. JavaScript uses camelCase and `#private`. Ruby uses snake_case and `@instance_variable`. Don't mix conventions within a project.

Use underscores for clarity, not obfuscation.The goal is to communicate intent to other developers. `_internal_data` says "this is for internal use." `__mangled_name` says "don't override this in subclasses."

Be consistent within your codebase.If you start using `_prefix` for internal methods, use it everywhere. If you use `CONSTANTS` for configuration values, don't mix with `regularVariables`.

Understand the actual behavior.Python's `__private` causes name mangling. JavaScript's `#private` is truly private. C's `_variable` is reserved. Know what the underscores actually do in your language.

When in doubt, look at popular projects.Check how major open-source projects in your language use underscores. Django (Python), React (JavaScript), Rails (Ruby) — these establish community conventions.

Frequently Asked Questions About Underscore Conventions

What does a single underscore prefix mean (_variable)?

A single underscore prefix (like `_variable`) conventionally indicates a 'protected' or 'internal use' variable. It's a hint to other developers that this variable is intended for internal use within the module/class and shouldn't be accessed directly from outside. Python doesn't enforce this, but linters and IDEs treat it specially.

What are double underscore (dunder) methods (__init__)?

Double underscore methods (like `__init__`, `__str__`, `__len__`) are Python's 'magic methods' or 'dunder' (double underscore) methods. They define special behavior for classes, such as initialization, string representation, length, etc. These are called automatically by Python in specific contexts.

What is name mangling in Python?

When a variable starts with double underscores (like `__private`), Python performs 'name mangling' — it renames the variable to `_ClassName__private` internally. This prevents accidental overriding in subclasses but isn't true privacy (it's still accessible if you know the mangled name).

What does a trailing underscore mean (variable_)?

A trailing underscore (like `class_` or `type_`) is used when you need to use a Python keyword as a variable name. For example, `class` is a keyword, so you'd use `class_` instead. It's also used to avoid shadowing built-in names like `list_`, `dict_`, etc.

What is snake_case and when is it used?

snake_case uses underscores between words (like `user_profile_picture`). It's the standard naming convention in Python for variables, functions, and methods. Other languages like Ruby and PHP also use snake_case. It's preferred for readability, especially with longer names.

Do other languages use underscores differently?

Yes. JavaScript typically uses camelCase for variables/functions, but underscores appear in CONSTANTS (UPPER_SNAKE_CASE) and sometimes for private fields (though modern JS uses `#` for true privacy). C++ uses `m_` prefix for member variables. Each language community has its own conventions.

Are underscores required for private variables?

In most languages, no — underscores are conventions, not enforcement. Python's `_private` is just a convention. Java uses `private` keyword. JavaScript now has `#` for true private fields. The underscore tells humans 'this is internal,' while access modifiers tell the compiler.

When should I NOT use underscores?

Avoid underscores: 1) In JavaScript variable/function names (use camelCase), 2) In CSS class names (use kebab-case), 3) In URLs (use hyphens), 4) When it conflicts with team/style guide conventions. Always follow the dominant convention of the language/framework you're using.

More Free Text Tools

Learn about underscore conventions here, then use our other tools for text conversion, cleaning, and analysis.