A neat trick for handling indents in Python

Anyone who has been working with Python knows that its rigid indentation rules can only do so much in the way of improving readability. However, with increasing code complexity, one can easily get lost without knowing their position in the “indent tree”. Some IDE’s give some sort of guidance bars on the side of the text hinting at the indent level you’re at. Moreover, strong indentation is good for reading the code, not necessarily a good thing when writing and testing.




But I am a vim user, and after a few years learning and harvesting the power of vim, one does not simply forfeit vim for an IDE. On top of this, I often program over the net and through a series of firewall, making the use of more advanced IDE’s somewhat unpractical. So there is a neat little trick I use. Let’s say I’m writing a function that must iterate over a series of compounds in a data base. For each compound I want it to collect some data, make a few calculations and then plot some data for each compound. The code starts like this:

def myAwesomeModel(myArg):
if(myArg != "Valid Option"):
print("ERROR: Your argument is invalid")
return(1)
for compound in compoundList:
name=getName(compound)
for method in methodsList:
for property in interestingPropertiesList:
(...)

Now the code starts to become a little bit over-indented, and when I start to think about writing the final code to plot my data, I might be off by one or two indents. My little trick for today is to make abundant use of comments before writing the code. On the first stage, my code usually looks something like one-line (or two-line) comments with the code layout (almost pseudo-code).

def myAwesomeModel(myArg):
if(myArg != "Valid Option"):
print("ERROR: Your argument is invalid")
return(1)
#for each compound
#plot the the data for the compound at this level

I can now start to layout what I intent to do for each compound:

def myAwesomeModel(myArg):
if(myArg != "Valid Option"):
print("ERROR: Your argument is invalid")
return(1)
#for each compound
# for each method
#determine functional groups
#collect charge for the atoms on each functional group
#save the charge of each functional group
#also save the data for the charge scattered on the
#remainder of the molecule
#plot the the data for the compound at this level




Two things are now evident. First, I’ll always be positive regarding the indentation level at which I want my plotting code to be. Second, without typing any code, I can see that determining functional groups is something that changes with each molecule, not with the method used to calculate effective atomic charges, so I can easily move that comment line without fear of creating new bugs because the indentation is wrong.

def myAwesomeModel(myArg):
if(myArg != "Valid Option"):
print("ERROR: Your argument is invalid")
return(1)
#for each compound
#determine functional groups
# for each method
#collect charge for the atoms on each functional group
#save the charge of each functional group
#also save the data for the charge scattered on the
#remainder of the molecule
#plot the the data for the compound at this level

Now I can start putting some “meat” on the code. I guess this is something computer scientists and programmers would do without even thinking, but for someone who comes from a different background, it is always important to highlight the importance of using comments on code… before starting to actually write the code!

Leave a Reply

Your email address will not be published. Required fields are marked *