Understanding other people's code

CM cmpython at gmail.com
Sun Jul 14 08:58:30 CEST 2013

> Basically the problem is I am new to the language and this was clearly 
> written by someone who at the moment is far better at it than I am!

Sure, as a beginner, yes, but also it sounds like the programmer didn't document it much at all, and that doesn't help you.  I bet s/he didn't always use very human readable names for objects/methods/classes, either, eh?

> I'm starting to get pretty worried about my lack of overall progress and so I 
> wondered if anyone out there had some tips and techniques for understanding 
> other peoples code. There has to be 10/15 different scripts with at least 10 
> functions in each file I would say.

Unless the programmer was really super spaghetti coding, I would think that there would be some method to the madness, and that the 10-15 scripts each have some specific kind of purpose.  The first thing, I'd think (and having not seen your codebase) would be to sketch out what those scripts do, and familiarize yourself with their names.  

Did the coder use this form for importing from modules?

from client_utils import *

If so, that's going to make your life much harder, because all of the names of the module will now be available to the script it was imported into, and yet they are not defined in that script.  If s/he had written:

import client_utils

Than at least you would expect lines like this in the script you're looking at:

customer_name = client_utils.GetClient()

Or, if the naming is abstruse, at very least:

cn = client_utils.GC()

It's awful, but at least then you know that GC() is a function within the client_utils.py script and you don't have to go searching for it.

If s/he did use "from module import *", then maybe it'd be worth it to re-do all the imports in the "import module" style, which will break everything, but then force you to go through all the errors and make the names like module.FunctionName() instead of just FunctionName().

Some of that depends on how big this project is, of course.

> Literally any idea will help, pen and paper, printing off all the code and 
> doing some sort of highlighting session - anything! 

What tools are you using to work on this code?  Do you have an IDE that has a "browse to" function that allows you to click on a name and see where in the code above it was defined?  Or does it have UML or something like that?  

More information about the Python-list mailing list