Is it wise to implement functions that just call other functions?

12

I've learned which functions should be small and concise. Would this rule also apply to functions like this?

def run_game(self):
    process_input()
    update_state()
    render()

What I mean is: run_game appears to be trying to do more than one thing at a time, but it seems to me that it is being used as an organizational unit, executing the functions that would compose the process labeled run_game .

Does this invalidate the "one thing well" principle? Is it normal to use functions as organizational units and gather related functions in a main function?

    
asked by anonymous 14.02.2014 / 23:13

1 answer

8

Yes, this form of organization is normal. Your run_game function is not trying to "do more than one thing at a time", it is simply operating at a higher abstraction level .

For example, a function that reads a file and stores its contents in a data structure would do something like:

def foo(arquivo):
    with open(arquivo, 'r') as f:
        conjunto = set([x.strp() for x in f.readlines() if x.strip()])
    return conjunto

This function is making use of the file manipulation library and set creation library. Each one of them "only does one thing and does it well," but you have to combine their functionality to implement something more complex (get the set of non-empty lines of the file).

If another bar function needs a set of rows stored in a file, it will make use of the foo function as if it were a simple command like x + y : already that at the level of abstraction of bar things like "getting a set of a file" are a simple operation - not a set of operations. Just as at the abstraction level of foo , things like "reading all the lines of a file" are one operation, even though in reality this involves a series of lower level operations.

    
15.02.2014 / 00:06