<?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>Cow Computing &#187; Programming</title>
	<atom:link href="http://www.cowcomputing.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cowcomputing.com</link>
	<description>Share Info about Cloud Computing &#38; Programming</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:10:24 +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>White List or Black List</title>
		<link>http://www.cowcomputing.com/2010/04/30/white-list-or-black-list/</link>
		<comments>http://www.cowcomputing.com/2010/04/30/white-list-or-black-list/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 13:56:39 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Black List]]></category>
		<category><![CDATA[Code Readability]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[White List]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=211</guid>
		<description><![CDATA[Ever get stuck in deciding whether to use white list or black list during application development? Imagine, when you have to create an user management module, where certain users would have more access than the others. Will you pick white list or black list when you are writing the logic? e.g. White List &#60;?php // [...]]]></description>
			<content:encoded><![CDATA[<p>Ever get stuck in deciding whether to use white list or black list during application development? Imagine, when you have to create an user management module, where certain users would have more access than the others. Will you pick white list or black list when you are writing the logic? e.g.</p>
<p><span style="text-decoration: underline;"><strong>White List</strong></span></p>
<pre class="brush:php">&lt;?php

// White List
// userid from 1 to 10
if ($userid == 1 || 3 || 5) {
// then allow access to manager admin module
} else {
// normal member module
}

?&gt;</pre>
<p><span id="more-211"></span><br />
<span style="text-decoration: underline;"><strong>Black List</strong></span></p>
<pre class="brush:php">&lt;?php

// Black List
// userid from 1 to 10
if ($userid != 1 || 3 || 5) {
// normal member module
} else {
// then allow access to manager admin module
}

?&gt;</pre>
<p>Both White List and Black List has its own advantage. White List offers better control and security (take the above for instance, when you forgotten to add a particular userid to the black list, anonymous access to admin module would happen; while missing an userid in whitelist would simply cause that user not able to access admin module which is less damaging); while Black List can help reduce the length of the code to get the same logic done.</p>
<p>In my opinion, lots of developers would pick Black List as the preferred approach, since it&#8217;s shorter and faster to write.</p>
<p>There are two guideline which i would suggest to follow when making the decision:</p>
<ol>
<li>Security Concern: Try to use White List whenever possible since it provide better security (this is an important factor especially when your application is already serving more than million of people).</li>
<li>Efficiency: Only use Black List when you have a huge amount of checking to do, which make White List not reasonable. (e.g. White-Listing over thousands/millions of tags created by users)</li>
</ol>
<p>So far i have been following the above rules as far as possible, and it reduces logic bugs and gives better code readability.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/04/30/white-list-or-black-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML 5 nth-Child</title>
		<link>http://www.cowcomputing.com/2010/02/07/html-5-nth-child/</link>
		<comments>http://www.cowcomputing.com/2010/02/07/html-5-nth-child/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 08:45:15 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=174</guid>
		<description><![CDATA[As we all know, HTML 5 is on its way, and there&#8217;s going to be a huge addition to the current syntax. In this post, i shall share with you about the &#8220;nth-child&#8221; feature. Often, we would like to create a table or list with alternate row color as follow: // HTML Snippet &#60;div id="test-area"&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>As we all know, HTML 5 is on its way, and there&#8217;s going to be a huge addition to the current syntax. In this post, i shall share with you about the &#8220;nth-child&#8221; feature. Often, we would like to create a table or list with alternate row color as follow:</p>
<pre class="brush:html">// HTML Snippet
&lt;div id="test-area"&gt;
  &lt;ul&gt;
    &lt;li&gt;row 1&lt;/li&gt;
    &lt;li class="even-row"&gt;row 2&lt;/li&gt;
    &lt;li&gt;row 3&lt;/li&gt;
    &lt;li class="even-row"&gt;row 4&lt;/li&gt;
    &lt;li&gt;row 5&lt;/li&gt;
    &lt;li class="even-row"&gt;row 6&lt;/li&gt;
    &lt;li&gt;row 7&lt;/li&gt;
    &lt;li class="even-row"&gt;row 8&lt;/li&gt;
    &lt;li&gt;row 9&lt;/li&gt;
    &lt;li class="even-row"&gt;row 10&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</pre>
<pre class="brush:css">// CSS Snippet
ul {
  list-style: none;
}

// color the even rows
.even-row {
  background-color: #ccc;
}
</pre>
<p><span id="more-174"></span>The above method is okay when you are working with simple rule, but how about if i only want 1, 7, 13, 19&#8230;(6n+1)th rows to be highlighted. Then you probably need to write some javascript and do calculation to do it. However, with HTML 5, we got a better yet easier way to accomplish this. We will be using the new property &#8220;nth-child&#8221;. Let me illustrate by rewriting the example above:</p>
<pre class="brush:html">// HTML Snippet
&lt;div id="test-area"&gt;
  &lt;ul&gt;
    &lt;li&gt;row 1&lt;/li&gt;
    &lt;li&gt;row 2&lt;/li&gt;
    &lt;li&gt;row 3&lt;/li&gt;
    &lt;li&gt;row 4&lt;/li&gt;
    &lt;li&gt;row 5&lt;/li&gt;
    &lt;li&gt;row 6&lt;/li&gt;
    &lt;li&gt;row 7&lt;/li&gt;
    &lt;li&gt;row 8&lt;/li&gt;
    &lt;li&gt;row 9&lt;/li&gt;
    &lt;li&gt;row 10&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</pre>
<pre class="brush:css">// CSS Snippet
ul {
  list-style: none;
}

// Note that n is a special symbol here
li:nth-child(2n) {
  background-color: #ccc;
}
</pre>
<p>See the power of nth-child? We now no longer need to label alternate rows, instead, we use the nth-child property. We simply tell nth-child with the calculation (e.g. any integers like &#8220;1&#8243;, &#8220;3&#8243;, &#8220;5&#8243; or using the special &#8220;n&#8221; variable for help, like: &#8220;2n&#8221; representing alternative rows, &#8220;5n&#8221; representing every fifth rows or even the word &#8220;odd&#8221; and &#8220;even&#8221;&#8230; you might try to edit the code above to use &#8220;even&#8221; instead of &#8220;2n&#8221; to accomplish the same effect.), and it will apply the CSS for you. To learn more, you might try the <a href="http://www.cowcomputing.com/demo/nth-child-demo/">demo page</a> i made.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/02/07/html-5-nth-child/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thunderbird Extension Development</title>
		<link>http://www.cowcomputing.com/2009/10/12/thunderbird-extension-development/</link>
		<comments>http://www.cowcomputing.com/2009/10/12/thunderbird-extension-development/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 03:09:52 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[thunderbird]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=50</guid>
		<description><![CDATA[Recently, i have been working on an productizing an product of my working company. It involves developing an extension for Thunderbird. During the development process, i have discovered there has been inadequate resources out there for Thunderbird Extension, especially when most developers are interested in working on another Mozilla&#8217;s flagship product &#8212; FireFox. So, i [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, i have been working on an productizing an product of my working company. It involves developing an extension for Thunderbird. During the development process, i have discovered there has been inadequate resources out there for Thunderbird Extension, especially when most developers are interested in working on another Mozilla&#8217;s flagship product &#8212; FireFox. So, i would like to share my experience on this</p>
<h4>To begin, we have to prepare the appropriate directory.</h4>
<p><strong>sample.xpi</strong></p>
<p style="padding-left: 30px;">/install.rdf<br />
/components/<br />
/defaults/<br />
/plugins/<br />
/chrome.manifest<br />
/chrome/icons/default/<br />
/chrome/<br />
/chrome/content<br />
/chrome/skin</p>
<h4><span id="more-50"></span>Then, we have to work on the install.rdf. It&#8217;s an RDF file to document about the extension&#8217;s information.</h4>
<pre class="brush:xml">&lt;?xml version="1.0"?&gt;

&lt;RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#"&gt;

&lt;Description about="urn:mozilla:install-manifest"&gt;
&lt;em:id&gt;sample@steve.chan&lt;/em:id&gt;
&lt;em:name&gt;Sample Plugin&lt;/em:name&gt;
&lt;em:version&gt;1.0&lt;/em:version&gt;
&lt;em:creator&gt;Steve Chan&lt;/em:creator&gt;
&lt;em:description&gt;First Plugin on Mozilla Thunderbird&lt;/em:description&gt;
&lt;em:homepageURL&gt;http://www.codetter.com/&lt;/em:homepageURL&gt;

&lt;em:targetApplication&gt;
&lt;Description&gt;
&lt;em:id&gt;GUID&lt;/em:id&gt;
&lt;em:minVersion&gt;1.5&lt;/em:minVersion&gt;
&lt;em:maxVersion&gt;2.0.0.*&lt;/em:maxVersion&gt;
&lt;/Description&gt;
&lt;/em:targetApplication&gt;

&lt;/Description&gt;
&lt;/RDF&gt;</pre>
<p>Note that,</p>
<ol>
<li>&lt;em:id&gt; shall be filled in with string formatted as &#8220;string&#8221;@&#8221;string&#8221; like an email.</li>
<li>under &#8220;targetApplication&#8221;, the &lt;em:id&gt; shall be a proper GUID, it could be easily gen with tools (Google it) <img src='http://www.cowcomputing.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>For minVersion and maxVersion, they determine which version of Thunderbird could install your plugin. View <a href="https://addons.mozilla.org/en-US/firefox/pages/appversions">Valid Application Versions</a>.</li>
</ol>
<h4>After finishing install.rdf, there&#8217;s another manifest to prepare. It&#8217;s chrome.manifest which describe the extension file structure of yours.</h4>
<pre class="brush:text">content sample chrome/content
overlay chrome://messenger/content/messenger.xul chrome://sample/content/message-overlay.xul

skin sample classic/1.0 chrome/skin/classic/

style chrome://messenger/content/messenger.xul chrome://sample/skin/style.css</pre>
<ol>
<li>content describes the location where content&#8217;s file and folder are located</li>
<li>overlay describes which UI of Thunderbird is to be worked on / Override. (Do read about XUL to get your familiar with this idea)</li>
<li>skin describes the location where the skin&#8217;s file and folder are located</li>
<li>style describes what stylesheet to apply on various UI.</li>
</ol>
<h4>Next step is to create an message-overlay.xul</h4>
<pre class="brush:xml">&lt;?xml version="1.0"&gt;
&lt;overlay id="sample" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"&gt;
&lt;statusbar id="status-bar"&gt;
&lt;statusbarpanel id="my-panel" label="Hello, World!" onclick="alert('Hello World!')"/&gt;
&lt;/statusbar&gt;
&lt;/overlay&gt;</pre>
<ol>
<li>statusbar with id=&#8221;status-bar&#8221; states the merge point of the extension, the while xul describe the addition of an &#8220;Hello, World!&#8221; label on the bottom right corner of Thunderbird (status bar).</li>
<li>The onclick is given a javascript function to create an alert box when the &#8220;Hello, World!&#8221; label is being clicked. (XUL works with javascript!)</li>
</ol>
<h4>Last but not least, we have to package the extension</h4>
<p>Zip all the Content of the extension (without the root folder), and rename .zip into .xpi, and it&#8217;s DONE! Congratulation.</p>
<h6>Ref: <a href="https://developer.mozilla.org/en/Extensions/Thunderbird/Building_a_Thunderbird_extension">https://developer.mozilla.org/en/Extensions/Thunderbird/Building_a_Thunderbird_extension</a></h6>
<h6>***Note: The extension describe in this post does not promise to build a proper extension, it just to give a rough idea on how the process is like, and this is a simplified one. If you need any help, you might leave your comment, and i will try help you as much as i can.</h6>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/10/12/thunderbird-extension-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java &#8211; Cloning an InputStream</title>
		<link>http://www.cowcomputing.com/2009/09/23/java-cloning-an-inputstream/</link>
		<comments>http://www.cowcomputing.com/2009/09/23/java-cloning-an-inputstream/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 02:38:04 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=42</guid>
		<description><![CDATA[There&#8217;s a moment when you have been given an InputStream, with its markSupported returned &#8220;False&#8221;. This means you could only read the stream one and only once, as you can&#8217;t call &#8220;reset&#8221; on it. So, what to do if you want to read the stream for more than one time and be able to re-read [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a moment when you have been given an InputStream, with its markSupported returned &#8220;False&#8221;. This means you could only read the stream one and only once, as you can&#8217;t call &#8220;reset&#8221; on it. So, what to do if you want to read the stream for more than one time and be able to re-read the previously read data. The answer is &#8220;CLONE&#8221;. My very own approach would be to use Apache Commons to turn the stream into string in which it acts as a base. Whenever i need that InputStream, i turns the string back into an stream without affecting the read position and data lost. OK, here&#8217;s the steps.</p>
<p>Suppose you got a request object with InputStream.</p>
<pre class="brush:java">// request is the Request Object
// Turns InputStream into string
String base = IOUtils.toString(request.getInputStream());

// Whenever you need the InputStream
// Turn the string back to InputStream this way
InputStream in = new ByteArrayInputStream(base.getBytes());</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/23/java-cloning-an-inputstream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET code to interact with Java&#8217;s Web Service</title>
		<link>http://www.cowcomputing.com/2009/09/07/net-code-to-interact-with-javas-web-service/</link>
		<comments>http://www.cowcomputing.com/2009/09/07/net-code-to-interact-with-javas-web-service/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 10:51:45 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Service]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=35</guid>
		<description><![CDATA[Recently, i am working on a library to allow .NET developer to interact with a web service built on Java. It&#8217;s a SwA (Soap with Attachment). The reason for me to do this is because there&#8217;s been no support for such operation in .NET framework and users who want to deal with such scenario will [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, i am working on a library to allow .NET developer to interact with a web service built on Java. It&#8217;s a SwA (Soap with Attachment). The reason for me to do this is because there&#8217;s been no support for such operation in .NET framework and users who want to deal with such scenario will have to write their own code bit by bit to accomplish the job, which is TEDIOUS&#8230;</p>
<p>The library i built relies on an open source library called <a href="http://www.pocketsoap.com">PocketSoap</a>. Its support on Attachment and its ease of use is appealing and i at last chosen to give it a try and build a POC on top of it. Below is a simple usage sample for the PocketSoap library.</p>
<pre class="brush:csharp">using System;
using PocketSOAP;
using PocketSOAPAttachments;

namespace PSTryout
{
    public class psWS
    {
        CoEnvelope e = new CoEnvelopeClass();
        e.Body.Create("id", 1, "", null, null);
        e.EncodingStyle = "";

        HTTPTransport h = new HTTPTransport();
        h.SOAPAction = "query";
        h.Timeout = 30000;
        h.Send("http://localhost:8080/ws/", e.Serialize());

        string enc = "";
        e.Parse(h, enc);
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/07/net-code-to-interact-with-javas-web-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JAVA Substring Bug!</title>
		<link>http://www.cowcomputing.com/2009/09/06/java-substring-bug/</link>
		<comments>http://www.cowcomputing.com/2009/09/06/java-substring-bug/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 09:53:41 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Bug]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=24</guid>
		<description><![CDATA[Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when we are working with string in java, we are always tempted to use the substring method for extracting part of a string. It’s neat and efficient when you are working with small program or little string processing. However, this method is evil. It is actually causing you whole lots of problem. The substring returns the part of the original string rather than a new one. Thus, GC wont collect the original string. So when the file or string get larger and larger, it’s actually taking up more and more memory, and thus OutOfMemory Error. To solve this, there’s nice workaround which is simple.</p>
<pre class="brush:java">String newString = new String( oldString.subString(0, 5) );</pre>
<p>Simple, Right?!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/06/java-substring-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Write Better Code</title>
		<link>http://www.cowcomputing.com/2009/09/06/how-to-write-better-code/</link>
		<comments>http://www.cowcomputing.com/2009/09/06/how-to-write-better-code/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 09:45:46 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://steve.katyyeung.com/?p=7</guid>
		<description><![CDATA[1. Use Descriptive Variable Names. Never ever try to use meaningless or single-character name for variables. This could mess up the whole class. Even when doing temporary storage, we should stick to some kind of rules like, having a prefix of “tmp” followed by meaningful name. 2. Remove unnecessary header Not only will this save [...]]]></description>
			<content:encoded><![CDATA[<h3>1. Use Descriptive Variable Names.</h3>
<p>Never ever try to use meaningless or single-character name for variables. This could mess up the whole class. Even when doing temporary storage, we should stick to some kind of rules like, having a prefix of “tmp” followed by meaningful name.</p>
<h3>2. Remove unnecessary header</h3>
<p>Not only will this save the time of code compilation but also reduce the ambiguity when using certain implementation type.</p>
<h3>3. When reusing code, NO CUT &amp; PASTE</h3>
<p>There’s always moments when we want to perform some sort of operation that has been coded before. DON’T COPY &amp; PASTE. Rather, we should create a function to encapsulate them, and reuse it. This does not only reduce lines of code, but also provide more clean and generic approach on refactoring code which could ease the trouble of test-case writing.</p>
<h3>4. Know what you are CATCHING</h3>
<p>Catching exception has been widely adopted in java and it’s sort of compulsory. In CECID’s code base, this is true as well. So one important thing to note is that, we should understand what we are catching when writing “catch”. Often eclipse and other IDE will generate codes for you. But, do you know why? Always include useful error code/warning rather than empty catch blocks.</p>
<h3>5. Don’t retain something useless</h3>
<p>Often functions are written to return value. However, this does not necessary mean you MUST store that return value. Sometimes, in certain circumstances, return value are useless, don’t keep them, it could create confusion. When you keep them, i would expect it to be used somewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2009/09/06/how-to-write-better-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
