[Tutor] functions first?

Ben Finney ben+python at benfinney.id.au
Tue Jan 27 07:30:41 CET 2015


Alex Kleider <akleider at sonic.net> writes:

> Please correct me if I am wrong, but I've assumed that it is proper to
> define all functions before embarking on the main body of a program.

I would say rather that as much code as possible should be in small
well-defined functions, with the “main body” a tiny and trivial call to
a function.

That way, all the code is easily tested by unit testing tools.

> I find myself breaking this rule because I want to set the default
> values of some named function parameters based on a configuration file
> which I have to first read, hence the need to break the rule.

Module-level constants are fine, and they obviously need to be bound
before the definition of the function which uses them for parameter
defaults.

But if they're not constants – as implied by your statement you read
them from a configuration file – then they should not be in the function
definition, because reading the configuration file should itself be
encapsulated in a well-tested function.

-- 
 \      “It is the integrity of each individual human that is in final |
  `\        examination. On personal integrity hangs humanity's fate.” |
_o__)               —Richard Buckminster Fuller, _Critical Path_, 1981 |
Ben Finney



More information about the Tutor mailing list