Pattern for error checking easiest-first?

J

jquinn+google

Heres the situation:

class AbstractThing():
def changeMe(self,blah):
if blah < 1:
raise MyException
self.blah = blah

class NetworkedThing(AbstractThing):
def changeMe(self,blah):
if blah > self.getUpperLimitOverTheNetworkSlowly:
raise MyOtherException
AbstractThing.changeMe(self,blah)


The problem is that code like this does error checking backwards. A
call to NetworkedThing.changeMe will first do a slow error check and
then a fast one. Obviously there are various ways to get around this -
either have the subclass explicitly ask the superclass to error check
first, or vice totally versa. Is there some accepted pattern/idiom for
handling this issue?
 
G

Gabriel Genellina

The problem is that code like this does error checking backwards. A
call to NetworkedThing.changeMe will first do a slow error check and
then a fast one. Obviously there are various ways to get around this -
either have the subclass explicitly ask the superclass to error check
first, or vice totally versa. Is there some accepted pattern/idiom for
handling this issue?

What about this:

class AbstractThing():
def changeMe(self,blah):
self.verify_blah(blah)
self.blah = blah

def verify_blah(self, blah):
if blah < 1:
raise MyException

class NetworkedThing(AbstractThing):
def verify_blah(self, blah):
AbstractThing.verify_blah(blah)
if blah > self.getUpperLimitOverTheNetworkSlowly:
raise MyOtherException

That is, it's the verify step that is overriden/enhanced, not the
changeMe method that stays the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top