vertical ordering of functions

John Roth johnroth1 at gmail.com
Wed May 4 07:14:09 EDT 2011


On May 3, 4:08 pm, Jabba Laci <jabba.l... at gmail.com> wrote:
> Hi,
>
> I'm just reading Robert M. Martin's book entitled "Clean Code". In Ch.
> 5 he says that a function that is called should be below a function
> that does the calling. This creates a nice flow down from top to
> bottom.
> However, when I write a Python script I do just the opposite. I start
> with the lines
>
> if __name__ == "__main__":
>     main()
>
> Then I add main() above, which is a control function that contains
> some function calls that decompose the problem into subproblems. Then
> I add these functions above, etc.
>
> Is there a convention for this? Should main() be at the top and called
> function below?
>
> Thanks,
>
> Laszlo

I order classes and methods as I'd expect someone approaching the
program for the first time to want to read it. I think Robert Martin's
recommendation of use before declaration makes it easier to read a
program for the first time. Some compilers require declaration before
use because they need to see the declaration before they see any uses.
Python is in between: you can treat Python as if the order you write
things doesn't matter, but there are cases where it really does
matter. When it does, you have to have the definition before the use.

John Roth



More information about the Python-list mailing list