vertical ordering of functions

J

Jabba Laci

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
 
R

Roy Smith

Jabba Laci said:
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.

There may have been some logic to this when people read programs on
stacks of green-bar or rolls of teletype paper. These days, we have
better tools.

When I'm reading some code and see a function call I want to explore, I
search for the function name in my text editor. People who use an IDE
can probably just click on the function name. In either case, the tool
takes me where I need to go. I'm generally completely unaware whether
it's taken be forward or backward in the file.
 
G

Grant Edwards

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.

I generally expect the opposite: callees above, callers below, main at
the bottom. However, that's mostly just a habit left over from C
programming where such an ordering avoids having to litter the file
with forward declarations for functions.
 
J

John Roth

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
 
H

Hans Georg Schaathun

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.

My advice would be to order it in the same way as you would if you
were presenting the algorithm in a paper.

From such a viewpoint, it depends on whether you consider the called
names to be terms which need definitions, or operations with an
intutive purpose, merely requiring an explanation (implentation) of
how it is done.

In a paper that means that definitions must come before the theorems
and lemmata very often between the theorem and its proof. If you
don't write papers, that may not be particularly useful :)

In programming, it means that names which have self-explanatory name
may very well be called first and defined later. The caller will
give a high-level view of what is going on. The callees are building
blocks whose purpose can be understood immediately, and whose inner
workings can be sought further down the file. Fundamental definitions
which may be as easy to implement as explain may often come first.

The question here, is didactics and not programming per se.
A simple rule does not make complex code significantly easier to read,
but a thought structure of the code (with comments and choice of names)
over-all does.

And let's not forget that calls may be circular or otherwise convolved
in a way that does not allow consistent sorting of caller before/after
callee.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top