Ruby Impressions
Adam Spitz
adamspitz at bigfoot.com
Sat Jan 12 20:24:35 EST 2002
> The thing you should learn from all this is: *most* Python users
> are less concerned with minimizing keystrokes than they are with
> minimizing the number of brain cycles it takes to understand a
> previously-written piece of code.
Um... so are we. We just think that eliminating duplication and giving
names to ideas makes our code *more* readable, not less.
When I look at a piece of code like this...
class Person
def initialize(name, age, gender)
@name, @age, @gender = name, age, gender
end
end
...my brain parses it, thinks, "Oh, it's just initializing some
attributes," and then thinks of the whole chunk as one "unit". Call it
an idiom, or a pattern, or whatever you want - point is, I see the
whole initialize() method as one thing, and I want to give a name to
that thing.
Maybe I settle on the name "init_attributes", since those are the
words my brain happened to use in the previous paragraph. So I create
a method called init_attributes (which I'm not going to show here,
because it's kinda hairy, but you can find it on that Wiki page I
mentioned), and from that point on I can express the "Oh, it's just
initializing some attributes" concept directly in my code:
class Person
init_attributes(:name, :age, :gender)
end
The resulting code requires less typing, yes, but that wasn't the
point. The point is that it expresses my intention directly, and
contains less duplication. This makes my code *easier* to read, as
long as the reader knows what the init_attributes() subroutine does.
Maybe now you're going to argue that we should never create any
subroutines, because they force readers to go to the extra trouble of
looking up what they do. I won't have an answer to that.
Adam Spitz
More information about the Python-list
mailing list