Generally speaking, for C#, the default Camel case is widely used. Summarizing, in this pattern, names are composed by simply uniting them without any sort of separator, such as underscore (_). What helps differentiate the words is the FatoDeCadaUmaIniciarComMaiúscula. In addition, choosing the name of the element can contribute - and greatly - to the understanding of your code by the team (and by yourself, when returning after a few months for an eventual maintenance). Below some considerations that may facilitate when choosing the name of your identifiers:
Be clear!
First, choose a name that gives meaning to that element. Read the code again, as if it were a text in Portuguese and try to evaluate if the sentence you just read makes sense, that is, if it is coherent and does not induce the reader to any kind of ambiguity. If you don’t remember what a variable is for (x
is a bad name), is likely to have chosen the wrong name.
Abbreviations
Generally, acronyms and abbreviations tend to hinder the understanding of the code, unless they are widely used by everyone in the team and in the company. An increasingly disused pattern, the hungarian notation, provides for the use of abbreviations for naming controls and to this day many people use it to give names to database elements. So a text box could be called txtNome
. By opting for more eloquent language, the abbreviation would be replaced by textBoxNome
.
Ubiquitous language
Under the influence of Eric Evans, in his book Domain Driven Design, there is a tendency to assign names that express before business and then technology. For a developer this represents a challenge, because we express most of the time using an essentially technical language and full of own terms. Focusing on the business, the txtNome control would be called nomeTextBox
, for example. Ubiquitous language is considered the one that all involved speak, which is present in the everyday vocabulary of the organization and facilitates the understanding of a concept by all.
Upper and lower case
In the pattern Camelcase there is a common distinction for the first initial:
- for Classes, it is in capital letters:
SituacaoVenda
. For Constants and Enumerators also.
- for objects, controls and variables, in lower case:
notaFiscal
, contadorItemNotaFiscal
.
Size
The limit for the size of an identifier’s name is common sense only, there is no limitation imposed by the framework. Thus, very long names can take up too much screen space and disturb reading. It is also worth remembering that, for names of controls on the web (in the view itself), the issue of size can impact data traffic, since each character represents a byte
.
Special characters
In C#, it is common to use underscore (underline, underscore) to initialize private fields, also known as attributes. They are those elements declared in the class scope and therefore available for all methods of this class. Thus, a field to represent the client repository could be called _clienteRepositorio
.
Every language has its reserved words (key words). In C# it is possible to use arroba (@) if it is very necessary for a variable to be called class, for example (@class).
Productivity tools
Visual Studio itself suggests names for controls using these patterns. If you create a menu item with the Clients description, the IDE will suggest that the respective control name be clientsMenuItem, for example.
Another tool that implements these practices is Resharper from Jetbrains. This tool already suggests a much more comprehensive set of good practices, highlighting in the code the nomenclature that escapes the standard.
"Try to use a more fluent way to give understanding to what you want but don’t overdo it. Try to use a language that is appropriate to the domain being developed for that specific task." - I think this is paramount, I believe it is the most that can facilitate the understanding of the code.
– Vítor Neil Avelino
Can you tell me if it’s better to use
_
(underline) at the beginning of a private field? I get very confused, I see codes with it, others do not. VS also confuses me, when it creates the private fields and assigns the value from the constructor parameters, it does not use the_
, but when is it to convert to a complete property, I think that’s how it’s there, it uses =/– Vinícius Lima
@Viníciusfile you can use, but it is not the standard adopts, there is no advantage in it if you adopt the other standards. Who does it does not follow the standard, including Visual Studio. It has bad things in it and in the documentation.
– Maniero
Excellent answer!! It should be as accepted.
– SUR1C4T3