<?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>Pushing Pixels &#187; opinion</title>
	<atom:link href="http://familywhitfield.co.uk/wordpress/tag/opinion/feed/" rel="self" type="application/rss+xml" />
	<link>http://familywhitfield.co.uk/wordpress</link>
	<description>Computing and Digital Imaging</description>
	<lastBuildDate>Thu, 24 Mar 2011 20:54:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Thoughts on a &#8216;Powerful&#8217; API</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/04/18/thoughts-on-a-powerful-api/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/04/18/thoughts-on-a-powerful-api/#comments</comments>
		<pubDate>Wed, 18 Apr 2007 16:02:41 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[opinion]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=17</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/04/18/thoughts-on-a-powerful-api/' addthis:title='Thoughts on a &#8216;Powerful&#8217; API' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div>One of the words that keeps coming up when I have conversations about new products is that the API must be &#8220;powerful&#8221;. While intuitively this sounds good &#8211; after all, who wants to develop an API that is a bit wimpy &#8211; how can you measure and judge the power of one API over another? [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/04/18/thoughts-on-a-powerful-api/' addthis:title='Thoughts on a &#8216;Powerful&#8217; API' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div><p>One of the words that keeps coming up when I have conversations about new products is that the API must be &#8220;powerful&#8221;. While intuitively this sounds good &#8211; after all, who wants to develop an API that is a bit wimpy &#8211; how can you measure and judge the power of one API over another? I&#8217;m pretty sure that you can&#8217;t assign a wattage to a function call.<br />
<span id="more-17"></span></p>
<p>Say we have a scene graph that lets a caller insert objects into a hierarchy. An object can reference other objects using  a standard instancing metaphor. Lets say this manifests itself like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Add child node to parent</span>
<span style="color: #0000ff;">int</span> SceneGraph<span style="color: #008080;">::</span><span style="color: #007788;">AddObject</span> <span style="color: #008000;">&#40;</span>Node <span style="color: #000040;">*</span>parent, <span style="color: #0000ff;">const</span> Node <span style="color: #000040;">*</span>child<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Such an API makes it easy to put in a circular dependency, which would cause subsequent scene graph traversals to loop ad-infinitum. So it would be trivial here to make the method check that parent and child are not the same. But what if parent is indirectly reference by child? E.g.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">start with A <span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> B <span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> C
call SceneGraph<span style="color: #008080;">::</span><span style="color: #007788;">AddObject</span> <span style="color: #008000;">&#40;</span>C, A<span style="color: #008000;">&#41;</span></pre></div></div>

<p>So, if AddObject were to be &#8220;powerful&#8221;, would it check for this and return an error? It would certainly make the method robust, as you could not poke invalid data at it. But does robust==powerful?</p>
<p>I would say no. Because even though it stops a user doing something daft that could be hard to diagnose, it does not scale. If the code has to traverse the graph every time a new node is added to the scene graph, then the act of building a model with hundreds or thousands of objects will execute in O(n^2) time complexity. By making this API robust, you actually reduce its power by imposing an overhead upon the user.</p>
<p>Most scene building code will be well behaved &#8211; there will be enough knowledge at the application end to &#8220;know&#8221; that the data is good. And for those times when the application does not know, we can supply a method like:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Check if one object is a child of another</span>
<span style="color: #0000ff;">bool</span> SceneGraph<span style="color: #008080;">::</span><span style="color: #007788;">IsChild</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Node <span style="color: #000040;">*</span>n0, <span style="color: #0000ff;">const</span> Node <span style="color: #000040;">*</span>n1<span style="color: #008000;">&#41;</span></pre></div></div>

<p>Application code can then query only when needed. The &#8220;power&#8221; here is in enabling the user to make the decision on balancing cost against convenience.</p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/04/18/thoughts-on-a-powerful-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

