Naming Conventions in Programming
Naming conventions are agreements about how to format identifier names in code. Following conventions improves readability, reduces cognitive load, and makes code feel professional. This guide explains the major conventions and when to use them.
camelCase
camelCase is characterized by lowercase initial letters with uppercase letters at the start of each subsequent word: firstName, getUserById, calculateTotalPrice. The uppercase letters create "humps" in the middle of names, resembling a camel's back. Where it's used: JavaScript and TypeScript use camelCase for variables, functions, and object properties by convention. Java uses it for methods and variables. It's also common in JSON property names and some API response formats. Why it works: camelCase is compact (no separator characters), distinguishable from PascalCase (which signals classes/types), and easy to type on any keyboard. The capital letters provide clear word boundaries without adding characters. Common mistakes: Inconsistent capitalization (getUSERData vs getUserData), using for class names (should be PascalCase), or mixing with snake_case in the same codebase. Handling acronyms is debated. Some prefer HTTPServer, others HttpServer. For consistency, many style guides recommend treating acronyms as words when they're not at the start: httpServer, xmlParser, userId. When starting a name, they remain uppercase or use first-letter-only: HTMLElement or HtmlElement depending on the style guide. lowerCamelCase refers to the standard form where the first letter is lowercase. UpperCamelCase (first letter uppercase) is just another name for PascalCase.
PascalCase
PascalCase (also called UpperCamelCase) capitalizes the first letter of every word including the first: FirstName, UserAccount, HttpRequestHandler. Named after the Pascal programming language where this convention originated. Primary use: Class and type names in most object-oriented languages. When you see PascalCase in code, it typically signals "this is a type/class, not a variable or function." This visual distinction aids code comprehension. Where it's standard: TypeScript and JavaScript interfaces and type aliases. React component names (mandatory—lowercase would be interpreted as HTML elements). C# uses PascalCase for classes, methods, and properties. Java uses it for class names. Distinction from camelCase: The first character tells you what you're dealing with. userService (camelCase) is an instance or variable. UserService (PascalCase) is a class or type. This pattern works across languages and helps readers understand code structure at a glance. File naming often follows component naming: UserProfile.tsx contains the UserProfile component. This convention is especially common in React projects. Acronyms in PascalCase: Similar debate as camelCase. XMLHttpRequest vs XmlHttpRequest. Modern style guides often prefer treating acronyms as words: HttpClient, XmlParser, UserId. Fully capitalized acronyms can create ambiguity: is HTTPAPI saying "HTTP API" or "HTTPAPI"? HttpApi is clearer.
snake_case
snake_case connects words with underscores, using all lowercase letters: first_name, get_user_by_id, calculate_total_price. The underscores make words clearly distinct without relying on capitalization. Where it's standard: Python embraces snake_case for functions, variables, and modules (PEP 8 style guide). Ruby uses it throughout. PostgreSQL and many databases use snake_case for table and column names. C uses it for function and variable names in many codebases. Advantages: Maximum readability—underscores act as spaces between words. No case-sensitivity issues—everything is lowercase. Works in contexts where case might be lost or mangled. Easy to convert to other formats. UPPER_SNAKE_CASE (SCREAMING_SNAKE_CASE) is snake_case with all capitals: MAX_CONNECTIONS, DEFAULT_TIMEOUT, API_BASE_URL. Nearly universal for constants across all languages. The caps signal "this value should not be modified." When Python meets JavaScript: APIs often need conversion. Python backends might return snake_case (user_id), while JavaScript frontends prefer camelCase (userId). Many projects include conversion utilities or serializers that handle this translation automatically. Database considerations: snake_case is safest in SQL databases because it avoids case-sensitivity issues. Some databases are case-insensitive (MySQL on Windows), others case-sensitive (PostgreSQL). Snake_case works consistently everywhere.
kebab-case
kebab-case connects words with hyphens, using all lowercase letters: first-name, get-user-data, button-primary. Called "kebab" because words are "skewered" by hyphens like meat on a kebab stick. Primary use in CSS: kebab-case is the standard convention for CSS class names and custom properties: .user-profile, .button-primary, --main-color. The hyphen makes class names readable and is valid in CSS selectors. URL slugs: Web URLs use kebab-case for readability: /blog/how-to-write-clean-code. Hyphens are URL-safe, clearly separate words, and are readable. Underscores can be confused with spaces when URLs are underlined. Why not in most programming languages: The hyphen character is the subtraction operator. first-name would be interpreted as "first minus name." This makes kebab-case unusable for variable names in JavaScript, Python, Java, and most other languages. HTML data attributes: Custom data attributes often use kebab-case: data-user-id, data-is-active. When accessed in JavaScript, they're converted to camelCase: element.dataset.userId, element.dataset.isActive. Command-line arguments commonly use kebab-case: --output-dir, --dry-run, --no-cache. The convention makes long-form options readable and consistent. React class names: When using CSS Modules or similar, you'll write .button-primary in CSS but access it as styles['button-primary'] or styles.buttonPrimary in JavaScript, depending on your tooling. Many setups auto-convert kebab-case to camelCase for JavaScript access.
Prova verktyget
Textkonverterare