Note that there are some explanatory texts on larger screens.

plurals
  1. POIndenting for code-generation
    text
    copied!<p>Often, programmers write code that generates other code.</p> <p>(The technical term is <a href="http://en.wikipedia.org/wiki/Metaprogramming" rel="nofollow noreferrer" title="Wikipedia article on metaprogramming">metaprogramming</a>, but it is more common than merely cross-compilers; think about every PHP web-page that generates HTML or every XSLT file.)</p> <p>One area I find challenging is coming up with techniques to ensure that <em>both</em> the hand-written source file, and the computer-generated object file are clearly indented to aid debugging. The two goals often seem to be competing.</p> <p>I find this particularly challenging in the PHP/HTML combination. I think that is because:</p> <ul> <li>there is sometimes more of the HTML code in the source file than the generating PHP</li> <li>HTML files tend to be longer than, say, SQL statements, and need better indenting</li> <li>HTML has space-sensitive features (e.g. between tags)</li> <li>the result is more publicly visible HTML than SQL statements, so there is more pressure to do a reasonable job.</li> </ul> <p>What techniques do you use to address this?</p> <p><hr/> Edit: I accept that there are at least three arguments to not bothering to generate pretty HTML code:</p> <ul> <li>Complexity of generating code is increased.</li> <li>Makes no difference to rendering by browser; developers can use Firebug or similar to view it nicely.</li> <li>Minor performance hit - increased download time for whitespace characters.</li> </ul> <p>I have certainly sometimes generated code without thought to the indenting (especially SQL).</p> <p>However, there are a few arguments pushing the other way:</p> <ul> <li>I find, in practice, that I <em>do</em> frequently read generated code - having extra steps to access it is inconvenient.</li> <li>HTML has some space-sensitivity issues that bite occasionally. </li> </ul> <p>For example, consider the code:</p> <pre><code>&lt;div class="foo"&gt; &lt;?php $fooHeader(); $fooBody(); $fooFooter(); ?&gt; &lt;/div&gt; </code></pre> <p>It is clearer than the following code:</p> <pre><code>&lt;div class="foo"&gt;&lt;?php $fooHeader(); $fooBody(); $fooFooter(); ?&gt;&lt;/div&gt; </code></pre> <p>However, it is also has different rendering because of the whitespace included in the HTML.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload