How to eliminate duplicate code?
3 posts
• Page 1 of 1
How to eliminate duplicate code?
Consider the following code which specifies three properties in a class:
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?
- 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: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: How to eliminate duplicate code?
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.Namewill contain the name of the property that is accessed.
- gil
- Posts: 71
- Joined: Mon Feb 15, 2010 12:42 am
Re: How to eliminate duplicate code?
Gil, thanks, that's great!
I had no idea you could do that!
I had no idea you could do that!
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
3 posts
• Page 1 of 1
Return to Object Oriented Programming
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group