Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I once read (I think it was in "Programming Pearls") that one should use templates instead of building the string through the use of concatenation.

For example, consider the template below (using C# razor library)

<in a properties file>
Browser Capabilities
Type = @Model.Type
Name = @Model.Browser
Version = @Model.Version
Supports Frames = @Model.Frames
Supports Tables = @Model.Tables
Supports Cookies = @Model.Cookies
Supports VBScript = @Model.VBScript
Supports Java Applets = @Model.JavaApplets
Supports ActiveX Controls = @Model.ActiveXControls

and later, in a separate code file

private void Button1_Click(object sender, System.EventArgs e)
{
    BrowserInfoTemplate = Properties.Resources.browserInfoTemplate; // see above
    string browserInfo = RazorEngine.Razor.Parse(BrowserInfoTemplate, browser);
    ...
}

From a software engineering perspective, how is this better than an equivalent string concatentation, like below:

private void Button1_Click(object sender, System.EventArgs e)
{
    System.Web.HttpBrowserCapabilities browser = Request.Browser;
    string s = "Browser Capabilities\n"
        + "Type = "                    + browser.Type + "\n"
        + "Name = "                    + browser.Browser + "\n"
        + "Version = "                 + browser.Version + "\n"
        + "Supports Frames = "         + browser.Frames + "\n"
        + "Supports Tables = "         + browser.Tables + "\n"
        + "Supports Cookies = "        + browser.Cookies + "\n"
        + "Supports VBScript = "       + browser.VBScript + "\n"
        + "Supports JavaScript = "     + 
            browser.EcmaScriptVersion.ToString() + "\n"
        + "Supports Java Applets = "   + browser.JavaApplets + "\n"
        + "Supports ActiveX Controls = " + browser.ActiveXControls 
              + "\n"
    ...
}
share|improve this question

migrated from stackoverflow.com Jun 6 at 15:56

This question came from our site for professional and enthusiast programmers.

8  
Which one would you rather look at and maintain? –  BradleyDotNET May 28 at 21:34
    
I think it just boils down to cleaner code. Not sure there's some fundamental software engineering principle at play here. –  univerio May 28 at 21:36
    
@univerio - The most significant engineering-esque difference I see is that the template is not coupled to "HttpBrowserCapabilities". Surely that's significant from an engineering perspective? –  stephen May 28 at 22:52
1  
Swap string concatenation to StringBuilder appends and you'll receive no complaints from me. Purely as a point of personal taste, I find templates are worse for readability and thus maintenance. –  lzcd May 28 at 23:11
1  
string is immutable and hence the amount of garbage produced after this single assign statement is high try using String builder other string manipulation datastructures –  linodh May 29 at 1:17

1 Answer 1

up vote 4 down vote accepted

The engineering principles at play here are readability and DRY.

The primary driver of readability is length. Shorter code is more readable (for readers of adequate skill level), so the first case reads better.

A secondary driver is clutter. The absence of punctuation and extraneous characters favours the first case.

The DRY point here is that symbols such as browser and \n are concealed in the abstraction in case 1, and repeated in case 2. If either of those needed to be changed there would be less places to change in case 1 than case 2.

Set against this is WYSIWYG. In the first case there is something missing between the code you see and how it is put to work, where in the second case all the machinery is plain to see.

[At the opinion level, I would strongly favour case 1; others would not. But that would make this an opinion-based question and it might get closed, so we wouldn't want to venture there.]

share|improve this answer
    
I see it this way: abstracting permits coding in domain terms, abstraction embiggens re-usability; thoughtful abstracting at all code levels effectively "pushes details down"; layered abstracting seems to naturally evolve code to the single responsibility principle; SIP tends to manage and distribute overall code complexity. Thus any given bit of code tends to be easer to read, easier to understand, easier to modify, easier to test. But one must "begin in the basement" with applying OO principles. –  radarbob Jun 7 at 1:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.