Should "new" methods do anything?

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Should "new" methods do anything?

    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.

    Philip Mak Guest

  2. Similar Questions and Discussions

    1. Proj cannot run on LCDS 2.6 ES due to "Unable to resolveresource bundle "datamanagement" for locale "en_US"
      hi, all, We have developped an application on Flex Build 3 (run successfully), but failed when we try to deploy it on Tomcat with LCDS 2.5 ES...
    2. Using Acrodat SDK methods with VB ("PDEText ")
      Has anyone used "PDEText PDETextCreate" methods in VB. If so, do you have an example and the modules for a VB.NET app? I've done C, but a long-long...
    3. Log::Dispatch - How to "die" a script after all other methods are called???
      I want to use Log::Dispatch to log activity using a number of methods (screen, files, e-mail, etc). If I encounter a certain error threashold...
    4. CFINPUT type="radio" w/ "value" requires "label"
      On a Flash form, when you specify type='radio' and value='whatever', the value of the 'value' attribute will be displayed as a label if no 'label'...
    5. "Start" "Program" "Menu" list is empty
      For what ever reason my list of installed programs in my "Start" "Programs" menu is empty. Anyone know how to restore the list. Thanks for your...
  3. #2

    Default Re: Should "new" methods do anything?


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

    On Fri, 2003-09-12 at 14:03, Philip Mak wrote:
    > 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+MNcRestyuLSw CgoNzX
    VsuKjF25CVlcnH0zm4ll288=
    =Ie5W
    -----END PGP SIGNATURE-----

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


    Aredridel Guest

  4. #3

    Default Re: Should "new" methods do anything?

    From: Philip Mak [mailto:pmak@aaanime.net]
    > Should "new" methods actually do anything, or should they only perform
    > initialization?
    I prefer the initialize only approach. If you tried to subclass Httpd, then
    running from initialize might start the server before the subclass has
    completed initialization.

    And if you want streamlined, Aredridel suggested chaining run.

    --
    -- Jim Weirich / Compuware
    -- FWP Capture Services
    -- Phone: 859-386-8855

    Weirich, James Guest

  5. #4

    Default 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

    Sean O'Dell Guest

  6. #5

    Default Re: Should "new" methods do anything?

    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
    Simon Strandgaard Guest

  7. #6

    Default Re: Should "new" methods do anything?

    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.
    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
    --
    austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.09.12
    * 18.25.22




    Austin Ziegler Guest

  8. #7

    Default Re: Should "new" methods do anything?

    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


    Gavin Sinclair Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139