<?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>www.hackification.com &#187; Tips</title>
	<atom:link href="http://www.hackification.com/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hackification.com</link>
	<description></description>
	<lastBuildDate>Sat, 05 Jun 2010 11:16:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Back Up And Running</title>
		<link>http://www.hackification.com/2010/06/05/back-up-and-running/</link>
		<comments>http://www.hackification.com/2010/06/05/back-up-and-running/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 11:16:35 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[cpanel]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=515</guid>
		<description><![CDATA[Following about three days of downtime, I&#8217;ve finally got my blog back up and running. I originally created hackification.com as an addon domain and WordPress installation to an existing cpanel account, which was hosting a different site. The original domain was coming up for renewal, and I decided to let it expire, since it wasn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Following about three days of downtime, I&#8217;ve finally got my blog back up and running.</p>
<p><span id="more-515"></span>I originally created hackification.com as an addon domain and WordPress installation to an existing cpanel account, which was hosting a different site. The original domain was coming up for renewal, and I decided to let it expire, since it wasn&#8217;t really doing anything much.</p>
<p>Unfortunately I&#8217;ve since found out that cpanel really doesn&#8217;t like having its primary domain changed &#8211; Fantastico basically stopped working correctly and would leave me with files with permissions set to &#8217;0000&#8242; &#8211; unmodifiable by me.</p>
<p>In the end I decided just to start again: I asked my hosting company to completely trash and recreate my hosting. I&#8217;ve now restored from backups, which is a bit of a hassle:</p>
<ol>
<li>Recreate the WordPress installation as closely as possible;</li>
<li>Import a previously exported WordPress XML file (which restores all the posts and comments);</li>
<li>Restore my &#8220;uploads&#8221; directory (which contains all the images referenced in my posts);</li>
<li>Restore various downloads directories (which contain various assets and demos);</li>
<li>Restore the custom theme I designed (the blue/orange one you can hopefully see);</li>
<li>Finally, try to remember my various WordPress and WordPress plug-in settings (particularly Akismet, since as soon as the blog went live the spam started appearing).</li>
</ol>
<p>If I had to provide advice, it would be:</p>
<ul>
<li>Backup everything, in various ways (eg WordPress export, plus entire directory dumps, plus even screenshots of settings);</li>
<li>If you&#8217;re planning to use a single cpanel to host multiple domains, make sure the primary domain is one you want to keep forever &#8211; it could even be a cheap-as-possible domain that no-one but you ever sees.</li>
</ul>
<p>I was also very lucky with my hosting company, <a href="http://www.webhosting.uk.com/" target="_blank">webhosting.uk.com</a>: I&#8217;m not paying very much (£60 per year), but they were very quick getting me back up and running, and provide good service in the evenings (which is the only time I get to work on my blog), so thanks guys.</p>
<p>As coincidence would have it, I noticed a SitePoint article regarding WordPress backups: <a href="http://articles.sitepoint.com/article/backup-your-wordpress-site" target="_blank">Keep Your Blog Safe: Back Up Your WordPress Installation</a>. Hopefully that might be of use to anyone else with a cpanel blog to manage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2010/06/05/back-up-and-running/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#039;Smart and Gets Things Done&#039; vs &#039;Done and Gets Things Smart&#039;</title>
		<link>http://www.hackification.com/2010/01/10/smart-and-gets-things-done-vs-done-and-gets-things-smart/</link>
		<comments>http://www.hackification.com/2010/01/10/smart-and-gets-things-done-vs-done-and-gets-things-smart/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 19:10:47 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Links]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[articles]]></category>
		<category><![CDATA[hiring]]></category>
		<category><![CDATA[recruitment]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=351</guid>
		<description><![CDATA[In this article I link to two excellent articles on the subject of hiring coders. They&#8217;re not new by any means, but if you haven&#8217;t read one or both of them, I thoroughly recommend them. (If you&#8217;re familiar with both these writers, then skip this article.) Smart, and Gets Things Done Joel Spolsky&#8217;s 2006 article [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I link to two excellent articles on the subject of hiring coders. They&#8217;re not new by any means, but if you haven&#8217;t read one or both of them, I thoroughly recommend them. (If you&#8217;re familiar with both these writers, then skip this article.)</p>
<p><span id="more-351"></span></p>
<h3>Smart, and Gets Things Done</h3>
<p><a href="http://www.hackification.com/wp-content/uploads/2010/01/spolsky.jpg"><img class="alignleft size-full wp-image-363" style="margin-right: 10px;" title="Joel Spolsky" src="http://www.hackification.com/wp-content/uploads/2010/01/spolsky.jpg" alt="" width="130" height="87" /></a>Joel Spolsky&#8217;s 2006 article &#8220;The Guerrilla Guide to Interviewing&#8221; is a real classic that&#8217;s since been made into a book. He argues that there are two things to look for when interviewing, and that a candidate needs to have both attributes &#8211; having just one is as bad as having neither.</p>
<blockquote><p>&#8220;Everybody gives lip service to the idea that people are the most important part of a software project, but nobody is quite sure what you can <em>do</em> about it. The very first thing you have to do right if you want to have good programmers is to <em>hire</em> the right programmers, and that means you have to be able to figure out who the right programmers <em>are</em>, and this is usually done in the interview process.&#8221;</p></blockquote>
<p>Read Joel&#8217;s Article: <strong><a title="The Guerrilla Guide to Interviewing (version 3.0)" href="http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html" target="_blank">The Guerrilla Guide to Interviewing (version 3.0)</a></strong></p>
<h3>Done, and Gets Things Smart</h3>
<p><a href="http://www.hackification.com/wp-content/uploads/2010/01/yegge.jpg"><img class="alignleft size-full wp-image-364" style="margin-right: 10px;" title="Steve Yegge" src="http://www.hackification.com/wp-content/uploads/2010/01/yegge.jpg" alt="" width="130" height="87" /></a>Steve Yegge&#8217;s 2008 response article reminds us that recognising smarts (in ourselves and others) isn&#8217;t even necessarily possible. He suggests that the best approach to hiring is finding &#8220;seed&#8221; employees (although obviously being able to identify them is a Catch-22 situation). To find them you either need to be lucky, or perhaps home in on them via word of mouth.</p>
<blockquote><p>&#8220;How do you hire someone who&#8217;s smarter than you?  How do you <em>tell</em> if someone&#8217;s smarter than you? This is a problem I&#8217;ve thought about, over nearly twenty years of interviewing, and it appears that the answer is: you can&#8217;t. You just have to get lucky.&#8221;</p></blockquote>
<p>Read Steve&#8217;s Article: <strong><a title="Done, and Gets Things Smart" href="http://steve-yegge.blogspot.com/2008/06/done-and-gets-things-smart.html" target="_blank">Done, and Gets Things Smart</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2010/01/10/smart-and-gets-things-done-vs-done-and-gets-things-smart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Tip: Avoid Enums If Possible</title>
		<link>http://www.hackification.com/2009/12/01/quick-tip-avoid-enums-if-possible/</link>
		<comments>http://www.hackification.com/2009/12/01/quick-tip-avoid-enums-if-possible/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 18:16:43 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[enums]]></category>
		<category><![CDATA[oo]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=330</guid>
		<description><![CDATA[One of the &#8220;anti-patterns&#8221; I come across from time to time is over-use of enumerations for control flow. This article describes why I consider it an anti-pattern, and how to counteract it. Let&#8217;s suppose we want to represent an arithmetic operation on two numbers &#8211; such as plus, minus, multiply, etc. We could represent that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the &#8220;anti-patterns&#8221; I come across from time to time is over-use of enumerations for control flow. This article describes why I consider it an anti-pattern, and how to counteract it.</p>
<p><span id="more-330"></span>Let&#8217;s suppose we want to represent an arithmetic operation on two numbers &#8211; such as plus, minus, multiply, etc. We could represent that operation as an enum (although to be clear &#8211; I don&#8217;t think you should):</p>
<pre>enum ArithmeticOperation
{
  Add,
  Subtract,
  Multiply,
  Divide
}</pre>
<p>We could then define a function to apply this operation to two numbers (highlighting will be explained later):</p>
<pre>int ApplyOperation(ArithmeticOperation op, int x, int y)
{
  switch(op)
  {
    <span style="color: #ffff00;">case ArithmeticOperation.Add: return x + y;</span>
    case ArithmeticOperation.Subtract: return x - y;
    case ArithmeticOperation.Multiply: return x * y;
    case ArithmeticOperation.Divide: return x / y;
    default: throw new InvalidOperationException();
  }
}</pre>
<p>That&#8217;s&#8230; sort of fine &#8211; but what happens when we want to extend it?</p>
<p><strong>Adding Operators: No Compile-Time Checking</strong></p>
<p>Let&#8217;s consider what happens if we want to add another operator &#8211; say exponentiation. If we add the enum value but forget to implement the switch case value, everything compiles fine. It&#8217;s only once we come to run the code, and actually hit that code path, that we discover the mistake, giving a run-time exception.</p>
<p><strong>Adding Methods: Bad Code Organisation</strong></p>
<p>Next, imagine what happens if we need to add more methods that switch on this enum. We might need a method returning the operator symbol, and another returning the operator precedence.</p>
<pre>string GetOperatorSymbol(ArithmeticOperation op)
{
  switch(op)
  {
    <span style="color: #ffff00;">case ArithmeticOperation.Add: return "+";</span>
    case ArithmeticOperation.Subtract: return "-";
    case ArithmeticOperation.Multiply: return "*";
    case ArithmeticOperation.Divide: return "/";
    default: throw new InvalidOperationException();
  }
}

int GetOperatorPrecedence(ArithmeticOperation op)
{
  switch(op)
  {
    <span style="color: #ffff00;">case ArithmeticOperation.Add: return 10;</span>
    case ArithmeticOperation.Subtract: return 10;
    case ArithmeticOperation.Multiply: return 20;
    case ArithmeticOperation.Divide: return 20;
    default: throw new InvalidOperationException();
  }
}</pre>
<p>I&#8217;ve highighted the relevant code for the &#8220;add&#8221; operator. Notice how it&#8217;s spread all over the place. Switch statements might group by operation, but they split by concept &#8211; and that&#8217;s the exact opposite of what&#8217;s usually intended with OO coding.</p>
<p><strong>A Better Way</strong></p>
<p>I&#8217;d suggest a better way of approaching this problem is to use an abstract class.</p>
<pre>abstract class ArithmeticOperation
{
  abstract int ApplyOperation(int x, int y);
  abstract string GetSymbol();
  abstract int GetPrecendence();
}

class AddArithmeticOperation : ArithmeticOperation
{
  override int ApplyOperation(int x, int y)
  {
    return x + y;
  }

  override string GetSymbol()
  {
    return "+";
  }

  override int GetPrecedence()
  {
    return 10;
  }
}

// ...and so forth for the other operators.</pre>
<p>That way, all the code for each operator sits in the same place, and adding any new methods for an operator requires all operators to be implemented before the code will even compile.</p>
<p><strong>When to Use Enums</strong></p>
<p>So if enums should be avoided, why do most languages include them? Personally I&#8217;d be happy without them,  but I can think of one possible reason you might want to use an enum. If you&#8217;re defining a public interface designed for consumption by other coders, you could argue that simplifying the interface at the expense of the code inside is a valid trade-off.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/12/01/quick-tip-avoid-enums-if-possible/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Resizing Single-Image-Rollovers Using jQuery</title>
		<link>http://www.hackification.com/2009/09/02/resizing-single-image-rollovers-using-jquery/</link>
		<comments>http://www.hackification.com/2009/09/02/resizing-single-image-rollovers-using-jquery/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 14:28:00 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rollovers]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=252</guid>
		<description><![CDATA[In this article I describe how to create shaped buttons in HTML/CSS that size to their content, have roll-over state, and require only a single image, using jQuery. I&#8217;m sure most web developers know about image rollovers (using separate images for normal and &#8220;hover&#8221; states of anchors). Hopefully also the use of &#8220;CSS Sprites&#8221; is [...]]]></description>
			<content:encoded><![CDATA[<p>In this article I describe how to create shaped buttons in HTML/CSS that size to their content, have roll-over state, and require only a single image, using jQuery.</p>
<p><span id="more-252"></span></p>
<p>I&#8217;m sure most web developers know about image rollovers (using separate images for normal and &#8220;hover&#8221; states of anchors). Hopefully also the use of &#8220;<a href="http://www.alistapart.com/articles/sprites" target="_blank">CSS Sprites</a>&#8221; is becoming better known &#8211; combining the normal and rollover images into a single physical image file to speed up load times and reduce flicker. I won&#8217;t cover those techniques here.</p>
<p>However, if you want the anchor or button to size to its contents, it&#8217;s more difficult to make use of these techniques. Here&#8217;s one way.</p>
<p><img class="aligncenter size-full wp-image-258" title="Sizing Rollovers" src="http://www.hackification.com/wp-content/uploads/2009/09/SizingRollovers.png" alt="Sizing Rollovers" width="414" height="127" /></p>
<p style="text-align: center;"><em>(The final result &#8211; <a href="http://www.hackification.com/jquery-examples/sizing-rollovers/sizing-rollovers.htm" target="_blank">view demo</a>).</em></p>
<p><strong>Start With Markup</strong></p>
<p>Before diving into fancy techniques, get the markup down as you would like it, if you didn&#8217;t have to style anything.</p>
<pre>&lt;a href="..." class="rollover"&gt;Button Text&lt;/a&gt;</pre>
<p>Ideally we want the markup to be clean and not compromised by styling requirements.</p>
<p><strong>Handle Degradation Gracefully</strong></p>
<p>My technique is going to make use of JavaScript, so let&#8217;s handle the case where the user has it disabled for some reason. Here&#8217;s some very basic CSS to give the button a simple style:</p>
<pre>a.rollover
{
  background-color: #00f;
  border: solid 1px #000;
  color: #fff;
  font-weight: bold;
  padding: 4px;
  text-decoration: none;
}

a.rollover:hover
{
  background-color: #008;
}</pre>
<p><strong>Add Styling Naively</strong></p>
<p>Next, I&#8217;m going to add some rollover styling &#8211; but in a way that isn&#8217;t complete. Firstly I created a simple rollover image, but made it very wide &#8211; it needs to be at least as wide as the widest button is ever going to be:</p>
<p><img class="aligncenter size-full wp-image-256" title="Button Rollover" src="http://www.hackification.com/wp-content/uploads/2009/09/buttons.png" alt="Button Rollover" width="657" height="123" /></p>
<p>I&#8217;m going to define my rollover styling against a class selector of &#8220;extra&#8221; &#8211; you&#8217;ll see where that comes from in a moment.</p>
<pre>a.rollover.extra
{
  background: url(buttons.png) repeat-x left 0;
  border: none;
  height: 27px;
  padding: 23px 40px 10px 40px;
  position: relative;
}

a.rollover.extra:hover
{
  background-position: left -57px;
}</pre>
<p>Note that we&#8217;ve had to undo some of the styles applied previously, such as border.</p>
<p>We&#8217;re kind of getting there, but our buttons won&#8217;t look correct on the right-hand-side.</p>
<p><strong>Use JavaScript To Add Styling Markup</strong></p>
<p>To fix the right-hand end-caps, we need to modify our markup. However, I really don&#8217;t want to have to do that on my pages &#8211; afterall, I might change my mind about the site styling.</p>
<p>Here I recommend using JavaScript to tweak at runtime the markup to what we need. I&#8217;m going to use jQuery for this:</p>
<pre>$(function() {
  $('.rollover')
    .addClass('extra')
    .append('&lt;div class="cap"&gt;&lt;/div&gt;');
});</pre>
<p>Basically, when the pad is ready, I find all elements with class &#8220;rollover&#8221;, add another class (&#8220;extra&#8221;), and append into the rollover button an empty span element which has a class of &#8220;cap&#8221;.</p>
<p>That class &#8220;extra&#8221; triggers the image rollovers. The &#8220;cap&#8221; span can be styled as follows:</p>
<pre>a.rollover.extra .cap
{
  background: url(buttons.png) no-repeat right 0;
  height: 100%;
  position: absolute;
  right: 0;
  top: 0;
  width: 40px;
}

a.rollover.extra:hover .cap
{
  background-position: right -57px;
}</pre>
<p>And voila!</p>
<blockquote><p><a href="http://www.hackification.com/jquery-examples/sizing-rollovers/sizing-rollovers.htm" target="_blank">If you&#8217;d like to see this technique in action, please view the demo</a>.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/09/02/resizing-single-image-rollovers-using-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HOWTO: Fix Windows Explorer After Installing Internet Explorer 8</title>
		<link>http://www.hackification.com/2009/07/10/howto-fix-windows-explorer-after-installing-internet-explorer-8/</link>
		<comments>http://www.hackification.com/2009/07/10/howto-fix-windows-explorer-after-installing-internet-explorer-8/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 17:53:43 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[ie8]]></category>
		<category><![CDATA[internet explorer 8]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=236</guid>
		<description><![CDATA[I wrote an article about this problem a while back &#8211; but it seems that the fix is relevant to more than just 64-bit installs of Vista, so I thought I&#8217;d re-release it in the hope that it reaches and helps a few more people. Unfortunately, it seems that installing Internet Explorer 8 (IE8) sometimes [...]]]></description>
			<content:encoded><![CDATA[<p><em>I wrote an <a href="http://www.hackification.com/2009/04/01/howto-repair-your-vista-64-bit-system-after-internet-explorer-8/" target="_blank">article</a> about this problem a while back &#8211; but it seems that the fix is relevant to more than just 64-bit installs of Vista, so I thought I&#8217;d re-release it in the hope that it reaches and helps a few more people.</em></p>
<p><span id="more-236"></span></p>
<p>Unfortunately, it seems that installing Internet Explorer 8 (IE8) sometimes has some adverse affects on other applications. Here’s how to fix this.</p>
<p>The problems we’ve experienced following installation of IE8 have been:</p>
<ul>
<li>Windows Explorer opens folders in new windows, even if you have “Open each window in the same folder” checked;</li>
<li>“Open in new tab” doesn’t work in IE8;</li>
<li>Can’t connect to databases using SQL Management Studio Express 2008 (”Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘Microsoft.VisualStudio.OLE.Interop.IServiceProvider’. Exception from HRESULT: 0×80004002 (E_NOINTERFACE)”).</li>
</ul>
<p>The solution is pretty simple:</p>
<ol>
<li>Go to Start -&gt; All Programs -&gt; Accessories, right-click “Command Prompt”, and choose “Run as Administrator”.</li>
<li>OK the user access warning dialogs.</li>
<li>Enter “regsvr32 actxprxy.dll”.</li>
<li>Reboot.</li>
</ol>
<p>Simples! No more tearing my hair out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/07/10/howto-fix-windows-explorer-after-installing-internet-explorer-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debug.Assert Considered Pointless</title>
		<link>http://www.hackification.com/2009/06/10/debug-assert-considered-pointless/</link>
		<comments>http://www.hackification.com/2009/06/10/debug-assert-considered-pointless/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 20:26:24 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[assert]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=214</guid>
		<description><![CDATA[One thing I was told as a young programmer was to make good use of assertions for checking code. As time goes by however, I can see less and less use for assertions. I&#8217;m starting to think they&#8217;re pretty useless. (I&#8217;m going to use C# in my examples here: some of these points might not [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-225" style="margin-left: 10px; margin-right: 10px;" title="Debug Assert Stop" src="http://www.hackification.com/wp-content/uploads/2009/06/stop.png" alt="Debug Assert Stop" width="96" height="96" />One thing I was told as a young programmer was to make good use of assertions for checking code. As time goes by however, I can see less and less use for assertions. I&#8217;m starting to think they&#8217;re pretty useless.</p>
<p><span id="more-214"></span></p>
<p>(I&#8217;m going to use C# in my examples here: some of these points might not apply to all languages. By default, an assertion will not fire in a C# release build, and furthermore, the code inside will not even be executed).</p>
<p><em>Update 2009-06-11: I probably haven&#8217;t been clear enough, but luckily various commenters have picked up on this: I&#8217;m not advocating removing assertions and replacing them with nothing! In general, I think assertions should be replaced with a stronger check that also runs (or can run) in a release build.</em></p>
<p><strong>1. Pointless Guard Checking</strong></p>
<p>Unfortunately in my coding travels I see quite a bit of code like this:</p>
<pre>void Foo(Bar bar)
{
  Debug.Assert(bar != null);

  bar.DoSomething();
}</pre>
<p>What&#8217;s the point? If bar is null, then using it will throw an exception anyway. Why add unnecessary lines of code that make reading harder?</p>
<p><strong>2. Bad Argument Checking</strong></p>
<p>But, you&#8217;re thinking, what if the usage is separated from the assignment?</p>
<pre>class Foo
{
  public Foo(Bar bar)
  {
    Debug.Assert(bar != null);

    _bar = bar;
  }

  public void Method()
  {
    _bar.DoSomething();
  }

  private Bar _bar;
}</pre>
<p>True, you want to catch the error as it happens, not later on in the method. But shouldn&#8217;t you be throwing an ArgumentNullException instead?</p>
<p><strong>3. Difficult Detective Work</strong></p>
<p>So now you&#8217;re telling me that you&#8217;re not writing component code, and the only client of this class will be the application it&#8217;s embedded in. Why not save some effort and skimp on the checking code, and make it debug-only?</p>
<p>Fine idea&#8230; until you get a crash report from one of your users, and the stacktrace points to a NullReferenceException in Method(). Now you have to figure out where the null value got passed to the constructor&#8230; a potentially much more difficult task.</p>
<p><strong>4. Differences Between Debug and Release</strong></p>
<p>If you&#8217;re like me, you generally run the applications in debug. Your customers however, run in release. Why make the differences between these two versions any bigger? Even if you don&#8217;t intend your debug statements to change program state, it&#8217;s all too easy to accidentally do so:</p>
<ul>
<li>Running lazy-initializers or singleton constructors;</li>
<li>Loading data from the database via an ORM;</li>
<li>Moving data in or out of a cache;</li>
<li>Running static constructors.</li>
</ul>
<p><strong>5. Too-Shy Sanity-Checking</strong></p>
<p>Sometimes, despite the above warning, you really do have sanity-checking code that you&#8217;d like to run in a development or test environment to catch regressions, but which is too slow to give to general users. (I read somewhere that Excel has two computation engines: a simple, slow, reference one, and a fast, parallel, release one. Testers can run the two in parallel, and any differences are automatically flagged as errors).</p>
<p>So you might be tempted to do something like:</p>
<pre>Debug.Assert( SlowlyAndCarefullyVerifyInternalState() );</pre>
<p>The problem I have with this is that the debug/release division is way too blunt.</p>
<ul>
<li>Suppose a tester wants to test a release build, but also perform the sanity-checking?</li>
<li>Suppose a customer finds a bug in a complex calculation, and you want them to run the checks?</li>
<li>Suppose a developer wants to debug performance issues in the non-checked code?</li>
</ul>
<p>I would suggest you move the conditionals for code like this to a (possibly hidden) configuration setting. Someone wants to run in slow-and-safe mode? No problem, just tell them the registry setting (or whatever). By all means, change the splash screen or title bar or whatever so it says &#8220;test mode&#8221;.</p>
<p>The advantages of this method are:</p>
<ul>
<li>Anyone can turn it on and off;</li>
<li>Sanity-checking can be controlled at a much finer granularity than all-on / all-off.</li>
</ul>
<p><strong>6. Is It Recoverable, Or Not?</strong></p>
<p>The final argument against debug assertions concerns just exactly what an assertion means.</p>
<p>To my mind, an assertion is different to a simple flow-control test (&#8220;did the user enter a valid number?&#8221;), which is something the program can (should) handle. By definition, an assertion is checking for a disallowed state. An assertion doesn&#8217;t check for incorrect data; it checks for an incorrect program. Continuing after an assertion leads to undefined behaviour.</p>
<p>So how can you continue? More to the point, why should a user even be allowed to continue? If the state really is unrecoverable, then fail. Fail fast, log the error, and bail (possibly trying to allow the user to save work, or whatever). Having a release-build application continue after what would have been an assertion is just plain wrong.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/06/10/debug-assert-considered-pointless/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Forcing The Browser To Cache Dynamic Content In ASP.NET</title>
		<link>http://www.hackification.com/2009/05/01/forcing-the-browser-to-cache-dynamic-content-in-aspnet/</link>
		<comments>http://www.hackification.com/2009/05/01/forcing-the-browser-to-cache-dynamic-content-in-aspnet/#comments</comments>
		<pubDate>Fri, 01 May 2009 09:14:41 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[caching]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=143</guid>
		<description><![CDATA[Usually, you don&#8217;t want the browser doing any kind of caching on dynamic content served from a generic handler (.ASHX) in ASP.NET &#8211; afterall, the content is usually changing (dynamic). Sometimes however, it&#8217;s handy to use a handler to serve content that effectively never changes. Here&#8217;s how. One common example (that we make use of [...]]]></description>
			<content:encoded><![CDATA[<p>Usually, you don&#8217;t want the browser doing any kind of caching on dynamic content served from a generic handler (.ASHX) in ASP.NET &#8211; afterall, the content is usually changing (dynamic). Sometimes however, it&#8217;s handy to use a handler to serve content that effectively never changes. Here&#8217;s how.</p>
<p><span id="more-143"></span>One common example (that we make use of heavily in <a title="EasyAs123Web.com" href="http://www.easyas123web.com" target="_blank">EasyAs123Web.com</a>) is using a handler to serve and generate images on-the-fly. Our user images are stored in the database, and are immutable &#8211; they never change. Hence we&#8217;d like the client browser to cache the image returned.</p>
<p><strong>Example Handler</strong></p>
<p>Let&#8217;s start with the shell of a generic handler, MyHandler.ashx.cs:</p>
<pre>public class MyHandler : IHttpHandler
{
  public void ProcessRequest( HttpContext context )
  {
    ...
  }

  public bool IsReusable
  {
    get
    {
      return true;
    }
  }
}</pre>
<p>Further code will go where I&#8217;ve marked with ellipses.</p>
<p><strong>Reponse Headers</strong></p>
<p>For some reason, ASP.NET automatically adds headers that stop the browser from caching the content, and you can&#8217;t just override them. Instead, you&#8217;ll need to clear them all out first:</p>
<pre>context.Response.ClearHeaders();</pre>
<p>Next we want to mark the content as fully cacheable, and expiring at some date in the future:</p>
<pre>context.Response.Cache.SetValidUntilExpires( true );
context.Response.Cache.SetCacheability( HttpCacheability.Public );
context.Response.Cache.SetExpires( DateTime.Now.AddMonths( 1 ) );
context.Response.Cache.SetLastModified( DateTime.Now.AddMonths( -1 ) );</pre>
<p>(I&#8217;ve marked the content as expiring in a month, but you could easily add a year or ten years or whatever).</p>
<p>If you are able to calculate some form of hash for your content (which you should be able to do pretty easily), you can add an <a href="http://en.wikipedia.org/wiki/HTTP_ETag" target="_blank">ETag</a>:</p>
<pre>Guid hash = ...;

context.Response.Cache.SetETag( "\"" + hash.ToString().Replace( "-", "" ) + "\"" );</pre>
<p>Finally, there&#8217;s one more trick which is incredibly useful for allowing browsers to avoid re-downloading the content.</p>
<p>Browsers may include the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25" target="_blank">&#8220;If-Modified-Since&#8221; request header</a>. If you ignore this header, no problems occur: you just send the content as usual. However, you can use it to inform the browser that your content hasn&#8217;t changed since the last time it was requested.</p>
<p>If your content really hasn&#8217;t changed, instead of sending a &#8220;200 OK&#8221; response with all the content, you can send back a &#8220;304 Not Modified&#8221; without any content whatsoever:</p>
<pre>var textIfModifiedSince = context.Request.Headers["If-Modified-Since"];

if( !string.IsNullOrEmpty( textIfModifiedSince ) )
{
  context.Response.Status = "304 Not Modified";
  context.Response.End();

  return;
}</pre>
<p>So instead of sending back a potentially large piece of content, you can send back just the tiny response headers.</p>
<p>You can do a similar trick with <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26" target="_blank">&#8220;If-None-Match&#8221;</a> &#8211; I&#8217;ll leave that as an exercise.</p>
<p><strong>Debugging</strong></p>
<p>Making these changes is all very well, but can you be sure that your changes are actually having the desired effects?</p>
<p>There are two great tools you can use to actually view the requests sent by the browser, and the response sent from your web-app:</p>
<ul>
<li>For debugging with Firefox: <a href="https://addons.mozilla.org/en-US/firefox/addon/3829" target="_blank">Live HTTP Headers</a>.</li>
<li>For Internet Explorer: <a href="http://www.fiddler2.com/fiddler2/" target="_blank">Fiddler 2</a>.</li>
</ul>
<p>If you care about the speed and bandwidth usage of your ASP.NET application, I&#8217;d say that these two are pretty much indispensable. You&#8217;ll want to test with both: different browsers make subtly different requests, and interpret the responses differently.</p>
<p>You might also want to use <a href="https://addons.mozilla.org/en-US/firefox/addon/5369" target="_blank">YSlow</a> for Firebug: this has some nice graphical displays of page assets.</p>
<p><strong>Example &#8211; Content Not In Cache</strong></p>
<p>Request and response, some lines omitted for clarity.<strong><br />
</strong></p>
<pre>GET /!!/_Serving/StaticFile/Style/Pro/Images/HomeCreate.jpg HTTP/1.1
Host: www.easyas123web.com</pre>
<pre>HTTP/1.x 200 OK
Cache-Control: public
Content-Type: image/jpeg
Expires: Mon, 01 Jun 2009 08:58:36 GMT
Last-Modified: Wed, 01 Apr 2009 08:58:36 GMT
Etag: "45403f772717d1c63c0b8774e4a124b7"
Content-Length: 20696
... 20K of content here ...</pre>
<p><strong>Example &#8211; Content In Cache</strong></p>
<p>Request and response, some lines omitted for clarity.<strong><br />
</strong></p>
<pre>GET /!!/_Serving/StaticFile/Style/Pro/Images/HomeCreate.jpg HTTP/1.1
Host: www.easyas123web.com
If-Modified-Since: Wed, 01 Apr 2009 08:58:36 GMT</pre>
<pre>HTTP/1.x 304 Not Modified
Cache-Control: public
Expires: Mon, 01 Jun 2009 09:03:51 GMT
Last-Modified: Wed, 01 Apr 2009 09:03:51 GMT
Etag: "45403f772717d1c63c0b8774e4a124b7"</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/05/01/forcing-the-browser-to-cache-dynamic-content-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Consolas: Visual Studio&#039;s Hidden Gem</title>
		<link>http://www.hackification.com/2009/04/16/consolas-visual-studios-hidden-gem/</link>
		<comments>http://www.hackification.com/2009/04/16/consolas-visual-studios-hidden-gem/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 11:44:45 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[consolas]]></category>
		<category><![CDATA[fonts]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=112</guid>
		<description><![CDATA[Out of the box, Visual Studio uses the Courier New font for displaying code. While that&#8217;s an acceptable font for programming, it&#8217;s hardly modern or easy on the eye. Did you know that Microsoft supply a much more readable alternative, for free? Courier New has been around since 1955 &#8211; and that it&#8217;s been around [...]]]></description>
			<content:encoded><![CDATA[<p>Out of the box, Visual Studio uses the Courier New font for displaying code. While that&#8217;s an acceptable font for programming, it&#8217;s hardly modern or easy on the eye. Did you know that Microsoft supply a much more readable alternative, for free?</p>
<p><span id="more-112"></span>Courier New has been around since 1955 &#8211; and that it&#8217;s been around so long is a testament to its readability. However since then, display technology has radically changed &#8211; I&#8217;d be amazed if you weren&#8217;t reading this article on some form of TFT panel, and if using Windows, with ClearType enabled.</p>
<p>Enter Consolas.  For the release of Office 2007, Microsoft commissioned a set of modern fonts, designed to be highly readable on screens that use ClearType: Calibri, Candara, Cambria, Consolas, Constantia and Corbel. Of that set, Consolas makes an <em>excellent </em>monospaced code font.</p>
<p><strong>Here&#8217;s Courier New:</strong></p>
<p><img class="alignnone size-full wp-image-115" title="Courier New Example" src="http://www.hackification.com/wp-content/uploads/2009/04/couriernew.png" alt="Courier New Example" width="350" height="256" /></p>
<p><strong>And here&#8217;s Consolas:</strong></p>
<p><img class="alignnone size-full wp-image-116" title="Consolas Example" src="http://www.hackification.com/wp-content/uploads/2009/04/consolas.png" alt="Consolas Example" width="306" height="241" /></p>
<p>To my eyes, that&#8217;s a big improvement. You&#8217;ll notice as well that Consolas is slightly narrower, enabling you to fit a bit more text on the screen.</p>
<p><strong>So where can you get it?</strong></p>
<p>If you have Visual Studio, you can download it directly from Microsoft:</p>
<p><a href="http://www.microsoft.com/downloads/details.aspx?familyid=22e69ae4-7e40-4807-8a86-b3d36fab68d3&amp;displaylang=en" target="_blank">http://www.microsoft.com/downloads/details.aspx?familyid=22e69ae4-7e40-4807-8a86-b3d36fab68d3&amp;displaylang=en</a></p>
<p>Or if not, it comes as part of the free Powerpoint viewer:</p>
<p><a href="http://www.microsoft.com/downloads/details.aspx?familyid=048DC840-14E1-467D-8DCA-19D2A8FD7485&amp;displaylang=en" target="_blank">http://www.microsoft.com/downloads/details.aspx?familyid=048DC840-14E1-467D-8DCA-19D2A8FD7485&amp;displaylang=en</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/04/16/consolas-visual-studios-hidden-gem/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Using WPF To Generate Web Images</title>
		<link>http://www.hackification.com/2009/04/10/using-wpf-to-generate-web-images/</link>
		<comments>http://www.hackification.com/2009/04/10/using-wpf-to-generate-web-images/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 21:27:37 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=95</guid>
		<description><![CDATA[Recently I needed to display rotated graphics within a web-page. Since there&#8217;s no way to do that cross-browser using CSS, I needed to auto-generate a collection of pre-rotated images that could be displayed as CSS sprites. I&#8217;ve found that WPF (Windows Presentation Foundation) is great for generating batches of images suitable for use in web [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to display rotated graphics within a web-page. Since there&#8217;s no way to do that cross-browser using CSS, I needed to auto-generate a collection of pre-rotated images that could be displayed as CSS sprites. I&#8217;ve found that WPF (Windows Presentation Foundation) is great for generating batches of images suitable for use in web apps.</p>
<p><span id="more-95"></span></p>
<p>Let&#8217;s start with an example graphic, Arrow.xaml:</p>
<pre>&lt;UserControl x:Class="WebImagesGenerator.Graphics.Arrow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Height="64" Width="64"&gt;
  &lt;Canvas Width="64" Height="64"&gt;
    &lt;Polygon Points="32,4 60,32 42,32 42,60 22,60 22,32 4,32"
      Fill="Red" Stroke="Black"
      StrokeThickness="3" StrokeLineJoin="Round" /&gt;
  &lt;/Canvas&gt;
&lt;/UserControl&gt;</pre>
<p>That&#8217;s just a standard XAML file, nothing fancy there.</p>
<p>Rendering WPF content to an image is really easy; you use the RenderTargetBitmap class. I wanted to be able to produce a strip of images, all the same except rotated, that can be used as CSS sprites. For my actual web application, I have a quick console application that generates the images as a batch. If I ever change the XAML files, I can quickly re-generate all the auto-generated images in my project just be running my console application.</p>
<p>Let&#8217;s get started on the console application:</p>
<pre>[STAThread]
static void Main( string[] args )
{
  var arrow = new Graphics.Arrow();

  SaveStrip( arrow, GenerateSeries( 0, 360, 10 )
           , "..\\..\\Testing\\arrows.png" );
}</pre>
<p>WPF must be run in a [STAThread] context. I create an instance of my graphic, then call my SaveStrip routine. GenerateSeries simply returns an array of integers:</p>
<pre>static int[] GenerateSeries( int start, int end, int step )
{
  var series = new List&lt;int&gt;();

  for( var v = start; v &lt; end; v += step )
  {
    series.Add( v );
  }

  return series.ToArray();
}</pre>
<p>The real magic happens in SaveStrip:</p>
<pre>static void SaveStrip( FrameworkElement fe, int[] angles, string filename )
{
  var outer = new Canvas();

  outer.Width = fe.Width * angles.Length;
  outer.Height = fe.Height;

  outer.Children.Add( fe );

  outer.Arrange( new Rect( 0, 0, outer.Width, outer.Height ) );

  var bmp = new RenderTargetBitmap
    ( (int) fe.Width * angles.Length
    , (int) fe.Height, 96, 96, PixelFormats.Pbgra32 );

  for( var i = 0; i &lt; angles.Length; ++i )
  {
    var transforms = new TransformGroup();

    transforms.Children.Add
      ( new RotateTransform( angles[i], fe.Width / 2, fe.Height / 2 ) );

    fe.RenderTransform = transforms;
    fe.SetValue( Canvas.LeftProperty, i * fe.Width );

    // The following line is VITAL!
    outer.Arrange( new Rect( 0, 0, outer.Width, outer.Height ) );

    bmp.Render( fe );
  }

  var encoder = new PngBitmapEncoder();

  encoder.Frames.Add( BitmapFrame.Create( bmp ) );

  using( var stream = File.Create( filename ) )
  {
    encoder.Save( stream );
  }
}</pre>
<p>When I first tried this technique, all my output bitmaps were completely blank. After debugging for a while, I realised that all my graphics were of zero size. You MUST call Arrange() on your elements before calling Render(), otherwise they won&#8217;t have been laid out properly.</p>
<p>When run, this console app produces the following (reduced in size to fit):</p>
<p><img class="alignnone size-full wp-image-102" title="WPF-Generated Images" src="http://www.hackification.com/wp-content/uploads/2009/04/arrows1.png" alt="WPF-Generated Images" width="580" height="15" /></p>
<p>Fantastic! To get a better idea of how such images might appear in a web-page, I&#8217;ve created a little HTML+jQuery demo to see a very quick example of the images in use:</p>
<blockquote><p><strong><a href="http://www.hackification.com/jquery-examples/test-wpf-images.htm" target="_blank">View Demo</a></strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/04/10/using-wpf-to-generate-web-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HOWTO: Repair Your Vista 64-bit System After Internet Explorer 8</title>
		<link>http://www.hackification.com/2009/04/01/howto-repair-your-vista-64-bit-system-after-internet-explorer-8/</link>
		<comments>http://www.hackification.com/2009/04/01/howto-repair-your-vista-64-bit-system-after-internet-explorer-8/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 18:28:47 +0000</pubDate>
		<dc:creator>Stu Smith</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[EasyAs123Web.com]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[ie8]]></category>

		<guid isPermaLink="false">http://www.hackification.com/?p=86</guid>
		<description><![CDATA[Internet Explorer 8 (IE8) has now been released to the public, and while it&#8217;s not enough to shift me from Firefox, I&#8217;m happy that it offers standards-compliant rendering and a useful &#8220;compatibility view&#8221;, meaning that web developers can essentially test on IE7 and IE8 without needing virtual machines. Unfortunately, when installed on 64-bit Vista (x64), [...]]]></description>
			<content:encoded><![CDATA[<p>Internet Explorer 8 (IE8) has now been released to the public, and while it&#8217;s not enough to shift me from Firefox, I&#8217;m happy that it offers standards-compliant rendering and a useful &#8220;compatibility view&#8221;, meaning that web developers can essentially test on IE7 and IE8 without needing virtual machines. Unfortunately, when installed on 64-bit Vista (x64), it seems to have some adverse affects on other applications. Here&#8217;s how to fix this.</p>
<p><span id="more-86"></span></p>
<p>The problems we&#8217;ve experienced following installation of IE8 have been:</p>
<ul>
<li>Windows Explorer opens folders in new windows, even if you have &#8220;Open each window in the same folder&#8221; checked;</li>
<li>&#8220;Open in new tab&#8221; doesn&#8217;t work in IE8;</li>
<li>Can&#8217;t connect to databases using SQL Management Studio Express 2008 (&#8220;Unable to cast COM object of type &#8216;System.__ComObject&#8217; to interface type &#8216;Microsoft.VisualStudio.OLE.Interop.IServiceProvider&#8217;. Exception from HRESULT: 0&#215;80004002 (E_NOINTERFACE)&#8221;).</li>
</ul>
<p>The solution is pretty simple:</p>
<ol>
<li>Go to Start -&gt; All Programs -&gt; Accessories, right-click &#8220;Command Prompt&#8221;, and choose &#8220;Run as Administrator&#8221;.</li>
<li>OK the user access warning dialogs.</li>
<li>Enter &#8220;regsvr32 actxprxy.dll&#8221;.</li>
<li>Reboot.</li>
</ol>
<p>Sorted! No more tearing my hair out.<a href="http://msmvps.com/blogs/martinpoon/archive/2009/03.aspx" target="_blank"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackification.com/2009/04/01/howto-repair-your-vista-64-bit-system-after-internet-explorer-8/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
