[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Should "new" methods do anything?

Philip Mak

9/12/2003 8:04:00 PM

A programming style question:

Should "new" methods actually do anything, or should they only perform
initialization?

For example, let's say I have a class called "Httpd" which runs an
HTTP server. Should its interface be like this:

server = Httpd.new(:listen_port => 80) # Initialize the server
server.run # Run it

Or like this?

Httpd.new(:listen_port => 80) # Initialize and run the server

The latter way is more streamlined, but feels bad for some reason that
I can't quite put into words right now.

4 Answers

Aria Stewart

9/12/2003 8:07:00 PM

0

On Fri, 2003-09-12 at 14:03, Philip Mak wrote:
> A programming style question:
>
> Should "new" methods actually do anything, or should they only perform
> initialization?
>
> For example, let''s say I have a class called "Httpd" which runs an
> HTTP server. Should its interface be like this:
>
> server = Httpd.new(:listen_port => 80) # Initialize the server
> server.run # Run it
>
> Or like this?
>
> Httpd.new(:listen_port => 80) # Initialize and run the server

Let it return self.

Then:

Httpd.new(:listen_port => 80).run

>
> The latter way is more streamlined, but feels bad for some reason that
> I can''t quite put into words right now.

Well, perhaps you want to wait to run until you''ve checked some
things...

Sean O'Dell

9/12/2003 8:21:00 PM

0

Philip Mak wrote:
> A programming style question:
>
> Should "new" methods actually do anything, or should they only perform
> initialization?
>
> For example, let''s say I have a class called "Httpd" which runs an
> HTTP server. Should its interface be like this:
>
> server = Httpd.new(:listen_port => 80) # Initialize the server
> server.run # Run it
>
> Or like this?
>
> Httpd.new(:listen_port => 80) # Initialize and run the server
>
> The latter way is more streamlined, but feels bad for some reason that
> I can''t quite put into words right now.

I don''t think it will cause any harm to your program, but to me it''s
non-intuitive as to what "new" does. I don''t think many people would
expect that simply creating the server also launches it.

In those cases, I usually make a class method to combine the
functionality, i.e.:

server = Httpd::launch_server(:listen_port => 80)

.... and keep the class like this:

class Httpd
def initialize(listen_port)
# init only
end

def run
# launch the server
end

Httpd::launch_server(listen_port)
server = Https.new(:listen_port = listen_port)
server.run
return server
end
end


Sean O''Dell

Simon Strandgaard

9/12/2003 10:08:00 PM

0

On Fri, 12 Sep 2003 21:21:01 +0000, Sean O''Dell wrote:

> Philip Mak wrote:
>> A programming style question:
>>
>> Should "new" methods actually do anything, or should they only perform
>> initialization?
[snip]
>
> server = Httpd::launch_server(:listen_port => 80)
[snip]
>
> Httpd::launch_server(listen_port)
> server = Https.new(:listen_port = listen_port)
> server.run
> return server
> end


Yes, Factory methods is nice.. it can also look like this :-)

Httpd.launch(options=nil)
Https.new(options).run
end

--
Simon Strandgaard

Gavin Sinclair

9/13/2003 12:57:00 AM

0

On Saturday, September 13, 2003, 6:03:51 AM, Philip wrote:

> A programming style question:

> Should "new" methods actually do anything, or should they only perform
> initialization?

> For example, let''s say I have a class called "Httpd" which runs an
> HTTP server. Should its interface be like this:

> server = Httpd.new(:listen_port => 80) # Initialize the server
> server.run # Run it

> Or like this?

> Httpd.new(:listen_port => 80) # Initialize and run the server

> The latter way is more streamlined, but feels bad for some reason that
> I can''t quite put into words right now.

The method is "doing too much", a common problem. You may want to
pass the server to another method which runs it, or not, based on some
other things.

Gavin