OT: Crazy Programming

Max M maxm at mxm.dk
Mon May 13 16:52:20 EDT 2002


Andrew Dalke wrote:

> Max M:
> 
>># 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)
> 
> 
> Why is this 'best practice'?  At the very least, I would have used
> "append = result.append" because "a" can be confused (in an HTML context)
> for the request to do an "a" tag


Oh ... you misunderstand my goal with this "game". The aim is not to do 
things absolutely correctly, but to get the same result from different 
versions of the code.

Trying to refactor it with different aprocahes to get a feel for what 
works in different situations.

By best practice I only meant that it was the best overall approach for 
refactoring this piece of code I could think of at the time.

 >The result makes it much easier to see how the HTML looks in one go
 >rather than building it up through many Python commands.

Yes, your aproach certainly has some merrit.

regards Max M




More information about the Python-list mailing list