Splitting source code

L

Luke Worth

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)
 
A

Austin Ziegler

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 * (e-mail address removed)
* Alternate: (e-mail address removed)
 
G

Gavin Kistner

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

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--
 
D

Daniel Brockman

Gavin Kistner said:
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?
 
G

Gavin Kistner

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*
 
A

Austin Ziegler

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 * (e-mail address removed)
* Alternate: (e-mail address removed)
 
L

Luke Worth

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
 
A

Austin Ziegler

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 * (e-mail address removed)
* Alternate: (e-mail address removed)
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top