[Tutor] What is the best approach to organizing the order of functions in a module?

Ben Finney ben+python at benfinney.id.au
Thu Mar 19 23:09:16 CET 2015


boB Stepp <robertvstepp at gmail.com> writes:

> I'm still working in the procedural paradigm of programming. Hopefully
> I will find time for OOP soon.

Fortunately, with Python, you're almost always doing OOP because all
Python values are accessed as objects :-)

But I think you mean you're not yet writing classes.

> But in some of my modules-in-progress,my collection of functions has
> gotten large enough that I feel I need to bring some sort of order to
> their positioning. Currently my best thought is to mimic the natural
> order, in so far as it is possible, in which the functions get called.
> Are there better ways to organize them?

You can migrate a step further to writing classes: organise your code by
*conceptual proximity*. Functions that are very closely related
conceptually get placed close together, and the data structures and
constants they use are also placed nearby.

Use a page break (the U+000C FORM FEED) white space character on a line
by itself, to break up the source code into conceptually-separate areas.
Your text editor should have a way of navigating to the previous/next
form-feed character, as a way of navigating your file at this level.

Deliberately start organising your code so conceptually-related code is
closer together, and you'll find it more natural when you begin writing
classes.

-- 
 \       “Repetition leads to boredom, boredom to horrifying mistakes, |
  `\       horrifying mistakes to God-I-wish-I-was-still-bored, and it |
_o__)              goes downhill from there.” —Will Larson, 2008-11-04 |
Ben Finney



More information about the Tutor mailing list