HomepageTeaching PageResearch PageResources Page

Homework: Generalization

Sometimes you have a function that could be more reusable if it took more arguments. Adding parameters makes it usable in more general cases, so we call this generalization. This is also a good code improvement tactic to use if you have two functions that are very similar. For example, starting with the code from partway-through last time, what if we also had a make_and_eat_cheese_sandwich() function:

def make_and_eat_tomato_sandwich():
    sandwich = make_sandwich(tomato)
    eat(sandwich)

...

def make_and_eat_cheese_sandwich():
    sandwich = make_sandwich(cheese)
    eat(sandwich)
        

We can combine these into just one function and make it more useful by generalizing!

def make_and_eat_sandwich(ingredient):
    sandwich = make_sandwich(ingredient)
    eat(sandwich)
    
...
        
#replacing this with make_and_eat_sandwich()
def make_and_eat_tomato_sandwich():
    sandwich = make_sandwich(tomato)
    eat(sandwich)

...

#replacing this with make_and_eat_sandwich()
def make_and_eat_cheese_sandwich():
    sandwich = make_sandwich(cheese)
    eat(sandwich)
        

Then, once you're all set, you can remove the other two functions. (Optionally, if you want to keep the old version, you can just have it call the new, generalized function.)

def make_and_eat_sandwich(ingredient):
    sandwich = make_sandwich(ingredient)
    eat(sandwich)
        

The issue with this is that you have to go back through and fix each of the function calls to use the new name and include the ingredient argument. In complex code editors (development environments) there are tools that help you change all the function calls at once.

Just as with encapsulation, generalization helps by making your function more useful. This means again that when it comes time to change some old code, you can do this by making a change in just one place instead of hunting all over to make a change.

Next, read section 4.6 in the book and do the examples.

I used to end the homework here, but I had to combine it with another homework to get to all of them in the semester. So... keep going...

Read sections 4.7 and 4.8 in the book and do the examples. I teach an entire course about good interfaces, refactoring, and code planning, so I don't expect you to master all of that now.

Documentation means writing (in English or another natural language) about how your code works. Comments are one form of documentation. Docstrings are another, and they're something you've already used. Let's do one more cool little thing with them. Consider this function:

def print_monkey():
    """Prints out the best word ever: monkey."""
    print("monkey")
        

We can actually access the docstring for that using the __doc__ attribute of the function. Run the code with print_monkey in interactive mode:

>>> print_monkey()
monkey
>>> print_monkey

>>> print_monkey.__doc__
'Prints out the best word ever: monkey.'
>>>
        

More importantly, docstrings are used in generating the external documentation for a package using the command-line instruction pydoc.