• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there somthing called optional parameters...

 
Shreyas Reddy
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reviewing someone else's code recently and came across a method defined as below(Method names have been changed...)



Though the comments indicate the reason for having the two parameters, i really don't understand how they are being used. Is not the scope of the parameters limited to the someMethod() body? I haven't seen the anotherMethod() definition yet, but i believe it shouldn't matter.
Can someone help me understand this?
Thanks in advance
 
Stephen Huey
Ranch Hand
Posts: 618
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitives passed as parameters to a method will not be modified, but Objects will! Both you (the code block calling the method) and the method you pass an Object to contain a reference to the SAME Object. Try playing around with this simple program to gain a better understanding:



You should find that the primitive value doesn't change, but the Map's size does!

At first I thought you were asking about how they could make the parameters optional, and in case you don't know, you could just pass null arguments in place of the Object parameters (as long as that method was set up to handle null values when you didn't have anything to give it).
[ October 27, 2004: Message edited by: Stephen Huey ]
 
Shreyas Reddy
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. But i think i didn't make my question clear
someMethod() has two parameters which are not being used within the method body. My original post has complete method definition as i found it. I just changed the method names. The method comments say the parameters are optional, sometimes they could be null. But within the method body since the parameters are not being used, are not they redundant? Can we not safely remove the paramters as shown below




I hope i was clearer this time. If not I will try to explain again.
thanks
 
Scott Dunbar
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everytime I've seen this type of situation it means that changing whatever calls this method is a pain. You're correct - since the method parameters are unused in the method they do not have to be there. However, if the method is called from many other places how difficult would it be to change those too? If it is easy then by all means do it.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shreyas Reddy:
The method comments say the parameters are optional, sometimes they could be null. But within the method body since the parameters are not being used, are not they redundant? Can we not safely remove the paramters[?]
It's possible that this class extends another class or implements an interface that defines that method signature, and this class is overriding/implementing it. If that's the case, removing the parameters will change the signature, most likely with detrimental results.

If that method only exists in this class, I'd wonder what the author was thinking and perhaps suggest a better tag for the parameters: "ignored".
 
Saket Barve
Ranch Hand
Posts: 229
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the method signature has been designed with the likely future requirements?

On the face of it though it does seem redundant.

Regards,
Saket
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This happens often.

Typically, it's because you are implementing some interface with an implementation that doesn't need the parameters to determine the required behaviour. Other implementations might.

The same can be said for checked exceptions declared to be thrown on an interface method, though implementations should leave the declaration off if it is not used as a matter of form.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you can over load the method. You can write two methods one with parameters one without parameters.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic