<?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>unitstep.net &#187; General</title>
	<atom:link href="http://unitstep.net/blog/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://unitstep.net</link>
	<description>the home of peter chng</description>
	<lastBuildDate>Mon, 19 Mar 2012 01:49:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Aspect-Oriented Programming</title>
		<link>http://unitstep.net/blog/2009/03/31/aspect-oriented-programming/</link>
		<comments>http://unitstep.net/blog/2009/03/31/aspect-oriented-programming/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 03:50:03 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=767</guid>
		<description><![CDATA[I&#8217;ve started looking at Aspect-Oriented Programming (AOP) recently, because of its potential for improving code readability and maintainability. This is mainly provided by the &#8220;separations of concerns&#8221; goal that AOP aims to achieve. To get started, I decided to jump in using the jQuery AOP plugin. Why jQuery/JavaScript instead of something more mature like AspectJ? [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started looking at <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">Aspect-Oriented Programming (AOP)</a> recently, because of its potential for improving code readability and maintainability.  This is mainly provided by the &#8220;separations of concerns&#8221; goal that AOP aims to achieve.  To get started, I decided to jump in using the <a href="http://plugins.jquery.com/project/AOP">jQuery AOP plugin</a>.  Why jQuery/JavaScript instead of something more mature like <a href="http://www.eclipse.org/aspectj/">AspectJ</a>?  Well, for one thing, I have spent quite a bit of personal time <a href="http://unitstep.net/blog/category/javascript/">developing with JavaScript</a> and it seemed like a logical place to get a good footing before diving deeper with something like AspectJ.</p>
<h2>AOP&#8217;s goals</h2>
<p>The main aims of AOP are to improve program modularity by increasing the &#8220;separation of concerns&#8221;.  What exactly does that mean? To be certain, plain old Object-Oriented Programming (OOP) already drastically increases program modularity; but there is still room for improvement.  In particular, AOP seeks to address the issue of <strong>crosscutting concerns</strong>, that is, functionality which &#8220;cuts across&#8221; or overlaps multiple areas of a system.</p>
<p>For example, most web applications will have <em>authentication</em> and/or <em>authorization</em> as cross-cutting concerns.  Some pseudo-code for showing a user&#8217;s personal &#8220;Todo&#8221; list is shown below:</p>
<pre><code>if (isUserAuthenticated())
{
  items = getUserTodoList(getUserContext());
  foreach (items as item)
  {
    // Output each todo item to the page...
  }
}
else
{
  // Redirect to login page...
}</code></pre>
<p>The act of checking if the user is authenticated has become entangled with the core business logic of showing the items of the &#8220;Todo&#8221; list.  Indeed, this can become cumbersome as the business logic functionality expands; for example, we might have to write such &#8220;boilerplate&#8221; code to check for authentication for each page that requires it.</p>
<p>Of course, this is just a contrived example.  This particular authentication problem has already been nicely solved in many languages such as Java (J2EE) and <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>.  In either of these languages, for instance, you&#8217;ll find multiple frameworks that take the grunt work out of a common problem like this, but it highlights how cross-cutting concerns can interfere with and decrease the modularity of a system, making it harder to maintain.</p>
<p>Separation of concerns aims to refactor this code to reduce the overlap in functionality, so that core business logic can be separated from the cross-cutting concerns, which then become known as <strong>aspects</strong>.  One area that AOP suffers from is the confusing terminology.  Thankfully, the folks behind the great Spring Framework have <a href="http://static.springframework.org/spring/docs/2.5.x/reference/aop.html">written an excellent guide to the terms and AOP itself</a>.  I suggest you take at least a quick look at it.</p>
<h2>Simplicity through Separation</h2>
<p>AOP achieves its goals by defining <strong>join points</strong> where certain <strong>advice</strong> can be executed before or after the code at the join point.  A join point is simply a certain point in the program code and is typically the point where a method begins execution.  </p>
<p>Typically, there are three types of &#8220;advice&#8221; that can be <strong>weaved</strong> or attached to a join point: <strong><em>Before, After</em></strong> and <strong><em>Around</em></strong>.  &#8220;Before&#8221; advice will execute just prior to the method to which it is attached, but cannot modify the execution of that method.  That is, the &#8220;Before&#8221; advice cannot stop the execution of the method.  &#8220;After&#8221; advice will execute directly proceeding the return from the method to which it is attached, and it can typically get access to the return value from the method.</p>
<p>&#8220;Around&#8221; advice is called before the method itself, and <em>can</em> alter the behaviour of the method to which it is attached.  Typically, this includes deciding whether or not the method should be invoked at all, or even modifying the arguments passed to it.  Thus &#8220;Around&#8221; advice is the most powerful.  </p>
<p>It should be noted that for AOP to be truly effective the invocation of the advice attached to a method/join point must be transparent.  That is, once the advice has been attached to a target object/method, one should not have to invoke the target method in any special manner in order to get the advice to fire.</p>
<p>One other term to note is the <strong>pointcut</strong>.  A pointcut is simply a predicate/expression that selects a set of join points.  Thus, by defining a pointcut, advice can be attached to multiple join points that meet/match the pointcut&#8217;s expression.  In fact, this is typically how AOP is modeled from a high-level point-of-view.  When reaching a join point that matches a certain pointcut predicate, the advice associated with that pointcut is invoked.</p>
<h2>AOP with jQuery</h2>
<p>But all of these explanations and terms may still be confusing without a concrete example, so for that we&#8217;ll take a look at the <a href="http://plugins.jquery.com/project/AOP">jQuery AOP</a> plugin.  JavaScript (especially with jQuery) is not really a place where AOP is used heavily or needed, since the use of callbacks already provides some level of separation of concerns.  Indeed, the <a href="http://docs.jquery.com/Ajax">Ajax Events</a> available for hooking into using jQuery already provide some sort of AOP-like functionality.</p>
<p>Having said that, the jQuery AOP plugin is quite easy to use.  Here&#8217;s a quick example that allows you to keep track of the number of event handlers invoked:</p>
<pre><code>Site = new Object();
Site.handlerInvokeCount = 0;
jQuery(function()
{
  // Note: Must apply the advice to the function(s) BEFORE binding as an event
  // handler.
  jQuery.aop.around( {target: window, method: /^handle[A-Z]/},
  function(invocation) {
    window.alert("Invoking `"  + invocation.method + "`");

    window.alert("Number of event handlers invoked so far: " + ++Site.handlerInvokeCount);

    // Note: Can stop the invocation of the target method by *not* calling
    // proceed().
    return invocation.proceed();
  });

  jQuery("form").submit(handleFormSubmission);

});

function handleFormSubmission(e)
{
  e.preventDefault();
  window.alert("Inside handleFormSubmission");
}</code></pre>
<p>Here, we attach advice around any global function whose name starts with &#8220;handle&#8221; and is followed by a camel-case name, such as <code><strong>handleFormSubmission</strong></code>.  We don&#8217;t do anything interesting but increment and report the current count for the number of &#8220;handle&#8221; methods invoked.  We then attach the <code>handleFormSubmission</code> to a form submission event.</p>
<p>It&#8217;s worthwhile to note that you must attach or &#8220;weave&#8221; the aspect to the function <strong>before</strong> binding the function any any events.  This is because when you bind a function to an event, <a href="/blog/2009/03/23/javascript-functions-first-class-objects/">it is copied over by reference</a>.  The jQuery AOP plugin essentially weaves advice to functions by creating a new function that includes both the advice and an invocation of the original function.  However, if you do this <em>after</em> binding the function to an event handler, the event handler will <strong>still refer to the original function</strong>, that is, the one without any advice attached to it!</p>
<h2>Conclusion</h2>
<p>AOP may have limited utility in JavaScript, as I&#8217;ve found, thanks to excellent JavaScript libraries such as jQuery.  jQuery already achieves decent separation of concerns thanks to the many built-in callbacks, mainly for Ajax events.  It also supports the use of <a href="http://docs.jquery.com/Events/trigger">custom events</a>, which may be used in a similar manner to some AOP advice types.</p>
<p>While a full-fledged AOP solution may not be needed in some situations, it&#8217;s worthwhile to note that many frameworks still adopt an AOP-like model.  For example, <a href="http://book.cakephp.org/view/60/Callbacks">CakePHP&#8217;s controllers have an extensive list of callbacks</a> that can be used to control execution of the main controller&#8217;s actions.  These allow you to separate out your concerns from the main program logic for maintainability and reuse.</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2009/03/31/aspect-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bought a PS3</title>
		<link>http://unitstep.net/blog/2008/07/31/bought-a-ps3/</link>
		<comments>http://unitstep.net/blog/2008/07/31/bought-a-ps3/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 00:02:53 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=352</guid>
		<description><![CDATA[After discovering that Rock Band for Wii turned out to be a disappointment, I recently decided to sell it off and save up for a Playstation 3, which I bought last week. I also picked up a copy of Rock Band for PS3 and am now playing Rock Band the way it was meant to [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/07/playstation-3.jpg" alt="" title="playstation-3" /></p>
<p>After discovering that <a href="/blog/2008/06/22/rock-band-finally-comes-to-the-wii/">Rock Band for Wii</a> turned out to be a <a href="http://arstechnica.com/journals/thumbs.ars/2008/07/22/game-review-rock-band-wii">disappointment</a>, I recently decided to sell it off and save up for a Playstation 3, which I bought last week.  I also picked up a copy of Rock Band for PS3 and am now playing Rock Band the way it was meant to be played: With downloadable content (DLC), properly-rendered gameplay and character creation.  (I actually bought a used copy of just the game and then forked over $100 for the PS2 special edition, taking advantage of the current promotion and saving about $40 after taxes)</p>
<p>Does this mean I&#8217;ve lost faith in the <a href="/blog/2007/07/19/wii-will-have-fun/">Wii</a>? Well, probably not.  My disappointment in Rock Band for Wii was not entirely Nintendo&#8217;s fault, even if they have acknowledged <a href="http://arstechnica.com/journals/thumbs.ars/2008/07/24/nintendo-wii-storage-issue-is-urgent">storage/space issues</a>, since many have pointed out that Harmonix may have cut a few corners by simply porting the PS2 version over to Wii, saving time but perhaps selling short its full capabilities.  This point is further underscored by the fact that the upcoming Guitar Hero IV: World Tour, will feature DLC in the Wii version &#8211; though perhaps we&#8217;ll see if that&#8217;s the case when it arrives later this year.</p>
<p>I think a better way to put it is that the Wii and PS3 occupy different segments with only partial overlap.  The Wii has a lot of <a href="/blog/2007/11/27/super-mario-galaxy/">fun</a> <a href="/blog/2008/06/07/bully-scholarship-edition-for-wii-is-a-great-game/">games</a>, but at times the graphical limitations can be annoying; one simply can&#8217;t rely on <a href="http://arstechnica.com/journals/thumbs.ars/2008/07/28/2k-sports-makes-dreams-comes-true-nhl-2k9-hits-the-wii">unique game play</a> for everything.</p>
<p>On the other hand, the PS3 is all about power and features: This thing is very much like a computer in terms of its utility, and I&#8217;m very impressed by what&#8217;s offered.  However, there are many games on PS3 that just don&#8217;t appeal to me, despite their graphical superiority.  Conversely, the fact that it&#8217;s so much like a computer can also be a detriment.  When I&#8217;m playing on a console, I don&#8217;t want to have to think about things like HDD space and so forth &#8211; I just want to game.  However, HDD space seems to be an issue, even with the 40GB PS3.  I only have two games &#8220;installed&#8221; &#8211; Rock Band and GTA IV, and I&#8217;m already using up 10 of the 37 GB available.  Perhaps this is because GTA IV is a space hog, but nevertheless I&#8217;m looking to upgrade to a bigger HDD once I can snag a deal.</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/07/31/bought-a-ps3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The dreaded shipment notice</title>
		<link>http://unitstep.net/blog/2007/09/25/the-dreaded-shipment-notice/</link>
		<comments>http://unitstep.net/blog/2007/09/25/the-dreaded-shipment-notice/#comments</comments>
		<pubDate>Tue, 25 Sep 2007 23:50:51 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[asides]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://unitstep.net/blog/2007/09/25/the-dreaded-shipment-notice/</guid>
		<description><![CDATA[You know the feeling &#8211; you arrive home from school or work to find a slip posted on your door advising you that an attempted delivery of your package failed. The slip will often also say that one or two more attempts will be made, but barring that, you&#8217;ll have to make a trip to [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><a rel="lightbox" href='/wordpress/wp-content/uploads/2007/09/shipping-companies.jpg' title='Shipping companies'><img src='/wordpress/wp-content/uploads/2007/09/shipping-companies.thumbnail.jpg' alt='Shipping companies' /></a></p>
<p>You know the feeling &#8211; you arrive home from school or work to find a slip posted on your door advising you that an attempted delivery of your package failed.  The slip will often also say that one or two more attempts will be made, but barring that, you&#8217;ll have to make a trip to a shipping depot that&#8217;s usually located in the middle of no where to obtain your goods.  </p>
<p>I&#8217;m willing to bet that 90% of adults aren&#8217;t home between the hours of 9 AM &#8211; 5 PM, the time that deliveries usually occur at.  So, why do most shipping companies only delivery parcels at these times?  I understand that likely a majority of their shipments are to business addresses, making the current operating hours suffice for those situations, but certainly a significant number of packages are addressed to homes as well.  While online shopping <a href="http://blogs.consumerreports.org/shopping/2007/06/online_shopping.html">has slowed in recent times</a>, it still accounts for $116 billion (USD) in the United States alone, and all that merchandise has to be shipped.  </p>
<h3>After hours shipping</h3>
<p>So, why not offer &#8220;after-hours&#8221; shipping for the majority of consumers who are working during the day? After all, there are many other services, such as banking, that are starting to be offered into the evening when they&#8217;re more accessible to the general population.  While this would undoubtedly cause some hassles with shipping companies, they&#8217;d save trouble in some respects, as there would be less missed deliveries and fewer repeated attempts.  Additionally, when ordering items, one should be able to indicate whether day or evening service is desired &#8211; this would effectively help separate shipments into &#8220;business hours&#8221; and &#8220;after hours&#8221; categories.</p>
<h3>It&#8217;s not all bad</h3>
<p>What prompted me to think about this are a few recent experiences I had with local delivery companies.  When I ordered furniture from IKEA for <a href="/blog/2007/09/23/life-transitions/">my new apartment</a>, they used a local delivery service to ship the items to me.  Thinking there was no way I&#8217;d be able to catch them, I let the superintendent of my building know about the shipment so that he would let them in on my behalf.  Surprisingly, the delivery man called me and left a message on my answering machine asking what time would be best for <em>me</em>.  Thankfully, after 6 PM worked for both of us.  Maybe this is standard practice for furniture deliveries, but it was still nice not to have that hassle.</p>
<p>I also recently ordered a keyboard tray for my new desk from <a href="http://www.officedepot.ca">Office Depot</a>.  I hate having the keyboard on the same surface as everything else, and the new desk I got from IKEA didn&#8217;t have a keyboard tray.  Shipping was free for the item, and again, the local delivery company (a different one from before) had the courtesy to call me before dropping off the item.  As before, I was able to schedule an after-hours drop off time.</p>
<p>Perhaps I&#8217;m just biased because I don&#8217;t yet have a car and going to out-of-the-way shipping depots is a lot of trouble for me.  However, judging from the <a href="http://unitstep.net/blog/2007/06/29/good-and-bad-companies/">amount of people</a> I saw picking up items at the Purolator shipping depot the last time I was there, I think the service would be warranted.</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2007/09/25/the-dreaded-shipment-notice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

