<?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>a.Tidwell blog &#187; Programming</title>
	<atom:link href="http://aarontidwell.com/wordpress/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://aarontidwell.com/wordpress</link>
	<description>Random thoughts...</description>
	<lastBuildDate>Sat, 05 Feb 2011 05:02:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>PHP vs Node.js for applications</title>
		<link>http://aarontidwell.com/wordpress/2011/02/php-node-js/</link>
		<comments>http://aarontidwell.com/wordpress/2011/02/php-node-js/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 02:36:01 +0000</pubDate>
		<dc:creator>Tidwell</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aarontidwell.com/wordpress/?p=67</guid>
		<description><![CDATA[A coworker of mine asked how I am able to easily switch between Javacript (node.js) and PHP for my server side programming&#8230; This is really a question, it seems, of enabling ones brain to context-switch between procedural and event based programming with minimal effort. The reason, I feel, I am able to do this, is [...]]]></description>
			<content:encoded><![CDATA[<p>A coworker of mine asked how I am able to easily switch between Javacript (node.js) and PHP for my server side programming&#8230;<span id="more-67"></span></p>
<p>This is really a question, it seems, of enabling ones brain to context-switch between procedural and event based programming with minimal effort.  The reason, I feel, I am able to do this, is primarily due to  playing a lot of trading card/board games growing up.  </p>
<p>In these games, sometimes things happen in order (the steps of the turn are: untap, upkeep, draw&#8230;), or sometimes you may be able to respond and deal with situations as they come up (if they attack, I can block, if I block, combat occurs).  This context-switching is easy for humans to do &#8211; go between resolving things in order to responding to events &#8211; but computers don&#8217;t have it quite as easy.  Fortunately, a large subset of server-side web development problems are event-based problems and not procedural problems.  Here is an example:</p>
<p>This is a server-side example, written with the PHP coder in mind &#8211; anyone familiar with node.js will understand the oversimplification, but it proves the point.</p>
<pre>PHP
$result = mysql_query("SELECT * FROM table");
print_r($result);

JAVASCRIPT (in a node.js-ish way)
mysql_query("SELECT * FROM table", function(result) {
    print_r(result);
});
</pre>
<p>So what is the fundamental difference here?  Unfortunately, it isn&#8217;t apparent &#8211; you have to understand what is occurring at a lower level, and it isn&#8217;t perfectly clear what the difference is within the language&#8217;s syntax.  </p>
<p>In PHP, when you ask a database application to retrieve the result of a query<br />
<code><br />
$result = mysql_query("SELECT * FROM table");<br />
</code></p>
<p>You are asking your application to sit idly until the database process returns the information.  Classic, DO THIS, then DO THAT (knowing that THIS has already happened).  </p>
<p>If you want to do a lot of computations in order, depending on the result of previous operations, this makes perfect sense.  However, if you have a state that is independent of previous computation, you don&#8217;t need the inherent &#8220;security&#8221; that procedural programming provides.  Surprisingly, a metric-shit-ton of modern programming has to do with shifting data from POINT A to POINT B.  This is not a procedural problem; it is an event based problem.</p>
<p>Javascript, particularly node.js on the server, is incredibly well-suited to solve this.  For years, web programmers have been writing code that (humanly) reads such:</p>
<pre>
When the user clicks "this button"
  Send a form to the server
    When the data comes back
        Tell the user the response

When the user does something else
  do something immediately
</pre>
<p>This is a series of event callbacks.  It is assumed, that if the user does something else &#8212; After they have clicked &#8220;this button&#8221;, but before the result has come back from the server &#8211; the other event will still &#8220;do something immediately&#8221;.  When the server responds, the implications of that response will occur &#8211; but the other even has already been handled.  This is how asynchronous ajax requests work (if that wasn&#8217;t clear).  To understand why this is beneficial on the server side, take a look at the following:</p>
<pre>
When I get a request for this url:  example.com/example.html
   I need to get the contents of example.html
       Once I have the file, I need to send it back to the client
When I get a request for how long this server has been running, I send back the time
</pre>
<p>Makes sense.  However, in PHP (or any non event-driven environment), your application sits around doing nothing for the time it takes to get the contents of the file before it can send the contents back to the client.  In event-driven server-side environments (node.js), your application can get a request for a file, and make the file system call to retrieve the file, declaring an event (function) to occur when the file system is done (usually sending back the data).  However, in the time between receiving the request for the file, and sending it back, your application is free to do other things (like send how long the server has been running).  You are effectively allowed to not care about the blocking calls you make until those calls return.  (After the file-system has finally figured out WTF example.html actually contains).</p>
<p>If you need the result of a request to be dependent on the result of previous calculations made within that request, or you need to collect a lot of data from different sources to respond to a request, go ahead &#8211; write some PHP or write in any other procedural language of your choice.  </p>
<p>However, if you are just asking the OS to serve files (a static http server), or waiting for another application to return the result of some task (A DATABASE QUERY), your program shouldn&#8217;t wait around until that other process finally gets around to finishing that task.  You should be able to still &#8220;do stuff,&#8221; within your application, and handle the response from the OS/other app whenever that response come back.</p>
<p>Syntactically &#8211; event-driven programming is an effort in indentation.  I am a visual thinker, so my coding style is as follow:</p>
<pre>
inProceduralProgramming();
if thisEventOccurs();
  thenThisEventHappens();
    $variable = anotherFunction();
if thisOtherEventOccured();
  doSomethingElse

inEventProgramming-ThisHappens(
  thenThisEventOccurs(
    whenThatEventIsDoneThisHappens(
      var = anotherFunction
ifSomethingElseHappens(
  wellThenDoThisInstead(
</pre>
<p>Simply redefining the meaning of indention provides clarity to an otherwise complex paradigm &#8211; visualize the multiple routes, and you will quickly see your code as a nested structure of event paths instead of a list of boring instructions and conditional statements.  (A choose your own adventure book! instead of those damn Ikea directions.)</p>
<p>I guess the end point for explaining these contexts in such a verbose way is this:  choose the right tool for the right job &#8211; and adjust your interpretation of syntax between differently constructed languages, because thinking visually helps communicate the structure and architecture of your code.  </p>
<p>Event-based server-side code (node.js) is amazing for games, static web servers, and a host of other concurrent-user problems.  PHP/Java/Ruby/etc are great for systems that depend on a 100% verifiable state (CRUD applications, etc).  You just need to be able to determine which methodology is best applicable to the task at hand &#8211; and chose the technology that makes the actual writing of code as easy as possible.</p>
<p>If this example interests you, please take a look at node.js: <a href="http://www.nodejs.org">http://www.nodejs.org</a></p>
<p>PS: I hope to do a write-up about my node+socket.io game framework in the near future.  You can find it on <a href="http://www.github.com/Tidwell">github.com/Tidwell</a> and see my current WIP state.</p>
]]></content:encoded>
			<wfw:commentRss>http://aarontidwell.com/wordpress/2011/02/php-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jquery + canvas</title>
		<link>http://aarontidwell.com/wordpress/2009/01/jquery-canvas-2/</link>
		<comments>http://aarontidwell.com/wordpress/2009/01/jquery-canvas-2/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 19:13:19 +0000</pubDate>
		<dc:creator>Tidwell</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aarontidwell.com/wordpress/?p=35</guid>
		<description><![CDATA[Why hasn&#8217;t anyone written a good jquery plugin for interacting, manipulating, and animating canvas elements via jquery? I&#8217;ve found some very basic libraries, but nothing that approaches the point where they could be mainstream&#8230; project?]]></description>
			<content:encoded><![CDATA[<p>Why hasn&#8217;t anyone written a good jquery plugin for interacting, manipulating, and animating canvas elements via jquery?  I&#8217;ve found some very basic libraries, but nothing that approaches the point where they could be mainstream&#8230; project?</p>
]]></content:encoded>
			<wfw:commentRss>http://aarontidwell.com/wordpress/2009/01/jquery-canvas-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Now this looks like fun&#8230;</title>
		<link>http://aarontidwell.com/wordpress/2008/04/now-this-looks-like-fun/</link>
		<comments>http://aarontidwell.com/wordpress/2008/04/now-this-looks-like-fun/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 13:10:18 +0000</pubDate>
		<dc:creator>Tidwell</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aarontidwell.com/wordpress/?p=32</guid>
		<description><![CDATA[Its nice that they changed the start date to be Saturday morning &#8211; it might give me time to do it. Looks like fun http://cssoff.com/]]></description>
			<content:encoded><![CDATA[<p>Its nice that they changed the start date to be Saturday morning &#8211; it might give me time to do it.  Looks like fun <img src='http://aarontidwell.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />
<a href="http://cssoff.com/">http://cssoff.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://aarontidwell.com/wordpress/2008/04/now-this-looks-like-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Holy Crap I need this on my ipod&#8230;</title>
		<link>http://aarontidwell.com/wordpress/2008/03/holy-crap-i-need-this-on-my-ipod/</link>
		<comments>http://aarontidwell.com/wordpress/2008/03/holy-crap-i-need-this-on-my-ipod/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 21:37:16 +0000</pubDate>
		<dc:creator>Tidwell</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aarontidwell.com/wordpress/?p=31</guid>
		<description><![CDATA[Found this on Digg earlier today&#8230; Dude is a bit stuttery..but the info is right at least Tag your look and feel on a separate stylesheet, yo.]]></description>
			<content:encoded><![CDATA[<p>Found this on Digg earlier today&#8230;</p>
<p>Dude is a bit stuttery..but the info is right at least <img src='http://aarontidwell.com/wordpress/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Tag your look and feel on a separate stylesheet, yo.</p>
<p>
<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/a0qMe7Z3EYg&#038;hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/a0qMe7Z3EYg&#038;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://aarontidwell.com/wordpress/2008/03/holy-crap-i-need-this-on-my-ipod/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Invisible Myspace Trakzor</title>
		<link>http://aarontidwell.com/wordpress/2007/10/invisible-myspace-trakzor/</link>
		<comments>http://aarontidwell.com/wordpress/2007/10/invisible-myspace-trakzor/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 15:53:00 +0000</pubDate>
		<dc:creator>Tidwell</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aarontidwell.com/wordpress/?p=28</guid>
		<description><![CDATA[So, I&#8217;ll keep this brief &#8211; anyone who has trakzor probably knows I registered for the dumb thing and dropped it on my myspace just because I&#8217;m curious as to how it works (which is now obviously ip logging &#8211; I was testing with proxies yesterday, and they showed up today &#8211; though it may [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ll keep this brief &#8211; anyone who has <a href="http://www.trakzor.com" target="new">trakzor</a> probably knows I registered for the dumb thing and dropped it on my myspace just because I&#8217;m curious as to how it works (which is now obviously ip logging &#8211; I was testing with proxies yesterday, and they showed up today &#8211; though it may read your myspace cookie information which is just scary).  Anyway, I don&#8217;t want to give them any free advertising and their graphics would look ugly on my myspace so all I had to do was modify their code a little bit and it works to not display the graphic, yet still track who looks @ your page&#8230;Heres the couple of steps to compile your code after the cut:</p>
<p><span id="more-28"></span></p>
<p>1.  Go to <a href="http://www.trakzor.com" target="new">Trakzor</a> and click on profile code at the top.  Copy that code into a text editor (or just into your myspace about me section)  then add the following around it:</p>
<pre>
<code>
<div style="display: none; height: 1px; width: 1px;">
THE CODE FROM TRAKZOR GOES HERE
</div>

</code>
</pre>
<p>After, you should have something that looks like this (you will see numbers instead of XXXX in the code, and there will be a space between the &#8216;a&#8217; and &#8216;href&#8217;):</p>
<pre>
<code>
<div style="display: none; height: 1px; width: 1px;">
<ahref="http://www.dlrowehtfodne.com/?ref=XXXXXXX">
<img src="http://www.dlrowehtfodne.com/images/XXXXXXXXX.gif" border="0"></a>
</div>

</code>
</pre>
<p>I&#8217;m 99% sure this works in safari, firefox 1.5+, ie7, and ie6.  I like the service, I just hate giving them advertising and cluttering my myspace with more ads than what myspace forces on ya&#8230;</p>
<p>If anyone hasn&#8217;t registered for trakzor before and you do register and want to get around the &#8220;you have to send out a bulletin&#8221; thing when you first login, then leave a comment &#8211; its a slightly more complicated method of viewing the source and modifying some variables in the URL header info, but I could lead ya through</p>
]]></content:encoded>
			<wfw:commentRss>http://aarontidwell.com/wordpress/2007/10/invisible-myspace-trakzor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sweet Regex thing&#8230;</title>
		<link>http://aarontidwell.com/wordpress/2007/10/sweet-regex-thing/</link>
		<comments>http://aarontidwell.com/wordpress/2007/10/sweet-regex-thing/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 18:53:38 +0000</pubDate>
		<dc:creator>Tidwell</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://aarontidwell.com/wordpress/?p=27</guid>
		<description><![CDATA[Most of you probably will have no idea what this is&#8230;but it its my first example of using regular expressions to do string parsing and feeding that back as a function call..its well documented &#8211; check it after the break]]></description>
			<content:encoded><![CDATA[<p>Most of you probably will have no idea what this is&#8230;but it its my first example of using regular expressions to do string parsing and feeding that back as a function call..its well documented &#8211; check it after the break</p>
<p><span id="more-27"></span></p>
<pre>
<code>
<?php
/*
Takes a string ($text) and replaces all regex matches with the return from a function call of the same name.
Usage:  run_replace_callback($regex, $text);
You can overload the defaults set below in the function call
	overloaded example: run_replace_callback('/:([a-zA-Z_]+)/', 'this is the text :functiontodo');
*/

//the regular expression used for parsing
$regex = '/:([a-zA-Z_]+)/';
//the preface tag to add to the string before calling the function
$func_preface = 'userfunction_';
//the string to be parsed
$text = "This is some text and do the following function :dofunction and more text";

//the functions that render the replacement text - note that all function names must start with the value of $func_preface
//Place all functions below -- if you have a lot, probably will want to put these in another included php file
function userfunction_dofunction() {
  return 'FUNCTION RESULT';
}
//END FUNCTIONS

//the function that takes the matched text, adds the preface, and runs the function if it exists.  Otherwise returns the full match
function run_user_function($matches) {
	global $func_preface;
	// as usual: $matches[0] is the complete match
	// $matches[1] the match for the first subpattern
	$function = $func_preface.$matches[1];
	if(function_exists($function)) {
		return call_user_func($function);
	}
}

//the function that accepts the regex to use and the text to use and runs the preg_replace_callback function
function run_replace_callback($regex_to_use, $text_to_use) {
	echo preg_replace_callback(
			$regex_to_use,
            'run_user_function',
            $text_to_use);
}
?>
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://aarontidwell.com/wordpress/2007/10/sweet-regex-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

