how to reduce bugs due to incorrect indentation

Asaf Las roegltd at gmail.com
Thu Feb 6 06:23:29 CET 2014


On Thursday, February 6, 2014 5:02:09 AM UTC+2, msu... at gmail.com wrote:
> I had a bug in a Python script recently. The code in question was 
> something along the lines of:
> if a == 1:
>     x = y
> else:
>     x = z
> y = z + y
> z = z + 1
> 
> While editing this file I accidentally pushed TAB on the line with 
> 'y = z + y'.
> My changes were elsewhere and I did not notice the above one line 
> change when I looked at the diffs before commit. I should have noticed 
> it...
> 
> It was rare that a was 1 and therefore the problem did not show up 
> for a while. (I know I should have had tests exercising all cases...)
> When the bug showed up, it was kind of difficult to remember what 
> was the original intent. Fortunately, looking at old versions allowed 
> me to find the problem commit and the bug.
> Any suggestion on how to avoid this type of error in the future?
> Thanks!

I see. 

The only solution to mimic C style curly brackets comes to my mind is 
to use hash to mark start and stop of your block as:

if a == 1:#
    x = y
#
else:#
    x = z
#
y = z + y
z = z + 1

and PEP ... Nulla poena sine lege :-)

/Asaf



More information about the Python-list mailing list