Should "new" methods do anything?

P

Philip Mak

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

Aredridel

--=-xt6+981JgEI+5w0faYqy
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

A programming style question:
=20
Should "new" methods actually do anything, or should they only perform
initialization?
=20
For example, let's say I have a class called "Httpd" which runs an
HTTP server. Should its interface be like this:
=20
server =3D Httpd.new:)listen_port =3D> 80) # Initialize the server
server.run # Run it
=20
Or like this?
=20
Httpd.new:)listen_port =3D> 80) # Initialize and run the server

Let it return self.

Then:

Httpd.new:)listen_port =3D> 80).run
=20
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...


--=-xt6+981JgEI+5w0faYqy
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQA/YicstP09exA3hooRAmRhAKDKuJR5RYYlaw0M1+MNcRestyuLSwCgoNzX
VsuKjF25CVlcnH0zm4ll288=
=Ie5W
-----END PGP SIGNATURE-----

--=-xt6+981JgEI+5w0faYqy--
 
S

Sean O'Dell

Philip said:
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
 
S

Simon Strandgaard

Philip said:
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
 
G

Gavin Sinclair

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,301
Latest member
SusannaCgx

Latest Threads

Top