from: http://objology.blogspot.com/2011/09/one-of-best-bits-of-programming-advice.html
Years ago (early 1992), I attached myself to this crazy skunkworks
project that was using this weird language called Smalltalk. "Object
Oriented" was in its infancy as a "hot" item. High paid consultants.
Lots of people laying claim to what this new object religion was all
about. This was 5 years before Alan Kay
would make the statement "I invented the term 'Object Oriented Programming' and this {Java and C++
} is not what I had in mind."
Shortly
after hooking up with this whacky group with the whacky language, still
confused about what the difference was between an instance variable, a
class variable, and a class instance variable, I found myself in a
training course taught by Russ Pencin, of ParcPlace. Russ would say
something that I didn't really appreciate at the time. Despite not
understanding the point behind this sage advice, I endeavored to follow
it. It would take years of experience and exposure to appreciate it's
value. The advice?
Don't make objects that end with 'er'.
That's
it. The OOP paradigm sprang to life amidst of a culture of what we
called "procedural programming." Now days we don't talk so much about
the comparison between the two paradigms. Probably in part because
Object Oriented languages are now a dime a dozen. The OOP religion, in a
multitude of flavors won out. Sadly, I often find myself echoing words I
heard Adele Goldberg
say around 2000: "Now days we have lots of Object Oriented Programming,
but not so many Object Oriented Programmers". If there was one piece of
advice I would pass on to the hordes of would be Object Oriented
Programmers, it would be the sage advice offered by Russ: "Don't make
objects that end with 'er'."
What's in a name anyway? Why is this
worth getting excited about? What I've discovered over the years, is
that the jst of OOP is that we bind behavior to data
. As long as you
haven't joined in the Functional Monks in their Monasteries of
Statelessness, programs are made of behavior and data.
In classic
structured/programming, we concentrate on behavior (verbs), and then
figure out what data (nouns) we need to make it all work. In other
words, we bind data to behavior. But in OOP, we make the focus of
programs be the nouns, the data, and then we figure out what kind of
behavior we can bind to them, and hope that the problems we hope to
solve get out of the emergent behaviors.
I recently posited to a
colleague that in nearly every "er" object case, there was a better name
for it. And that giving it a better name would tend to make the design
more encapsulated, less spaghetti code, in short more object oriented.
It's not a hard and fast rule, but there are a lot of cases where it can
improve things.
Take some sort of "Loader" for example. The
focus here is on the unit of work it does. It'll have lots of instance
variables, lots of arguments, and pass lots of data around probably. Now
instead replace that with a LoadRecord and a LoadStream. I'm reasonably
confident you'll end up with something that is more akin to what the
original Founding Fathers of OOP had in mind. We want to create objects
that describe what they are, and then bind behavior to them, rather than
focus on what they do, and then figure out what data they'll need to do
that.
Some er's that I've learned to avoid over the years:
- Managers
- Every time I see one of these, I cringe. People will usually tell me
what it does, long before they can tell me what it is. Is it a registry?
Fine call it a registry. Is it a history or a log? Call it that. Is it a
factory? Call it that.
- Controllers - Only good controller
object I've made in the last 20 years was an interface to a
BallastVoltageController that represented a real world object. The fact
that every single MVC implementation in the world has had a different
role for Controller ought to tell us something about how well that idea
fit.
- Organizer (and many like them) - Focus is on what it does.
This is a great example of how easy it is to turn many of these 'ers'
into nouns. Call it an Organization. Now we're focusing on what it is.
- Analyzer/Renderer/etc - Definitely examples of "worker" objects. What if they had been Analysis/Rendering/etc.
- Builder/Loader/Reader/Writer/etc
- Remove the focus from the objects being manipulated, and tend assume
to much responsibility themselves.
There's lots of exceptions to such a rule of course.
- There are lots of noun words that end in 'er'. Register. Border. Character. Number. If it's really a noun, fine.
- There
are many 'er' words that despite their focus on what they do, have
become so commonplace, that we're best to just stick with them, at least
in part. Parser. Compiler. Browser.
- When you are trying to
model a domain object that ends in 'er'. I'm fine with a Manager
subclass of Personel, which is there to refine a type of personal that
has management behavior to it.
Your mileage may vary, I'm
sure there are those that disagree with this. Until you apply the
mindset for a while though, you'll never really know. Give it a whirl on
one of your projects/designs and see what happens.
分享到:
相关推荐
One of the best-selling programming books published in the last fifty years, "K&R" has been called everything from the "bible" to "a landmark in computer science" and it has influenced generations of...
Indeed, the best way to understand how computers work is to build one from scratch, and this textbook leads students through twelve chapters and projects that gradually build a basic hardware platform...
Another "best" value that is tracked by the particle swarm optimizer is the best value, obtained so far by any particle in the neighbors of the particle. This location is called lbest. when a ...
We derive an analytical expression of the best approximate solution in the leastsquares solution set SE of the matrix equation AXB + CY D = E to a given matrix pair (Xf, Yf ), where A, B, C, D, and E ...
Swift builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, ...
初级入门吉他谱 guitar tab
Chapter 32: The Art of Debugging Chapter 33: Where and How to Ask Django Questions Chapter 34: Closing Thoughts Appendix A: Packages Mentioned in This Book Appendix B: Troubleshooting Installation ...
Swift builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, ...
Python 2 to Python 3 by comparing the scripts used in each chapter of the second edition of this book with the listings here in the third edition—both of which are available at ...
The program is developed through Micro C compiler, one of the best solutions for embedded programming in PIC family. It is compatible for Windows XP and Windows 7 platforms and comes with internal ...
best-of-bits-Matlab工具箱使用资源源代码 best-of-bits-Matlab工具箱使用资源 best-of-bits-Matlab工具箱使用资源 best-of-bits-Matlab工具箱使用资源 best-of-bits-Matlab工具箱使用资源 best-of-bits-Matlab工具...
Learn the all basics and advanced features of C programming in no time from Bestselling Programming Author Harry. H. Chaudhary. This Book, starts with the basics; I promise this book will make you ...
“This book is one of the best guides through the language that I've seen.” – Andrew Wray, D Enthusiast “I encourage anyone considering D to read this book. Not exactly 'D for Dummies' but it's ...
Welcome to ASP Programming for the Absolute Beginner. By developing an interest in Active Server Pages (ASP), you stand at the threshold of a very exciting programming adventure. When first introduced...
This edition of the best-selling “Red Book” describes the features through OpenGL version 4.3. It also includes updated information and techniques formerly covered in OpenGL® Shading Language (the...