Re: reduce redundant code




"MP" <NoSpam@xxxxxxxxxx> wrote in message
news:OnSAUgXfIHA.3400@xxxxxxxxxxxxxxxxxxxxxxx
Is there any merit to the following idea?

I find myself repeating lines similar to below in many routines
the idea being I may want to process all objects of just objects who have
certain property or whose "container" or "owner" or "child" has certain
property(ies)

Function RunOnCertainObjects(obj, x, y, z, cx, dx)
If TypeOf obj Is objType1 Or _
TypeOf obj Is objType2 Then
If objA.Propa = y Then
If objB.Propb = z Then
If objC.Propc = Cx Then
If objD.Propd = Dx Then
'do something


I was considering idea of creating cFilterObject class to encapsulate and
"standardize" property and object type tests

cFilterObject
Public Property Get/Let Propa() As Double
Public Property Get/Let Propb() As Long
Public Property Get/Let Propc() As String
Public Property Get ValidObject(obj As Object) As Boolean
for each test in mcolValidObjects
if TypeName (obj) = test then
ValidObject = True
Exit For
End if
Next
End Property

then the funcion call becomes
dim oFilter as cFilterObject
'set oFilter.properties

Function RunOnCertainObjects(obj, oFilter)
If oFilter.ValidObject(obj) Then
If objA.Propa = oFilter.Propa Then
If objB.Propb = oFilter.Propb Then
If objC.Propc = oFilter.Propc Then
If objD.Propd = oFilter.Propd Then
'do something

thanks for any comments
mark


Your actually chewing on several things at once here.
Get Fowler's book "Refactoring: Improving the Design of Existing Code".
Impossible to give a specific answer as it depends on the over-all object of
your design.
Hints: (???? no idea of this is useful or not. <g>)
Whenever you find yourself with a ton of parameters, think object.
If only certain objects of a specific type support something, why not it let
the object handle it or ignore it?
"Filters" are usually only concerned with bags of stuff. Make it a
propery/method of the Bag.

-ralph



.


Loading