[stringtemplate-interest] Advice: Avoiding duplication via iterating of list and applying template

Udo Borkowski ub at abego-software.de
Wed May 25 16:46:03 PDT 2011


Iteration is one way to do this. However you could also avoid duplication by just using a template with parameters.

E.g.

t(p1, p2, p3, p4, p5, p6, p7) ::= <<
<p1> | <p2> | <p3> | <p4> | **    ;  handle any hyphens in a unit field ; <p5> - <p6> <p7> **
<_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
>>

And then you call this template with the different arguments:

<t("^", "-", ">", "^", "1", "3EBA", "1")>
<t("^", "-", ">", ">", "1", "3EBA", "3EBA")>
<t("^", "-", ">", "L", "1", "3EBA", "LOT")>
<t("^", "-", ">", "<", "1", "3EBA", "A501")>
<t(">", "-", "^", "^", "3EBA", "1", "1")>

This makes it more obvious what are the variable and the fixed part.

Also: currently the variable parts belong to your template definition (as arguments to the template calls). Depending on where you get these values from you may want to consider these "variable values" to be part of the "model". Then you would pass them pass them from the model to the template, e.g. as a list of maps, each map containing the values for "one line". Then you would again use the iteration to process it.


Udo


On 25.05.2011, at 01:48, Barrie Treloar wrote:

> Is this sane?
> It works and it is easier to read, but I am wondering if there is a
> better way...
> Note: I created the template _handleUnitNumbers() because of the
> duplication there, so I may inline it instead.
> 
> I've replacing
> 
> === orginal:start ===
> ^ | - | > | ^ | **    ;  handle any hyphens in a unit field ; 1 - 3EBA 1 **
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> 
> ^ | - | > | > | **    ;  handle any hyphens in a unit field ; 1 - 3EBA 3EBA **
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> 
> ^ | - | > | L | **    ;  handle any hyphens in a unit field ; 1 - 3EBA LOT **
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> 
> ^ | - | > | @ | **    ;  handle any hyphens in a unit field ; 1 - 3EBA 6H46K **
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> 
> ^ | - | > | \< | **   ;  handle any hyphens in a unit field ; 1 - 3EBA A501 **
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> 
>> | - | ^ | ^ | **    ;  handle any hyphens in a unit field ; 3EBA - 1 1 **
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> === original:end ===
> 
> With
> 
> === new:start ===
> <[
> "^ | - | > | ^ | **    ;  handle any hyphens in a unit field ; 1 - 3EBA 1 **",
> "^ | - | > | > | **    ;  handle any hyphens in a unit field ; 1 -
> 3EBA 3EBA **",
> "^ | - | > | L | **    ;  handle any hyphens in a unit field ; 1 - 3EBA LOT **",
> "^ | - | > | @ | **    ;  handle any hyphens in a unit field ; 1 -
> 3EBA 6H46K **",
> "^ | - | > | < | **   ;  handle any hyphens in a unit field ; 1 - 3EBA A501 **",
> "> | - | ^ | ^ | **    ;  handle any hyphens in a unit field ; 3EBA - 1 1 **",
> ] : { pattern |
> <pattern> (NEW)
> <_handleUnitNumbers(unitNumberSupported,["1", "2", "3"])>
> 
> }>
> === new:end ===
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list