-3

I have two files with same name in different packages, in certain function i need to check the argument passed is of the instanceof which class it is.

Eg.

if(input instanceof x.y.test) {
      //do something
    } else if(input instanceof x.y.z test) {
      //do something
    }

But in many places I read that using instanceof is not a good practice. What kinda of alternate I can apply here.

Note: One file is of .groovy and other one is .java file, but both representing the same domain

  • 1
    What does `instanceof` has to do with files with identical names? –  May 03 '15 at 15:03
  • 1
    One file is of .groovy and other one is .java file, but both representing the same domain – Suganthan Madhavan Pillai May 03 '15 at 15:05
  • @Tichodroma I have the same question but giving the OP the benefit of the doubt, it is possible that the code deals with a scenario where multiple libraries have the same class name and a decision must be made based on what type of object was created. +1 from me because the question is pretty clear. – Chetan Kinger May 03 '15 at 15:05
  • @Suganthan Please [edit] your question and explain this important point. –  May 03 '15 at 15:06
  • @Tichodroma This was pretty clear to me. Not sure why the question needs editing. – Chetan Kinger May 03 '15 at 15:06
  • I think you might be mixing packages and folders. – PM 77-1 May 03 '15 at 15:06
  • @Suganthan Good that you did clarify that: instaceof checks objects for their class! You can't use it at all to differentiate between a groovy text file; and another text file ending with .java. For that, you can use the standard java file classes; where you would make sure to compare absolute file names. – GhostCat May 03 '15 at 15:07
  • @Jägermeister, comparing file name wouldn't be a standard solution right, still we have to use reflection which is much costlier – Suganthan Madhavan Pillai May 03 '15 at 15:11
  • @PM77-1, thanks i did the edit – Suganthan Madhavan Pillai May 03 '15 at 15:11
  • 1
    @Suganthan The question: do you want to compare **files**; or objects, which were created using `new`? You only need instanceof for the later case. And yes, then using "instanceof" (simply using the absolute class name, including the complete path) is to be preferred over reflection. – GhostCat May 03 '15 at 16:09

1 Answers1

1

It's a bit general claim, to say "instanceof is a bad practice". It depends on the requirements, technical limitations, personal taste etc.

However, it is a point worth considering. Sometimes it means someone missed an opportunity to use polymorphism. For example, if your code says

if (input instanceof x.y.test){
    greeting = "hello from xy";
}
else if (input instanceof x.y.z.test){
   greeting = "hello from xyz";
}

Then it might be nicer if each 'test' had a method 'greet()':

greeting = input.greet();
// where class x.y.test  has public String greet(){return "hello from xy";}
// and class x.y.z.test  has public String greet(){return "hello from xyz";}

I say might, because it has a strong advantage: people can now add lots of other 'test' implementations, and your main code will accept them seamlessly without having to add 'if-else'.
However, it might also have disadvantages: if you feel it's wrong for your 'test' class to be responsible for the greeting calculation; or if it's a 3rd party outside your control. That's why I'm against generalizations.

There are more elaborate solutions - e.g. lookup the 'visitor' design pattern.

Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11