Tuesday, September 13, 2011

Learning from bad examples

I saw this in a python tutorial today:
if os.path.exists("/tmp/uPlayer"):
   f = open("/tmp/uPlayer/STATUS", "w")
   f.write("IDLE")
   f.close
else:
   os.mkdir("/tmp/uPlayer")
   f = open("/tmp/uPlayer/STATUS", "w")
   f.write("IDLE")
   f.close

Really, now? We start by duplicating the whole open/write thing in order to create a directory? Maybe one of the problems people have with clean coding is that their examples are poor. If you write, it behooves you to write refrigerator code.

Not to mention that close is a function, so it needs parentheses. This code doesn't even do what it thinks it is doing. It's closing the file when f goes out of scope, not when we reference the f.close bound method.

It makes me sad for the children.

1 comment:

  1. "It makes me sad for the children." LOL

    Tell me about it.
    Dr. Bubba

    ReplyDelete