This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A method argument that is never assigned can be declared final ?

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running a PMD analyzer on my code. It has recommended me some good practices.

This is the recommendation given by it:

A method argument that is never assigned can be declared final.
Example:


Even the above recommendation is given by Eclipse 3.4. I enabled some code checks which are disabled by default.

But my code is:
I am assigning the method argument to the instance variable. So why it is offering me this recommendation.
Any Idea guys ?


 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:But my code is:
I am assigning the method argument to the instance variable. So why it is offering me this recommendation.
Any Idea guys ?




Well, this is independent of what they are recommending. The method parameter dbRecordKey never finds itself on the LEFT side of an assignment. Which means you can make it final - which prevents accidental assignment and sometimes can help the Run Time run things more efficiently.

The fact that the parameter is on the RIGHT side of the equal sign has no affect. You can assign the value of a final variable to another variable if you want, no problems, it doesn't change the value of the variable in anyway.
 
Marc Maréchal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your argument is not modified in the body of the function, declaring it final enables developper reading your code that they don't have to check if the argument is modified or not in the body of the function.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i am using the getter and setter methods. Making the method argument final is advised.

Will it not cause any other side effect. Because mostly i never see anywhere this type of advice
being implemented.

So here making the argument final means, nobody can change its value. Only it can be assigned to a new variable. Then modified.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, no other side affects (as in unexpected behavior). Another affect it has, though, is you can directly use the method parameter inside anonymous inner classes you generate in the method. For example:


Because it has some benefits, and no real disadvantages, I tend to use it often.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you. Then why it is not seen anywhere use of this practice. Why it is mostly ignored ?

Even in many books example, like making beans. This is being ignored. Do you know any API implementation where it is being used ?

So that i can follow that. Except the example given by you. Means API example similar to using the beans or my example.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:I agree with you. Then why it is not seen anywhere use of this practice. Why it is mostly ignored ?

Because it requires 6 extra characters per parameter I have to type in myself

Honestly, the only reason I hardly ever use final parameters is laziness. I only make them final when I need it for inner classes.
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And plus, its for something that doesn't get you much.... setters are generally one line long. It only takes a split-second to notice that the param isn't changed in the method. And that is assuming that you even care. Laziness wins out here.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah-for setters there's hardly any point, except to make code quality tools be quiet.

For larger methods it can aid program correctness by avoiding unwanted assignments. I used to be a proponent of making method parameters final, but I eventually just turned the rule off because my methods are usually so short that it doesn't really aid in making my intent more clear, and I never assign to parameters anyway.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All your replies give me the answer why it is being ignored.

Do any java API use this practice ? (if the answer is in top of your head)
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not many, as far as I've seen so far. Guess Sun's employees are just as lazy as we are
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. Your responses are always crystal clear to me.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:All your replies give me the answer why it is being ignored. Do any java API use this practice ? (if the answer is in top of your head)


For a while, WebSphere Application Developer (now called Rational Developer) used final parameters in generated setters. I'm not sure why they stopped -- maybe it caused confusion, since you don't see it very much.

I think it's a good practice, since it would avoid the mistake illustrated below:
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Max Rahder wrote:
Amandeep Singh wrote:All your replies give me the answer why it is being ignored. Do any java API use this practice ? (if the answer is in top of your head)


For a while, WebSphere Application Developer (now called Rational Developer) used final parameters in generated setters. I'm not sure why they stopped -- maybe it caused confusion, since you don't see it very much.

I think it's a good practice, since it would avoid the mistake illustrated below:



Max, i am confused with your reply. Which good practice you are referring to, whether to use the final or not as per my question ?
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:Max, i am confused with your reply. Which good practice you are referring to, whether to use the final or not as per my question ?


The last one, with the parameter being declared as final.

I used to follow that practice, but I stopped, partly out of laziness, and partly out of not wanting to explain what I was doing over and over again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic