Forget this business of good practice. You have to do what’s right for every situation. Since I don’t know the context of this situation I can’t say what is ideal to do in it.
This whole code is weird. If it was a real code maybe you could say it better.
I particularly do not create variables that will only be used once unless it is too complex code and a separation helps readability, a variable serves to document the code in these cases.
There are people who love to create a lot of variable to document, create much more than one in this case. One thinks that making everything explicit is the most important thing for readability, spatially Java programmers. From Groovy they shouldn’t since it’s a more strenuous language for scripts. It’s a matter of culture.
I like to analyze each case and see what’s best and for me the best by default is short code, with few tokens, until it proves necessary to have more tokens to be more readable. My code would look like this:
private fazerAlgo(AlgumaCoisa algumaCoisa) {
Utilidades.converter(formatador.formatarParaTal(algumaCoisa.a, algumaCoisa.b, algumaCoisa.c))
}
I put in the Github for future reference.
Especially in Groovy. I’d love that one formatarParaTal()
accept the object and it takes the elements, but I do not know if it is possible in this "case".
So it’s taste, it’s choice of style. Preferably maintain consistency. Either avoid variables or always use variables until it is proven necessary to be different. Choosing one or the other randomly is not good. Maintaining consistency alone helps communicate the intent of the code.
If people think more about the intent of the code and seek simplicity first of all they will end up having more quality and consequently more productivity in the long run.
Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?
– Maniero
Read the book called: Clean Code (Clean Code).
– fsi