Potential benefits
Some of the potential benefits that can be obtained by adopting a naming convention include the following: * to provide additional information (i.e.,Challenges
The choice of naming conventions (and the extent to which they are enforced) is often a contentious issue, with partisans holding their viewpoint to be the best and others to be inferior. Moreover, even with known and well-defined naming conventions in place, some organizations may fail to consistently adhere to them, causing inconsistency and confusion. These challenges may be exacerbated if the naming convention rules are internally inconsistent, arbitrary, difficult to remember, or otherwise perceived as more burdensome than beneficial.Readability
Well-chosen identifiers make it significantly easier for developers and analysts to understand what the system is doing and how to fix or extend the source code to apply for new needs. For example, althoughCommon elements
The exact rules of a naming convention depend on the context in which they are employed. Nevertheless, there are several common elements that influence most if not all naming conventions in common use today.Length of identifiers
Fundamental elements of all naming conventions are the rules related to identifier length (i.e., the finite number of individual characters allowed in an identifier). Some rules dictate a fixed numerical bound, while others specify less precise heuristics or guidelines. Identifier length rules are routinely contested in practice, and subject to much debate academically. Some considerations: * shorter identifiers may be preferred as more expedient, because they are easier to type (although many IDEs and text-editors provide text-completion, which mitigates this) * extremely short identifiers (such as 'i' or 'j') are very difficult to uniquely distinguish using automated search and replace tools (although this is not an issue for regex-based tools) * longer identifiers may be preferred because short identifiers cannot encode enough information or appear too cryptic * longer identifiers may be disfavored because of visual clutter It is an open research issue whether some programmers prefer shorter identifiers because they are easier to type, or think up, than longer identifiers, or because in many situations a longer identifier simply clutters the visible code and provides no perceived additional benefit. Brevity in programming could be in part attributed to: * early linkers which required variable names to be restricted to 6 characters to save memory. A later "advance" allowed longer variable names to be used for human comprehensibility, but where only the first few characters were significant. In some versions ofLetter case and numerals
Some naming conventions limit whether letters may appear in uppercase or lowercase. Other conventions do not restrict letter case, but attach a well-defined interpretation based on letter case. Some naming conventions specify whether alphabetic, numeric, or alphanumeric characters may be used, and if so, in what sequence.Multiple-word identifiers
A common recommendation is "Use meaningful identifiers." A single word may not be as meaningful, or specific, as multiple words. Consequently, some naming conventions specify rules for the treatment of "compound" identifiers containing more than one word. As most programming languages do not allowmypackage
for Java package names, though legibility suffers for longer terms, so usually some form of separation is used.
Delimiter-separated words
One approach is to delimit separate words with a nonalphanumeric character. The two characters commonly used for this purpose are the hyphen ("-") and the underscore ("_"); e.g., the two-word name "two words
" would be represented as "two-words
" or "two_words
". The hyphen is used by nearly all programmers writing Letter case-separated words
Another approach is to indicate word boundaries using medial capitalization, called " camelCase", "PascalCase", and many other names, thus respectively rendering "two words
" as "twoWords
" or "TwoWords
". This convention is commonly used in XMLHttpRequest
) varies. Some dictate that they be lowercased (e.g. XmlHttpRequest
) to ease typing, readability and ease of segmentation, whereas others leave them uppercased (e.g. XMLHTTPRequest
) for accuracy.
Examples of multiple-word identifier formats
Metadata and hybrid conventions
Some naming conventions represent rules or requirements that go beyond the requirements of a specific project or problem domain, and instead reflect a greater overarching set of principles defined by the software architecture, underlying programming language or other kind of cross-project methodology.Hungarian notation
Perhaps the most well-known isPositional notation
A style used for very short (eight characters and less) could be: LCCIIL01, where LC would be the application (Letters of Credit), C for COBOL, IIL for the particular process subset, and the 01 a sequence number. This sort of convention is still in active use in mainframes dependent upon JCL and is also seen in the 8.3 (maximum eight characters with period separator followed by three character file type) MS-DOS style.Composite word scheme (OF Language)
IBM's "OF Language" was documented in an IMS ( Information Management System) manual. It detailed the PRIME-MODIFIER-CLASS word scheme, which consisted of names like "CUST-ACT-NO" to indicate "customer account number". PRIME words were meant to indicate major "entities" of interest to a system. MODIFIER words were used for additional refinement, qualification and readability. CLASS words ideally would be a very short list of data types relevant to a particular application. Common CLASS words might be: NO (number), ID (identifier), TXT (text), AMT (amount), QTY (quantity), FL (flag), CD (code), W (work) and so forth. In practice, the available CLASS words would be a list of less than two dozen terms. CLASS words, typically positioned on the right (suffix), served much the same purpose asLanguage-specific conventions
ActionScript
Adobe's Coding Conventions and Best Practices suggests naming standards for ActionScript that are mostly consistent with those of ECMAScript. The style of identifiers is similar to that of Java.Ada
In Ada, the only recommended style of identifiers isMixed_Case_With_Underscores
.
APL
In APL dialects, the delta (Δ) is used between words, e.g. PERFΔSQUARE (no lowercase traditionally existed in older APL versions). If the name used underscored letters, then the delta underbar (⍙) would be used instead.C and C++
In C and C++, keywords andisalnum
for a function testing whether a character is alphanumeric), while the C++ standard library often uses an underscore as a word separator (e.g. out_of_range
). Identifiers representing macros are, by convention, written using only uppercase letters and underscores (this is related to the convention in many programming languages of using all-upper-case identifiers for constants). Names containing double underscore or beginning with an underscore and a capital letter are reserved for implementation ( compiler, __reserved
or _Reserved
). This is superficially similar to stropping, but the semantics differ: the underscores are part of the value of the identifier, rather than being quoting characters (as is stropping): the value of __foo
is __foo
(which is reserved), not foo
(but in a different namespace).
C#
C# naming conventions generally follow the guidelines published by Microsoft for all .NET languages (see the .NET section, below), but no conventions are enforced by the C# compiler. The Microsoft guidelines recommend the exclusive use of only PascalCase
and camelCase
, with the latter used only for method parameter names and method-local variable names (including method-local const
values). A special exception to PascalCase is made for two-letter acronyms that begin an identifier; in these cases, both letters are capitalized (for example, IOStream
); this is not the case for longer acronyms (for example, XmlStream
). The guidelines further recommend that the name given to an interface
be PascalCase
preceded by the capital letter I
, as in IEnumerable
.
The Microsoft guidelines for naming fields are specific to static
, public
, and protected
fields; fields that are not static
and that have other accessibility levels (such as internal
and private
) are explicitly not covered by the guidelines. The most common practice is to use PascalCase
for the names of all fields, except for those which are private
(and neither const
nor static
), which are given names that use camelCase
preceded by a single underscore; for example, _totalCount
.
Any identifier name may be prefixed by the commercial-at symbol (@
), without any change in meaning. That is, both factor
and @factor
refer to the same object. By convention, this prefix is only used in cases when the identifier would otherwise be either a reserved keyword (such as for
and while
), which may not be used as an identifier without the prefix, or a contextual keyword (such as from
and where
), in which cases the prefix is not strictly required (at least not at its declaration; for example, although the declaration dynamic dynamic;
is valid, this would typically be seen as dynamic @dynamic;
to indicate to the reader immediately that the latter is a variable name).
Go
In Go, the convention is to useMixedCaps
or mixedCaps
rather than underscores to write multiword names. When referring to structs or functions, the first letter specifies the visibility for external packages. Making the first letter uppercase exports that piece of code, while lowercase makes it only usable within the current scope.
Java
In Java, naming conventions for identifiers have been established and suggested by various Java communities such as Sun Microsystems,"Code Conventions for the Java Programming Language"widget.expand()
and Widget.expand()
imply significantly different behaviours: widget.expand()
implies an invocation to method expand()
in an instance named widget
, whereas Widget.expand()
implies an invocation to static method expand()
in class Widget
.
One widely used Java coding style dictates that UpperCamelCase
Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. The format indicates the separation of words with a single c ...
be used for classes and lowerCamelCase
be used for instances and methods.
Recognising this usage, some IDEs, such as NullPointerException
).
Initialisms of three or more letters are CamelCase instead of uppercase (e.g., parseDbmXmlFromIPAddress
instead of parseDBMXMLFromIPAddress
). One may also set the boundary at two or more letters (e.g. parseDbmXmlFromIpAddress
).
JavaScript
The built-in JavaScript libraries use the same naming conventions as Java. Data types and constructor functions use upper camel case (RegExp
, TypeError
, XMLHttpRequest
, DOMObject
) and methods use lower camel case (getElementById
, getElementsByTagNameNS
, createCDATASection
). In order to be consistent most JavaScript developers follow these conventions.
See alsoLisp
Common practice in mostwith-open-file
and make-hash-table
. Dynamic variable names conventionally start and end with asterisks: *map-walls*
. Constants names are marked by plus signs: +map-size+
.
.NET
Microsoft .NET recommendsUpperCamelCase
Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. The format indicates the separation of words with a single c ...
, also known as ''PascalCase'', for most identifiers. ( lowerCamelCase
is recommended for parameters and variables) and is a shared convention for the .NET languages. Microsoft further recommends that no type prefix hints (also known as LoginButton
instead of BtnLogin
.
Objective-C
Objective-C has a common coding style that has its roots inNSString
, UIAppDelegate
, NSApp
or CGRectMake
. Constants may optionally be prefixed with a lowercase letter "k" like kCFBooleanTrue
.
Instance variables of an object use lowerCamelCase prefixed with an underscore, like _delegate
and _tableView
.
Method names use multiple lowerCamelCase parts separated by colons that delimit arguments, like: application:didFinishLaunchingWithOptions:
, stringWithFormat:
and isRunning
.
Pascal, Modula-2 and Oberon
Wirthian languages Pascal, Modula-2 and Oberon generally useCapitalized
or UpperCamelCase
identifiers for programs, modules, constants, types and procedures, and lowercase
or lowerCamelCase
identifiers for math constants, variables, formal parameters and functions. While some dialects support underscore and dollar signs in identifiers, snake case and macro case is more likely confined to use within foreign API interfaces.
Perl
Perl takes some cues from its C heritage for conventions. Locally scoped variables and subroutine names are lowercase with infix underscores. Subroutines and variables meant to be treated as private are prefixed with an underscore. Package variables are title cased. Declared constants are all caps. Package names are camel case excepting pragmata—e.g.,strict
and mro
—which are lowercase.
PHP
PHP recommendations are contained in PSR-1 ( PHP Standard Recommendation 1) and PSR-12. According to PSR-1, class names should be in PascalCase, class constants should be in MACRO_CASE, and function and method names should be in camelCase.Python and Ruby
Python and Ruby both recommendUpperCamelCase
for class names, CAPITALIZED_WITH_UNDERSCORES
for constants, and snake_case
for other names.
In Python, if a name is intended to be " private", it is prefixed by one or two underscores (in Python it's more or less a hack). Private variables are enforced in Python only by convention. Names can also be suffixed with an underscore to prevent conflict with Python keywords. Prefixing with double underscores changes behaviour in classes with regard to name mangling. Prefixing ''and'' suffixing with double underscores - the so-called "dunder" ("double under") methods in Python - are reserved for "magic names" which fulfill special behaviour in Python objects.Style Guide for Python Code PEP8R
While there is no official style guide for R, the tidyverse style guide from R-guru Hadley Wickham sets the standard for most users.Style Guide for RCodeRaku
Raku follows more or less the same conventions as Perl, except that it allows an infix hyphen-
or an apostrophe '
(or single quote) within an identifier (but not two in a row), provided that it is followed by an alphabetic character. Raku programmers thus often use ''kebab case'' in their identifiers; for example,
fish-food
and don't-do-that
are valid identifiers.
Rust
Rust recommendsUpperCamelCase
for type aliases and struct, trait, enum, and enum variant names, SCREAMING_SNAKE_CASE
for constants or statics and snake_case
for variable, function and struct member names.
Swift
Swift has shifted its naming conventions with each individual release. However a major update with Swift 3.0 stabilised the naming conventions forlowerCamelCase
across variables and function declarations. Constants are usually defined by enum types or constant parameters that are also written this way. Class and other object type declarations are UpperCamelCase
.
As of Swift 3.0 there have been made clear naming guidelines for the language in an effort to standardise the API naming and declaration conventions across all third party APIs.
See also
* :Naming conventions *References
{{ReflistExternal links