Best way to use "separator" in an if chain [message #1808561] |
Wed, 26 June 2019 20:57 |
Elder Rodrigues Messages: 7 Registered: June 2019 |
Junior Member |
|
|
Hello everyone,
I'm wondering how is the best way to implement the code that I will explain below:
Given an instance of MetaclassX, I need to generate specific text for each of its non-null properties and then merge all the texts using a 'and' keyword as a separator.
So let's say that MetaClassX has three properties: propA, propB, and propC. There are at least two approaches that I have thinked:
Approach #1:
[template public generateText(anElement: MetaClassX])]
[if not anElement.propA.oclIsUndefined] <text of propA>[/if]
[if not anElement.propB.oclIsUndefined] [if not anElement.propA.oclIsUndefined] and [/if] <text of propB>[/if]
[if not anElement.propC.oclIsUndefined] [if (not anElement.propA.oclIsUndefined) or (not anElement.propB.oclIsUndefined)] and [/if] <text of propC>[/if]
[/template]
Approach #2:
[template public generateText(anElement: MetaClassX])]
[let allTexts: Set(String) = Set(String){
anElement.generateTextPropA(),
anElement.generateTextPropB(),
anElement.generateTextPropC()
}]
[for (text: String | allTexts) separator(' and ')]
[text /] [/for]
[/template]
[template public generateTextPropA(anElement: MetaClassX])]
[if not anElement.propA.oclIsUndefined] <text of propA>[/if]
[/template]
[template public generateTextPropB(anElement: MetaClassX])]
[if not anElement.propB.oclIsUndefined] <text of propB>[/if]
[/template]
[template public generateTextPropC(anElement: MetaClassX])]
[if not anElement.propC.oclIsUndefined] <text of propC>[/if]
[/template]
The Appraoch#1 is not scalable in the sense that if the MetaClassX has many properties, the if statement will have many conditions and will leave the code difficult to maintain.
The Approach#2 is more scalable in the sense that if MetaClassX has many properties, you just need to create templates for each property to generate the specific texts and then include
the results of each template within the set. However, I'm not sure if it's the best way. Any suggestion?
Thanks
[Updated on: Wed, 26 June 2019 21:48] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02907 seconds