Re: Mixing OO and DB

From: Robert Martin <unclebob_at_objectmentor.com>
Date: Wed, 5 Mar 2008 00:15:48 -0600
Message-ID: <2008030500154825485-unclebob_at_objectmentorcom>


On 2008-03-04 00:47:07 -0600, topmind <topmind_at_technologist.com> said:

>
>
> Robert Martin wrote:

>> On 2008-03-03 10:38:36 -0600, topmind <topmind_at_technologist.com> said
>> 
>>> Using methods/functions in
>>> place of comments is not economic,
>> 
>> It's much easier on the eye, and much easier on the brain of the person
>> reading the code.  Since code is read much more than it is written,
>> this makes it very economical.

>
> I generally do not like that style. Pascal programmers used to try to
> write "self-documenting" names, and it made me appreciate skillful use
> of comments. And, you are extrapolating your personal preferences to
> others. I won't dictate how your brain and eyes *should* work if you
> don't dictate how mine should work.

I think that this:

// Calculate Pay
Money pay = hours * rate;
int overtime = max(0, hours-40);
pay += overtime * overtimeRate;
if (flags && 0x02 == 1)
  pay *= 1.02; // bump for union dues.

is a lot hard to read than this:

Money pay = calculatePay();

>

>> 
>>> and makes the code unnecessarily
>>> cluttered with micro-methods, making it harder to find the "meat"
>>> methods.
>> 
>> We don't want meat methods.  You want many small methods that build
>> upon each other and tell a story.
>> 
>> We divide english into smallish paragraphs in order to separate
>> individual concepts.  A thousand line paragraph is an abomination and
>> indicates a lack of discipline and skill on the part of the author.

>
> To mirror a newspaper in a rough sense, I prefer the "headline"
> technique:
>
> //---- Wake Up ----
>
> foo = bar + adsfsdf()
> blah go ga do dad
> zarg(aasdf, sddfsdf, sdf, asdf, asdf)
>
> //---- Shower ----
>
> if (asdfsdf() > sfssdf) {
> foo = bar + adsfsdf()
> blah go ga do dad
> zarg(aasdf, sddfsdf, sdf, asdf, asdf)
> }
>
> //---- Shave ----
>
> foo = bar + adsfsdf()
> blah go ga do dad
> zarg(aasdf, sddfsdf, sdf, asdf, asdf)
>
> //---- Get Dressed ----
>
> ....
>
> Over this:
>
> void GoToWork?(){ //look ma, no complex conditionals
> WakeUp?();
> Shower();
> Shave();
> }
> void WakeUp?(){
> foo = bar + adsfsdf()
> blah go ga do dad
> zarg(aasdf, sddfsdf, sdf, asdf, asdf)
> }
> void Shower(){
> if {......}
> foo = bar + adsfsdf()
> blah go ga do dad
> zarg(aasdf, sddfsdf, sdf, asdf, asdf)
> }
> void Shave(){
> foo = bar + adsfsdf()
> blah go ga do dad
> zarg(aasdf, sddfsdf, sdf, asdf, asdf)
> }

You are kidding right? The first is monolithic inline code, the second is nicely partitioned and self describing. You can't seriously tell me you prefer the first to the second.
>
> The second often requires a lot of parameter maintenance

No it doesn't because the second is inside an object where all the variables are fields. That's one of the benefits of having objects.

> and this is also clutter and busy work and a source of
> potential typos. It adds more parts to the machine.

So you simply don't believe in partitioning code at all. You'd rather have one big monolith. Come now.

> Note that I sometimes do split up functions even if they are called
> only once. It depends on the situation.

That's good to hear. I think you should be doing a lot more of it.

>

>> 
>> We separate programs into methods for the same reason. We want each
>> method to be trivial to understand.  We want it's name to describe it's
>> function.  We want to read the program from the top to the bottom like
>> a newspaper article.  It should start with the big concepts, and get
>> more and more detailed as your read down through the little functions.
>> Like a newspaper article, you should be able to stop reading when you
>> have gotten to the desired level of detail.

>
> That's what the headline technique is for.

Nope, sorry, your monoliths are long. You can't get the simple sense of what they are doing in a single glance. With small methods you can.

>> A function should be no longer than five or ten lines of code.  A
>> function should have no more than one indent level.  The body of each
>> if statement should call one function.  The body of each loop statement
>> should call one function.

>
> Dictated by the Vatican? Does an angry nun beat us on the tush with a
> ruler if we violate this?

Dictated by a sense of discipline and professionalism. If we claim to be professionals, we need to show it by being _neat_.



So this whole OOP vs Database debate -- this whole "oop is mad" philosophy -- seems to have come down to a preference between small partitioned methods and monolithic programs. OO people like to divide their programs into many small methods that are as self descriptive as possible. Database people like to wad a whole bunch of code into a single highly indented function. Is that what you are saying?
-- 
Robert C. Martin (Uncle Bob)  | email: unclebob_at_objectmentor.com
Object Mentor Inc.            | blog:  www.butunclebob.com
The Agile Transition Experts  | web:   www.objectmentor.com
800-338-6716                  |
Received on Wed Mar 05 2008 - 07:15:48 CET

Original text of this message