Ok, about a year ago, I reluctantly adopted PEP8
formatting style for my Python code. Lord knows, we need to make
sure that our lines aren't too long for the transition when punch
cards come back into vogue. PEP8 also dictates that we have certain
numbers of blank lines in certain places to make code more readable.
This is where I think that PEP8 doesn't go far enough.
Perhaps the visual pattern matching
system in my brain has lapsed into senility, but empty space isn't
good enough for me. Maybe I'm just a product of early corruption by
COBOL, but I like horizontal lines to mark out blocks in my code.
Compare these examples:
from numbers import Number
class Alpha(object):
"""This is a silly class that does very very little"""
def __init__(self, config):
self.config = config
def do_something(self, data):
if isinstance(data, basestring):
return ''.join(x for x in 'you think this is silly?'
if x not in 'aoeui')
elif isinstance(data, Number):
return 2 * data
else:
return data
def do_something_else(self, data):
if data is None:
return True
else:
raise TypeError('only None is acceptable')
class Alpha(object):
"""This is a silly class that does very very little"""
def __init__(self, config):
self.config = config
def do_something(self, data):
if isinstance(data, basestring):
return ''.join(x for x in 'you think this is silly?'
if x not in 'aoeui')
elif isinstance(data, Number):
return 2 * data
else:
return data
def do_something_else(self, data):
if data is None:
return True
else:
raise TypeError('only None is acceptable')
I have a tough time seeing the method definitions in the code above. A single blank line and the indentation is insufficient to make them stand out. The following code is what I'm likely to write:
from numbers import Number
#==============================================================================
class Alpha(object):
"""This is a silly class that does very very little"""
#--------------------------------------------------------------------------
def __init__(self, config):
self.config = config
#--------------------------------------------------------------------------
def do_something(self, data):
if isinstance(data, basestring):
return ''.join(x for x in 'you think this is silly?'
if x not in 'aoeui')
elif isinstance(data, Number):
return 2 * data
else:
return data
#--------------------------------------------------------------------------
def do_something_else(self, data):
if data is None:
return True
else:
raise TypeError('only None is acceptable')
For me, the method definitions stand out. Scanning down through a long file, I can quickly see and understand the structure.
Am I just a product of a different era? Why is this style eschewed in most of the code that I see these days?
"Maybe I'm just a product of early corruption by COBOL"
ReplyDeleteBah! Nothing gets corrupted by COBOL... (Just joking)
As a COBOL developer, I agree that getting grasp of structure is aided greatly by horizontal lines, marking new sections, so it may be that I'm biased towards more visible method delimiters.
However, I fear that too many horizontal lines will decrease readability if each method is only a few lines long, imho.
I'm thinking about what you just posted here. What I've done since about 60 minutes into my Python career is add comments such as
ReplyDelete# end if/else blahphoo > 0
whenever I found myself working on a block taller than my screen window, or confusingly indented or whatever. When looking for the end of a function/method, I've just scanned for the next "def " and then backed up. Not a great method, but it does work for me since I *never* (ahem) use globals that aren't near the top of the module and very seldom make function aliases
Oh my god, no leading 5 garbage columns in your brains? How you'd ever sort your punchcards?
ReplyDeleteGo FORTRAN!
Try adding docstrings to the methods as well, you may find that's enough to trip your recognition of the block structure.
ReplyDelete