Splitting source code

Discussion in 'Ruby' started by Luke Worth, Jul 12, 2005.

  1. Luke Worth

    Luke Worth Guest

    Hi. Coming from a c++/java background, i'm having a bit of trouble in
    deciding how to split up source code. I currently have this:

    #----program_tree.rb
    require 'gtk2'
    require 'program_tree_populate'

    module HSGui
    # GtkTreeStore is the generic GtkTreeModel implementation
    class ProgramTree < Gtk::TreeStore
    def initialize(source_code)
    super(String)
    populate(source_code)
    end
    end
    end

    #----program_tree_populate.rb
    require 'gtk2'
    module HSGui
    class ProgramTree < Gtk::TreeStore
    def populate(source_code)
    #etc...
    end
    end
    end


    This seems slightly dodgy to me... Would anyone do this some other way?
    (like define in a module then include the module)
    --
    Luke Worth
     
    Luke Worth, Jul 12, 2005
    #1
    1. Advertising

  2. On 7/11/05, Luke Worth <> wrote:
    > Hi. Coming from a c++/java background, i'm having a bit of trouble in
    > deciding how to split up source code. I currently have this:

    ...
    > This seems slightly dodgy to me... Would anyone do this some other way?
    > (like define in a module then include the module)


    I personally think that's very dodgy. In general, most people define
    Ruby in such a way as to logically place that which belongs together
    together. That may mean that there is more than one class defined in a
    single file. I find it very rare to find that a single class is
    defined over multiple files, except in the possible case of a C
    extension.

    -austin
    --=20
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Jul 12, 2005
    #2
    1. Advertising

  3. --Apple-Mail-4--1044021463
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed

    On Jul 11, 2005, at 10:29 PM, Austin Ziegler wrote:
    > I find it very rare to find that a single class is
    > defined over multiple files, except in the possible case of a C
    > extension.


    FWIW, I did this something like this with my ValidForm library. A
    single file defined a base set of functionality for many classes, and
    then a second file could optionally be included that added a custom
    #to_html method to every one of those classes.

    If I had to do it again, I might have subclassed all the originals in
    a separate file...but then again I might not have.

    It feels sketchy-but-acceptable to me to have FileB require FileA to
    be meaningful, as long as FileA can stand on its own. It feels
    pointless to me to have FileA and FileB mutually require each other
    to be useful, for if that's the case, why not have them as one file?

    --Apple-Mail-4--1044021463--
     
    Gavin Kistner, Jul 12, 2005
    #3
  4. Gavin Kistner <> writes:

    > It feels sketchy-but-acceptable to me to have FileB require FileA to
    > be meaningful, as long as FileA can stand on its own.


    Isn't this a typical and completely normal dependency?

    --
    Daniel Brockman <>
     
    Daniel Brockman, Jul 12, 2005
    #4
  5. On Jul 12, 2005, at 10:15 AM, Daniel Brockman wrote:
    >> It feels sketchy-but-acceptable to me to have FileB require FileA to
    >> be meaningful, as long as FileA can stand on its own.
    >>

    >
    > Isn't this a typical and completely normal dependency?


    Damn, I was afraid someone might notice that :)

    Yes, it is. Yet I can't figure out what the difference is between my
    Bar class requiring Foo from another file to make sense, versus my
    Bar class requiring other pieces of Bar defined in another file.
    Something smells slightly off (and yet it's what I was describing
    that I did previously). *shrug*
     
    Gavin Kistner, Jul 13, 2005
    #5
  6. On 7/12/05, Daniel Brockman <> wrote:
    > Gavin Kistner <> writes:
    >> It feels sketchy-but-acceptable to me to have FileB require FileA to
    >> be meaningful, as long as FileA can stand on its own.

    > Isn't this a typical and completely normal dependency?


    IMO, it's not what was originally described by the OP, which was something =
    like:

    A.rb:
    require 'B'
    class Foo; def initialize; process(); end; end

    B.rb:
    class Foo; def process(); end; end

    IMO, classes should -- as much as is possible -- have all of their
    definition in a single location, using alternate locations only as
    necessary.

    -austin
    --=20
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Jul 13, 2005
    #6
  7. Luke Worth

    Luke Worth Guest

    > IMO, classes should -- as much as is possible -- have all of their
    > definition in a single location, using alternate locations only as
    > necessary.

    Righto then, it's just that the function was originally 80 lines long
    and took up room in my editor. its now 30 (original way was stupid).
    If this were c++, i could have had

    file a: class x { void process(); }

    file b: void x::process() {}

    and was wondering if a similar thing is possible with ruby - apparently
    not.
    Perhaps i need to use an editor that can fold methods instead of
    emacs :p
    --
    Luke Worth
     
    Luke Worth, Jul 13, 2005
    #7
  8. On 7/12/05, Luke Worth <> wrote:
    >> IMO, classes should -- as much as is possible -- have all of
    >> their definition in a single location, using alternate locations
    >> only as necessary.

    > Righto then, it's just that the function was originally 80 lines
    > long and took up room in my editor. its now 30 (original way was
    > stupid). If this were c++, i could have had
    >=20
    > file a: class x { void process(); }
    >=20
    > file b: void x::process() {}
    >=20
    > and was wondering if a similar thing is possible with ruby -
    > apparently not. Perhaps i need to use an editor that can fold
    > methods instead of emacs :p


    Sure, it's possible. It's just not necessasrily advisable. In the
    case of C++, file A would have been "x.h", and Ruby doesn't really
    have the concept of an include file (only a required resource, which
    currently maps to a file).

    Most of my project files are several *hundred* lines long, but I try
    to keep each method as short as possible, and only tend to include
    that which is necessary in a given file.

    I also tend to treat Ruby a little like Java, defining one "logical"
    class per file (like Ruwiki and PDF::Writer), only defining multiple
    classes in a file when they are subclasses of Exception related to
    the main class of the file. On the other hand, in
    Archive::Tar::Minitar, almost everything is defined in a single
    class.

    -austin
    --=20
    Austin Ziegler *
    * Alternate:
     
    Austin Ziegler, Jul 13, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    427
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    442
  3. John Dibling
    Replies:
    0
    Views:
    417
    John Dibling
    Jul 19, 2003
  4. Ben Pfaff

    Re: Splitting a source code file

    Ben Pfaff, Jun 2, 2010, in forum: C Programming
    Replies:
    27
    Views:
    673
    Richard Bos
    Jun 9, 2010
  5. Gene

    Re: Splitting a source code file

    Gene, Jun 2, 2010, in forum: C Programming
    Replies:
    5
    Views:
    279
    Tim Rentsch
    Jun 19, 2010
Loading...

Share This Page