<?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>Chad Austin &#187; php</title>
	<atom:link href="http://chadaustin.me/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://chadaustin.me</link>
	<description></description>
	<lastBuildDate>Tue, 17 Aug 2010 08:51:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing Solid PHP</title>
		<link>http://chadaustin.me/2006/10/writing-solid-php/</link>
		<comments>http://chadaustin.me/2006/10/writing-solid-php/#comments</comments>
		<pubDate>Sun, 01 Oct 2006 07:05:00 +0000</pubDate>
		<dc:creator>Chad Austin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[imvu]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://aegisknight.org/new/2006/10/01/writing-solid-php/</guid>
		<description><![CDATA[
I borrowed Eric&#8217;s Writing Solid Code by Steve Maguire, because I&#8217;ve always wanted to read it.  It&#8217;s one of those software development classics, etc.  At a high level, it describes Microsoft&#8217;s approach to writing bug-free code by using a set of techniques that catch bugs as soon as possible &#8212; before they reach [...]]]></description>
			<content:encoded><![CDATA[<p>
I borrowed Eric&#8217;s <i>Writing Solid Code</i> by Steve Maguire, because I&#8217;ve always wanted to read it.  It&#8217;s one of those software development classics, etc.  At a high level, it describes Microsoft&#8217;s approach to writing bug-free code by using a set of techniques that catch bugs as soon as possible &#8212; before they reach the source repository, Testing, or, heaven forbid, the customer.
</p>

<p>
At first, I was surprised at how old it was &#8212; 1993!  Before Win95 even!  Then, it seemed quite out of date.  It mostly talks about programming in C on some old, anemic platforms without the niceties that we take for granted, such as hardware faults when dereferencing invalid pointers.  So, of course, I&#8217;m trying to take higher-level lessons from it, such as the approach he&#8217;s taken to develop these bug-eliminating techniques, and then apply that approach to the environment in which I work.  Unfortunately, a lot of the techniques have to do with low-level C, where you don&#8217;t have a powerful and safe type system or GC to eliminate whole classes of errors.  Most of the specific bugs he&#8217;s mentioned wouldn&#8217;t even arise in Haskell, Python, Java, or C#.  Also, this book was written before unit testing and test-driven development were really commonplace, and I think that these supplant some of the techniques Steve mentions.  (Not all; there is some overlap between test-driven development and writing bug-free code, but neither TDD nor after-the-fact comprehensive tests give you license to write sloppy code.)
</p>

<p>
And as I&#8217;m thinking about how spoiled we are with our strong typing, garbage collection, and hardware exceptions, I realized that I program every day in an unsafe language that tries as hard as it can to hide errors from you, just like the microcomputers of yore.  PHP.  I don&#8217;t think there&#8217;s been a day yet where learning about a PHP design decision hasn&#8217;t made me think &#8220;WTF&#8221;.  I mean, why do you need a rich collection of container types such as in STL or java.util?  PHP&#8217;s array does it all!  If you pass too few arguments to a PHP function, that&#8217;s okay, the missed ones will just be null.  If you pass too many, no problem!  They&#8217;ll be ignored.  Try to append something to an array that _doesn&#8217;t even exist_, no problem!  It&#8217;ll magically create the array!  I could go on and on.  Anyway, the language actively prevents you from finding bugs.
</p>

<p>
So, from here on out, I&#8217;m going to read the book, trying to apply every technique to PHP.  I think there is a lot we can learn.  Here&#8217;s the first technique that would catch bugs in our code that popped into my head:
</p>

<pre>
function foo($a, $b) {
  tep_assert(count(func_get_args()) == 2);
...
</pre>

<p>
Another one:  null, true, and false are considered &#8216;magic numbers&#8217; without immediately visible context.  What does&#8230;
</p>

<pre>
updateProgress(true);
</pre>

<p>&#8230;mean?  Compare that to&#8230;</p>

<pre>
updateProgress(showNewMessage=true);
</pre>

<p>It&#8217;s well-known that true and false make bad function parameters, but I&#8217;d never thought about &#8216;null&#8217; being a magic number, however.  And it is, for the same reasons as true and false above.  When you see code like &#8216;new DialogBox(null)&#8217;, what does that null represent?</p>

<p>
I&#8217;m sure more will come&#8230;
</p>]]></content:encoded>
			<wfw:commentRss>http://chadaustin.me/2006/10/writing-solid-php/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
