<?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>the kiZZ notes &#187; geek stuffs</title>
	<atom:link href="http://kizzx2.com/blog/index.php/tag/geek-stuffs/feed/" rel="self" type="application/rss+xml" />
	<link>http://kizzx2.com/blog</link>
	<description>random stupid ideas from a narcissistic chode</description>
	<lastBuildDate>Thu, 01 Dec 2011 16:25:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>unit testing is teh suck, or is it?</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/06/unit-testing-is-teh-suck-or-is-it/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/06/unit-testing-is-teh-suck-or-is-it/#comments</comments>
		<pubDate>Tue, 06 May 2008 15:52:36 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=49</guid>
		<description><![CDATA[I was reading about unit testing and I came across Wil&#8216;s blog article: Unit testing is teh suck, which was dated back in 2005. That title looked like it was asking for a flame war (as it happened) and it sounded very interesting to me right after I recently discovered the value in Test Driven [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading about unit testing and I came across <a href="http://www.wilshipley.com/blog/">Wil</a>&#8216;s blog article: <a href="http://www.wilshipley.com/blog/2005/09/unit-testing-is-teh-suck-urr.html">Unit testing is teh suck</a>, which was dated back in 2005. That title looked like it was asking for a flame war (as it happened) and it sounded very interesting to me right after <a href="http://kizzx2.com/blog/?p=46">I recently discovered</a> the value in Test Driven Development.</p>

<p>The article was great, with many valid point. One particular comment from Shalev probably sums up my  view:</p>

<blockquote>I feel as if a great light has finally dawned upon me. The dark hypocrisy&#8217;s of people yelling &#8220;Don&#8217;t Repeat Yourself!&#8221;, and then turning around and writing their application twice have finally given way to the light of a new way.

The fact is that code that fully tests an application will always be bigger than the application code itself. This has always grated against the grain of my least-code-possible programmer soul. I thank you for finally validating what I others claimed a defect in my programming nature.</blockquote>

<p>The least-code-possible programmer soul, huh? That&#8217;s my philosophy all along.</p>

<p>So what&#8217;s my take on unit testing now? To test or not to test? I would say, <strong>be flexible</strong>.</p>

<p>From another of Wil&#8217;s article (forgot which one), he said something along the lines of &#8220;When people first learned objective languages, they go and create a God class, which inherits like 40 subclasses each with probably a couple of lines of code difference&#8221; That&#8217;s right, that&#8217;s probably also the case for unit testing: when people first heard of unit tests, <em>they became advocates</em>. (For the case of unit test, perhaps more people run away than to become advocates, but you get the point)</p>

<p>Does that mean objective languages and OO are teh suck? No! It&#8217;s the master programmer&#8217;s decision as to when to use what tools and when to not use them. These kinds of experience, instead of things like which language 0wnz which language, are what separate generic programmers from the true masters.</p>

<p>Unit testing in general is a good idea, but it becomes an <a href="http://en.wikipedia.org/wiki/Anti-pattern">anti-pattern</a> when one gets restricted by it: waste too much time writing tests other than writing actual code, obsessing over than 10% of code coverage which is just a number (as high-scores from video games) Similarly, I think completely ditching unit tests is a waste of such valuable tools.</p>

<p>In the end, there is no need for black-and-white thinking here, just do what feels right at the moment based on your experience.</p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/06/unit-testing-is-teh-suck-or-is-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>K2HttpRequest: asynchronous HTTP request using IWebBrowser2</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/05/k2httprequest-asynchronous-http-request-using-iwebbrowser2/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/05/k2httprequest-asynchronous-http-request-using-iwebbrowser2/#comments</comments>
		<pubDate>Mon, 05 May 2008 15:34:01 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[BHO]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=47</guid>
		<description><![CDATA[In my previous battle against Internet Explorer add-on (BHO), I discovered some valuable (and scarce) resources to my aid. Pete&#8216;s TinyBHO and CPeteHttpRequest probably saved the most of my day. Then I realized a problem: for the browser add-on that I was writing, I would have to fetch a Web page from a server that [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://kizzx2.com/blog/?p=45">previous battle</a> against Internet Explorer add-on (BHO), I discovered some valuable (and scarce) resources to my aid. <a href="http://petewarden.typepad.com">Pete</a>&#8216;s <a href="http://petesearch.com/wiki/index.php/SimpleBho">TinyBHO</a> and <a href="http://petewarden.typepad.com/searchbrowser/2007/06/xmlhttprequest__1.html">CPeteHttpRequest</a> probably saved the most of my day.</p>

<p>Then I realized a problem: for the browser add-on that I was writing, I would have to fetch a Web page from a server that requires user login. Under typical usage scenarios, the users are assumed to have logged in the server in a previous session, so the Web server will authorize the user from cookies. However, using <code>WinHttp</code> on which <code>CPeteHttpRequest</code> is based on, there seems to be no (simple) ways to obtain the user&#8217;s cookies and embed it in the HTTP request. I couldn&#8217;t find another piece that is as simplistic as <code>CPeteHttpRequest</code> and also able to deal with my problem at hand.</p>

<p>So I wrote one myself.</p>

<p><code>K2HttpRequest</code> has the usage of:</p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">K2HttpRequest request<span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Synchronous</span><br />
wstring innerHTML <span style="color: #008000;">=</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">request</span> <span style="color: #008000;">&#40;</span> L<span style="color: #666666;">&quot;http://www.google.com&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Asynchronous</span><br />
<span style="color: #6666cc; font-weight: bold;">void</span> foo <span style="color: #008000;">&#40;</span> wstring innerHTML <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
request<span style="color: #008000;">.</span><span style="color: #0000FF;">SetOnComplete</span> <span style="color: #008000;">&#40;</span> foo <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
request<span style="color: #008000;">.</span><span style="color: #0000FF;">request</span> <span style="color: #008000;">&#40;</span> L<span style="color: #666666;">&quot;http://www.google.com&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><code>K2HttpRequest</code> uses <code>IWebBrowser2</code> internally, which means it opens a new Internet Explorer instance under the hood and loads the Web page with it. Thus, the server will receive login cookies as if the user visited the Web page. It will fetch the Web page at the specified URL and return the HTML in the <code>&amp;lt;body&amp;gt;</code> element (I actually intended return the whole <code>&amp;lt;html&amp;gt;</code> element but I can&#8217;t find an easy way to do it, as <code>IHTMLDocument2</code> doesn&#8217;t have <code>get_innerHTML()</code>)</p>

<p>This is still a very preliminary version, and will probably improve as my COM/ATL skills improve some day.</p>

<p class="download"><a title="k2httprequest.zip" href="http://kizzx2.com/blog/wp-content/uploads/2008/05/k2httprequest.zip">Download K2HttpRequest</a>(source code ~5KB, Visual C++ 2008)</p>

<p class="download"><em>(Update: The asynchronous part of this doesn&#8217;t work very well. I didn&#8217;t know COM enough to use Marshalling when I wrote this version. I&#8217;ll probably update this some day. If you want to get the asynchrounous part working, you&#8217;ll probably have to add in marshalling yourself :p)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/05/k2httprequest-asynchronous-http-request-using-iwebbrowser2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>why Test Driven Development (TDD) matters</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/02/why-test-driven-development-tdd-matters/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/02/why-test-driven-development-tdd-matters/#comments</comments>
		<pubDate>Fri, 02 May 2008 09:56:16 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[test driven development]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=46</guid>
		<description><![CDATA[Like many programmers who first heard of concepts of Test Driven Development (TDD), I was like &#8220;What the heck? Yet more code to write for something that won&#8217;t produce end user output?&#8221;. I&#8217;ve been puzzling for quite some time to get myself accepting this concept, because Code Complete mentioned it so I thought it must [...]]]></description>
			<content:encoded><![CDATA[<p>Like many programmers who first heard of concepts of <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test Driven Development (TDD)</a>, I was like &#8220;What the heck? Yet more code to write for something that won&#8217;t produce end user output?&#8221;. I&#8217;ve been puzzling for quite some time to get myself accepting this concept, because <a href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;tag=kizzx2-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735619670">Code Complete</a> mentioned it so I thought it must have been a good thing (very great book by the way, every serious programmer should read it).</p>

<p><a href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;tag=kizzx2-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735619670"><img src="http://ecx.images-amazon.com/images/I/51I%2BCgVe4oL._SS500_.jpg" alt="Code Complete by Steve McConnell" /></a></p>

<p>I sometimes write unit tests and I think unit tests are very useful in some tricky areas. It&#8217;s just that I haven&#8217;t been motivated enough to try TDD (test first then write code).</p>

<p>Then today I realized how TDD is a natural progression for people writing unit tests. TDD <em>promotes</em> clean OO designs. In short, I can even almost say that <em>&#8220;If you can test it, it has good design”</em>.</p>

<p>I came across this realization while trying to write unit tests for this piece of JavaScript code<em> afterwards</em>:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* Existing (bad design) JavaScript<br />
&nbsp;*/</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> Gateway <span style="color: #339933;">=</span><br />
<span style="color: #009900;">&#123;</span><br />
ajaxGet<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// AccountManager uses Gateway, this is the “class” we want to test in this example</span><br />
<span style="color: #003366; font-weight: bold;">var</span> AccountManager <span style="color: #339933;">=</span><br />
<span style="color: #009900;">&#123;</span><br />
get<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span> id<span style="color: #339933;">,</span> onComplete <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
Gateway.<span style="color: #660066;">ajaxGet</span> <span style="color: #009900;">&#40;</span> “<span style="color: #339933;">/</span>accounts<span style="color: #339933;">/</span>” <span style="color: #339933;">+</span> id <span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span> profile <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> onComplete <span style="color: #009900;">&#40;</span> profile <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>That’s intuitive enough, huh? As much as I thought it was, until I tried to write unit tests for it using <a href="http://jsmock.sourceforge.net/">JsMock</a>, which has the typical usage of:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* Typical JsMock usage example<br />
*/</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> mockControl <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> Worker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
Worker.<span style="color: #660066;">prototype</span><span style="color: #339933;">&lt;</span>span<span style="color: #339933;">&gt;</span> &nbsp;<span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;=</span><br />
<span style="color: #009900;">&#123;</span><br />
doWork<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
mockControl <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MockControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> testWorker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> workerMock <span style="color: #339933;">=</span> mockControl.<span style="color: #660066;">createMock</span> <span style="color: #009900;">&#40;</span> Worker <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
workerMock.<span style="color: #660066;">expects</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">doWork</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
workerMock.<span style="color: #660066;">doWork</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
workerMock.<span style="color: #660066;">verify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>With the code I have written, it is impossible to do testing with JsUnit:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/**<br />
&nbsp;* Attempt to test my existing code with JsMock (in JsUnit)<br />
&nbsp;*/<br />
<br />
var mockControl;<br />
<br />
function setUp()<br />
{<br />
mockControl = new MockControl();<br />
}<br />
<br />
function testAccountManagerGet()<br />
{<br />
var accountManagerMock = mockControl.createMock ( Gateway );<br />
&lt;span style=&quot;color: red;&quot;&gt;<br />
// ERROR! mockControl.createMock() only accepts a class name,<br />
// but Gateway is a variable name<br />
&lt;/span&gt;<br />
}</div></td></tr></tbody></table></div>

<p>So to make things compatible with JsUnit, I had to rewrote the whole stuff:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* My revised JavaScript compatible with JsUnit<br />
&nbsp;*/</span><span style="color: #003366; font-weight: bold;">function</span> Gateway <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
Gateway.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span><br />
<span style="color: #009900;">&#123;</span><br />
ajaxGet<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> AccountManager <span style="color: #009900;">&#40;</span> gateway <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gateway</span> <span style="color: #339933;">=</span> gateway<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
AccountManager.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span><br />
<span style="color: #009900;">&#123;</span><br />
get<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span> id <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gateway</span>.<span style="color: #660066;">ajaxGet</span> <span style="color: #009900;">&#40;</span> “<span style="color: #339933;">/</span>accounts<span style="color: #339933;">/</span>” <span style="color: #339933;">+</span> id <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/**<br />
&nbsp;* JsUnit tests for my revised code<br />
&nbsp;*/</span><span style="color: #003366; font-weight: bold;">var</span> mockControl <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
mockControl <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MockControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> testAccountManagerGet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #006600; font-style: italic;">// OK now, because Gateway is a class</span><br />
<span style="color: #003366; font-weight: bold;">var</span> gatewayMock <span style="color: #339933;">=</span> mockControl.<span style="color: #660066;">createMock</span> <span style="color: #009900;">&#40;</span> Gateway <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> accountManager <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> AccountManager <span style="color: #009900;">&#40;</span> gatewayMock <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// test fixtures</span><br />
<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> “kizzx2”<span style="color: #339933;">;</span><br />
<br />
gatewayMock.<span style="color: #660066;">expects</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxGet</span> <span style="color: #009900;">&#40;</span> “<span style="color: #339933;">/</span>accounts<span style="color: #339933;">/</span>” <span style="color: #339933;">+</span> id <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
accountManager.<span style="color: #660066;">get</span> <span style="color: #009900;">&#40;</span> id <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
mockControl.<span style="color: #660066;">verify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>The above changes might look redundant at first and not so intuitive. But then I came to think of it, it <strong>removed the dependency </strong>between my AccountManager class and the Gateway class. I wasn’t quite aware of such anti-pattern until JsUnit/JsMock brought it to my attention.</p>

<p>If I had used TDD in the first place, where I would have to write the tests first, it would have <strong>enforced</strong> me to use clean OO models to design things. Now I think that’s a pretty solid justification for using TDD.</p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/02/why-test-driven-development-tdd-matters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>making an Internet Explorer add-on</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/#comments</comments>
		<pubDate>Thu, 01 May 2008 13:37:02 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[drumming]]></category>
		<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[add-on]]></category>
		<category><![CDATA[BHO]]></category>
		<category><![CDATA[browser helper object]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=45</guid>
		<description><![CDATA[I was recently doing a project to make an add-on for Internet Explorer. How do I put it? I must say that was an extremely frustrating experience. The documentation is severely lacking, if not non-existent. One page from MSDN (which states the psarray is a BSTR is even dead wrong). I have never been more [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing a project to make an add-on for Internet Explorer. How do I put it? I must say that was an extremely frustrating experience. The documentation is severely lacking, if not non-existent. <a href="http://msdn.microsoft.com/en-us/library/aa752639(VS.85).aspx">One page from MSDN</a> (which states the <code>psarray</code> is a <code>BSTR</code> is even dead wrong).</p>

<p>I have never been more motivated to contribute to the scene by writing my own &#8220;Internet Explorer add-on writing survival guide&#8221; sort of thing, but then I realized <a href="http://petewarden.typepad.com/">Pete</a> has beat me to it by opening an entire [bho-wiki][wiki].</p>

<p>Nevertheless, I&#8217;m writing this article so more people can find these scarce information on IE add-on. On the other hand, here are some thoughts I had when writing the IE add-on. If you&#8217;re about to write an IE add-on, these would probably be the &#8220;survival guides&#8221; you&#8217;re using (as there aren&#8217;t many).</p>

<p><strong>1. An IE add-on is a Browser Helper Object (BHO)</strong></p>

<p>To save you some time searching in vain, try using BHO in your Google searches. This would give you programming-related information instead of where to download the latest IE add-on product.</p>

<p><strong>2. Use C#</strong></p>

<p>C++ just plain sucks, in terms of writing an IE add-on. Don&#8217;t get me wrong here, the language is marvelous, but Microsoft&#8217;s support just seems broken and unfinished. The whole Win32 hacking, COM stuffs involved when using C++ just makes your life 10 times more difficult.</p>

<p>The interface for C# is much more usable, and you won&#8217;t have to deal with all the bullshits when programming in the outdated (and most probably unmaintained) Win32 API.</p>

<p>Another major issue with C++ is Visual C++ 2008 Express. It doesn&#8217;t come with ATL, which means a ridiculous amount of grunt work dealing with pointers, releasing and stuffs (as illustrated below, as I happened to use VC++ Express). VC# doesn&#8217;t have this problem and it even has a number of libraries built-in (such as regular expressions).</p>

<p><em>Example 1: getElementById from BHO</em></p>

<p><em>Using C++</em></p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IHTMLCollection <span style="color: #000040;">*</span> elements<span style="color: #008080;">;</span><br />
HRESULT hr <span style="color: #000080;">=</span> document<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>get_all <span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>elements <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">long</span> numElements<span style="color: #008080;">;</span><br />
hr <span style="color: #000080;">=</span> elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>get_length <span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>numElements <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
IHTMLElement <span style="color: #000040;">*</span> element<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> numElements<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
VARIANT index<span style="color: #008080;">;</span><br />
index.<span style="color: #007788;">vt</span> <span style="color: #000080;">=</span> VT_I4<span style="color: #008080;">;</span><br />
index.<span style="color: #007788;">intVal</span> <span style="color: #000080;">=</span> i<span style="color: #008080;">;</span><br />
<br />
IDispatch <span style="color: #000040;">*</span> elementDispatch<span style="color: #008080;">;</span><br />
<br />
hr <span style="color: #000080;">=</span> elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>item <span style="color: #008000;">&#40;</span> index, index, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>elementDispatch <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
hr <span style="color: #000080;">=</span> elementDispatch<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>QueryInterface <span style="color: #008000;">&#40;</span> IID_IHTMLElement, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>element <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
elementDispatch<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// After all this hard work, we have finally obtained our element!</span><br />
<span style="color: #666666;">// GetAttribute is a custom function, I didn't even bother to</span><br />
<span style="color: #666666;">// list out it's content here</span><br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> GetAttribute <span style="color: #008000;">&#40;</span> element, L<span style="color: #FF0000;">&quot;id&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> id <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> element<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> element <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">return</span> element<span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p><em>Using C#</em></p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">document<span style="color: #008000;">.</span><span style="color: #0000FF;">getElementById</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><em>Example 2: execute some Javascript</em></p>

<p><em>Using C++</em></p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">BSTR code <span style="color: #000080;">=</span> SysAllocString <span style="color: #008000;">&#40;</span> L<span style="color: #FF0000;">&quot;alert ( 'Hello World' );&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
BSTR language <span style="color: #000080;">=</span> SysAllocString <span style="color: #008000;">&#40;</span> L<span style="color: #FF0000;">&quot;javascript&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
VARIANT result <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
window<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>execScript <span style="color: #008000;">&#40;</span> code, language, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>result <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
SysFreeString <span style="color: #008000;">&#40;</span> code <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
SysFreeString <span style="color: #008000;">&#40;</span> language <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p><em>Using C#</em></p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window<span style="color: #008000;">.</span><span style="color: #0000FF;">execScript</span> <span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;alert ( 'Hello World' )&quot;</span>, <span style="color: #666666;">&quot;javascript&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><strong>3. Use jQuery for DOM manipulation</strong></p>

<p><a href="http://jquery.com">jQuery</a> is a client side JavaScript library to simplify common tasks (the &#8220;write less, do more&#8221; library as they say). I found it delightfully pleasant to use. It has some powerful functions like CSS selectors that would be a pain in the ass to implement in BHO. With jQuery, you can do stuff like:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Find all the &amp;lt;a&amp;gt; elements with class &quot;bookmark&quot; and</span><br />
<span style="color: #006600; font-style: italic;">// target attribute &quot;_blank&quot; and hide</span><br />
<span style="color: #006600; font-style: italic;">// them smoothly using a slow slide up animation</span><br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.bookmark[@target=&quot;_blank&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>To use jQuery, simply use <code>execScript</code> to load the jQuery file on document complete.</p>

<p><strong>4. About XmlHttpRequest
</strong></p>

<p>Fetching external Web pages using client side JavaScript will give you a permission denied error because of the cross-domain policies of most modern browsers. XmlHttpRequest must be done in the BHO. <a href="http://www.ookii.org/">Sven</a> offers <a href="http://www.ookii.org/post/using_ixmlhttprequestonreadystatechange_from_c.aspx">an example code</a> to use IXmlHttpRequest, which is very valuable considering the lack of official documentation. Pete has rolled his own <a href="http://petewarden.typepad.com/searchbrowser/2007/06/xmlhttprequest__1.html">CPeteHttpRequest</a> class using WinHttp.</p>

<p>(Strangely enough, both of the above are coded in C++. I have yet to come across an equally efficient way to do XmlHttpRequest in C#. Please feel free to leave a comment if you have good ideas)</p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>one of the best programmers</title>
		<link>http://kizzx2.com/blog/index.php/2008/04/02/one-of-the-best-programmers/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/04/02/one-of-the-best-programmers/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 16:08:35 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[anything]]></category>
		<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[random thoughts]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=38</guid>
		<description><![CDATA[I just read Teach Yourself Programming in Ten Years by Peter Norvig, the director of research at Google. The paragraph quoted below was pretty gross: One of the best programmers I ever hired had only a High School degree; he&#8217;s produced a lot of great software, has his own news group, and made enough in [...]]]></description>
			<content:encoded><![CDATA[<p>I just read <a href="http://norvig.com/21-days.html">Teach Yourself Programming in Ten Years</a>  by Peter Norvig, the director of research at Google. The paragraph quoted below was pretty gross:</p>

<blockquote>One of the best programmers I ever hired had only a High School degree; he&#8217;s produced a lot of great software, has his own news group, and made enough in stock options to buy his own nightclub.</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/04/02/one-of-the-best-programmers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

