Page 1 of 1

How to eliminate duplicate code?

Posted: Sat Sep 30, 2017 4:35 am
by paulmansour
Consider the following code which specifies three properties in a class:

Code: Select all

Property MyPropA
:Access Public
 ∇ z←get x
   z←GetProperty x
 ∇
 ∇ set x;z
   z←SetProperty x
 ∇
:EndProperty

:Property MyPropB
:Access Public
 ∇ z←get x
   z←GetProperty x
 ∇
 ∇ set x;z
   z←SetProperty x
 ∇
:EndProperty

:Property MyPropC
:Access Public
 ∇ z←get x
   z←GetProperty x
 ∇
 ∇ set x;z
   z←SetProperty x
 ∇
:EndProperty


The work required for getting and setting each property is identical, and thus extracted into two common sub functions, GetProperty and SetProperty.

However, despite this, the code is still massively repetitive, which makes me feel like writing it is a colossal waste of time. How can I get rid of it? I guess I have to write a function which generates the class script and then fixes it, is that the only way?

Re: How to eliminate duplicate code?

Posted: Sun Oct 01, 2017 7:13 am
by gil
If you know the names of all the properties at the time of designing the class you can simply provide a list of names:

Code: Select all

:Property MyPropA,MyPropB,MyPropC
:Access Public
 ∇ z←get x
   z←GetProperty x
 ∇
 ∇ set x;z
   z←SetProperty x
 ∇
:EndProperty


      x.Name
will contain the name of the property that is accessed.

Re: How to eliminate duplicate code?

Posted: Sun Oct 01, 2017 1:57 pm
by paulmansour
Gil, thanks, that's great!

I had no idea you could do that!