TL;DR
This is a topic whose personal opinion influences a lot, but there are some general good practices that can be extracted for nomenclature and standardization of methods.
Standard
Each language suggests or even limits the nomenclature of methods and functions.
Usually when dealing with objects the preference is to use the pattern Camel Case, that is, the initials of the words are capitalized:
comerCoxinha();
When we talk about functions in languages such as Python, PHP or Javascript, it is not uncommon to find the use of underline or underlined:
comer_kibe();
In this aspect, the important thing is to maintain pattern and consistency both in how the words that make up the name are linked as in relation to the verb, language and word order.
Grammar class and tense
Different grammatical classes such as verbs, nouns, prepositions and adjectives can be used to name functions and methods if used within the correct context.
Nouns and Adjectives
Nouns and adjectives are usually used to name variables, classes, and attributes, since these elements usually refer to something or someone.
Example:
class Carro {
}
class CarroColorido extends Carro {
String cor;
}
carrinhoVermelho = new CarroColorido("vermelho");
Note how CarroColorido
and carrinhoVermelho
are compositions of noun and adjective that help to understand what these elements refer to.
Verbs
There is no general consensus, but many books and implementations suggest the use of verbs in methods, since they denote stocks of an object.
However, in practice many end up using different tenses. For example, some understand that they should use infinitive tenses. In this case, the correct for the Calculadora
would be the method somar
.
Others prefer to use the imperative, as ordering the object to do something. In this case, the order for the Calculadora
would be some
.
When naming the method soma
:
- Or you are using the imperative incorrectly, which is not uncommon in Brazil;
- Or are you using the noun to refer to the action of
somar
, as saying to the object "make the sum".
A lot of confusion arises because methods named in English can have both the connotation of imperative and infinitive. For example, execute()
can be understood as much as to execute (perform, in infinitive) how much execute (perform, in the imperative).
Methods getters and setters
Attribute access methods are usually an exception and are named according to their attributes.
Most of the time you’ll find something like setNome(String)
and getNome()
to change and recover the value of an attribute, respectively nome
.
It is also not uncommon to find such methods without prefixes get
and set
, such as nome()
to recover and nome(String)
to change. Particularly I prefix this pattern.
Fluent interfaces
Several grammatical classes end up being welcome when we use the standard Fluent Interfaces.
The goal here is to make the code more readable and easy to use and not simply follow a pattern blindly.
An example of a possible database access API:
query = Consulta.select("nome").from("cliente")
.where(Criterio.maiorOuIgual("credito", 1000))
.and(Criterio.igual("cidade", "Foz do Iguaçu"));
Although I have mixed languages, nouns, prepositions, the above code can be easily understood. The parts in English are SQL language and the rest can be understood without reference to the documentation.
Putting it all together
As I said, there is no rule, but it is recommended to follow a standard. Even so, there are different patterns for different contexts that, coupled with different techniques, allow a coherent class model.
I believe that the question to be asked of each method created is:
From the point of view of client code (which calls the method), this name makes sense and makes clear the purpose of the method, including its side effects?
Related: Should I write my program in English or Portuguese?.
– utluiz
It is a broader and specific question for C# but it may help to see how to name things in general in the code: http://answall.com/q/31646/101
– Maniero
@bigown Actually the question had tags of C# and Java, and I removed it. I thought it would work better if it was generic, I don’t know if I did well or not.
– bfavaretto
Don’t Create Objects That End With -ER
– rray
From the answers, I think the question is broad. Although it has been closed as based on opinions, I think the problem is amplitude, especially given to responses of utluiz and bigown, which are objective, but at different angles of the same problem. The answer of utluiz seems to me very clear if focus on OOP, the bigown seems to me very comprehensive in more varied development contexts, but the important thing is that both answers show that you can have very objective answers. Only the question doesn’t help much in the way it is, so I agree with leaving it pending.
– Bacco