Variable & Function Name Length Guide — Programming Naming Conventions

In programming, naming is one of the most important factors affecting code readability. Names that are too short convey no meaning; names that are too long make code verbose. Choosing the right length requires judgment based on scope and complexity. This article covers naming length guidelines and language-specific conventions. Check your identifier lengths with Character Counter.

Variable Name Length by Scope

The appropriate length of a variable name is proportional to its scope. This principle is widely endorsed in Robert C. Martin's "Clean Code" and Google's style guides.

ScopeRecommended LengthExamplesRationale
Loop counter (1–3 lines)1–2 charsi, j, kUniversally understood convention
Lambda / short block (≤5 lines)3–8 charsitem, user, valContext makes type/role clear
Local variable in function8–15 charsuserName, totalPriceRole must be clear within the function
Class field / property10–20 charsmaxRetryCount, isAuthenticatedReferenced across the entire class
Global variable / constant15–25 charsMAX_CONNECTION_TIMEOUT, DEFAULT_PAGE_SIZEMust be unambiguous across the codebase

Function and Class Name Guidelines

Identifier TypeRecommended LengthNaming PrincipleExamples
Function name10–25 charsverb + object formatcalculateTotalPrice, sendEmailNotification
Class name10–25 charsNoun or noun phraseUserRepository, PaymentProcessor
Interface name10–25 charsAdjective or noun describing behaviorSerializable, EventListener
Constant name10–30 charsUPPER_SNAKE_CASE with specific meaningMAX_RETRY_COUNT, DEFAULT_TIMEOUT_MS
Boolean variable/function10–20 charsis/has/can/should prefixisValid, hasPermission, canExecute

Functions should always start with a verb. fetchData() is clearer than data(); validateInput() is better than validation().

Language-Specific Conventions

LanguageVariables/FunctionsClassesConstantsStyle Guide
JavacamelCasePascalCaseUPPER_SNAKE_CASEGoogle Java Style Guide
Pythonsnake_casePascalCaseUPPER_SNAKE_CASEPEP 8
JavaScriptcamelCasePascalCaseUPPER_SNAKE_CASEAirbnb Style Guide
GocamelCase / PascalCasePascalCasePascalCaseEffective Go
Rubysnake_casePascalCaseUPPER_SNAKE_CASERuby Style Guide
C#camelCase / PascalCasePascalCasePascalCaseMicrosoft C# Conventions

Naming Trivia

The Linux kernel coding style guide favors short variable names. Linus Torvalds himself has stated that loop variables should use i rather than loop_counter. In contrast, Google's Java style guide discourages single-character names outside of loop counters and lambda parameters. This contrast reflects different contexts: kernel code read by a small group of experts vs. large-scale web services read by many developers. Studies of open-source projects on GitHub suggest the average variable name length is approximately 8.5 characters.

Common Mistakes

Pro Naming Techniques

Conclusion

Appropriate name length scales with scope: 1–2 characters for loop counters, 8–15 for local variables, 15–25 for global constants. Avoid excessive abbreviation and Hungarian notation, and unify naming conventions across your team. Use Character Counter to check your identifier lengths.