Advertisement

CONSTANT_CASE Guide: Why Programmers Use All Caps with Underscores

If you have written code in almost any language, you have seen identifiers like MAX_RETRY_COUNT, DATABASE_URL, or HTTP_TIMEOUT_SECONDS. This is CONSTANT_CASE — also called SCREAMING_SNAKE_CASE, UPPER_SNAKE_CASE, or MACRO_CASE. It is the universal signal that a name represents a value that does not change at runtime. This guide explains where the convention comes from, where it is required, and how to apply it consistently.

Case Converter Transform any text or identifier into CONSTANT_CASE in one click.
Try Case Converter →

What Is CONSTANT_CASE?

CONSTANT_CASE writes identifiers in all uppercase letters with underscores separating words. It is essentially snake_case (lowercase letters with underscores) but capitalized. Examples: MAX_BUFFER_SIZE, API_BASE_URL, DEFAULT_LOCALE, USER_ROLE_ADMIN.

The convention dates back to C, where the preprocessor's #define directive is conventionally written in all caps to distinguish macro substitutions from real variables. From there it spread to nearly every C-influenced language: Java, C++, JavaScript, Python, Ruby, Go, Rust, and PHP all use CONSTANT_CASE for the same purpose.

Why All Caps?

The convention exists for one reason: visual contrast. When you read code top to bottom, all-caps tokens jump out as different from everything around them. That visual difference is a useful cognitive signal — it tells you immediately "this value is fixed; you cannot reassign it." A variable named maxRetries blends in with the rest of the code; a constant named MAX_RETRIES stands apart.

Why CONSTANT_CASE helps readability:

if (attempts > MAX_RETRIES) throw new Error('Too many attempts');

The reader instantly knows MAX_RETRIES is a fixed value defined elsewhere, while attempts is the runtime counter. No documentation needed.

Where to Use CONSTANT_CASE

Compile-Time Constants

Any value that is known at compile time and should never change at runtime: configuration limits, magic numbers, error codes, status strings, regex patterns. In Java these are static final fields; in C# they are const; in C++ they are constexpr; in Rust they are const.

Enum Members

Most enum conventions use CONSTANT_CASE for individual members. enum Color { RED, GREEN, BLUE } in Java, OrderStatus.PENDING in Python's Enum class, HttpMethod.GET in TypeScript enums. The capitalization signals that these are fixed, named constants rather than instance values.

Environment Variables

Environment variables in Unix-like systems are conventionally CONSTANT_CASE: HOME, PATH, NODE_ENV, DATABASE_URL, AWS_SECRET_ACCESS_KEY. This is technically a soft convention, but every tool, every Dockerfile, and every CI system follows it. Lowercase env vars work but look broken.

Preprocessor Macros (C/C++)

Anything defined with #define should be in CONSTANT_CASE so the reader knows it is a textual substitution, not a variable. #define MAX_PATH 260, #define DEBUG_LOG(x).

Cross-Language Examples

LanguageExampleNotes
Javapublic static final int MAX_SIZE = 100;Required by Java conventions
JavaScriptconst API_URL = 'https://api.example.com';Convention for true module constants
PythonMAX_RETRIES = 3PEP 8 standard for module-level constants
C/C++#define BUFFER_SIZE 1024Standard for macros
Goconst MaxSize = 100Go prefers PascalCase — exception to the rule
Rustconst MAX_SIZE: u32 = 100;Compiler warns if not CONSTANT_CASE
RubyMAX_SIZE = 100Capital first letter makes it a constant
Shellexport NODE_ENV=productionUniversal for env vars

Go is the one major exception. Idiomatic Go uses PascalCase for exported constants and camelCase for package-private ones, treating constants like any other identifier and using capitalization for export visibility instead.

Snake Case Converter Need the lowercase variant? Convert to snake_case instantly.
Try Snake Case Converter →

Rules for Writing Good CONSTANT_CASE Names

  1. Use full words, not abbreviations. MAXIMUM_BUFFER_SIZE reads better than MAX_BUF_SZ. The verbosity is fine because you only type the name once at the definition.
  2. Group related constants with a common prefix. HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND, HTTP_STATUS_INTERNAL_ERROR. This makes autocomplete more useful.
  3. Avoid putting units in values; put them in names. TIMEOUT_MS = 5000 is clearer than just TIMEOUT = 5000.
  4. Don't use CONSTANT_CASE for things that aren't constants. A function parameter or local variable in all caps is misleading even if you never reassign it.
  5. Spell numbers, don't abbreviate. TWO_HUNDRED_FIFTY_SIX is silly; BLOCK_SIZE_256 is clear.

Common Anti-Patterns

Anti-Pattern 1: SHOUTING in Logs and Comments

CONSTANT_CASE is for identifiers, not prose. Writing log messages in all caps reads as shouting. Use sentence case in log strings and reserve CONSTANT_CASE for variable names.

Anti-Pattern 2: Constants That Aren't Actually Constant

In JavaScript, const myArray = [] declares a constant binding, but the array itself can still be mutated. Naming it MY_ARRAY falsely suggests immutability. Reserve CONSTANT_CASE for primitive values or genuinely immutable structures.

Anti-Pattern 3: Mixing CONSTANT_CASE with camelCase Members

If your enum or class uses CONSTANT_CASE for some members and camelCase for others, the convention loses all signal value. Pick one and apply it across the entire type.

Frequently Asked Questions

Is CONSTANT_CASE the same as SCREAMING_SNAKE_CASE?

Yes — they are two names for the exact same convention. "SCREAMING_SNAKE_CASE" is the more colorful informal name; "CONSTANT_CASE" and "MACRO_CASE" are more technical synonyms.

Should I use CONSTANT_CASE for class-level constants?

Yes, in almost every language except Go. Java, C#, Python, JavaScript, Ruby, Rust, and PHP all expect CONSTANT_CASE for class-level constants and static finals.

Why are environment variables uppercase?

By convention dating to early Unix. The shell does not require it, but every tool, library, and documentation example assumes it. Lowercase env var names will work but will confuse every reader.

Advertisement