I don’t know if this can be considered subjective or not, but there is evidence that the number of bugs in a program is proportional to the amount of lines of code in that program. Regardless of language. More expressive languages/libraries (i.e. that do a lot with little code) tend to produce shorter code, and therefore fewer bugs.
And what does this have to do with the question? Simple: if you pass optional parameters to make your code more concise, then do it! Don’t worry too much about the "purity" of the code: good practices are good not because "someone said they are", but because they solve/avoid a number of problems. But if you don’t see any problem in an approach, there’s no reason to avoid it.
I don’t like getters and setters. I don’t like private attributes. But depending on the type of project and team, they can be necessary things (e.g., numerous teams composed of inexperienced programmers, who are more likely to make mistakes). In this case, more important than following the X or Y standard, it is important to follow a pattern consistent.
For example, jQuery has several functions that function as properties: html
, text
, attr
, prop
, css
, data
... They all work the same way: without arguments, they are one getter; with one argument, are a Setter (additional arguments may exist at the beginning). The name is short, no need to keep typing the prefix (getText
vs. setText
)... And most importantly, the same convention is used throughout the library, so no surprise!
In Java, there is a very strict convention about the names of the methods and what they should do. Evading this convention is problematic, as it forces those unfamiliar with your code to learn/decorate a new convention. However, getAllNome
does not follow - to my knowledge - any pre-established convention, so that you can adopt yours. And even if it did, we need to weigh the pros and cons, as discussed earlier.
Finally, consider transferring this responsibility to another class in order to make its use more consistent. Java unfortunately has no first class functions (nor Extension methods), then it is necessary to do this through an auxiliary class. For example, if your function returns a List
, create the interface/class:
interface ListaFiltravel<T> extends List<T> {
public ListaFiltravel<T> filtrar(String variacao);
public ListaFiltravel<T> filtrar(String variacao, boolean inPlace);
}
class ArrayListFiltravel<T> extends ArrayList<T> implements ListaFiltravel<T> {
public ListaFiltravel<T> filtrar(String variacao) {
return filtrar(variacao, false);
}
public ListaFiltravel<T> filtrar(String variacao, boolean inPlace) {
// Código para filtrar
// Retorne a própria lista filtrada (inPlace == true) ou uma outra lista
}
}
And call your method as follows:
meuObjeto.getAllNome().filtrar("M")
In this way, responsibilities are very separate. (But again: does this produce shorter codes? If this is something you only have to do once, I remain in favor of leaving the purity aside and putting the functionality on itself getter. But if it can be repurposed in various parts of your system, then that approach is better.)
The question is very interesting. The answer would be very broad. C#, for example, does not allow argument in property, but this because the language is better built than Java, which allows because the
get()
in general is any method of a class. Therefore, correct is not. If some languages allow, the problem is the construction of these languages.– Leonel Sanches da Silva
Regardless of the language, I think one should ask: does my class need to provide these accessors? Is it a good design for my class that they are provided? The answers to this will depend on the situation.
– Piovezan
Related: Getters and Setters are mandatory or facilitators?
– Math