[Tutor] Linear programming - is it bad?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Aug 10 02:03:24 CEST 2004



On Mon, 9 Aug 2004, Eric Belanger wrote:

> So my question: is it bad to write a python program/script in a linear
> way? By linear, I mean: the code is executed line by line, without any
> (programmer defined) classes or functions, a bit like [Q]BASIC without
> GOTO's, or , while we're at it, HTML.


Hi Eric,

Truthfully?  It's really painful for me to debug straight-line code that
goes on for several pages.  It rambles on and on, and the letters just
start swimming in my head because I have a puny brain.  I don't usually
say that out loud, but you did ask if long code was bad.  *grin*


As far as the computer is concerned, it's fine with code as long as it's
syntactically correct.  But boxing related code into functions does help
other people read the code.  Using functions is really more for people
than for Python.  There are some places where writing a function is
technically necessary, but usually, it's more in consideration for human
readers.


> I assume that by writing huge projects/programs, im assured programmers
> *needs* to use functions and classes. But since Im beginning, I dont see
> the point.

They can help clean up some code.  I hate talking in abstractions though.
*grin*



Let me take a look at a portion of the 'AutoLogC.py' code you put up, and
see if we can show how functions can help.  Thanks for sharing your code,
by the way!  Ok, here's a good block we can talk about:


###
#For each strings, check if it exists (if not, create without any value)
#otherwise, gather its current value for displaying later.
try:
	data = QueryValueEx(reg,"DefaultUserName")
	regusername = data[0]
except WindowsError:
	print "Error: DefaultUserName string missing from registry:
creating an empty string."
	nothing = SetValueEx(reg,"DefaultUserName",0,REG_SZ,"")
	regusername = ""
try:
	data = QueryValueEx(reg,"DefaultPassword")
	regpassword = data[0]
except WindowsError:
	print "Error: DefaultPassword string missing from registry:
creating an empty string."
	nothing = SetValueEx(reg,"DefaultPassword",0,REG_SZ,"")
	regpassword = ""
try:
	data = QueryValueEx(reg,"DefaultDomainName")
	regdomain = data[0]
except WindowsError:
	print "Error: DefaultDomainName string missing from registry:
creating an empty string."
	nothing = SetValueEx(reg,"DefaultDomainName",0,REG_SZ,"")
	regdomain = ""
try:
	data = QueryValueEx(reg,"AutoAdminLogon")
	regautolog = data[0]
except WindowsError:
	print "Error: AutoAdminLogon string missing from registry:
creating an empty string."
	nothing = SetValueEx(reg,"AutoAdminLogon",0,REG_SZ,"0")
	regautolog = "0"
###


There are four similar-looking blocks here that try to lookup registry
keys for :

    DefaultUserName
    DefaultPassword
    DefaultDomainName
    AutoAdminLogon



We can capture the thing that's pretty constant within all those blocks as
a function.  Something like:

###
def queryForValue(reg, name):
    try:
        values = QueryValueEx(reg, name)
        return values[0]
    except WindowsError:
        print "Error:", name, "string missing from registry:",
        print "creating an empty string."
        nothing = SetValueEx(reg,name,0,REG_SZ,"0")
        return "0"
###

that we can then use this user-defined queryForValue() function to do the
registry lookup for us and handle the ugliness of WindowsError exceptions:

###
regusername = queryForValue(reg, "DefaultUserName")
regusername = queryForValue(reg, "DefaultUserName")
regpassword = queryForValue(reg, "DefaultPassword")
regdomain = queryForValue(reg, "DefaultDomainName")
regautolog = queryForValue(reg, "AutoAdminLogon")
###


We don't gain much in terms of the capability of the program:  this does
everything that the previous code did before.  What we do win, though, is
a lot of brevity, and brief is good.  *grin*


I hope this was a compelling example of why programmers use functions.
Good luck!



More information about the Tutor mailing list