[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Coding recommendations from you

dkmd_nielsen

7/3/2006 3:19:00 PM

I have a base class into which I'm coding a number of my companies
standard business rules into. This class is used time and time again
amongs all the other processing classes. There are multiple fields
that have a static number of values. For example, something called the
house/rental indicator can only have values of H|R|P|F|S|<blank>.

The first suggestion I'm seeking is how to properly code for those
values. I've started by coding the following constants, followed by
the validation routine. I would like the individual values available
for testing (IF X=House, etal...), but I also wanted all of them in a
group so the field presented can tested in one function (.index). What
do you suggest?

# List type acceptable values
House = 'H' # House list indicator
Rental = 'R' # Rental list indicator
Purge = 'P' # Purge list indicator
Flag = 'F' # Flag list indicator
Seed = 'S' # Seed list indicator
ValidHrInd = Flag+House+Purge+Rental+Seed

def Standards.valid_hrind?(id)
return true if (id.length == 1 && ValidHrInd.index(id))
false
end


On the heels of that is the second recommendation I am seeking. This
standards class is used over and over again. I was hoping that I could
code the require 'standards.rb' into each class that will require it.
This way, if I have a module that needs multiple classes and some of
them (or all of them) require the standards.rb, then the classes have
taken care of their own requirements. But with the constants coded in
standards.rb, I get warning messages that the constants are being
reassigned for each require 'standards.rb'. What is the best way to
have a class define its own requirements?

== module DataCard.rb ==
require 'standards.rb'
class DataCard
....
end

== module Project.rb ==
require 'standards.rb'
class Project
....
end


Thanks for your time and advice,
dvn

2 Answers

Robert Klemme

7/3/2006 3:29:00 PM

0

dkmd_nielsen@hotmail.com wrote:
> I have a base class into which I'm coding a number of my companies
> standard business rules into. This class is used time and time again
> amongs all the other processing classes. There are multiple fields
> that have a static number of values. For example, something called the
> house/rental indicator can only have values of H|R|P|F|S|<blank>.
>
> The first suggestion I'm seeking is how to properly code for those
> values. I've started by coding the following constants, followed by
> the validation routine. I would like the individual values available
> for testing (IF X=House, etal...), but I also wanted all of them in a
> group so the field presented can tested in one function (.index). What
> do you suggest?
>
> # List type acceptable values
> House = 'H' # House list indicator
> Rental = 'R' # Rental list indicator
> Purge = 'P' # Purge list indicator
> Flag = 'F' # Flag list indicator
> Seed = 'S' # Seed list indicator
> ValidHrInd = Flag+House+Purge+Rental+Seed
>
> def Standards.valid_hrind?(id)

You can omit the duplicate test. This smells like premature
optimization. Only do this if this method imposes a performance problem.

> return true if (id.length == 1 && ValidHrInd.index(id))
> false
> end

Why don't you just use symbols for that?

VALID_HR_INDICATORS = [:H, :R, :P, :F, :S].freeze

# this method is barely worth defining
def self.valid_hr_indicator? ind
VALID_HR_INDICATORS.include? ind
end

Another option:
VALID_HR_INDICATORS = [
HOUSE = 'H',
RENTAL = 'R',
PURGE = 'P',
FLAG = 'F',
SEED = 'S',
].freeze

(test method like above)

Other than that there are some implementations of the enum pattern in
the RAA if you need more fancy stuff.

> On the heels of that is the second recommendation I am seeking. This
> standards class is used over and over again. I was hoping that I could
> code the require 'standards.rb' into each class that will require it.
> This way, if I have a module that needs multiple classes and some of
> them (or all of them) require the standards.rb, then the classes have
> taken care of their own requirements. But with the constants coded in
> standards.rb, I get warning messages that the constants are being
> reassigned for each require 'standards.rb'. What is the best way to
> have a class define its own requirements?

Um, a file is required at most once - so you normally cannot have this
warning. What do you really do in your code?

> == module DataCard.rb ==
> require 'standards.rb'
> class DataCard
> ...
> end
>
> == module Project.rb ==
> require 'standards.rb'
> class Project
> ...
> end

Btw, you can simply write "require 'standards'". You don't need the
file extension.

Kind regards

robert

dkmd_nielsen

7/3/2006 4:33:00 PM

0

Thank you. I like this implementation:

VALID_HR_INDICATORS = [
HOUSE = 'H',
RENTAL = 'R',
PURGE = 'P',
FLAG = 'F',
SEED = 'S',
].freeze

It provides readability and future maintainability. Using :H and :R
don't really convey the meaning in code that I am looking for, where
HOUSE and RENTAL do. Beside, I was not getting a crystal clear picture
of how to use symbols from the documentation and online available.

With regards to the classes, here is what is happening. I have
developed a script for users to use. Users, as you know, are not
programmers. So I want everything to be as clear as possible.


require 'Standards' # would like to eliminate the need for this
require 'FLCXLS' # requires Standards, and it requires Card which also
requires standards
require 'Project' # requires Standards
require 'net/ftp' # would like to bury this in FLCXLS

# setup project details
prj=Project.new
prj.client="LEN119"
prj.proj="66354"
prj.user="DONN"
prj.run="01"
prj.dept="IM"
prj.networkfolder=("K:\\LEN119 Lenox Collections\\List
Processing\\projects\\66354 SPRING TREASURY #5")

# describe xls spreadsheet
xls = FLCXLS.new
xls.Keycode 'd'
xls.Description 'e'
xls.Type 'x'
xls.Priority 'w'
xls.Subtype 'y'

# Changes to apply before building
xls.ChangeFromTo("collection","clctn")
xls.ChangeFromTo("catalog","catlg")
xls.ChangeFromTo("touch of class","toc")

# Build FLC/PRTY cards and write them to the specified destination
# (client's network folder)
xls.BuildFlcCards(prj.networkfolder,"#{prj.deptrun}_flc_cards.txt")
xls.BuildPrtyCards(prj.networkfolder,"#{prj.deptrun}_prty_cards.txt")


The proper implementation would be to have the require 'Standards' in
the Project.rb, FLCXLS.rb, and Card.rb. Each has its own requirement
of Standards, and buries that need within itself. There is not reason
for requiring the use of "require Standards" in this module. It should
be transparent. But the warning messages hinder that kind of
implementation...unless I'm not implementing it correctly.

In my opinion, I should be able to get this modules requirements down
to just "require 'Project' and require 'FLCXLS'. The others should be
hidden.

Thanks again for everything.
dvn

Robert Klemme wrote:
> dkmd_nielsen@hotmail.com wrote:
> > I have a base class into which I'm coding a number of my companies
> > standard business rules into. This class is used time and time again
> > amongs all the other processing classes. There are multiple fields
> > that have a static number of values. For example, something called the
> > house/rental indicator can only have values of H|R|P|F|S|<blank>.
> >
> > The first suggestion I'm seeking is how to properly code for those
> > values. I've started by coding the following constants, followed by
> > the validation routine. I would like the individual values available
> > for testing (IF X=House, etal...), but I also wanted all of them in a
> > group so the field presented can tested in one function (.index). What
> > do you suggest?
> >
> > # List type acceptable values
> > House = 'H' # House list indicator
> > Rental = 'R' # Rental list indicator
> > Purge = 'P' # Purge list indicator
> > Flag = 'F' # Flag list indicator
> > Seed = 'S' # Seed list indicator
> > ValidHrInd = Flag+House+Purge+Rental+Seed
> >
> > def Standards.valid_hrind?(id)
>
> You can omit the duplicate test. This smells like premature
> optimization. Only do this if this method imposes a performance problem.
>
> > return true if (id.length == 1 && ValidHrInd.index(id))
> > false
> > end
>
> Why don't you just use symbols for that?
>
> VALID_HR_INDICATORS = [:H, :R, :P, :F, :S].freeze
>
> # this method is barely worth defining
> def self.valid_hr_indicator? ind
> VALID_HR_INDICATORS.include? ind
> end
>
> Another option:
> VALID_HR_INDICATORS = [
> HOUSE = 'H',
> RENTAL = 'R',
> PURGE = 'P',
> FLAG = 'F',
> SEED = 'S',
> ].freeze
>
> (test method like above)
>
> Other than that there are some implementations of the enum pattern in
> the RAA if you need more fancy stuff.
>
> > On the heels of that is the second recommendation I am seeking. This
> > standards class is used over and over again. I was hoping that I could
> > code the require 'standards.rb' into each class that will require it.
> > This way, if I have a module that needs multiple classes and some of
> > them (or all of them) require the standards.rb, then the classes have
> > taken care of their own requirements. But with the constants coded in
> > standards.rb, I get warning messages that the constants are being
> > reassigned for each require 'standards.rb'. What is the best way to
> > have a class define its own requirements?
>
> Um, a file is required at most once - so you normally cannot have this
> warning. What do you really do in your code?
>
> > == module DataCard.rb ==
> > require 'standards.rb'
> > class DataCard
> > ...
> > end
> >
> > == module Project.rb ==
> > require 'standards.rb'
> > class Project
> > ...
> > end
>
> Btw, you can simply write "require 'standards'". You don't need the
> file extension.
>
> Kind regards
>
> robert