• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting null when calling method that is not overloaded  RSS feed

 
Jash Shah
Greenhorn
Posts: 1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did code review today and found that in various places the call to the method passes a null that is type casted .

Example


The doSomthing method is not overloaded so there is no ambiguity. When I ask the reason for type casting to team they mention this is good practice and is recommended. I am not sure if other then adding some readability to code this is doing anything special and is really needed.

Does this help with enhancing any performance? To me this is compile time feature only.

Am I missing anything? Is this a good practice?

Another thing I observed was having a inner Factory class in a Interface .

Example



Is this a good practice form design point of view? The argument is that there are many interfaces and if we create external factory or each interface this will result in too many java files and hard to navigate. Having a factory with in an Interface is un-usual to me, I would only create inner class if the parent class needed some specific functionality that can be encapsulated in inner class.
 
J Steven Perry
Greenhorn
Posts: 13
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really have two questions.

First question: In my opinion there is no reason to explicitly typecast a null to match the argument type to a method that is not overloaded. If you ever create an overloaded method down the road, the compiler will detect the ambiguity and force you to resolve it then. For example, if I code this up:



I get a compiler error: "The method method(String, String) is ambiguous for the type CodeRanch20131211a". But if there is just one method, it is not ambiguous, and therefore unnecessary. However, this is a philosophical argument that neither you or I will ever win. It boils down to opinion. Why is it better style to add an unnecessary cast? Beats me.

In regards to your second question, defining a factory within an interface defeats the purpose of the interface (i.e., multiple, varied implementations). Declaring the factory in this manner requires you to provide an implementation inside of the Factory class (I've taken the liberty of cleaning up the code you posted, which does not compile and is incomplete):



While this code works, it is definitely the strangest marriage of a Java interface and the factory pattern I've ever seen.

Good luck.

--jsp
 
Yasser Shadid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First Question - For explicitly typecast to null, I think there is no reason to do this in your case because you are introducing code complexity.
Second Question - I don't think its valid, because you are putting implementation inside the interface.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Yasser Shahid.

I think there may be confusion that the method could have been overloaded like that in the first place.
 
Yasser Shadid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie
Ok, but i think it will make it complex because you are thinking about cases maybe will not happen in the future
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!