Hook Methods

From Dabo Wiki
Jump to: navigation, search

This is a generic term for methods of the framework that are empty by default so that developers can add their code to them without fear of overriding any important framework code. They are placed at critical parts of framework processes in anticipation of developers needing to influence those processes at those points.

The most common example are beforeXX() and afterXX() methods. Let's say that there is an important method called "XX()". As a developer, when you want to modify XX for your own needs, the most common need is to execute code either right before or right after the method, so your code would look like:

class MySubClass(FrameworkClass):
    def XX(self):
        #do your pre-code...
        super(MySubClass, self).XX()
        #do your post-code...

One of the most common mistakes is to forget the super() call, which can introduce some extremely hard-to-find bugs into your code. So instead of following this pattern, Dabo adds hook methods that never need to call their superclass versions. Here's how the XX() method would be written in Dabo:

def XX(self):
    if self.beforeXX() is False:
        return
    # Main body of the XX method...
    self.afterXX()

def beforeXX(self):
    pass

def afterXX(self):
    pass

Now you are free to override one or the other hook methods, or neither, or both, without ever having to call super(). What's more, you also have the chance to abort the XX() method by returning False from the beforeXX() method.

Hook methods are used throughout Dabo; be sure to check the docs or ask on the lists before overriding framework methods.