Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Surviving 'Illegal division by zero'

by dragonchild (Archbishop)
on Jun 23, 2004 at 14:36 UTC ( #369046=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Surviving 'Illegal division by zero'
in thread Surviving 'Illegal division by zero'

This will make your code easier to understand. And, this is the way to "directly re-open the method definitions". In fact, it's safer to do it this way than it is to redefine it for the whole program. That kind of "action-at-a-distance" is the source of more maintenance nightmares than anything else.

Another way to look at it is that you can now parse, format, and handle anything to do with numbers in one place. If you want to change how you deal with numeric values, simply change the class. There's your global change, but it's nicely encapsulated.

A last concept to leave you with - if someone were to take your code and wrap it in something else, which is the politer way to handle things?

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested


Comment on Re^3: Surviving 'Illegal division by zero'
Re^4: Surviving 'Illegal division by zero'
by ViceRaid (Chaplain) on Jun 23, 2004 at 22:17 UTC
    And, this is the way to "directly re-open the method definitions". In fact, it's safer to do it this way than it is to redefine it for the whole program. That kind of "action-at-a-distance" is the source of more maintenance nightmares than anything else.

    Totally agree. Before Abigail's post I hadn't really thought of it in OO (obj-oriented and op-overloading) terms. To put what you're saying in, uh, ahem, pseudocode, it's the difference between globally redefining a core method

    class Float alias :old_divide :/ def / (other) other == 0 ? nil : old_divide(other) end end

    and subclassing, which is all good

    class MyFloat < Float def MyFloat.new(from) from.to_f() end def / (other) other == 0 ? nil : super(other) end end MyFloat.new(4) / 0 # nil
    A last concept to leave you with - if someone were to take your code and wrap it in something else, which is the politer way to handle things?

    Sure, it's a library. I was wondering if there was something scoped lexically analogous to:

    use warnings; { no warnings qw/once/; $foo = 5 - $bar; } { no warnings qw/uninitialized/; $foo = 5 - $qux; }

    Thanks

Re^4: Surviving 'Illegal division by zero'
by melora (Scribe) on Jun 24, 2004 at 02:34 UTC
    I agree, especially from the viewpoint of maintainability. Better to handle such situations (there may be cases other than division by zero which need special attention) in one encapsulated place. The other choices are to dig through all the code, restructuring where needed to handle the exceptional case, or to try to capture the exception (which tells you when the problem has happened rather than letting you avoid it).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://369046]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-07-30 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (237 votes), past polls