<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Databases and Life &#187; Coding</title>
	<atom:link href="http://www.databasesandlife.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.databasesandlife.com</link>
	<description>Adrian Smith's blog</description>
	<lastBuildDate>Mon, 14 Jun 2010 09:41:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The special variable &#8220;_&#8221;</title>
		<link>http://www.databasesandlife.com/the-underscore-variable/</link>
		<comments>http://www.databasesandlife.com/the-underscore-variable/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 14:05:39 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=479</guid>
		<description><![CDATA[Reading this blog post, Destructuring binds in Ruby just now reminded me of a feature I love about Prolog which I wish would make it into other languages.
Firstly, I love assigning a list to a list of lvalues i.e. variables; this is possible in both PHP and Perl which I use regularly; and no doubt [...]]]></description>
			<content:encoded><![CDATA[<p>Reading this blog post, <a href="http://citizen428.net/archives/427-Destructuring-binds-in-Ruby.html">Destructuring binds in Ruby</a> just now reminded me of a feature I love about Prolog which I wish would make it into other languages.</p>
<p>Firstly, I love assigning a list to a list of <a href="http://cplus.about.com/od/glossar1/g/lvalue.htm">lvalues</a> i.e. variables; this is possible in both PHP and Perl which I use regularly; and no doubt many other languages. (But not Java: why not!?)</p>
<pre>
    ($a, $b) =      ($b, $a);  // Perl
list($a, $b) = array($b, $a);  // PHP
</pre>
<p>PHP, as always, wins in inelegance, having the left side syntactically different to the right side. While it&#8217;s obviously the case that a list of values and a list of lvalues are technically different, I don&#8217;t think this difference should be expressed in the syntax.</p>
<p>I mean, in most languages you write e.g. $foo=4 and $bar=$foo; in both those cases you write $foo but yet they do something different (lvalue and rvalue); given that you write them the same there i think the same should apply to lists.</p>
<p>But I digress &#8211; What I want to mention is using &#8220;underscore&#8221; to mean &#8220;any variable&#8221;. I first saw this in Prolog.</p>
<p>For example, imagine you have to implement an interface (e.g. in Java), it requires you to write a function taking two parameters, but one of the parameters you don&#8217;t care about. Wouldn&#8217;t it be nice to write</p>
<pre>
interface ExistingApi {
   public void createObject(String name, Object otherInformation);
}

class MyInstanceOfTheApi {
   public void createObject(String name, _) {
      ...
   }
}
</pre>
<p>i.e. this shows clearly you do not care about the second parameter. </p>
<p>In current Java (and all languages I program including Perl, PHP) you have to give all variables a name even if you don’t use them, either in function definitions or in “assign to a list” scenarios mentioned above. It is then left as an exercise to the reader to determine if these variables are used or not, and indeed an exercise to the writer to name the variable they are never going to use.</p>
<p>I mean yes, technically you can actually just call variables “_” (or “$_” (except in Perl where “$_” already means something)) but that would then be a coding convention as opposed to a language feature, and who knows if the coding convention is actually used correctly by a programmer. (If “_” is a variable there’s nothing stopping someone from using its value.)</p>
<p>And then you have the problem if you have two variables you don&#8217;t care about, you can&#8217;t call them both &#8220;_&#8221;.</p>
<p>By the way, <a href="http://books.google.com/books?id=VjHk2Cjrti8C&#038;printsec=frontcover&#038;dq=Programming+in+Prolog&#038;source=bl&#038;ots=IHQtLmvJqC&#038;sig=RFhnlEzs_Nu_GkwTQD_Z3som5Ds#v=onepage&#038;q&#038;f=false">Programming in Prolog</a> is an excellent book.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/the-underscore-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java is lacking a String &#8220;join&#8221; function</title>
		<link>http://www.databasesandlife.com/java-is-lacking-a-string-join-function/</link>
		<comments>http://www.databasesandlife.com/java-is-lacking-a-string-join-function/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 10:49:49 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=465</guid>
		<description><![CDATA[Between Java 1.0 and Java 1.3 (1996-2002 according to Wikipedia) there was no way to split strings into an array or a list.
In Java 1.4 the authors of Java saw it fit to introduce a method to split strings,
String csvData = "field1,field2,field3";
String[] fields = csvData.split(",");

However they did not introduce a method to &#8220;join&#8221; strings! Even [...]]]></description>
			<content:encoded><![CDATA[<p>Between Java 1.0 and Java 1.3 (1996-2002 according to Wikipedia) there was no way to split strings into an array or a list.</p>
<p>In Java 1.4 the authors of Java saw it fit to introduce a method to split strings,</p>
<pre>String csvData = "field1,field2,field3";
String[] fields = csvData.split(",");
</pre>
<p>However they did not introduce a method to &#8220;join&#8221; strings! Even in Java 7 there is no way to do this, e.g. via a static String.join method (2002-now).</p>
<p>OK I realize this is not &#8220;rocket science&#8221;, and I appreciate it exists in various versions in various third-party libraries, but still, it&#8217;s something every program needs to do at some point, it&#8217;s annoying to have to re-define it or think about it for each application.</p>
<p>For example, in one project I was working on in the last 6 months, such a function was created, and then had a bug! (OK but to be fair, that was not the only bug in the application!)</p>
<p>Come on, I mean this is a totally trivial function, totally necessary, available in all scripting lanuags why is it still not in Java?!</p>
<p>(P.S. Want to see an &#8220;enterprise java&#8221; solution to this problem? Check out the <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Joiner.html#method_summary">number of methods on this class</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-is-lacking-a-string-join-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java varargs: inconsistent behaviour if you pass an array</title>
		<link>http://www.databasesandlife.com/java-varargs-inconsistent-behaviour-if-you-pass-an-array/</link>
		<comments>http://www.databasesandlife.com/java-varargs-inconsistent-behaviour-if-you-pass-an-array/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 10:39:07 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=461</guid>
		<description><![CDATA[In Java 1.4 there was the function Arrays.asList. You could pass it an array and it would make a list out of it.
String[] myArray = new String[] { "foo", "bar" };
List myList = Arrays.asList(myArray);

In Java 1.5 this was retrofitted for varargs; you could simply pass elements to the function
List&#60;String&#62; myList = Arrays.asList("foo", "bar");

I never really [...]]]></description>
			<content:encoded><![CDATA[<p>In Java 1.4 there was the function Arrays.asList. You could pass it an array and it would make a list out of it.</p>
<pre>String[] myArray = new String[] { "foo", "bar" };
List myList = Arrays.asList(myArray);
</pre>
<p>In Java 1.5 this was retrofitted for varargs; you could simply pass elements to the function</p>
<pre>List&lt;String&gt; myList = Arrays.asList("foo", "bar");
</pre>
<p>I never really understood how that worked in a backwards-compatible way; I mean either the function takes an array of stuff, or it takes individual elements, surely?</p>
<p>It turns out, that with the varargs syntax, the caller is not forced to pass individual elements, the caller can instead pass an array of elements.</p>
<pre>List&lt;String&gt; myList = Arrays.asList("foo", "bar");
List&lt;String&gt; myList = Arrays.asList(new String[] { "foo", "bar" });
</pre>
<p>The above two calls are identical, both return a List&lt;String&gt;.</p>
<p>But surely this is really dangerous? I mean Arrays.asList does not make any assumptions about what types of arguments it accepts; the list can be composed of any object.</p>
<p>How can it be certain that you want to have an List of Strings, and not a List containing a single element which is a String array? (An array is an object.)</p>
<p>To demonstrate this inconsistency:</p>
<pre>String[] arr = new String[] { "foo", "bar" };
Arrays.asList(arr);            // returns List&lt;String&gt;
Arrays.asList(arr, arr);       // returns List&lt;String[]&gt;
Arrays.asList(arr, arr, arr);  // returns List&lt;String[]&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-varargs-inconsistent-behaviour-if-you-pass-an-array/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>foreach syntax</title>
		<link>http://www.databasesandlife.com/foreach-syntax/</link>
		<comments>http://www.databasesandlife.com/foreach-syntax/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 13:04:26 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=442</guid>
		<description><![CDATA[Most modern languages use very similar syntax inspired by C; but the features added since C are really non-standard! The &#8220;for-each&#8221; syntax annoys me particularly. I mean none of these is significantly better/worse than the others, but I program in all these languages (apart from C#) on a regular basis and I always have to [...]]]></description>
			<content:encoded><![CDATA[<p>Most modern languages use very similar syntax inspired by C; but the features added since C are really non-standard! The &#8220;for-each&#8221; syntax annoys me particularly. I mean none of these is significantly better/worse than the others, but I program in all these languages (apart from C#) on a regular basis and I always have to think when typing in the line in order not to get the wrong syntax.</p>
<table style="width: 100%;">
<tbody>
<tr>
<td style="width: 100px;"><strong>PHP</strong></td>
<td style="font-family: monospace;">foreach (<em>list </em>as <em>element</em>)</td>
</tr>
<tr>
<td><strong>Perl</strong></td>
<td style="font-family: monospace;">foreach my <em>element </em>(<em>list</em>)</td>
</tr>
<tr>
<td><strong>Java</strong></td>
<td style="font-family: monospace;">for (<em>element </em>: <em>list</em>)</td>
</tr>
<tr>
<td><strong>Javascript</strong></td>
<td style="font-family: monospace;">for (var <em>element </em>in <em>list</em>)</td>
</tr>
<tr>
<td><strong>C#</strong></td>
<td style="font-family: monospace;">foreach (<em>element </em>in <em>list</em>)</td>
</tr>
</tbody>
</table>
<p>For what it&#8217;s worth, I think &#8220;foreach&#8221; is nicer than &#8220;for&#8221; as it reads more like a sentence (the word &#8220;for&#8221; really makes no sense at all in that context); and about &#8220;in&#8221; vs. colon I&#8217;ve got no preference really.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/foreach-syntax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Code generation? Don&#8217;t generate to Java</title>
		<link>http://www.databasesandlife.com/java-method-64k-limit/</link>
		<comments>http://www.databasesandlife.com/java-method-64k-limit/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 16:40:13 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=407</guid>
		<description><![CDATA[I tried to write a program using Java; it all seemed to be going well but then I hit a ridiculous limit. Java cannot be used for this type of problem. I have now completely re-written it in a different programming language, and that works fine.
Be aware of this limit. I was unaware of it [...]]]></description>
			<content:encoded><![CDATA[<p><strong>I tried to write a program using Java; it all seemed to be going well but then I hit a ridiculous limit. Java cannot be used for this type of problem. I have now completely re-written it in a different programming language, and that works fine.</strong></p>
<p>Be aware of this limit. I was unaware of it when I started this project. But it makes Java completely unsuitable for a whole class of problem.</p>
<p>My customer supplies me with a config file from time to time, this specifies a certain algorithm. When the user enters data, this algorithm must be applied. The algorithm is complex, so performance is an issue.</p>
<p>The solution I chose was to <strong>generate code</strong> to execute the algorithm, based on the information in the config file. This is a valid computer-science approach, and is used for similar problems. For example, language parsers are often expressed as a grammar, and code to parse documents in the grammar are generated. JSPs are turned into Java classes which are then compiled and executed. WebTek <a href="http://github.com/prozessor13/webtek/blob/master/lib/WebTek/Compiler.pm">pre-compiles</a> HTML templates containing macros into code which produces the resulting HTML when executed.</p>
<p>However, <strong>don&#8217;t try this in Java</strong>, unless you are only working with small problems. A single method in Java can only be 64KB in size, once compiled.<br />
<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262078"> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262078</a></p>
<p>This means, JSPs can only be of a certain length, parsers can only parse languages of a certain complexity, if WebTek were written in Java then templates could only be of a certain length and complexity, and so on. Do you want to place such restrictions on the software you produce?</p>
<p>My specific problem involves simulating one million variations to a particular solution. How can I fit that into 64K?</p>
<ul>
<li>That is 0.06 bytes per solution variation; yet the simulation of a single variation involves many lines of code (i.e. in total compiling to more than 0.06 bytes!).</li>
<li>I could put each variation into its own method, and have a big method which calls them all&#8212;but a method call takes more than 0.06 bytes!</li>
<li>I could have a hierarchy of methods: one main method which calls, say, 100 sub-methods, each of those call 100 sub-sub-methods, and finally those methods call the methods for the individual variations.</li>
</ul>
<p>It&#8217;s not even possible to know how many bytes a method will generate to! So, as the complexity of the simulation of a variation is expressed in the config file, I would have to essentially have to do a &#8220;trial and error&#8221; approach: generate a method, compile it, if I get the error concerning the 64KB limit, split the problem up into slightly smaller methods, try the compilation again, repeat, etc. (And the Java compiler is not even very fast.)</p>
<p><strong>This is all so wrong! </strong>This is complexity, which isn&#8217;t solving the customer&#8217;s problem. This complexity costs me time (and thus my customer money), complexity leads to bugs and difficulty of maintenance, etc.</p>
<p>So I have changed the language. <strong>Rather than generate Java, I generate C</strong> and compile it using the GNU gcc compiler. <a href="http://www.gnu.org/prep/standards/standards.html#Semantics">From the GNU coding standards</a>:</p>
<blockquote><p>
Avoid arbitrary limits on the length or number of any data structure, including file names, lines, files, and symbols, by allocating all data structures dynamically.
</p></blockquote>
<p>This is a good standard! I like it. All programs should be written with this in mind. Your program may well be online in 10 or 20 years, and the hardware may well have changed: a 64KB limit may seem reasonable one year but is a real limitation 10 or 20 years later in software which would otherwise still be useful.</p>
<p>So, if you are solving this type of problem, don&#8217;t use Java.</p>
<p>P.S. On a separate project I used a similar approach using Perl, and that worked out fine too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-method-64k-limit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP infinite recursion</title>
		<link>http://www.databasesandlife.com/php-infinite-recursion/</link>
		<comments>http://www.databasesandlife.com/php-infinite-recursion/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 12:50:11 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=388</guid>
		<description><![CDATA[What can I say? How about &#8220;toy language&#8221;?

$ php -r 'function foo() { foo(); } foo();'
Segmentation fault

I&#8217;m not saying that infinite recursion is a good idea, but during development it can happen by accident, and I don&#8217;t expect such a simple error to crash the PHP interpreter! (Also it took me about 20 minutes to [...]]]></description>
			<content:encoded><![CDATA[<p>What can I say? How about &#8220;toy language&#8221;?</p>
<pre>
$ php -r 'function foo() { foo(); } foo();'
Segmentation fault
</pre>
<p>I&#8217;m not saying that infinite recursion is a good idea, but during development it can happen by accident, and I don&#8217;t expect such a simple error to crash the PHP interpreter! (Also it took me about 20 minutes to debug this problem, as I had no idea where it happened, nor indeed what the problem was..)</p>
<p><i>PHP 5.2.6 on Linux 2.6.26 Debian</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/php-infinite-recursion/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8220;Go&#8221; programming language</title>
		<link>http://www.databasesandlife.com/go-programming-language/</link>
		<comments>http://www.databasesandlife.com/go-programming-language/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 16:46:06 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=372</guid>
		<description><![CDATA[Before everyone gets too excited about the &#8220;Go&#8221; language, let&#8217;s not forget it lacks:

Exceptions
Assertions
Inheritance
Generic types

Generics are necessary to express concepts like List&#60;List&#60;MyType&#62;&#62;. If you are in to static typing, then you need to be able to express those concepts. (And if you are not, then a statically typed language like Go is not for you [...]]]></description>
			<content:encoded><![CDATA[<p>Before everyone gets too excited about the &#8220;Go&#8221; language, let&#8217;s not forget it lacks:</p>
<ul class=tight>
<li><a href="/exceptions-use-them/">Exceptions</a></li>
<li>Assertions</li>
<li>Inheritance</li>
<li>Generic types</li>
</ul>
<p>Generics are necessary to express concepts like List&lt;List&lt;MyType&gt;&gt;. If you are in to static typing, then you need to be able to express those concepts. (And if you are not, then a statically typed language like Go is not for you anyway.)</p>
<p>Obviously it has good features as well, such as its concurrency constructs and type inference, but without those features above, I can&#8217;t see how you can do any useful modeling in the language; or rather how can you can express a useful model in the language without losing a lot of information.</p>
<p><a href="http://golang.org/doc/go_lang_faq.html">http://golang.org/doc/go_lang_faq.html</a></p>
<p>Perhaps those features will be added in future versions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/go-programming-language/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java: Always explicitly specify which XML parser to use</title>
		<link>http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/</link>
		<comments>http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 12:42:26 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=351</guid>
		<description><![CDATA[There is the following design error in Java (at least in Servlets):

A server may serve multiple applications; each application may use different libraries or even different versions of the same library, &#8220;side by side&#8221;.
XML parsers, transformers (XSLT), etc., have a standard interface, and there may be different implementations of this interface from different vendors, open-source [...]]]></description>
			<content:encoded><![CDATA[<p>There is the following design error in Java (at least in Servlets):</p>
<ol>
<li>A server may serve multiple applications; each application may use different libraries or even different versions of the same library, &#8220;side by side&#8221;.</li>
<li>XML parsers, transformers (XSLT), etc., have a standard interface, and there may be different implementations of this interface from different vendors, open-source projects, etc.</li>
<li>Which XML parser, transformed etc. is actually used depends on a <strong>global system variable</strong>.</li>
</ol>
<p>And it&#8217;s point 3 that&#8217;s the problem really. Points 1 and 2 are debatable: they certainly bring advantages, but they certainly bring complexity too.</p>
<p>I just had the problem that one of my web applications stopped working, but only intermittently. Restarting the server led to everything being OK, but later things would not be OK. I do hate environments where everything appears to work, yet in fact doesn&#8217;t. I mean how do you know when you&#8217;re &#8220;done&#8221; in such an environment? (Or how do you even know you are in such an environment?)</p>
<p>The bug was caused by:</p>
<ol>
<li>Application one used the default XML parser, and didn&#8217;t have any extra JARs (libraries) for reading XML</li>
<li>Application two required a special XML parser, set the global variable so it would be used, and included the JARs necessary for the special XML parser</li>
</ol>
<p>So when a request came to application 1, <em>after </em>a request had come to application 2, then the system would try to instantiate the special XML parser within application 1 (specified in the global variable set by application 2), but wouldn&#8217;t find it, as it wasn&#8217;t deployed in application 1 (and applications can&#8217;t use one another&#8217;s libraries, due to feature #1).</p>
<p>This seems obvious when one describes it, but looking at the logs, on a live server, with the system down and the clock ticking? &#8211; Far from obvious.</p>
<p>So now, I assert, every time you want to create an XML parser, do the following:</p>
<h3>If you require a special XML library, use:</h3>
<pre>
System.<strong>setProperty</strong>("javax.xml.parsers.DocumentBuilderFactory",
    "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
...
</pre>
<h3>If you require the standard XML library, use:</h3>
<pre>
Properties systemProperties = System.getProperties();
systemProperties.<strong>remove</strong>("javax.xml.parsers.DocumentBuilderFactory");
System.setProperties(systemProperties);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
...
</pre>
<p>There is also the possibility to pass a parameter to DocumentBuilderFactory to specify which XML parser technology to use. That&#8217;s a good option too, as it wouldn&#8217;t &#8220;corrupt&#8221; this global variable (&#8220;system property&#8221;). However I think one should be defensive, and always delete the global variable if one wishes to use the standard XML parser, and therefore it doesn&#8217;t matter if this global variable gets corrupted or not.</p>
<h3>Never do the following:</h3>
<pre>
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
</pre>
<p>This simply relies on whichever XML parser is currently set in the global variable. You have no way to guarantee that some other application running on the same server won&#8217;t set the global variable to use an XML parser you don&#8217;t have installed in your application. Even if you have control of the server and all applications, you don&#8217;t know what software you&#8217;ll be writing in the future. (In this case I installed a new application to a server which&#8217;d been running fine for 1 year, but due to setting the global variable, the old application broke..)</p>
<p>The same applies for all those other &#8220;factory&#8221; situations such as TransformerFactory.newInstance() etc.</p>
<p>This feels all quite inelegant to me, and has just cost me a lot of time, and it&#8217;s not as if I&#8217;m so new to programming Java. I am wondering if there is a better way to approach it? Or is Java just broken in this particular respect?</p>
<p>P.S. This is not the only thing that went wrong with the old application today. I upgraded from Java 5 to Java 6 and suddenly some XML was not compliant against a schema according to Java &#8211; I had hit <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6529766">this error</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Never close PHP class files with the &#8220;?&gt;&#8221; tag</title>
		<link>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/</link>
		<comments>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 09:47:38 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=348</guid>
		<description><![CDATA[When developing PHP, a front-end PHP file will include other files: classes,  utilities, etc.
When writing those class files, one also needs to use the &#60;?php tag at the start of the file, otherwise PHP will simply take the text and output it unchanged to the browser. (PHP&#8217;s assumption is that it sits in a [...]]]></description>
			<content:encoded><![CDATA[<p>When developing PHP, a front-end PHP file will include other files: classes,  utilities, etc.</p>
<p>When writing those class files, one also needs to use the <strong>&lt;?php</strong> tag at the start of the file, otherwise PHP will simply take the text and output it unchanged to the browser. (PHP&#8217;s assumption is that it sits in a web page, with probably more markup than code, so by default characters in the source code get copied one-to-one to the browser and the <strong>&lt;?php</strong>&#8230;<strong>?&gt;</strong> tags are necessary to introduce PHP to the &#8220;exceptional circumstance&#8221; that one might actually want to program some PHP.)</p>
<p>If one must open the class source file with <strong>&lt;?php</strong> then it would seem to make aesthetic sense to close it with <strong>?&gt;</strong>. However, there are no negative  side-effects if one does not close the tag, plus one very negative side-effect  if one does close it.</p>
<p>We performed a minor release a while ago, after which the display of generated PDF files no longer worked. Yet the minor release had nothing to do with the section of code  that produced PDFs. What sort of weird <a href="http://en.wikipedia.org/wiki/Action_at_a_distance_(computer_science)">action-at-a-distance</a> could possibly be happening here?</p>
<p>The reason was that one class file in the minor release had a blank line after the <strong>?&gt;</strong> tag. This was impossible to spot in the text editor. The blank line was printed to the browser, which was also invisible in nearly all of the site, as HTML ignores blank lines. PDFs probably do as well (I haven&#8217;t checked) but the problem wasn&#8217;t with the content. As HTTP response content is streamed to the browser (as opposed to being collected first and then sent to the browser at the end of the request), HTTP headers can only be set before the first byte of output has been produced by the software. As the blank line in the class source file consituted  content, and the source file was (necessarily) parsed before the code could be executed, the HTTP header &#8220;Content-Type: text/pdf&#8221; couldn&#8217;t be sent, and various errors about headers not being able to be sent, combined with the binary source  of the PDF, arrived at the user&#8217;s screen.</p>
<p>So given there  are no disadvantages, and one particulary weird source of bugs can be removed, I  would say one should never end PHP files with <strong>?&gt;</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting Jetty: FAILED</title>
		<link>http://www.databasesandlife.com/starting-jetty-failed/</link>
		<comments>http://www.databasesandlife.com/starting-jetty-failed/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 21:28:45 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=346</guid>
		<description><![CDATA[It is literally 23:19 on a Sunday and I&#8217;ve been working through the weekend to get a release out of some software I&#8217;m working on.
The Java application webserver (Jetty) was taking a long time to restart each time I did a change, so for some reason I thought I&#8217;d experiment with some new command-line options. [...]]]></description>
			<content:encoded><![CDATA[<p>It is literally 23:19 on a Sunday and I&#8217;ve been working through the weekend to get a release out of some software I&#8217;m working on.</p>
<p>The Java application webserver (Jetty) was taking a long time to restart each time I did a change, so for some reason I thought I&#8217;d experiment with some new command-line options. Probably not the right time to do that.</p>
<p>Normally I would type</p>
<pre>
$ sudo  /etc/init.d/jetty6 restart
Stopping Jetty: OK
Starting Jetty: OK
</pre>
<p>and everything would be good. I tried typing</p>
<pre>
$ sudo /etc/init.d/jetty6 supervise
</pre>
<p>Then some stuff happened that I didn&#8217;t really understand. Rather than try and work out what it did I tried to restart it again using the old restart mechanism</p>
<pre>
$ sudo  /etc/init.d/jetty6 restart
...
<b>Starting Jetty: FAILED</b>
</pre>
<p>OK I mean that was basically what was going on, it just wrote FAILED. How helpful! There was no info in the logfile. I searched Google but didn&#8217;t come up with anything.</p>
<p>A reboot later, and about half an hour of looking into /etc/init.d/jetty6 with vi and randomly making changes and printing more stuff out yielded the fact that the &#8220;supervise&#8221; command had evidently run Jetty &#8220;as me&#8221; and not as the &#8220;jetty&#8221; user. So when the normal &#8220;restart&#8221; command came along and tried to run the program as &#8220;jetty&#8221; then there were files it couldn&#8217;t write to.</p>
<p>Solution:</p>
<pre>
$ sudo chown jetty /var/log/jetty6/2009_07_12.stderrout.log
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/starting-jetty-failed/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
