[Tutor] Staring myself blind

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Jan 20 22:16:00 CET 2007


On Sat, 20 Jan 2007, Toon Pieton wrote:

> In a program I'm writing, I'm getting a Tab/space error, with the usual 
> 1) identation is incorrect or 2) mixes tabs and spaces. I just can't 
> seem to find. I tried selecting everything and untabbing it, no workie. 
> I searched an searched and searched, but couldnt find anything that is 
> really wrong.

Hi Toon,

If you think you're running into tab/space issues, you can try running 
tabnanny on your program; it should help you detect the problem:

     http://docs.python.org/lib/module-tabnanny.html


But actually, there is a definite syntactic problem on one of the lines 
before the one you're looking at.  Take a very close look at:

>                   z = [yx[0],xz[0],xz[1],xz[2],xz[3],xz[4],xz[5)],xz[6]]

There is an extra right parenthesis there that is almost certainly not 
supposed to be here.


By the way, the code you've shown us so far looks very syntactically 
fragile; I'd have to look at more of the code, but a "code smell" I'm 
sniffing is a lot of hardcoded array indexing.  Are you trying to treat an 
list as if it were a data structure?


Concretely, code that looks like the snippet above, or this code:

>                       highcard = [z[8],z[7],z[6],z[5],z[4]]

is very susceptible to off-by-one errors (as well as syntactic typos). 
But even if it is technically correct, there's a more important reason why 
we should avoid code like this: there's no explicit, human reasoning in 
the numbers 8, 7, 6, 5, or 4 that would make it easy to know why the 
highcard depends on those values.  At least, we'd have to look at the rest 
of the program to figure this out.  So that knowledge is not as localized 
as it should be.

There are often different ways to write this that are less susceptible to 
syntactic typos.  If you could put up your code somewhere on the web, some 
of us here would be happy to do a code review with you.



> I can't post the whole program, since it's ~1000 lines long.

This is also a possible code smell.  Unless my code estimation is way off, 
I don't believe that code shouldn't be so long.  Get your code reviewed: 
we might be able to help you hack down the size of that code to something 
significantly tighter.


Good luck to you.


More information about the Tutor mailing list