[Chicago-talk] error reporting in overridden method

Jim Thomason thomasoniii at gmail.com
Sun Sep 19 16:35:25 CDT 2004

I can't believe I haven't had to do this yet. I probably just didn't
think of this as an issue at the time when I have.

Okay, I have a class that has a method. The method validates input and
returns errors if anything invalid is passed in. It does it via error
codes, but it's not important. Exceptions would function the same way
for purposes of this example.

Anyway, I subclassed it and overrode the method, providing different
functionality in terms of how the arguments are used and returned.
But, I realized that I still wanted to validate the arguments in the
same way that the superclass does. And that's my conundrum.

I can duplicate the validation in the subclass, but code duplication
is bad. Especially in this case, since I'd be concerned about the
error values getting out of sync. Since the super and sub objects are
designed to be drop in replacements for each other (love that
polymorphism!) this is potentially a huge problem, if code is looking
for an error that one reports one way and the other reports another

In this case, I realized that I was able to simply twiddle the
arguments slightly and hand them back up to the superclass method to
let it handle all validation. But I haven't come up with a clever
solution to do it in the general case where you can't hand off to the
super method. The closest thing to a general method I can think of is
to have another method in the super class specifically to do the
validation of the args passed into the original method, but that
really doesn't seem like a good solution to me (too many extra
validation methods need to run around then).

Anybody have any good ideas?


More information about the Chicago-talk mailing list