How to eliminate duplicate code?

Writing and using Classes in Dyalog APL

How to eliminate duplicate code?

Postby paulmansour on Sat Sep 30, 2017 4:35 am

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?
paulmansour
 
Posts: 341
Joined: Fri Oct 03, 2008 4:14 pm

Re: How to eliminate duplicate code?

Postby gil on Sun Oct 01, 2017 7:13 am

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.
gil
 
Posts: 68
Joined: Mon Feb 15, 2010 12:42 am

Re: How to eliminate duplicate code?

Postby paulmansour on Sun Oct 01, 2017 1:57 pm

Gil, thanks, that's great!

I had no idea you could do that!
paulmansour
 
Posts: 341
Joined: Fri Oct 03, 2008 4:14 pm


Return to Object Oriented Programming

Who is online

Users browsing this forum: No registered users and 1 guest