Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Short routines matter more in OO?

by Anonymous Monk
on Oct 13, 2003 at 05:17 UTC ( #298766=note: print w/ replies, xml ) Need Help??


in reply to Short routines matter more in OO?

I don't think it is so much a matter of short routines mattering more in OO as it is that short routines make more sense and are more natural. If am procedural coding and following a rule of thumb that each routine should do one thing (and do it well), and if a particular one-thing is even moderately complex then I can easily wind up with 20 line, 40 line, or even 100 line routines. Iff that code is tightly integrated and really involved in doing just one thing, then even the 100 line routine flows naturally.

Some people might find several seemingly logical chunks of that 100 line routine and pull them into their own routines giving say a 30 line routine with half a dozen 10-20 line helper routines. This is usually false complexity reduction. The code is highly integrated, and all we've done is push the complexity around a smattering of highly coupled routines, rather than leaving it in a single logical flow (we've probably raised the overall complexity in our attempt to reduce local complexity). It is *often* (not always) a mistake to take a nice integrated 100 line routine and break it into 10 coupled routines (the double meaning of 'break' is intentional).

In the OO world we live with highly coupled code all the time. But in OO it is an organized coupling that guides the design from the beginning, leading to a multitude of short methods in a class that rely on each other as well as state. It is not simply a way of collecting and grouping coupled code, modules (or packages, or libraries, etc) can do that, it is a way of structuring and organizing the coupling in a manner that relieves the programmer of having to manage it. Thus, not only are short methods more natural, very long methods are *often* (not always) indicative of something amiss in the design.

But all this is but one viewpoint, and simplified for expedience.


Comment on Re: Short routines matter more in OO?
Re: Re: Short routines matter more in OO?
by tilly (Archbishop) on Oct 13, 2003 at 14:30 UTC
    If this is but one viewpoint, then it is a viewpoint that I like hearing from. :-)

    I will have to think about that. I've found that code where I setting up a lot of handlers (often with closures) that my function length goes back up because one function creates several closely related small ones. Thinking about that in terms of managing coupling could explaing why I'm naturally inclined to group things the way that I do.

    I also had a vaguely related thought at Pass by reference vs globals.

      If this is but one viewpoint, then it is a viewpoint that I like hearing from. :-)
      I still have much thinking to do with regards to this notion. But I do think one of OO's non-explicitly recognized advantages is that of managing coupling. The object is an interface to a set of coupled code, relieving both the object writer and the object user from having to manage that aspect of code complexity themselves.

      And, one of OO's disadvantages is not recognizing this explicitly, because we then we could have much more meaningful discussions about such controversies as: Are getter/setter methods evil when used from outside the object? from inside the object? Should objects be divorced from their UI via a controller? encapsulate their UI? or provide proxy UI objects? How do we reconcile full encapsulation with inheritance?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://298766]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-11-28 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (195 votes), past polls