Good practice passing arguments to a method when they come from another function

Asked

Viewed 70 times

0

I came across a code similar to the following:

private fazerAlgo(AlgumaCoisa algumaCoisa) {

  Utilidades.converter(
    formatador.formatarParaTal(
      algumaCoisa.a,
      algumaCoisa.b,
      algumaCoisa.c
    )
  )

}

At first glance I thought the code was kind of messy and complicated like this. Is it good practice to call a method within an argument to another method in such a way? Or it is better to extract the content of the arguments to a variable and use it, for example like this:

private fazerAlgo(AlgumaCoisa algumaCoisa) {

  def minhaMensagem = formatador.formatarParaTal(
    algumaCoisa.a,
    algumaCoisa.b,
    algumaCoisa.c
  )

  Utilidades.converter(minhaMensagem)

}
  • 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?

  • Read the book called: Clean Code (Clean Code).

1 answer

3


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.

  • and if the line gets too long and hard to read?

  • There goes the taste of each one. I do not care, until pq I use line-wrap in the editor. If the person does not like this, breaks in one or another line

Browser other questions tagged

You are not signed in. Login or sign up in order to post.