Design best practice : put state-independent methods on class side?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Design best practice : put state-independent methods on class side?

Sophie424
I have some methods that currently refer to iVars, called from other methods
of the same class. I need similar functionality in other classes, not
related by inheritance, and want to keep it DRY.

If I move the iVar references into explicit method args they can be easily
re-located and shared.

Is it then good or bad practice to take a such collection of related
state-independent methods (they don't depend on any iVar) and put them on
the class-side of some suitable class?

My options and concerns:
- I could move them up the hierarchy on the instance side but sometimes hit
single inheritance limits
- I could use traits for these but am unclear about the future of traits in
Squeak
- I can put on the class side and call easily from instance-side methods,
but is this OK practice?

Thanks,

Sophie



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methods on class side?

Marcin Tustin
What's wrong with keeping them instance-side?

On Tue, Mar 25, 2008 at 5:41 PM, itsme213 <[hidden email]> wrote:
I have some methods that currently refer to iVars, called from other methods
of the same class. I need similar functionality in other classes, not
related by inheritance, and want to keep it DRY.

If I move the iVar references into explicit method args they can be easily
re-located and shared.

Is it then good or bad practice to take a such collection of related
state-independent methods (they don't depend on any iVar) and put them on
the class-side of some suitable class?

My options and concerns:
- I could move them up the hierarchy on the instance side but sometimes hit
single inheritance limits
- I could use traits for these but am unclear about the future of traits in
Squeak
- I can put on the class side and call easily from instance-side methods,
but is this OK practice?

Thanks,

Sophie



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methods onclass side?

Sophie424
"Marcin Tustin" <[hidden email]> wrote in message

> What's wrong with keeping them instance-side?

I need similar functionality in other classes, not related by inheritance
(unless I push things really high where they do not below). MI or traits
might be options.

Sophie



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methods on class side?

David Mitchell-10
In reply to this post by Sophie424
I'm guessing you like that the class methods can be invoked easily
from the class name. What you want is a well known object.

I try to do all my work on the instance side. That's where most of the
objects are.

I wouldn't use a class object just to create a well known object. I'd
probably start with a Singleton and work from there.

You can see drawbacks and alternatives to Singleton at c2.com:

http://c2.com/cgi/wiki?SingletonPattern



On Tue, Mar 25, 2008 at 12:41 PM, itsme213 <[hidden email]> wrote:

> I have some methods that currently refer to iVars, called from other methods
>  of the same class. I need similar functionality in other classes, not
>  related by inheritance, and want to keep it DRY.
>
>  If I move the iVar references into explicit method args they can be easily
>  re-located and shared.
>
>  Is it then good or bad practice to take a such collection of related
>  state-independent methods (they don't depend on any iVar) and put them on
>  the class-side of some suitable class?
>
>  My options and concerns:
>  - I could move them up the hierarchy on the instance side but sometimes hit
>  single inheritance limits
>  - I could use traits for these but am unclear about the future of traits in
>  Squeak
>  - I can put on the class side and call easily from instance-side methods,
>  but is this OK practice?
>
>  Thanks,
>
>  Sophie
>
>
>
>  _______________________________________________
>  Beginners mailing list
>  [hidden email]
>  http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Design best practice : put state-independent methods onclass side?

Ramon Leon-5
> I'm guessing you like that the class methods can be invoked
> easily from the class name. What you want is a well known object.

Like a class?

> I wouldn't use a class object just to create a well known
> object. I'd probably start with a Singleton and work from there.

Like a class?  Classes are singletons, what do you have against using them
as such?

Ramon Leon
http://onsmalltalk.com

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Design best practice : put state-independent methods onclass side?

David Mitchell-10
(forgot to copy the list)

Classes are certainly well known, but not the only way to get a well
 known object.

 My pragmatic issue with using class side methods for Singletons is
 that it is a bunch of work to refactor the class side behavior to
 instance side later. Look at the original PWS server for a squeak
 specific example.

 My design issue is that classes are for making instances (technically
 for defining behavior of instances). Making them the building block of
 the program means that I'm giving them extra responsibility. I like to
 keep the responsibility list as small as possible.

 (Someone walked off with my Design Patterns Smalltalk Companion, which
 wrote about these issues better than I can.)



 On Tue, Mar 25, 2008 at 2:51 PM, Ramon Leon <[hidden email]> wrote:
 > > I'm guessing you like that the class methods can be invoked
 >  > easily from the class name. What you want is a well known object.
 >
 >  Like a class?
 >
 >
 >  > I wouldn't use a class object just to create a well known
 >  > object. I'd probably start with a Singleton and work from there.
 >
 >  Like a class?  Classes are singletons, what do you have against using them
 >  as such?
 >
 >  Ramon Leon
 >  http://onsmalltalk.com
 >
 >
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Design best practice : put state-independent methodsonclass side?

Ramon Leon-5
> (forgot to copy the list)
>
> Classes are certainly well known, but not the only way to get
> a well  known object.
>
>  My pragmatic issue with using class side methods for
> Singletons is  that it is a bunch of work to refactor the
> class side behavior to  instance side later. Look at the
> original PWS server for a squeak  specific example.

Maybe it used to be, but it's a simple refactoring at the moment, refactor
method >> move to instance side.  Secondly, I'll worry about later... later,
i.e. YAGNI, classes are nice singletons and it seems like useless work to
manually implement a singleton when singleton is a built in feature.

>  My design issue is that classes are for making instances
> (technically  for defining behavior of instances). Making
> them the building block of  the program means that I'm giving
> them extra responsibility. I like to  keep the responsibility
> list as small as possible.

Classes are objects too, just because most classes build instances doesn't
mean they all have to.  You might not want a class doing both, but I see no
reason it can't do one *or* the other.  Serving as a singleton works well
when it's needed and keeps code simple.  Saying a class should only create
instances seems a rather arbitrary restriction to place upon your designs.

>  (Someone walked off with my Design Patterns Smalltalk
> Companion, which  wrote about these issues better than I can.)

I'd love to see the arguments if you do recall them because so far, I just
don't agree.

Ramon Leon
http://onsmalltalk.com

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methodsonclass side?

Sophie424
In reply to this post by Ramon Leon-5
"Ramon Leon" <[hidden email]> wrote

> Like a class?  Classes are singletons, what do you have against using them
> as such?

That's my feeling too, in general.

Is my specific usage style OK -- many instance methods from many classes
calling shared state-less methods from a common class? Or does it have some
kind of code smell? Any thought on whether traits (simply mixing them in on
the instance side) would be a preferred approach to this?

Foo>>m1
  Library doX: iVar1 with: iVar2

Foo>>m2
  Library doZ: iVar2

Bar>>m3
  Library doX: iVar7 with: ivar8

Library class>>doX: anX with: aY

Library class>>doZ: aZ

- Sophie



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methodsonclass side?

Rob Rothwell
In reply to this post by Ramon Leon-5
On Tue, Mar 25, 2008 at 5:44 PM, Ramon Leon <[hidden email]> wrote:
Classes are objects too, just because most classes build instances doesn't
mean they all have to.  You might not want a class doing both, but I see no
reason it can't do one *or* the other.  Serving as a singleton works well
when it's needed and keeps code simple.  Saying a class should only create
instances seems a rather arbitrary restriction to place upon your designs.
 
As a beginner who is finally starting to do some useful [Smalltalk] work in a Healthcare organization, I am finding this conversation very interesting because I write a lot of "utility" code...mostly data "pre-processing" before it goes to the Warehouse, the Reporting engine, or to files to go out the door for required reporting to other organizations.  

Should I consider something more than just "XXFileProcessor file: aFile" if it will only ever run on one file that shows up in a directory once a day and the whole point is that there is only ONE processor?

Just interested, because in such "utility" cases I end up with all my helper methods on the class side and wonder to myself, "is this wrong?" because it feels very similar to writing subroutines in any other language...

On that note, isn't that sort of all traits really are?  A subroutine not attached to any particular object?   But with no access to object data short of resorting to things like self classPool at: #Var, which I suppose defeats the point, but darn it, I keep running into situations where multiple inheritence would be great! 

Rob Rothwell

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methodsonclass side?

Tapple Gao
On Tue, Mar 25, 2008 at 07:13:27PM -0400, Rob Rothwell wrote:

>    On Tue, Mar 25, 2008 at 5:44 PM, Ramon Leon <[hidden email]>
>    wrote:
>
>      Classes are objects too, just because most classes build instances
>      doesn't
>      mean they all have to.  You might not want a class doing both, but I see
>      no
>      reason it can't do one *or* the other.  Serving as a singleton works
>      well
>      when it's needed and keeps code simple.  Saying a class should only
>      create
>      instances seems a rather arbitrary restriction to place upon your
>      designs.
>
>    
>    As a beginner who is finally starting to do some useful [Smalltalk] work
>    in a Healthcare organization, I am finding this conversation very
>    interesting because I write a lot of "utility" code...mostly data
>    "pre-processing" before it goes to the Warehouse, the Reporting engine, or
>    to files to go out the door for required reporting to other organizations.
>    
>    Should I consider something more than just "XXFileProcessor file: aFile"
>    if it will only ever run on one file that shows up in a directory once a
>    day and the whole point is that there is only ONE processor?
>    Just interested, because in such "utility" cases I end up with all my
>    helper methods on the class side and wonder to myself, "is this wrong?"
>    because it feels very similar to writing subroutines in any other
>    language...

If it works, cool. You can always refactor later

>    On that note, isn't that sort of all traits really are?  A subroutine not
>    attached to any particular object?   But with no access to object data
>    short of resorting to things like self classPool at: #Var, which I suppose
>    defeats the point, but darn it, I keep running into situations where
>    multiple inheritence would be great!
>    Rob Rothwell

No. Traits are units of behavior that can be shared among
objects.

> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


--
Matthew Fulmer -- http://mtfulmer.wordpress.com/
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methodsonclass side?

David Mitchell-10
In reply to this post by Ramon Leon-5
>  >  My pragmatic issue with using class side methods for
>  > Singletons is  that it is a bunch of work to refactor the
>  > class side behavior to  instance side later. Look at the
>  > original PWS server for a squeak  specific example.
>
>  Maybe it used to be, but it's a simple refactoring at the moment, refactor
>  method >> move to instance side.  Secondly, I'll worry about later... later,
>  i.e. YAGNI, classes are nice singletons and it seems like useless work to
>  manually implement a singleton when singleton is a built in feature.
>
I'll have to try it with a real example (like make PWS support
multiple apps) before I change my practice.

>
>  >  My design issue is that classes are for making instances
>  > (technically  for defining behavior of instances). Making
>  > them the building block of  the program means that I'm giving
>  > them extra responsibility. I like to  keep the responsibility
>  > list as small as possible.
>
>  Classes are objects too, just because most classes build instances doesn't
>  mean they all have to.  You might not want a class doing both, but I see no
>  reason it can't do one *or* the other.  Serving as a singleton works well
>  when it's needed and keeps code simple.  Saying a class should only create
>  instances seems a rather arbitrary restriction to place upon your designs.

Singleton-ness is quite easy to code (and with class-instance
variables can often be coded once and inherited). So it isn't worth it
to me to mix-in my new object along with the class creation stuff from
Behavior. My favorite superclass is Object (or my own, domain-specific
superclass).

Do one thing and do it well is a great way to tease apart and find new
objects. When the real world gives me a new problem, time for a new
object. Object-oriented programming, after all.

You can do stateless, functional programming in Smalltalk. Some of the
most beautiful parts of Smalltalk use a functional approach.

But loading a bunch of class side utility methods on some stateless
Class doesn't feel like good object-oriented design.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: Design best practice : put state-independent methodsonclass side?

Ralph Johnson
In reply to this post by Sophie424
On Tue, Mar 25, 2008 at 5:51 PM, itsme213 <[hidden email]> wrote:

>  Is my specific usage style OK -- many instance methods from many classes
>  calling shared state-less methods from a common class? Or does it have some
>  kind of code smell? Any thought on whether traits (simply mixing them in on
>  the instance side) would be a preferred approach to this?
>
>  Foo>>m1
>   Library doX: iVar1 with: iVar2
>
>  Foo>>m2
>   Library doZ: iVar2
>
>  Bar>>m3
>   Library doX: iVar7 with: ivar8
>
>  Library class>>doX: anX with: aY
>
>  Library class>>doZ: aZ

My gut reaction is that you are missing an abstraction.  I can't tell
for sure, because you don't give enough details.  Why don't you put
this code in the class of the ivars?  Maybe the ivars are integers or
strings, and they should be a value object.  Unless you explain more
about the problem domain, there is no way for me to tell.

-Ralph
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Design best practice : put state-independent methodsonclass side?

stephane ducasse
In reply to this post by Ramon Leon-5
Hi ramon

I agree with you. Now just without reading the complete thread, and  
for more complex situations
here is my guideline to use singleton:
I use one when I need one object "at a time" and not a single access  
point.

Often people confuse time and access. If you can get rid of a  
singleton just by adding an iv to you domain
and adding a reference to point to your "singleton" then it was not a  
singleton but a global object access.
Is it difficult to write that in a mail, a good singleton is a object  
representing really the fact that you
cannot have two objects of the same class at the same time.
Stef



On Mar 25, 2008, at 10:44 PM, Ramon Leon wrote:

>> (forgot to copy the list)
>>
>> Classes are certainly well known, but not the only way to get
>> a well  known object.
>>
>> My pragmatic issue with using class side methods for
>> Singletons is  that it is a bunch of work to refactor the
>> class side behavior to  instance side later. Look at the
>> original PWS server for a squeak  specific example.
>
> Maybe it used to be, but it's a simple refactoring at the moment,  
> refactor
> method >> move to instance side.  Secondly, I'll worry about  
> later... later,
> i.e. YAGNI, classes are nice singletons and it seems like useless  
> work to
> manually implement a singleton when singleton is a built in feature.
>
>> My design issue is that classes are for making instances
>> (technically  for defining behavior of instances). Making
>> them the building block of  the program means that I'm giving
>> them extra responsibility. I like to  keep the responsibility
>> list as small as possible.
>
> Classes are objects too, just because most classes build instances  
> doesn't
> mean they all have to.  You might not want a class doing both, but I  
> see no
> reason it can't do one *or* the other.  Serving as a singleton works  
> well
> when it's needed and keeps code simple.  Saying a class should only  
> create
> instances seems a rather arbitrary restriction to place upon your  
> designs.
>
>> (Someone walked off with my Design Patterns Smalltalk
>> Companion, which  wrote about these issues better than I can.)
>
> I'd love to see the arguments if you do recall them because so far,  
> I just
> don't agree.
>
> Ramon Leon
> http://onsmalltalk.com
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners