String Manipulation DSL’s
Mats comments on my previous post:
“But I guess what I’m saying is that the code for inserting that string using just strings may look even worse when you come back to look at it a week later…”
If your thinking that I’m talking about code like:
StringBuilder sb = new StringBuilder();
sb.Append(”xxx - yyy [somevar] zzz”);
Response.Write(sb.ToString().Replace(”[somevar]“,”somevalue”));
Then I’d agree. This is a maintenance nightmare. Solving this is a well known “pattern”. Use a domain specific language (DSL). In this case - we’d use a template language. Imagine a hypothetical template DSL:
TemplateContext context = new TemplateContext(”somevar”,”somevalue”);
Response.Write(Template.Eval(”xxx - yyy <%=somevar %> zzz”,context));
It is possible to write this in a way that is maintainable and still readable a week later. Scott Bellware’s comment still sums this up for me.
1 Comment