Proc objects and Maintainability - a design note to self

J

John Carter

So I use a couple of proc objects around my system.

Sort of nice in the sense that they can tell me where they were create
(File/Line number)

Sort of nice in the sense that they parcel up a bit of lexical scope
and allow me to use it elsewhen.

But I'm starting not to like them from a long term maintenance point
of view.

They're opaque. What was that scope that got parcelled? Tell me more
about this proc instance? What is the responsibility of this
particular instance? Pretty print what it knows so I can debug it...

So now I'm gently and quietly dropping them out of my system instead
of doing something like...

proc_obj = Proc.new{|a,b,c| doStuffWith(a,b,c,andD,andE,andFfromMyLexicalScope)}

I'm refactoring that to....

class SensibleNameProcObject
def initialize( d, e, f)
@d, @e, @f = d, e, f
end

def to_s
"#{self.class}:The dowhop is #{@d} for egg #{@e} on fries #{@f}"
end

def call( a, b, c)
doStuffWith(a,b,c,@d, @e, @f)
end
end

proc_obj = SensibleNameProcObj.new( andD,andE,andFfromMyLexicalScope)

More lines of code to define, but allows me more leeway to expand
things, and thanks to duck typing, I don't (unless I want to) have to
change a single byte of client code.

Proc objects are a handy shorthand... but like many handy shorthands,
sometimes in the long term you replace them with a long hand version
for maintainability.



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
K

Kalman Noel

John Carter:
proc_obj = Proc.new{|a,b,c| doStuffWith(a,b,c,andD,andE,andFfromMyLexicalScope)}

I'm refactoring that to....

class SensibleNameProcObject
def initialize( d, e, f)
@d, @e, @f = d, e, f
end

def to_s
"#{self.class}:The dowhop is #{@d} for egg #{@e} on fries #{@f}"
end

def call( a, b, c)
doStuffWith(a,b,c,@d, @e, @f)
end
end

This makes me think that a Ruby programmer should maybe use the word »call«
in several cases where you would use »run« or »start« or »process« in
other languages. Provided that »call« makes some sense in a specific case,
it will line up beautifully with the core library. »alias [] call« may
come in handy, too.

Kalman
 
G

Gary Wright

This makes me think that a Ruby programmer should maybe use the =20
word =BBcall=AB
in several cases where you would use =BBrun=AB or =BBstart=AB or = =BBprocess=AB in
other languages. Provided that =BBcall=AB makes some sense in a =20
specific case,
it will line up beautifully with the core library. =BBalias [] call=AB = may
come in handy, too.

It might also make sense to define #to_proc as in:

class Extra
def initialize(i)
@i =3D i
end
def call(a)
a + @i
end
def to_proc
lambda {|a| call(a) }
end
end

add5 =3D Extra.new(5)

[1,2,3,4].map &add5 # [6,7,8,9]


Gary Wright
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top