The benefit of properties (was Re: PEP 8: on member variables vs. attributes)
johnroth at ameritech.net
Fri Jan 17 16:28:51 CET 2003
"Peter Hansen" <peter at engcorp.com> wrote in message
news:3E280FE3.EB4033F at engcorp.com...
> John Roth wrote:
> > Properties are simply a piece of syntactic sugar on top
> > of accessor functions. As such, they provide the same
> > information hiding benefits as accessors.
> > It's a nice piece of syntactic sugar, but it does not provide
> > anything fundamental that can't be accomplished in some
> > other way.
> > What it does provide is clarity and a level of encapsulation
> > that facilitates maintenance.
> They can also provide an obfuscation technique because they
> make implicit things that should sometimes be explicit. I
> think this might be Yet Another Tool That can Easily be
> Abused in the hands of an unthinking programmer.
> When I was leading a team doing lots of Delphi development,
> several developers (who had used Delphi much longer than I)
> insisted on using properties for just about everything. No
> accessor methods at all.
> I rebelled against this at the time, almost outlawing properties,
> because it got to the point where I couldn't tell when something
> was just setting a value, possibly with a check or clipping or
> something applied, and when the code -- which looked like a simple
> assignment -- was actually storing some data in a file, sending
> a message to another thread, stopping the machine's motion, and
> then launching a Space Shuttle.
> No doubt forbidding them was an overreaction, but at least it
> let us start getting a handle on the complexity of the code.
I'd say it was a slight overreaction, but only slight. My take on
the underlying problem is that there wasn't a good understanding
of what looks like a variable and what's a method.
My feeling is that if I can't name it as a variable, then it's not
a variable, it's a method.
> I've been looking forward to having a chance to let them redeem
> themselves when I start using them with Python... if they can.
> Do they really enhance clarity and maintenance, or might they be
> more _in the way_ of easy comprehension?
I think naming standards help here, but I've certainly got nothing
against using accessor functions for everything. What I don't like
is exposing the *real* instance variables. That way leads to more
coupling and a harder time maintaining the program.
More information about the Python-list