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.
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…
···
On Fri, 2003-09-12 at 14:03, Philip Mak wrote:
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
I would say:
server = Httpd.new(:listen_port => 80)
server.run
or
Httpd.new(:listen_port => 80) do |server|
…
end
If there’s a block given, then both run it and stop it when the block
completes.
-austin
···
On Sat, 13 Sep 2003 05:03:51 +0900, Philip Mak wrote:
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.
–
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.09.12
* 18.25.22
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
···
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.