Let's say I have a basic procedural program which is well structured into decomposed functions. For example, a
main() function which calls functions
b, which in turn each call functions
f. In pseudo-Python, this could be written bottom-up like this:
def c(): # Do stuff def d(): # Do stuff def e(): # Do stuff def f(): # Do stuff def a(): c() d() def b(): e() f() if __name__ == "__main__": a() b()
However, it could also be written top-down like this:
if __name__ == "__main__": a() b() def a(): c() d() def b(): e() f() def c(): # Do stuff def d(): # Do stuff def e(): # Do stuff def f(): # Do stuff
In general, in many languages, both of these would be valid, but is there a preferred coding style? I'm primarily thinking of readability/maintainability here, not efficiency of compilation or execution. Does it vary between languages? I'm thinking firstly of Python here, but there are many other languages this question could apply to here, so I'm happy to make it broader.
Note: I am not thinking about the design of the program here (whether to think top-down or bottom-up first), I am specifically talking about the ordering of the source code.