OT: Crazy Programming

Max M maxm at mxm.dk
Mon May 13 06:56:58 EDT 2002


Alex Martelli wrote:

> Imagine two painters.  One spends half his time at arts supplies stores,
> checking out the tiniest differences between different models of
> brushes, different brands of paint, canvases prepared with minutely
> distinct processes yielding minute roughness and absorption differences,
> and so on.


Well I sometimes play a little game for myself where I try to refactor 
short snippets of code which does some common idioms.

I then try different approaches to my code. Here below is a few simple 
pieces that does the same thing. From a list of objects they render a 
list in html.

The funny thing I have found out is that the most elegant is often also 
the fastest.

Another thing is that if you try to refactor too much and really live 
out the "Don't Repeat Yourself" slogan, your code will get harder to 
read and maintain.

Also it works best if you use different "shades" of Python to solve 
specific "shades" of problems that are quite similar.

So there really is a lot of craftsmanship in writing good code.

regards Max M

################################################################3


#   object.id
#   object.title
#   object.summary
#   object.objectType

#   listOfObjects # List of objects



# typical code from a new programmer (603 chars)
def listView(listOfObjects):
     result = ''
     for object in listOfObjects:
         if (object.objectType = 'folder'
             or object.objectType = 'content holder':
             result = result + '<img src="folder.gif">'
         else:
             result = result + '<img src="content.gif">'
         result = result + object.title + '<br>'
         result = result + '<font size="-1"><b>' + object.summary \
                  + '</b></font>'
         result = result + '[<a href="view.asp?id=' + object.id \
                  + '">View</a>]'
         result = result + '[<a href="edit.asp?id=' + object.id \
                  + '">Edit</a>]<br>'
     return result



# completely refactored, no redundancy (579 chars)
def listView(listOfObjects):
     result = []
     a = result.append
     href = '[<a href="%s.asp?id=%s">%s</a>]'
     img = '<img src="%s">'
     for object in listOfObjects:
         if object.objectType in ['folder', 'content holder']:
             a(img % 'folder.gif')
         else:
             a(img % 'content.gif')
         a('%s<br>' % object.title)
         a('<font size="-1"><b>%s</b></font>' % object.summary)
         oId = object.id
         a(href % ('view', oId, 'View'))
         a(href % ('edit', oId, 'Edit'))
         a('<br>')
     return ''.join(result)



# template based (539 chars)
def listView(listOfObjects):
     result = []
     rTxt = """<img src="%(type)s.gif">
     %(title)s<br>
     <font size="-1"><b>%(summary)s</b></font>
     [<a href="view.asp?id=%(id)s">View</a>]
     [<a href="edit.asp?id=%(id)s">Edit</a>]<br>""
     for object in listOfObjects:
         if object.objectType in ['folder', 'content holder']:
             vars_ = {'type':'folder'}
         else:
             vars_ = {'type':'content'}
         vars_.update(vars(object))
         result.append(rTxt % vars_)
     return ''.join(result)



# Best practice (519 chars)
def listView(listOfObjects):
     result = []
     a = result.append
     for object in listOfObjects:
         if object.objectType in ['folder', 'content holder']:
             a('<img src="folder.gif">')
         else:
             a('<img src="content.gif">')
         a('%s<br>' % object.title)
         a('<font size="-1"><b>%s</b></font>' % object.summary)
         a('[<a href="view.asp?id=%s">View</a>]' % object.id)
         a('[<a href="edit.asp?id=%s">Edit</a>]<br>' % object.id)
     return ''.join(result)




More information about the Python-list mailing list