Fixing up code

Tags: python

Martin Aspeli wrote about digging yourself out of a debugging hole. A tip that resonated with me was to clean up the code first.

Cleaning up code is something I do regularly. Whether I need to debug some code or have to get to know some currently-unknown piece of code: I go through it with pyflakes and pep8 for syntax fixes, removing unneeded imports, fixing too long lines, etcetera. The pep8 police hits again.

If I don’t know the code beforehand: by prettying up the code I automatically go through the whole file and get to know it a bit more.

What also helps: write down the goal of classes/methods in a docstring if it isn’t available. Or fix up the existing docstring. As you need to write down what the method is supposed to be doing, you need to understand what it is supposed to be doing. It is a way of forcing yourself to understand.

Note to self: mentioning “pyflakes” here reminds me that I need to get my zc.buildout branch merged. The branch fixes the bug that old-style distutils scripts (as opposed to setuptools’ console_scripts entry point) aren’t created by buildout. Pyflakes and docutils are the two major victims.

 
vanrees.org logo

Reinout van Rees

My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):