[Tutor] Looking for some comments on my code segment...
Cameron Simpson
cs at cskk.id.au
Fri Feb 24 17:26:55 EST 2023
On 25Feb2023 09:18, Cameron Simpson <cs at cskk.id.au> wrote:
>Two other things: by using global variables and calling exitscript()
>from your set up function you're embedding policy in the function i.e.
>that this function will only ever be called once and therefore globals
>can store the result, and that failure of this function should exit the
>entire programme.
I meant to add some commentry here:
I find it helpful to distinguish in my mind mechanism and policy.
Mechanism is how something is done eg: tn = telnetlib.Telnet(HOST, PORT,
TIMEOUT)
Policy is deciding how to handle the ways that might play out,
particularly failure.
By putting eg exitscript() in the function you're putting policy about
what happens on failure in the low level function opening the telnet
connection. This leaves little flexibility for the user of your
function.
I know in this case it's a new script and you're the only person using
it, but later when you keep code around to solve problems it becomes
more and more important.
My general principle here is that mechanism lives in the low level
functions (well, it happens where it happens, or your programme doesn't
work), but policy should be in the outer levels i.e the caller of your
functions - as far out as possible.
One nice thing about exceptions is that they let you punt on policy as
much as you like. Don't know how some exception should be handled
(because _in_ the functin you don't know what the function's being used
for)? Ignore it and let the exception bubble out - some claller with
more context can decide what to do.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list