Recipe 10.5. Fixing Bugs in Someone Else's ClassProblemYou're using a class that's got a bug in one of its methods. You know where the bug is and how SolutionsExtend the class from within your program and overwrite the buggy method with an implementation that fixes the bug. Create an alias for the buggy version of the method, so you can still access it if necessary. Suppose you're trying to use the buggy method in the Multiplier class defined below:
Reopen the class, alias the buggy method to another name, then redefine it with a correct implementation:
DiscussionIn many programming languages a class, function, or method can't be modified after its initial definition. In other languages, this behavior is possible but not encouraged. For Ruby programmers, the ability to reprogram Since Ruby is (at least right now) a purely interpreted language, you should be able to find the source code of any Ruby class used by your program. If a method in one of those classes has a bug, you should be able to copy and paste the original Ruby implementation into your code and fix the bug in the new copy.[1] This is not an elegant technique, but it's often better than distributing a slightly modified version of the entire class or library (that is, copying and pasting a whole file).
When you fix the buggy behavior, you should also send your fix to the maintainer of the software that contains the bug. The sooner you can get the fix out of your code, the better. If the software package is abandoned, you should at least post the fix online so others can find it. If a method isn't buggy, but simply doesn't do what you'd like it to do, add a new method to the class (or create a subclass) instead of redefining the old one. Methods you don't know about may use the behavior of the method as it is. Of course, there could be methods that rely on the buggy behavior of a buggy method, but that's less likely. See Also
|
Tuesday, November 3, 2009
Recipe 10.5. Fixing Bugs in Someone Else's Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment