Convert Text to CONSTANT_CASE (SCREAMING_SNAKE_CASE)
Learn how to convert text to CONSTANT_CASE (SCREAMING_SNAKE_CASE) for defining constants, environment variables, and enum values. Understand its conventions across programming languages.
Detailed Explanation
Converting Text to CONSTANT_CASE
CONSTANT_CASE (also called SCREAMING_SNAKE_CASE or UPPER_SNAKE_CASE) uppercases every letter and separates words with underscores. It is the universally recognized convention for constants and configuration values across programming languages.
Basic Conversion
Input: max retry count
Output: MAX_RETRY_COUNT
Input: backgroundColor
Output: BACKGROUND_COLOR
Input: api-base-url
Output: API_BASE_URL
Where CONSTANT_CASE Is Used
Constants
// JavaScript / TypeScript
const MAX_RETRIES = 3;
const DEFAULT_TIMEOUT_MS = 5000;
const PI = 3.14159265359;
// Java
public static final int MAX_CONNECTIONS = 100;
public static final String DEFAULT_CHARSET = "UTF-8";
# Python
MAX_RETRIES = 3
DATABASE_URL = "postgresql://localhost/mydb"
Environment Variables
export DATABASE_URL="postgresql://localhost:5432/mydb"
export NODE_ENV="production"
export AWS_ACCESS_KEY_ID="AKIA..."
export NEXT_PUBLIC_API_URL="https://api.example.com"
Environment variables are universally written in CONSTANT_CASE across all operating systems and container platforms.
Enum Values
// TypeScript enum
enum HttpStatus {
OK = 200,
NOT_FOUND = 404,
INTERNAL_SERVER_ERROR = 500,
}
// Python enum
class Color(Enum):
DARK_RED = "#8B0000"
FOREST_GREEN = "#228B22"
ROYAL_BLUE = "#4169E1"
Conversion Algorithm
- Split the input into words (by spaces, hyphens, dots, or case transitions).
- Uppercase every word.
- Join with underscores (
_).
CONSTANT_CASE vs. snake_case
CONSTANT_CASE: MAX_RETRY_COUNT
snake_case: max_retry_count
Same structure, different casing. CONSTANT_CASE signals "this value should not be reassigned."
Handling Acronyms
Acronyms remain fully uppercased since the entire string is uppercase:
"http response code" → HTTP_RESPONSE_CODE
"xml parser" → XML_PARSER
"api base url" → API_BASE_URL
Edge Cases
- Single word:
"timeout"→"TIMEOUT". - Already CONSTANT_CASE: passes through unchanged.
- Numbers:
"http2MaxStreams"→"HTTP_2_MAX_STREAMS"or"HTTP2_MAX_STREAMS". - Leading/trailing delimiters: trimmed before conversion.
Use Case
CONSTANT_CASE is the universal convention for defining constants, environment variables, and enum values in virtually every programming language. It is essential when generating .env files, defining configuration constants, creating enum types, and setting up CI/CD pipeline variables. Converting from other cases to CONSTANT_CASE is common when promoting magic values to named constants during code refactoring.