From: "Sean O'Dell" Date: 2003-09-13T05:38:46+09:00 Subject: Re: Should "new" methods do anything? 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