<?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"
	>

<channel>
	<title>Cognitive Cow</title>
	<atom:link href="http://cognitivecow.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cognitivecow.com</link>
	<description>I am not a cow.</description>
	<pubDate>Thu, 09 Oct 2008 15:12:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Thoughts in No Particular Order</title>
		<link>http://cognitivecow.com/2008/10/09/thoughts-in-no-particular-order/</link>
		<comments>http://cognitivecow.com/2008/10/09/thoughts-in-no-particular-order/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 15:12:03 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=181</guid>
		<description><![CDATA[The phrase &#8216;as it were&#8217; is amazing.  It&#8217;s great, just one of those things you can say but is most of the time implied with the use of metaphor.  I&#8217;m guessing that philosophers love this phrase the most so they can protect themselves from their own metaphorical terminology.  I love hearing this [...]]]></description>
			<content:encoded><![CDATA[<p>The phrase &#8216;as it were&#8217; is amazing.  It&#8217;s great, just one of those things you can say but is most of the time implied with the use of metaphor.  I&#8217;m guessing that philosophers love this phrase the most so they can protect themselves from their own metaphorical terminology.  I love hearing this phrase used in conversation.  Consequently, I dislike when people misuse it.</p>
<p>Human emotion is a fickle and misunderstood thing.  A man who wins the lottery might find himself more unhappy than when he was working for $10 an hour.  A paraplegic might live a more fulfilling life than somebody without any disabilities.  I don&#8217;t just say this without evidence, this has been observed and studied.  What, then, is the very thing that triggers a feeling of happiness instead of just general contentness?  Consider this: even regular sex or a fat wallet will over time become the norm and fail to generate the happiness that it once did.  </p>
<p>I&#8217;ve recently been thinking about an article written by Peter Norvig about mastering programming in a decade.  His argument is that mastery in anything will realistically take 10 years.  Naturally, I considered where I am in various fields of my interest.  I have been programming for almost 10 years now.  The sad part is that I&#8217;m no where near an expert.  I&#8217;d consider myself an intermediate programmer.  I&#8217;ve been playing saxophone on and off for about 10 years and I&#8217;m still a novice.  Guitar is a hobby I started about 4 years ago.</p>
<p>Sadly, Norvig argues that mastery also takes consistency.  Practicing everyday and pushing oneself to learn things outside of one&#8217;s comfort zone.  It&#8217;s never easy to venture outside of the comfort of what you know but that&#8217;s the way we grow, right?</p>
<p>One last thought.  It seems that if one has specific interests that one must accept that it is extremely unlikely that anyone else shares your interests, no matter how much you want them to.  The socialite will focus on lowest common denominator interests to connect with people and the &#8216;geek&#8217; will focus on specialised interests at the expense of being able to relate to people.  I find myself constantly leaning towards the &#8216;geek&#8217; side of the spectrum though I do believe there is room for a comfortable hybrid.  I hope my gross over-simplification doesn&#8217;t offend anyone!</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/10/09/thoughts-in-no-particular-order/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cognitive Cow Returns; Explanation Follows.</title>
		<link>http://cognitivecow.com/2008/09/08/cognitive-cow-returns/</link>
		<comments>http://cognitivecow.com/2008/09/08/cognitive-cow-returns/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 14:34:48 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Thoughts]]></category>

		<category><![CDATA[comcast]]></category>

		<category><![CDATA[internet]]></category>

		<category><![CDATA[net neutrality]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=177</guid>
		<description><![CDATA[Let&#8217;s consider the past couple of months to be a summer hiatus.  I didn&#8217;t willfully turn off my server, for those curious but instead was shut down by Comcast because this site is hosted from home.
To any Comcast employees reading this post:
I&#8217;m not making any money from this site and the bandwidth usage is [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s consider the past couple of months to be a summer hiatus.  I didn&#8217;t willfully turn off my server, for those curious but instead was shut down by Comcast because this site is hosted from home.</p>
<p><strong>To any Comcast employees reading this post:</strong></p>
<p>I&#8217;m not making any money from this site and the bandwidth usage is embarrassingly low.  Take a look around my sea of spelling errors and poor sentence structure and you will not find any Google Adwords or even a clever product placement (this post brought to you by the good people at the Coca Cola Company).  Perhaps, just maybe, the guy who&#8217;s about to call me to shut down my service can say, &#8220;Ya know, he&#8217;s a pretty okay guy and I&#8217;d hate to take away the one thing that makes him cool.&#8221;  Seriously, this site is the only reason I have friends.  By friends, I mean MySpace friends, of course.</p>
<p>Okay now that that&#8217;s out of the way, I want to go into some topics about the future of the Internet that I&#8217;ve been debating with myself recently.</p>
<p>Comcast recently announced that they will be enforcing a 250 gb/month cap on Internet usage per customer.  Does this bother me?  Well, yes and no.  On one hand, it&#8217;s a very generous cap for the average user with a median bandwidth usage of 3 gb/month.  Then again, it does go against my philosophy that the Internet(s) should be unhindered to allow for the most growth of Internet enabled applications.</p>
<p>What&#8217;s important to note here is that Comcast is a company with infrastructure costs and limitations.  Comcast is one of the largest cable providers in the US which means that any increase in median bandwidth usage will be increasingly hard to deal with.  Routers process so quickly and bandwidth is clearly limited.  Even with the upcoming cap, Comcast will surely not be able to deal with every customer capping out every month.  It&#8217;s really just a policy to curb the top x% of users who leave bit torrent running 24/7.</p>
<p>It seems obvious to me that limiting bandwidth is far better than protocol throttling alternatives which Comcast has already tried when they were throttling bit torrent traffic.</p>
<p>Anyway, just some random thoughts</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/09/08/cognitive-cow-returns/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Post-Redirect-Get Pattern</title>
		<link>http://cognitivecow.com/2008/06/02/post-redirect-get-pattern/</link>
		<comments>http://cognitivecow.com/2008/06/02/post-redirect-get-pattern/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 14:16:42 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[design patterns]]></category>

		<category><![CDATA[internets]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=176</guid>
		<description><![CDATA[Ah design patterns, a breath of fresh air for solving annoying little problems, especially in the web world.  Today&#8217;s design pattern is the Post-Redirect-Get pattern.  It&#8217;s not a crucial pattern to follow though it&#8217;s one of those things that can change a site from &#8216;good&#8217; to &#8216;great&#8217; in terms of usability.
The goal of [...]]]></description>
			<content:encoded><![CDATA[<p>Ah design patterns, a breath of fresh air for solving annoying little problems, especially in the web world.  Today&#8217;s design pattern is the Post-Redirect-Get pattern.  It&#8217;s not a crucial pattern to follow though it&#8217;s one of those things that can change a site from &#8216;good&#8217; to &#8216;great&#8217; in terms of usability.</p>
<p>The goal of this pattern is to separate control URLs with user displayed URLs.  For example, I might have a registration URL: http://mysite.com/register.  On that page is a submit button that will POST to http://mysite.com/register?action=go.  Normally, after registering for a site, this second page might say &#8220;thank you for registering for mysite.com.&#8221;  However, the URL /register?action=go doesn&#8217;t match the content of the page.  What&#8217;s worse is that if a user hits the refresh button on the browser, most browsers will ask if they would like to re-post data which could mean multiple registration attempts.</p>
<p>What Post-Redirect-Get does is give the programmer much more control over how URLs are handled.  A simple example on how to improve the registration page would be as follows:</p>
<ol>
<li>User goes to http://mysite.com/register and fills out data, hits submit</li>
<li>The web form sends a POST request to http://mysite.com/register?action=go with the form data</li>
<li>The script does the form processing and issues a header(&#8221;Location: http://mysite.com/thankyou&#8221;) in PHP</li>
<li>When the web browser sees the &#8216;Location&#8217; header, it then issues a GET request for that URL.</li>
<li>User sees http://mysite.com/thankyou.
</ol>
<p>The end result of this pattern is that a user starts at http://mysite.com/register, fills out the form, submits it, and is then brought right to http://mysite.com/thankyou.  The user never sees the ugly and potentially cryptic URL of http://mysite.com/register?action=go.</p>
<p>To make this pattern more flexible, PHP sessions can be used to store status and error messages.  Using a reserved value, like $_SESSION[&#8217;error_message&#8217;] can be set before the redirect and the display page can check if this value is set and act accordingly.</p>
<p>Perhaps a simple concrete example will shed more light on this design pattern.  Below is the code for the registration page.  Please note that this is example code and will probably not run if simply copied and pasted into a page called &#8216;register.php&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000066;">session_start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'db.php'</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'action'</span><span style="color: #66cc66;">&#93;</span> == <span style="color: #ff0000;">'go'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$status</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO users (first, last) VALUES ('&quot;</span> . <span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'first'</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">&quot;', '&quot;</span> . <span style="color: #0000ff;">$_POST</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'last'</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">&quot;');&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$status</span> != <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'error_message'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">'There was a database error!'</span>;
        <span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Location: http://mysite.com/register&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'status_message'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">'Registration complete!'</span>;
        <span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Location: http://mysite.com/thankyou&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Registration Page&lt;/title&gt;
    &lt;/head&gt;
&nbsp;
    &lt;body&gt;
    &lt;h1&gt;Registration&lt;/h1&gt;
&nbsp;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'error_message'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;p&gt;ERROR: <span style="color: #000000; font-weight: bold;">&lt;?</span>=<span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'error_message'</span><span style="color: #66cc66;">&#93;</span>?&gt;&lt;/p&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
    &lt;form action=<span style="color: #ff0000;">&quot;/register?action=go&quot;</span> method=<span style="color: #ff0000;">&quot;post&quot;</span>&gt;
        &lt;p&gt;First Name: &lt;input type=<span style="color: #ff0000;">&quot;text&quot;</span> name=<span style="color: #ff0000;">&quot;first&quot;</span> /&gt;&lt;/p&gt;
        &lt;p&gt;Last Name: &lt;input type=<span style="color: #ff0000;">&quot;text&quot;</span> name=<span style="color: #ff0000;">&quot;last&quot;</span> /&gt;&lt;/p&gt;
    &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>And here is the &#8216;thank you&#8217; page:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000066;">session_start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Thanks <span style="color: #b1b100;">for</span> registering&lt;/title&gt;
    &lt;/head&gt;
&nbsp;
    &lt;body&gt;
&nbsp;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'status_message'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;p&gt;NOTICE: <span style="color: #000000; font-weight: bold;">&lt;?</span>=<span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'status_message'</span><span style="color: #66cc66;">&#93;</span>?&gt;&lt;/p&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
    &lt;p&gt;Thank you <span style="color: #b1b100;">for</span> registering!&lt;/p&gt;
&nbsp;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/06/02/post-redirect-get-pattern/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Math of Geohashing</title>
		<link>http://cognitivecow.com/2008/05/21/the-math-of-geohashing/</link>
		<comments>http://cognitivecow.com/2008/05/21/the-math-of-geohashing/#comments</comments>
		<pubDate>Thu, 22 May 2008 02:05:19 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Journal]]></category>

		<category><![CDATA[geohash]]></category>

		<category><![CDATA[math]]></category>

		<category><![CDATA[nerdism]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=174</guid>
		<description><![CDATA[Although the recent xkcd comic about geohashing was supposed to be part joke, part computer geek, I decided to take it to the next level of computer geekiness.  It took me a minute to understand geohashing when I first read that comic and I started to think about it for a while and I [...]]]></description>
			<content:encoded><![CDATA[<p>Although the recent xkcd comic about <a href="http://xkcd.com/426/" onclick="javascript:urchinTracker ('/outbound/article/xkcd.com');">geohashing</a> was supposed to be part joke, part computer geek, I decided to take it to the next level of computer geekiness.  It took me a minute to understand geohashing when I first read that comic and I started to think about it for a while and I once I figured it out, I asked myself an interesting question: What is the maximum distance that I can be from a Munroe Point?</p>
<p>Perhaps I should back up for a second.  I need to define some terminology to make this a little easier to talk about.  The way he set up the geohashing algorithm creates zones on the map, which I shall call Munroe Zones.  Each Munroe Zone has exactly one Munroe Point which is a statistically random point within the Munroe Zone.  The zone I live in the rectangle spanning from (42.000000, -72.000000) to (43.000000, -71.000000).  This spans an area of 3542.96 square miles.  I will spare you the gory details of this calculation.  Verify it for yourself <img src='http://cognitivecow.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I can calculate the maximum distance that I can be from a Munroe Point within my zone with the cunning use of middle school geometry.  The distance between (42.000000, -72.000000) and (43.000000, -72.000000) is 69 miles, approximately.  The distance between one longitude measurement (42.000000, -72.000000) and (42.000000, -71.000000) is 51 miles.  This means that the maximum distance one can be away from the Munroe Point in their Munroe Zone is the hypotenuse of the triangle assuming that the Munroe Point is in the far NW and I live in the far SE.  This distance is about 86 miles.  However, if I lived in the far SE and the Munroe Point is in the NW, that means I live directly on top of the Munroe Point for another Zone.</p>
<p>Because of this inter-zone relationship, the maximum distance I can be away turns into half of the maximum zone distance, or about 43 miles.  The proof is pretty simple.</p>
<p>I thought that since Munroe Points were meant to be meeting points, one might be interested in finding the closest Munroe Point which may or may not be within the &#8216;zone&#8217; they live in.  I wrote a little Python script to help out.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>, <span style="color: #dc143c;">md5</span>, <span style="color: #dc143c;">struct</span>, <span style="color: #dc143c;">urllib</span>, <span style="color: #dc143c;">datetime</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Your coordinates!</span>
me = <span style="color: black;">&#91;</span><span style="color: #ff4500;">42.18719304724962</span>, <span style="color: #ff4500;">-71.5403938293457</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Dow Jones Industrial Average for today</span>
djia = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">datetime</span>.<span style="color: black;">date</span>.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://irc.peeron.com/xkcd/map/data/%Y/%m/%d&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Returns distance, in miles, between two coordinates</span>
<span style="color: #ff7700;font-weight:bold;">def</span> gps_distance<span style="color: black;">&#40;</span>gps1, gps2<span style="color: black;">&#41;</span>:
   r = <span style="color: #ff4500;">6367442.5</span> * <span style="color: #ff4500;">0.000621371192</span> <span style="color: #808080; font-style: italic;"># earth radius, miles.</span>
   dLat = <span style="color: #dc143c;">math</span>.<span style="color: black;">radians</span><span style="color: black;">&#40;</span>gps1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>-gps2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
   dLon = <span style="color: #dc143c;">math</span>.<span style="color: black;">radians</span><span style="color: black;">&#40;</span>gps1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>-gps2<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
   x = <span style="color: #dc143c;">math</span>.<span style="color: black;">sin</span><span style="color: black;">&#40;</span>dLat / <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> ** <span style="color: #ff4500;">2</span> + <span style="color: #dc143c;">math</span>.<span style="color: black;">cos</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">radians</span><span style="color: black;">&#40;</span>gps1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> * <span style="color: #dc143c;">math</span>.<span style="color: black;">cos</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">radians</span><span style="color: black;">&#40;</span>gps2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> * <span style="color: #dc143c;">math</span>.<span style="color: black;">sin</span><span style="color: black;">&#40;</span>dLon / <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> ** <span style="color: #ff4500;">2</span>
   y = <span style="color: #ff4500;">2</span> * <span style="color: #dc143c;">math</span>.<span style="color: black;">atan2</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">sqrt</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>, <span style="color: #dc143c;">math</span>.<span style="color: black;">sqrt</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>-x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
   d = r * y
   <span style="color: #ff7700;font-weight:bold;">return</span> d
&nbsp;
<span style="color: #808080; font-style: italic;"># Determines if coord is NW, SW, NE, or SE relative to my_munroe_point</span>
<span style="color: #ff7700;font-weight:bold;">def</span> position<span style="color: black;">&#40;</span>coord, my_munroe_point<span style="color: black;">&#41;</span>:
   ns = <span style="color: #483d8b;">'N'</span> <span style="color: #ff7700;font-weight:bold;">if</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> &gt; my_munroe_point<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #483d8b;">'S'</span>
   ew = <span style="color: #483d8b;">'E'</span> <span style="color: #ff7700;font-weight:bold;">if</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> &gt; my_munroe_point<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #483d8b;">'W'</span>
   <span style="color: #ff7700;font-weight:bold;">return</span> ns + ew
&nbsp;
<span style="color: #808080; font-style: italic;"># Determines the Munroe point within your zone, as the comic does.</span>
<span style="color: #ff7700;font-weight:bold;">def</span> my_zone_munroe_point<span style="color: black;">&#40;</span>coord<span style="color: black;">&#41;</span>:
    day = <span style="color: #dc143c;">datetime</span>.<span style="color: black;">date</span>.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%Y-%m-%d&quot;</span><span style="color: black;">&#41;</span>
    munroe_coord = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    md5_hash = <span style="color: #dc143c;">md5</span>.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s-%5.2f&quot;</span> % <span style="color: black;">&#40;</span>day, djia<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hexdigest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    adjust = <span style="color: black;">&#91;</span><span style="color: #008000;">long</span><span style="color: black;">&#40;</span>md5_hash<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">16</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span>.<span style="color: black;">**</span><span style="color: #ff4500;">64</span>, 
              <span style="color: #008000;">long</span><span style="color: black;">&#40;</span>md5_hash<span style="color: black;">&#91;</span><span style="color: #ff4500;">16</span>:<span style="color: black;">&#93;</span>, <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span>.<span style="color: black;">**</span><span style="color: #ff4500;">64</span><span style="color: black;">&#93;</span>
    munroe_coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">math</span>.<span style="color: black;">floor</span><span style="color: black;">&#40;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + adjust<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">if</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> &gt; <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> - adjust<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    munroe_coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">math</span>.<span style="color: black;">floor</span><span style="color: black;">&#40;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + adjust<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">if</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> &gt; <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> - adjust<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> munroe_coord
&nbsp;
<span style="color: #808080; font-style: italic;"># Calculates the nearest Munroe Point, which may or may not be the one</span>
<span style="color: #808080; font-style: italic;"># in your zone.</span>
<span style="color: #ff7700;font-weight:bold;">def</span> nearest_munroe_point<span style="color: black;">&#40;</span>coord<span style="color: black;">&#41;</span>:
    my_munroe = my_zone_munroe_point<span style="color: black;">&#40;</span>coord<span style="color: black;">&#41;</span>
    pos = position<span style="color: black;">&#40;</span>coord, my_munroe<span style="color: black;">&#41;</span>
    points = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>pos == <span style="color: #483d8b;">'NW'</span><span style="color: black;">&#41;</span>:
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>pos == <span style="color: #483d8b;">'SW'</span><span style="color: black;">&#41;</span>:
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>pos == <span style="color: #483d8b;">'NE'</span><span style="color: black;">&#41;</span>:
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: black;">&#40;</span>pos == <span style="color: #483d8b;">'SE'</span><span style="color: black;">&#41;</span>:
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
        points.<span style="color: black;">append</span><span style="color: black;">&#40;</span>my_zone_munroe_point<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #dc143c;">dis</span> = gps_distance<span style="color: black;">&#40;</span>coord, my_munroe<span style="color: black;">&#41;</span>
    nearest = my_munroe
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> point <span style="color: #ff7700;font-weight:bold;">in</span> points:
        tmp = gps_distance<span style="color: black;">&#40;</span>coord, point<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>tmp &lt; <span style="color: #dc143c;">dis</span><span style="color: black;">&#41;</span>:
            nearest = point
            <span style="color: #dc143c;">dis</span> = tmp
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> nearest
&nbsp;
my_munroe = my_zone_munroe_point<span style="color: black;">&#40;</span>me<span style="color: black;">&#41;</span>
my_munroe_dis = gps_distance<span style="color: black;">&#40;</span>me, my_munroe<span style="color: black;">&#41;</span>
nearest_munroe = nearest_munroe_point<span style="color: black;">&#40;</span>me<span style="color: black;">&#41;</span>
nearest_munroe_dis = gps_distance<span style="color: black;">&#40;</span>me, nearest_munroe<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Munroe Point coordinates  (my zone):&quot;</span>, my_munroe
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Distance to my Munroe Point (my zone):&quot;</span>, <span style="color: #008000;">round</span><span style="color: black;">&#40;</span>my_munroe_dis, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;miles&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Nearest Munroe Point coordinates:&quot;</span>, nearest_munroe
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Distance to nearest Munroe Point:&quot;</span>, <span style="color: #008000;">round</span><span style="color: black;">&#40;</span>nearest_munroe_dis, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;miles&quot;</span></pre></div></div>

<p>The output I got when I ran this for today&#8217;s data is as follows:</p>
<p>Munroe Point coordinates  (my zone): [42.179467994697781, -71.86153601245762]<br />
Distance to my Munroe Point (my zone): 42.77 miles<br />
Nearest Munroe Point coordinates: [42.179467994697781, -70.86153601245762]<br />
Distance to nearest Munroe Point: 14.94 miles</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/05/21/the-math-of-geohashing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C Static Memory Allocation</title>
		<link>http://cognitivecow.com/2008/05/19/c-static-memory-allocation/</link>
		<comments>http://cognitivecow.com/2008/05/19/c-static-memory-allocation/#comments</comments>
		<pubDate>Mon, 19 May 2008 19:07:34 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[c]]></category>

		<category><![CDATA[pointers]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=173</guid>
		<description><![CDATA[I think one of the most common things to do in C is to perform static memory allocation, even if one is not aware of it.  C does some cool stuff with static memory allocation and understanding what happens behind the scenes definitely helps reduce bugs.  
Below I have provided some example code [...]]]></description>
			<content:encoded><![CDATA[<p>I think one of the most common things to do in C is to perform static memory allocation, even if one is not aware of it.  C does some cool stuff with static memory allocation and understanding what happens behind the scenes definitely helps reduce bugs.  </p>
<p>Below I have provided some example code that I compiled with GCC 3.4.6.  The point of this program is to show how static memory allocation works.  Try to execute the code in your head before compiling it and running it.</p>

<div class="wp_syntax"><div class="code"><pre class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> argc, <span style="color: #993333;">char</span> * argv<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #993333;">char</span> * numbers = <span style="color: #ff0000;">&quot;1234567890&quot;</span>;
  <span style="color: #993333;">char</span> * letters = <span style="color: #ff0000;">&quot;abcdefghij&quot;</span>;
  <span style="color: #993333;">char</span> * a;
  <span style="color: #993333;">char</span> * b;
  <span style="color: #993333;">char</span> * c;
  <span style="color: #993333;">char</span> tmp;
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>strcmp<span style="color: #66cc66;">&#40;</span>letters, <span style="color: #ff0000;">&quot;abcd&quot;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;This should not print although we will use it later!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* 
     TEST 1:
&nbsp;
     Print address of numbers and letters the addresses
     should be 11 bytes apart.
  */</span>
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;numbers: %p<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, numbers<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;letters: %p<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, letters<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;diff:    %d bytes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #66cc66;">&#40;</span>letters - numbers<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* 
     TEST 2:
&nbsp;
     A and B should be the same address because they
     reference the same static value 
  */</span>
&nbsp;
  a = <span style="color: #ff0000;">&quot;1234567890&quot;</span>;
  b = numbers;
&nbsp;
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a:       %p<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, a<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;b:       %p<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, a<span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* 
     EXERCISE 1:
&nbsp;
     Take a moment to look at the following code
     and figure out what will be printed to the
     screen
  */</span>
&nbsp;
  c = <span style="color: #ff0000;">&quot;abcd&quot;</span>;
&nbsp;
  <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>a = numbers; a &lt; c + <span style="color: #cc66cc;">5</span>; a++<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">/* get the character that 'a' points to */</span>
      tmp = a<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
      <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>tmp<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>:
          <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>0&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #000000; font-weight: bold;">break</span>;
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>:
          <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #000000; font-weight: bold;">break</span>;
        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span>:
          <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>r&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #000000; font-weight: bold;">break</span>;
        <span style="color: #b1b100;">default</span>:
          <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%c&quot;</span>, tmp<span style="color: #66cc66;">&#41;</span>;
          <span style="color: #000000; font-weight: bold;">break</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000066;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The key to understanding this is that each variable in double quotes is put into a &#8217;static&#8217; section of the compiled binary in the order which they were declared.  The loop that I write iterates over the first three static variables and prints them out highlighting the null terminating character \0.  It&#8217;s important to note that the compiler tightly packs each static value separating each string only by a null terminating \0 character.  The point of this static data section is that it will be immutable.</p>
<p>As an exercise, Try changing the c + 5 in the for loop to c + 10 and guess what the next 4 characters will be.</p>
<p>On unix-like systems, attempting to write to this static section will result in segmentation faults.  For example, if I try to do numbers[0] = &#8216;@&#8217;, I should get a segmentation fault.  However, one could copy &#8216;numbers&#8217; into a variable pointing to the heap or stack and perform this operation just fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/05/19/c-static-memory-allocation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Apple Store, Boylston St.</title>
		<link>http://cognitivecow.com/2008/05/16/apple-store-boylston-st/</link>
		<comments>http://cognitivecow.com/2008/05/16/apple-store-boylston-st/#comments</comments>
		<pubDate>Fri, 16 May 2008 13:51:08 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Journal]]></category>

		<category><![CDATA[apple]]></category>

		<category><![CDATA[computers]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=172</guid>
		<description><![CDATA[I&#8217;d consider myself an Apple user rather than a fanboy, though some people might disagree.  Well going to the grand opening of the new Apple Store on Boylston St. in Boston definitely felt like a fanboy thing to do.
I arrived at 5:50, 10 minutes before the opening.  To no surprise, there were hundreds [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d consider myself an Apple user rather than a fanboy, though some people might disagree.  Well going to the grand opening of the new Apple Store on <a href="http://maps.google.com/maps?f=q&#038;hl=en&#038;geocode=&#038;q=Boylston+St,+Boston,+MA&#038;sll=37.0625,-95.677068&#038;sspn=46.543597,82.265625&#038;ie=UTF8&#038;z=14" onclick="javascript:urchinTracker ('/outbound/article/maps.google.com');">Boylston St.</a> in Boston definitely felt like a fanboy thing to do.</p>
<p>I arrived at 5:50, 10 minutes before the opening.  To no surprise, there were hundreds of people crowded around the entrance and across the street.  I was in the back of the crowd trying to get a glimpse of the fuss.  Every news van in the area was there as they got early entrance into the store for interviews and story coverage.  It was really quite a spectacle.</p>
<p>Let me talk about the actual building for a second.  Sandwiched between two classic old-Boston style buildings, Apple has erected a post-modern looking all glass building.  The three story marvel is basically right across the street from the Hynes Convention Center in what Apple called a prime location.  This store is the largest in the nation, to no surprise.  The giant Apple logo itself hangs from the third floor standing 8 feet tall.  I like the building, it seems to add a nice feeling to the area.  Then again, I&#8217;ve never been attached to traditional Boston architecture.</p>
<p>At 6:00, cheers started erupting as the doors opened and I noticed that there actually was a line that had formed that headed down to the corner of Boylston and Fairfield.  As I saw the people start to filter in, I decided that I should probably get in line too.  I headed towards the corner to realize, to no surprise, that the line wrapped around down Fairfield.  What I didn&#8217;t expect was how long it was.  Apple blocked off an entire lane of the road and the line extended, tightly packed, all the way down to Commonwealth Avenue.</p>
<p>I figured that I came this far already so I might as well wait.  Well, two difficult sudoku puzzles and an hour later, I arrived back at the entrance to the sound of cheering.  They had about 20 employees who&#8217;s job was simply to cheer as people entered the building and hand out shirts and give high fives.  After receiving an XL shirt (why can&#8217;t they give out mediums?!) and about 30 high-fives, I was in.  The only way to describe it is as an orgy of fanboys drooling over absolutely everything.  There must have been twenty employees per floor and each went out of their way to say hi and try to make shopping a good experience.  Overall, it was a good time.  Would I go to another grand opening?  Probably not.  I&#8217;m not enough of a fanboy for this sort of thing <img src='http://cognitivecow.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Only Apple can make a grand opening of a computer store like going to an exclusive nightclub.  It&#8217;s amazing that they managed to turn tech support into marketing.  The entire third floor of the Boylston St. Apple Store is a genius bar, training, and tech support.  It&#8217;s also amazing that Apple has turned their retail stores into a &#8216;cool&#8217; place to hang out.  People young and old from all demographics showed up just to hang out.  I don&#8217;t know how they did it, but it certainly made for a good experience.</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/05/16/apple-store-boylston-st/feed/</wfw:commentRss>
		</item>
		<item>
		<title>URLs and User Experience</title>
		<link>http://cognitivecow.com/2008/04/02/urls-and-user-experience/</link>
		<comments>http://cognitivecow.com/2008/04/02/urls-and-user-experience/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 17:20:38 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Web Development]]></category>

		<category><![CDATA[clean url]]></category>

		<category><![CDATA[mvc]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[ui]]></category>

		<category><![CDATA[url]]></category>

		<category><![CDATA[user experience]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/?p=167</guid>
		<description><![CDATA[I really like URLs as a topic for user experience.  Today, I found somebody else who also likes URLs and UI.  Interestingly enough, he goes over what a good URL is but not how to create them in a working environment.
For anyone who&#8217;s programmed PHP, a URL like this is not uncommon: http://localhost/blog/index.php?page=photo&#038;name=a-sunset-on-the-beach. [...]]]></description>
			<content:encoded><![CDATA[<p>I really like URLs as a topic for user experience.  Today, I found somebody else who <a href="http://www.darowski.com/tracesofinspiration/2008/03/16/url-as-ui/" onclick="javascript:urchinTracker ('/outbound/article/www.darowski.com');">also likes URLs and UI</a>.  Interestingly enough, he goes over what a good URL is but not how to create them in a working environment.</p>
<p>For anyone who&#8217;s programmed PHP, a URL like this is not uncommon: http://localhost/blog/index.php?page=photo&#038;name=a-sunset-on-the-beach.  It&#8217;s gross, unreadable, and for a non-programmer the parsing of the URL stops at the question mark.  A URL is supposed to point, unambiguously, to a resource and that paradigm should not be broken when making pretty URLs.  However, there does not need to be complete transparency from the resource indicator that the user sees to the resource that&#8217;s actually on the server.  Consistency is important though.  A URL should not ever go out of date and if it does change, proper redirections should be in place to aid users using older URLs.</p>
<p>Commonly, Apache&#8217;s mod_rewrite module is used to translate a pretty URL to an ugly one.  For example, one might translate <code>/blog/photo/a-sunset-on-the-beach</code> to <code>/blog/index.php?page=photo&#038;name=a-sunset-on-the-beach</code>.  I agree that this is great for user experience but I do not think this is an optimal solution.</p>
<p>Why should we bother translating to a nasty URL when the nice URL comes to the server?  Why not just pass PHP the nice URL and parse it as such.  One could define a standardized structure for the URL:</p>
<p><code>/[base directory]/[category]/[item]</code></p>
<p>This URL would then be passed and parsed by PHP and the necessary PHP files would be included conditionally based on the [category] and [item] values.</p>
<p>Obviously, this approach requires a single point of entry into your PHP application where you parse the URL and dispatch to other PHP files as necessary.   The first thing that needs to be done to get this working is to tell apache to pass the URL as is to PHP via an .htaccess file:</p>

<div class="wp_syntax"><div class="code"><pre class="apache"><span style="color: #00007f;">Options</span> +<span style="color: #0000ff;">FollowSymLinks</span>
<span style="color: #00007f;">IndexIgnore</span> */*
<span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">RewriteCond</span> %<span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> !-f
<span style="color: #00007f;">RewriteCond</span> %<span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> !-d
<span style="color: #00007f;">RewriteRule</span> . index.php</pre></div></div>

<p>Within PHP, we need to define our base URL and then parse the incoming URL.  Below is a sample index.php file which resides in the same directory as the .htaccess file created above:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Tells that our base is /blog</span>
<span style="color: #0000ff;">$base_directory</span> = <span style="color: #000066;">array_filter</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">explode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/'</span>, <span style="color: #ff0000;">'/blog'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Produces an array of our request vars.</span>
<span style="color: #0000ff;">$request_url</span> = <span style="color: #000066;">explode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/'</span>, <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'REQUEST_URI'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Remove the base directory from the request URL</span>
<span style="color: #0000ff;">$request</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$count</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$request_url</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$elem</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">array_search</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$elem</span>, <span style="color: #0000ff;">$base_directory</span><span style="color: #66cc66;">&#41;</span> === <span style="color: #000000; font-weight: bold;">FALSE</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$request</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$count</span>++<span style="color: #66cc66;">&#93;</span> =  <span style="color: #0000ff;">$elem</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Call the necessary controller object</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">$request</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">&quot;.php&quot;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If we had passed the URL http://localhost/blog/photo/a-sunset-on-the-beach, we&#8217;d end up getting a <code>$request</code> variable that looked like this: <code>array(0 => 'photo', 1 => 'a-sunset-on-the-beach')</code>.  The bottom line, which includes the file, is basically a controller which will use the $request array to generate what is needed and display the correct template to the browser.</p>
<p>I like this approach because it provides a write-once approach to URLs and allows the programmer to not think in terms of standard GET variables and start with a clean slate of pretty URLs.  This example is quite rudimentary and to the point, which is why I recommend making this more robust before deploying it on a real site.</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/04/02/urls-and-user-experience/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Software and the Nature of &#8216;Free&#8217;</title>
		<link>http://cognitivecow.com/2008/03/29/software-and-the-nature-of-free/</link>
		<comments>http://cognitivecow.com/2008/03/29/software-and-the-nature-of-free/#comments</comments>
		<pubDate>Sat, 29 Mar 2008 05:04:40 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/2008/03/29/software-and-the-nature-of-free/</guid>
		<description><![CDATA[Software seems to always be the target of debates of what should free and what should be &#8216;locked up in corporations&#8217; as some people put it.  Many agree that all software should be free.  In fact, many prominent software engineers have this view.  While I do agree with the GPL and open [...]]]></description>
			<content:encoded><![CDATA[<p>Software seems to always be the target of debates of what should free and what should be &#8216;locked up in corporations&#8217; as some people put it.  Many agree that all software should be free.  In fact, many prominent software engineers have this view.  While I do agree with the GPL and open source software, I don&#8217;t think that ALL software should be free just because SOME software is free.  My reasons are simple.  Software is creative property, and the creator should be able to set the price.</p>
<p>I bring this up because <a href="http://leoville.com/" onclick="javascript:urchinTracker ('/outbound/article/leoville.com');">Leo Laporte</a> recently commented on an application called <a href="http://codesorcery.net/pukka/" onclick="javascript:urchinTracker ('/outbound/article/codesorcery.net');">Pukka</a>, priced at $15.  He had many great things to say about it and how he used it everyday, but claimed that this application &#8217;should be free.&#8217;  It&#8217;s one thing to say that the software sucks and it should be free, but to say that it&#8217;s a quality product and should be free?  Ridiculous.  Just because I&#8217;d like it if cars were free, doesn&#8217;t mean that they <em>should</em> be free.  We&#8217;ve all been somewhat spoiled by free software.  VLC, Firefox, and many other software bundles are taken for granted by everybody.  Would we say that Firefox &#8217;should be free&#8217;?  If so, WHY should it be free?  Is it because software should be the right of everybody?  Don&#8217;t get me wrong here, the Free Software Foundation is doing good things and I quite like the fact that there is free and open source software, but we must realize that this software is <strong>charity</strong>, usually done with another capitalistic motive in mind.</p>
<p>If we agree that intellectual property has value, we should concede that software has value.  In fact, I KNOW software has value because I, like so many others, have a job in software engineering.  Software is the only field, anywhere, where consumers <em>expect</em> a free lunch.  People don&#8217;t seem to know that software actually takes time and energy.  This stuff doesn&#8217;t just appear from nowhere.  A good application isn&#8217;t written in 2 hours in some guys basement while watching America&#8217;s Next Dance Crew.  Real time, energy, and expertise are needed to create good software.  Software engineers and hobbyist programmers are among the worst culprits of software oversimplification.  It&#8217;s amazing how people can reduce complex programs to simple one line statements: &#8220;Oh, it just displays text and highlights the syntax, what&#8217;s the big deal?  That&#8217;s not worth 20 euros.&#8221;  This line of reasoning is used to improperly justify piracy.  If one can convince themselves that the software in question is simple enough to have been written by themselves, then they are allowed to pirate it.  My challenge to people who say that is to actually try to write the software in question.  For that matter, try writing any full featured piece of software that another person might consider using.  It&#8217;s not nearly as simple as you think.</p>
<p>We must debunk this notion that those who charge for software are evil.  I&#8217;m guessing there are a lot of people who think that those charging for software they write are just scamming us and are being greedy.  Instead, let&#8217;s look at this in a different light.  An engineer who charges a modest fee for a software license actually does want to bring us quality software.  Quality software needs funding.  Without corporate funding, an independent developer must rely on direct payments.  It&#8217;s not all bad.  Some of these people might actually be nice.</p>
<p>What else bothers me is when people say, &#8220;There&#8217;s no way that&#8217;s worth $15, I&#8217;d pay $5, maximum.&#8221;  Well, that&#8217;s all well and good, if you choose not to buy the software.  However, if you complain about it being way too expensive and then pirate it, you&#8217;re admitting that this software is worth enough to use it but not worth enough for you to pay for it.  This is a direct parallel to stealing a bike from a bike shop on weekends because the full value of the bike is too much but you still want the benefit of riding the bike at your convenience.  In the end, the bike shop still has the bike to sell, because you didn&#8217;t steal it and they&#8217;re getting no profit from you.  Imagine if everybody used the bike shop like a rental store which they didn&#8217;t pay for.</p>
<p>The problem is that if you steal software, you ARE taking away income.  The argument &#8220;Well, I wouldn&#8217;t buy it anyway so it&#8217;s okay to steal it,&#8221; is a logical fallacy.  Imagine if I made this claim: &#8220;Well, I wouldn&#8217;t watch any TV or movies, use any internet access, use any proprietary software, use electricity, or pay for trash removal if I had to buy it, so it&#8217;s okay for me to steal all of those things from my neighbor.&#8221;  The argument falls apart.  Even worse: &#8220;I wouldn&#8217;t buy anything therefore I should be able to steal everything.&#8221;  If you really wouldn&#8217;t use this product if you had to pay for it, then don&#8217;t use it.</p>
<p>My main point is that we should be sympathetic towards independent developers and not immediately assume that we&#8217;re being ripped off by having to buy software.  I also would hope that we can at least see piracy from independent developers as hurting them and hurting independent software in general.  Free software may be great but let&#8217;s think of this as an exception to the rule rather than the rule!</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/03/29/software-and-the-nature-of-free/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ActiveRecord Approach to PHP</title>
		<link>http://cognitivecow.com/2008/03/14/activerecord-approach-to-php/</link>
		<comments>http://cognitivecow.com/2008/03/14/activerecord-approach-to-php/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 17:27:53 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/2008/03/14/activerecord-approach-to-php/</guid>
		<description><![CDATA[I&#8217;ve noticed that Ruby on Rails did something amazingly correct when it comes to bridging the database with the scripting language.  That concept is the one of ActiveRecords.  An active record, as I&#8217;ve come to understand it, is an object that represents on entity in the database, usually a single row of a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve noticed that Ruby on Rails did something amazingly correct when it comes to bridging the database with the scripting language.  That concept is the one of ActiveRecords.  An active record, as I&#8217;ve come to understand it, is an object that represents on entity in the database, usually a single row of a table.  For example, one might have a <code>products</code> table and the active record object would be called <code>Product</code>.</p>
<p>In terms of abstraction, this makes perfect sense.  Represent all real-life things as simple objects with modifiable attributes that correspond to column names in the database.  My approach to ActiveRecords in PHP started with an abstract class:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> ActiveRecord
<span style="color: #66cc66;">&#123;</span>
	protected <span style="color: #0000ff;">$exists</span>;
	protected <span style="color: #0000ff;">$is_changed</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Every entry has an ID</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$id</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">exists</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">is_changed</span> = <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$var</span>;<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __isset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span> == <span style="color: #ff0000;">'exists'</span> || <span style="color: #0000ff;">$var</span> == <span style="color: #ff0000;">'is_changed'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>;	
		<span style="color: #b1b100;">return</span> <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$var</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __unset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span> == <span style="color: #ff0000;">'exists'</span> || <span style="color: #0000ff;">$var</span> == <span style="color: #ff0000;">'is_changed'</span><span style="color: #66cc66;">&#41;</span><span style="color: #b1b100;">return</span>;	
		<span style="color: #000066;">unset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$var</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span>;
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #66cc66;">&#40;</span>TableFilter <span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>From this base class, I can create a new class that extends ActiveRecord.  The first thing I&#8217;d have to do is implement __set, load, save, delete, and find.  The point of these functions is to do the correct database queries to get the data into the private members of the function, modify the fields as necessary (with validation) and then save the state of the object to the database.</p>
<p>The variables <code>$exists</code> and <code>$is_changed</code> help with determining how the object matches the data in the database.  For example, upon doing a load(), <code>$exists</code> is true and <code>$is_changed</code> is false.  Doing a delete puts <code>$exists</code> to false.  Here&#8217;s a sample of an object that would extend ActiveRecord.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// Foreign Keys</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$department_id</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Attributes</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$name</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$in_stock</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$description</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$price</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span>, <span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$var</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'name'</span>:
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'description'</span>:
				<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$var</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">sanitize</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'price'</span>:
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> !is_currency<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$price</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
					throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;price is not a valid value&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$var</span> = <span style="color: #0000ff;">$val</span>;
				<span style="color: #b1b100;">break</span>;
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'in_stock'</span>:
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$val</span> != <span style="color: #cc66cc;">1</span> &amp;&amp; <span style="color: #0000ff;">$val</span> != <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
					throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;not a boolean&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$var</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #0000ff;">$val</span> == <span style="color: #cc66cc;">1</span> ? <span style="color: #000000; font-weight: bold;">true</span> : <span style="color: #000000; font-weight: bold;">false</span>;
				<span style="color: #b1b100;">break</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">is_changed</span> = <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066;">global</span> <span style="color: #0000ff;">$db</span>;
		<span style="color: #0000ff;">$row</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">queryRow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * FROM products P WHERE p.id='$id';&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;does not exist&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">is_changed</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">exists</span>= <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066;">global</span> <span style="color: #0000ff;">$db</span>;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">exists</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0000ff;">$return</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;.... do an INSERT query ....&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">else</span>
			<span style="color: #0000ff;">$return</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;.... do an UPDATE query ....&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$return</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;save failed&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">is_changed</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">exists</span>= <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066;">global</span> <span style="color: #0000ff;">$db</span>;
		<span style="color: #0000ff;">$row</span> = <span style="color: #0000ff;">$db</span>-&gt;<span style="color: #006600;">queryRow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;DELETE FROM products P WHERE p.id='$id';&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">is_changed</span> = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">exists</span>= <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #66cc66;">&#40;</span>TableFilter <span style="color: #0000ff;">$filter</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// This is where you do a custom query</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Obviously, this code is just for an example and likely needs to be tweaked to work in an actual environment.  The code I use is pretty close to this, with a little bit more robustness in the error checking.</p>
<p>I find that this approach starts to converge to an MVC type of design where the ActiveRecord very clearly represents the model aspect of things.  If I were to program a Product Controller, the controller might instantiate new Product objects, alter the data within, and save it back without ever seeing an SQL statement.  In fact, most controller actions might never be more than 15 lines of code, which certainly seems the best way to get interactions between the user&#8217;s web browser and the database as efficiently as possible</p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/03/14/activerecord-approach-to-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>On Philosophy</title>
		<link>http://cognitivecow.com/2008/03/10/on-philosophy/</link>
		<comments>http://cognitivecow.com/2008/03/10/on-philosophy/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 17:30:27 +0000</pubDate>
		<dc:creator>Scott Frazer</dc:creator>
		
		<category><![CDATA[Atheism]]></category>

		<category><![CDATA[Comics]]></category>

		<guid isPermaLink="false">http://cognitivecow.com/2008/03/10/on-philosophy/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><img src='http://cognitivecow.com/wp-content/uploads/2008/03/philosophyvsgods.jpg' alt='Friggin’ Agnostics' /></p>
]]></content:encoded>
			<wfw:commentRss>http://cognitivecow.com/2008/03/10/on-philosophy/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
