471,337 Members | 1,242 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Pattern for error checking easiest-first?

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?

Aug 20 '07 #1
1 1074
On 20 ago, 18:01, jquinn+goo...@cs.oberlin.edu wrote:
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.

--
Gabriel Genellina

Aug 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Nicolla MacPherson | last post: by
1 post views Thread by Einar Høst | last post: by
4 posts views Thread by FluffyCat | last post: by
3 posts views Thread by devikaapk | last post: by
5 posts views Thread by Alan Isaac | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.