thinking in geek tagline’s are so web2.0

Posted
5 September 2007 @ 10pm

Tagged
.net, asp.net

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

Posted by
Mats Helander
18 September 2007 @ 11am

DSLs are cool, no question about that, but when did “let’s invent a new language” become the default approach to any problem?

To me, looking through someone’s code and discovering a DOM representing their structure and a visitor using a StringBuilder for turning into a string (the problem is not with using a StringBuilder – it lies with mixing StringBuilder code with code that may use logic to build up the structure) makes me much happier than if I discover there is a new language devised for this. In your example, the logic seems to become intertwined with the string DSL code, which imo is roughly as bad as mixing that logic in with StringBuilder code.

/Mats


Leave a Comment