[Tutor] howto keep a program organised?

Alan Gauld alan.gauld at freenet.co.uk
Mon Sep 11 12:35:04 CEST 2006


Hi Rob,

> For example, I may need to mount the usb storage device at some 
> point
> in the program.
> For this end I made the following functions:
> - usbMount() -> main function.
> - usbUmount() -> main function.
> - usbMounted() -> help function. Checks if usb already mounted.
> - usbConnected() -> help function. Checks id usb connected to 
> computer.
> - usbValid() -> help function. Checks if usb contains certain info
> (only these may be mounted).
> The help functions are called from the main functions.
> So I have multiple help functions, while only two are actively 
> called.
>
> What is a good way to keep this organised?
> - Just put it in a usb.py module?

Thats a very good place to start.

> - Should I make a class?

I think, without seeing the internals of the code, that this is
likely to be a good idea in this case. If you have a number of
shared global variables between functions then I'd certainly
say yes.

> Are there any advantages if I put this in a class, instead of in a 
> module?

The advantage of a class is that if you ever need more than
one USB device mounted then the instances of the class can
each hold their own state information, whereas with global
variables you would be limited to one mounted instance
- unless you stuck very rigorously to functional programming
principles...

> This is only a small example, but I run into the same problem with
> other parts of my program, that are larger, and where the functions
> also depend on each other, while only a few are called (and the rest
> just clutter my view in the IDE).

Where things depend on each other a module is the minimum
organising device. Where functions share data a class is usually
a good idea on top of that.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



More information about the Tutor mailing list