Why shouldn't all function arguments be declared final?

Ok, so I understand why we should declare the argument final from this question , but I do not understand why we should not ...

Since Java always uses pass by value, this means that we cannot return a new value through this argument, we can only overwrite it and make the argument useless, because we are not using the passed value ...

Is the only advantage of non-final argument arguments in Java is that you don't need to create a local variable of type arguments?

PS This question was raised by the PMD rule MethodArgumentCouldBeFinal

+7
java oop pmd
source share
4 answers

I can only imagine 2 reasons not to make the final parameter:

  • to save the use of a local variable if you need to overwrite the parameter value in some cases (for example, set the default value if the parameter is null, etc.). However, I would not consider this a good practice in general.

  • to save 6 characters for each parameter, which improves readability.

Reason 2 is what makes me not write it most of the time. If you assume that people follow the practice of never assigning a new value to a parameter, you can consider all parameters as implicitly final . Of course, the compiler will not stop you from assigning a parameter, but I can live with it, given the readability coefficient.

+4
source share

This prevents unintentional error in the code. The rule of thumb here is to make every field and every function argument you know you shouldn't change (I mean the link, you can still change the value) in your code as final.

Thus, basically this means that the programmer is not shooting in the leg. Nothing more.

+1
source share

If you must declare the local variable final or not (the method parameter relates to this question), this is more a requirement than an agreement. You will not get a definite answer saying that you should always use final , or you should never use final . Because it is really a personal preference.

I personally mark final parameters or local variables when I really don't want their values ​​to change, and this even shows my intention to other developers not to overwrite the values. But I do not do this for each parameter. Also for some, using final seems like noise, as it really does increase the code base.

+1
source share

Rule of thumb: do not use it.

Final cannot stop you by changing objects, only its reference, and this is because objects in java, as a rule, are not amenable to processing.

Take a look at this code:

 class Example{ // think about this class as a simple wrapper, a facade or an adapter SomeClass inner = new SomeClass(); setInnet(Someclass inner){ this.inner = inner; } // delegate methods..... } 

Now, in the method:

 private void (final Example examp){ .... 

examp will always be the same object, but inner can change ... And inner is an important object here, one that does everything!

This might be an extreme example, and you might think that inner might be final, but if it's a utillery class, maybe it shouldn't. It is also easy to find a more common example:

 public void (final Map map){; .... //funny things .... //and then, in a line, someone does: map.clear() // no we have the same reference... but the object has change... .... 

So, my point in the Final argument in the arguments is that it does not guarantee that all the code inside the Final class is indestructible, and therefore the word Final can put an end to you and mascarading an error ...

Assuming Final in params, you can show only wishes, not facts, and for this you should use comments, not code. Moreover: it is standard (de facto) in java that all arguments are only input arguments (99% of the code). Thus, the word Final in the parameters is NOISE because it exists and does not mean anything.

And I don't like the noise. Therefore, I try to avoid this.

I use the word Final to mark internal variables that will be used in an anonymous inner class (you can not mark it if they are really final, but cleaner and more readable).

UPDATED

final means "assigned once", which when applied to the arguments of a method means nothing in the program logic or in its design.

You can assign arguments to a new object inside the method and there will be no changes outside it.

The only difference in the final argument will be that you cannot assign these objects to other objects. Assigning arguments is something that can be ugly and something to avoid, but its only style problem, and at this point the style problem should be the assignment itself, and not the activity of the "final" word.

I think the ending is useless in arguments (and so, noise), but if someone can use it, I will be glad to know it. What can I achieve in order to put on a β€œfinale” that cannot be achieved without setting it?

0
source share

All Articles