• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code puzzling me  RSS feed

 
Jax Blunt
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm staring at a bit of code in the project I'm working on, and I've paraphrased it below:



Does this code make any sense? Is there any way in which it can? I'm really struggling.

Thanks for any help you can offer.

Jax
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds as if the shakeHand() method should be in the Hand superclass. If the person writing the code has access to all the source code, then surely they should make that change.

Perhaps the person writing the code only has access to change LeftHand and RightHand, not Hand. In that case, they might have to do something ugly like that.

However, perhaps they could instead invent an interface, say Shakeable, with the shakeHand() method in it. LeftHand and RightHand could both be declared Shakeable. This would allow the ugly instanceof test to be removed.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't say if shakeHand() exists in Hand, or if LeftHand and RightHand's shakeHand() methods even have identical signatures.

If both subclasses are overriding the same method in Hand, then I'd say the code in FemaleBody is just plain wrong; either someone doesn't understand polymorphism, or (charitably) maybe you're looking at a partially completed refactoring. In either case, there's no reason not to replace the whole thing with

hand.shakeHand(var);

If, on the other hand, the methods aren't actually related by inheritance, then you probably are looking at code that is evolving towards polymorphism, and just hasn't made it there yet.
 
Jax Blunt
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Due to numerous other spaghetti characteristics of the code I'm actually looking at, I'd mistakenly thought that the methods were related by inheritance. On further investigation (following your responses!) I've realised they are not.

The code is not really evolving though, as there are no other ppl about who quail at the sight of frequent instanceof tests in the same way I do.

Oh well, at least I know what I'm looking at now - thanks for your help.

Jax
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!