<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">

<channel>
	<title>ShaderOp.com</title>
	
	<link>http://shaderop.com</link>
	<description>Professional code monkey, hobbyist pixel pusher.</description>
	<lastBuildDate>Thu, 26 Aug 2010 21:32:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Shaderop" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="shaderop" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license><item>
		<title>RAII by Example: Implementing GenerateSha1Hash</title>
		<link>http://shaderop.com/2010/08/raii-by-example-implementing-generatesha1hash/</link>
		<comments>http://shaderop.com/2010/08/raii-by-example-implementing-generatesha1hash/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 21:18:30 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[RAII]]></category>
		<category><![CDATA[sha-1]]></category>
		<category><![CDATA[sha1-saga]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=330</guid>
		<description><![CDATA[In which I own up and actually implement a function I have written about earlier, and use the chance to talk about RAII for a bit.]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://shaderop.com/2010/08/minimizing-header-bloat-in-c-an-example/">previous installment</a> of the <a href="http://shaderop.com/tag/sha1-saga/">SHA-1—saga</a> I didn’t provide an implementation for the function <strong>GenerateSha1Hash</strong>. So I thought it might be a good idea to milk the subject even more by coding it up and using the opportunity to demonstrate how the <a href="http://en.wikibooks.org/wiki/C%2B%2B_Programming/RAII">Resource Acquisition Is Initialization</a> (or RAII, not be confused with <a href="http://en.wikipedia.org/wiki/Ra%C3%AF">Rai</a>) idiom leads to code that is more maintainable and fault-tolerant.</p>
<p>If I were doing something more involved or required cross-platform portability, I would have been better off with the <a href="http://cryptopp.com/">Crypto++</a> library or something similar. But instead I’ll be using the <a href="http://msdn.microsoft.com/en-us/library/aa380255%28VS.85%29.aspx">Cryptography functions</a> from the Win32 API because they rely on manual acquisition and release of resources through handles, which is a better fit for demonstrating RAII in action.</p>
<p>I ended up using the following functions:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/aa379886%28VS.85%29.aspx">CryptAcquireContext</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/aa380268%28VS.85%29.aspx">CryptReleaseContext</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/aa379908%28VS.85%29.aspx">CryptCreateHash</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/aa379917%28VS.85%29.aspx">CryptDestroyHash</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/aa379947%28VS.85%29.aspx">CryptGetHashParam</a></li>
</ul>
<p>This is my initial, RAII-less implementation:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">"GenerateSha1Hash.h"</span>

<span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;windows.h&gt;</span>

<span style="color: #cc7832;">int</span> GenerateSha1Hash(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data, int8_t** hash, <span style="color: #cc7832;">int</span> hashSize)
{
    <span style="color: #cc7832;">if</span> (hashSize == <span style="color: #6897bb;">0</span>)
        <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;

    HCRYPTPROV hCryptProvider = NULL;
    HCRYPTHASH hHash = NULL;

    BOOL status = ::CryptAcquireContext
        ( &amp;hCryptProvider
        , NULL
        , NULL
        , PROV_RSA_FULL
        , <span style="color: #6897bb;">0
</span>        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    status = ::CryptCreateHash(hCryptProvider, CALG_SHA1, <span style="color: #6897bb;">0</span>, <span style="color: #6897bb;">0</span>, &amp;hHash);
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }

    status = ::CryptHashData
        ( hHash
        , <span style="color: #cc7832;">reinterpret_cast</span>&lt;<span style="color: #cc7832;">const</span> BYTE*&gt;(data)
        , strlen(data) + <span style="color: #6897bb;">1
</span>        , <span style="color: #6897bb;">0
</span>        );

    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        ::CryptDestroyHash(hHash);
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }

    DWORD hashBufferSize = <span style="color: #6897bb;">0</span>;
    DWORD hashBufferSizeSize = <span style="color: #cc7832;">sizeof</span>(DWORD)/<span style="color: #cc7832;">sizeof</span>(BYTE);

    status = ::CryptGetHashParam
        ( hHash
        , HP_HASHSIZE
        , <span style="color: #cc7832;">reinterpret_cast</span>&lt;BYTE*&gt;(&amp;hashBufferSize)
        , &amp;hashBufferSizeSize
        , <span style="color: #6897bb;">0</span>
        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        ::CryptDestroyHash(hHash);
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }

    BYTE* hashBuffer = <span style="color: #cc7832;">new</span> BYTE[hashBufferSize];
    status = ::CryptGetHashParam
        ( hHash
        , HP_HASHVAL
        , hashBuffer
        , &amp;hashBufferSize
        , <span style="color: #6897bb;">0
</span>        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        <span style="color: #cc7832;">delete</span> hashBuffer;
        ::CryptDestroyHash(hHash);
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }
       
    <span style="color: #cc7832;">int</span> numberOfBytesToCopy = (<span style="color: #cc7832;">static_cast</span>&lt;<span style="color: #cc7832;">int</span>&gt;(hashBufferSize) &lt; hashSize)?
                              hashBufferSize : hashSize;
    <span style="color: #cc7832;">for</span> (<span style="color: #cc7832;">int</span> i = <span style="color: #6897bb;">0</span>; i &lt; numberOfBytesToCopy; ++i)
        (*hash)[i] = hashBuffer[i];

    <span style="color: #cc7832;">delete</span> hashBuffer;
    ::CryptDestroyHash(hHash);
    ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);

    <span style="color: #cc7832;">return</span> numberOfBytesToCopy;
}</pre>
<p>The mechanics are not that relevant to our example, and anyone could arrive to a similar implementation by reading the MSDN documentation at the links to above. But here are the basic, broad strokes:</p>
<ol>
<li>Ask the Win32 API for a <strong>CryptContext</strong>.</li>
<li>Ask the <strong>CryptContext</strong> for a <strong>CryptHash</strong> that can produce SHA-1 hashes.</li>
<li>Pass the data to be hashed to the <strong>CryptHash</strong>.</li>
<li>Ask the <strong>CryptHash</strong> for the number of bytes it will need to store the hash.</li>
<li>Pass a buffer to the <strong>CryptHash</strong> and ask it to store the hash in the buffer.</li>
<li>Release all resources.</li>
</ol>
<p>Which is simple enough. What complicates the code is this refrain:</p>
<pre style="font-family: consolas;">    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }</pre>
<p>What later repeats and grows into this:</p>
<pre style="font-family: consolas;">    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        ::CryptDestroyHash(hHash);
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }</pre>
<p><span style="font-size: small;">Finally ending in this off-key crescendo:</span></p>
<pre style="font-family: consolas;">    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
    {
        <span style="color: #cc7832;">delete</span> hashBuffer;
        ::CryptDestroyHash(hHash);
        ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    }</pre>
<p>I’m perfectly fine with the <strong>if</strong> statements being there. If one of those functions fail, then there’s nothing more to do and the code has to return. What I <em>do </em>have a problem with is the book keeping code that has to tag along at every step of the way, and that’s because it leads to two problems.</p>
<p>First one is maintainability. Repetition and maintainability don’t go well together.</p>
<p>The second and more important issue is this: What about errors I did <em>not</em> account for? What about this line for example:</p>
<pre style="font-family: consolas;">BYTE* hashBuffer = <span style="color: #cc7832;">new</span> BYTE[hashBufferSize];</pre>
<p>According to the standard, a call to <strong>new</strong> could fail by throwing a <strong>bad_alloc</strong> exception if memory can’t be allocated for one reason or another. Yes, I could surround it with a <strong>try-catch</strong> block and then repeat the clean up code yet again. But that’s not the point. The point is that exceptions can be thrown from seemingly innocent looking code, and accounting for all such cases is no trivial task.</p>
<p>One might argue that worrying about releasing resources after <strong>bad_alloc</strong> exceptions is like agonizing over filing the change of address forms at the local post office while the apocalypse is going down in T-minus five minutes.</p>
<p>Similarly one could argue that the chances of any of those cryptography functions failing is next to nil. How dangerous can generating a digest actually be?</p>
<p>I don’t know. And that’s the problem. I can reason, I can theorize, I can guestimate, but I simply <em>don’t know</em> the actual reliability of those functions, and I wouldn’t even know how to start testing it.</p>
<p>And that which you can’t test, you must guard against.</p>
<h4>RIAA In Longhand</h4>
<p>The best scenario would be to centralize the clean up code somewhere and still maintain robustness against both expected and unexpected errors. That’s where RIAA comes in.</p>
<p>Take for example the pair <strong>CryptAcquireContext</strong> and <strong>CrypeReleaseContext</strong>. If <strong>CryptAcquireContext</strong> is called successfully then its counterpart <strong>CryptReleaseContext</strong> must also be called at some point, regardless of what happens in the rest of the code.</p>
<p>In RIAA this is done by creating a class that has the sole purpose of calling the clean up code in its destructor. Here’s what such a class might look like in its simplest form:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> CrytpContextHandle{
<span style="color: #cc7832;">public</span>:
    CrytpContextHandle(HCRYPTPROV cryptContextHandle)
        : m_cryptContextHandle(cryptContextHandle)
    {
    }

    ~CrytpContextHandle()
    {
        <span style="color: #cc7832;">if</span> (m_cryptContextHandle == NULL)
            ::CryptReleaseContext(m_cryptContextHandle, <span style="color: #6897bb;">0</span>);
    }

    HCRYPTPROV Handle() <span style="color: #cc7832;">const</span>
    {
        <span style="color: #cc7832;">return</span> m_cryptContextHandle;
    }

<span style="color: #cc7832;">private</span>:
    HCRYPTPROV m_cryptContextHandle;
};</pre>
<p>And here’s how to use it:</p>
<pre style="font-family: consolas;">    HCRYPTPROV hCryptProvider = NULL;
    HCRYPTHASH hHash = NULL;

    BOOL status = ::CryptAcquireContext
        (&amp;hCryptProvider
        , NULL
        , NULL
        , PROV_RSA_FULL
        , <span style="color: #6897bb;">0
</span>        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    CrytpContextHandle cryptContextHandle(hCryptProvider);</pre>
<p>Now <strong>CryptReleastContext</strong> will be called once the function terminates, whether it’s a normal termination or as a result of an unhandled exception.</p>
<p>Notice that the purpose of the class <strong>CrytpContextHandle</strong> isn’t to wrap the behavior of the Win32 Cryptography API in something more OO-friendly. It has a simpler and much narrower purpose, and the provided implementation satisfies that purpose succinctly.</p>
<p>A similar RAII class needs to be provided for <strong>CryptCreateHash</strong> and its counterpart <strong>CryptDestroyHash</strong>, and possibly another one to delete the <strong>hashBuffer</strong>. This is not a major burden, but these classes have limited utility since they’ll probably never be used elsewhere in the program.</p>
<p>Wouldn’t be nice if there was a simpler way yet?</p>
<p>Fortunately, there is.</p>
<h4>RIAA in shorthand</h4>
<p>Deleting the dynamically allocated <strong>hashBuffer</strong> can be made trivial by using <a href="http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/scoped_array.htm">boost::scoped_array</a>. For the rest of the clean up code I’ll use a bit of magic called <a href="http://www.boost.org/doc/libs/1_44_0/libs/scope_exit/doc/html/index.html">BOOST_SCOPE_EXIT</a>. Here’s the revised implementation:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">"GenerateSha1Hash.h"</span>

<span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;windows.h&gt;
</span><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;algorithm&gt;</span><span style="color: #da4832;">
#include</span> <span style="color: #a5c25c;">&lt;boost/scoped_array.hpp&gt;
</span><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;boost/scope_exit.hpp&gt;</span>

<span style="color: #cc7832;">int</span> GenerateSha1Hash(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data, int8_t** hash, <span style="color: #cc7832;">int</span> hashSize)
{
    <span style="color: #cc7832;">if</span> (hashSize &lt;= <span style="color: #6897bb;">0</span>)
        <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;

    HCRYPTPROV hCryptProvider = NULL;
    HCRYPTHASH hHash = NULL;

    BOOL status = ::CryptAcquireContext
        ( &amp;hCryptProvider
        , NULL
        , NULL
        , PROV_RSA_FULL
        , <span style="color: #6897bb;">0
</span>        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    BOOST_SCOPE_EXIT((&amp;hCryptProvider))
    {
        <span style="color: #cc7832;">if</span> (hCryptProvider != NULL)
            ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
    } BOOST_SCOPE_EXIT_END

    status = ::CryptCreateHash(hCryptProvider, CALG_SHA1, <span style="color: #6897bb;">0</span>, <span style="color: #6897bb;">0</span>, &amp;hHash);
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    BOOST_SCOPE_EXIT((&amp;hHash))
    {
        <span style="color: #cc7832;">if</span> (hHash != NULL)
            ::CryptDestroyHash(hHash);
    } BOOST_SCOPE_EXIT_END
   
    status = ::CryptHashData
        (hHash
        , <span style="color: #cc7832;">reinterpret_cast</span>&lt;<span style="color: #cc7832;">const</span> BYTE*&gt;(data)
        , strlen(data) + <span style="color: #6897bb;">1
</span>        , <span style="color: #6897bb;">0</span>
        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;

    DWORD hashBufferSize = <span style="color: #6897bb;">0</span>;
    DWORD hashBufferSizeSize = <span style="color: #cc7832;">sizeof</span>(DWORD)/<span style="color: #cc7832;">sizeof</span>(BYTE);

    status = ::CryptGetHashParam
        ( hHash
        , HP_HASHSIZE
        , <span style="color: #cc7832;">reinterpret_cast</span>&lt;BYTE*&gt;(&amp;hashBufferSize)
        , &amp;hashBufferSizeSize
        , <span style="color: #6897bb;">0
</span>        );
     <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;

    boost::scoped_array&lt;BYTE&gt; hashBuffer(<span style="color: #cc7832;">new</span> BYTE[hashBufferSize]);
    status = ::CryptGetHashParam
        ( hHash
        , HP_HASHVAL
        , hashBuffer.get()
        , &amp;hashBufferSize
        , <span style="color: #6897bb;">0
</span>        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
   
    <span style="color: #cc7832;">int</span> numberOfBytesToCopy = (<span style="color: #cc7832;">static_cast</span>&lt;<span style="color: #cc7832;">int</span>&gt;(hashBufferSize) &lt; hashSize)?
                              hashBufferSize : hashSize;	

    std::copy(hashBuffer.get(), hashBuffer.get() + numberOfBytesToCopy, *hash);

    <span style="color: #cc7832;">return</span> numberOfBytesToCopy;}</pre>
<p>This is much better and would have been even shorter if it wasn’t for my funky argument formatting style.</p>
<p>Here’s a closer look at one of the juicer bits:</p>
<pre style="font-family: consolas;">    BOOL status = ::CryptAcquireContext
        ( &amp;hCryptProvider
        , NULL
        , NULL
        , PROV_RSA_FULL
        , <span style="color: #6897bb;">0
</span>        );
    <span style="color: #cc7832;">if</span> (status == <span style="color: #cc7832;">false</span>)
        <span style="color: #cc7832;">return</span> -<span style="color: #6897bb;">1</span>;
    BOOST_SCOPE_EXIT((&amp;hCryptProvider))
    {
        <span style="color: #cc7832;">if</span> (hCryptProvider)
            ::CryptReleaseContext(hCryptProvider, <span style="color: #6897bb;">0</span>);
    } BOOST_SCOPE_EXIT_END</pre>
<p>Here a resource is acquired and the code that releases it is declared right underneath it inside the <strong>BOOST_SCOPE_EXIT</strong> macro, which will be called upon exit from the scope in which it resides.</p>
<p>The only hurdle is getting past the crusty <strong>((&amp;variableName))</strong> syntax (which is explained in the <a href="http://www.boost.org/doc/libs/1_44_0/libs/scope_exit/doc/html/scope_exit/tutorial.html">Scope Exit tutorial</a>). Otherwise, this is easier to maintain since the clean up code sits right next to the corresponding acquisition code and only has to be written once.</p>
<h4>Summing Up</h4>
<p>The RAII idiom rose out of necessity to compensate for missing features in C++ like automatic memory management. But I believe its usefulness extends well beyond that into areas like maintainability and fault tolerance. So much so that the RAII idiom is built into some languages that already have garbage collection, like C# with its <a href="http://msdn.microsoft.com/en-us/library/yh598w02.aspx">using keyword</a> and Python and its <a href="http://www.python.org/dev/peps/pep-0343/">with statement</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/08/raii-by-example-implementing-generatesha1hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minimizing Header Bloat in C++: An Example</title>
		<link>http://shaderop.com/2010/08/minimizing-header-bloat-in-c-an-example/</link>
		<comments>http://shaderop.com/2010/08/minimizing-header-bloat-in-c-an-example/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 22:45:34 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[hpp]]></category>
		<category><![CDATA[sha-1]]></category>
		<category><![CDATA[sha1-saga]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=325</guid>
		<description><![CDATA[In which I waste way too much time debating the merit of std::vector<int8_t> vs. boost::shared_array<int8_t> vs. plain ol' int8_t*.]]></description>
			<content:encoded><![CDATA[<p>I claimed in my <a href="http://shaderop.com/2010/08/bad-assumptions-hashing-algorithms/">previous post</a> that validating one’s assumptions before committing to a particular solution is a good habit, but I was being slightly hypocritical. The fact of the matter is that I <em>did</em> actually write a SHA-1—based ID generator before doing any tests.</p>
<p>The effort wasn’t a total waste. It reminded me of a subject that I had long forgotten since living in .NET land for so long, and that is importance of <strong>minimizing header file dependencies</strong> in one’s code. The problem and the solution are explained nicely in the <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Google C++ Style Guide</a>, in the section aptly titled “<a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_File_Dependencies">Header File Dependencies</a>.” But the gist of it is to try to use as few <strong>#include</strong> directives as possible in your own header files.</p>
<p>This is often easier said than done, and sometimes leads to interface design choices that might look unintuitive to someone coming from a programming language that has proper support for modules, like C# and Java for instance. But such choices are usually justified.</p>
<p>I’ll try to present such an example here from my own SHA-1 experiment.</p>
<p>I wanted to write a function that takes a string and returns its SHA-1 hash. SHA-1 hashes are 160 bits long, so the return type has to be some sort of array or container.</p>
<p>I could have written something like the following in the header file:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#ifndef</span> GENERATESHA1HASH_H_
<span style="color: #da4832;">#define</span> GENERATESHA1HASH_H_

<span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;stdint.h&gt;
</span><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;vector&gt;</span><span style="color: #da4832;">
#include</span> <span style="color: #a5c25c;">&lt;string&gt;</span>

std::vector&lt;int8_t&gt; GenerateSha1Hash(<span style="color: #cc7832;">const</span> std::string data);

<span style="color: #da4832;">#endif</span> <span style="color: gray;">// GENERATESHA1HASH_H_</span></pre>
<p>But then any code that includes this header will also take a dependency on the headers <strong>&lt;vector&gt;, </strong><strong>&lt;string&gt;</strong>, and <strong>&lt;stdint.h&gt;</strong>, which will lead to longer compilation times. And no one likes longer compilation times.</p>
<p>The argument can be changed to <strong>const char*</strong> without any loss of readability or ease of use:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#ifndef</span> GENERATESHA1HASH_H_
<span style="color: #da4832;">#define</span> GENERATESHA1HASH_H_

<span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;stdint.h&gt;
</span><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;vector&gt;</span>

std::vector&lt;int8_t&gt; GenerateSha1Hash(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data);

<span style="color: #da4832;">#endif</span> <span style="color: gray;">// MESSAGEBUS_GENERATESHA1HASH_H_</span></pre>
<p>That’s one less header file to worry about. What about <strong>&lt;stdint.h&gt;</strong>? Do I really need <strong>int8_t</strong> in there? Firstly, I don’t want to worry about compiler- and platform-dependent sizes of the native types. Secondly, while it’s possible to return a container of <strong>int</strong>s and document somewhere that only the lower 8 bits of each <strong>int</strong> will be used, I would rather have the intent clearly defined in code.</p>
<p>So <strong>&lt;stdint.h&gt;</strong> stays. I can take solace in the fact that it’s a smaller header file and won’t be too much of a hit on compilation times.</p>
<p>Next up is &lt;vector&gt;. I can opt to use a different container, but that will only mean switching one header for another. Using a <a href="http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/shared_array.htm">shared_array</a> has the same issue. Returning a naked dynamically allocated pointer to the data and expecting the calling code to free it is simply unacceptable and is just plain asking for trouble.</p>
<p>There’s another possibility: Make the calling code carry the burden of providing the storage for the hash:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;stdint.h&gt;</span>

<span style="color: #cc7832;">void</span> GenerateSha1Hash(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data, int8_t** hash, <span style="color: #cc7832;">int</span> hashSize);</pre>
<p>With this version the caller is expected to pass in a pointer to an array of bytes in <strong>hash</strong> and the size of the array in <strong>hashSize</strong>. The two possible issues with this is that the caller has to allocate the memory himself, and he also has to know that a SHA-1 hash will take 20 bytes of storage.</p>
<p>It can be made slightly better:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;stdint.h&gt;</span>

<span style="color: #cc7832;">int</span> GenerateSha1Hash(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data, int8_t** hash, <span style="color: #cc7832;">int</span> hashSize);</pre>
<p><span style="font-size: small;">In this case the function will return –1 in case of errors, the value of <strong>hashSize</strong> if the output buffer is smaller than the size of the SHA-1 hash, or the actual size of the SHA-1 hash if the output buffer is larger.</span></p>
<p>This is slightly more resilient. I made the choice to be too forgiving and allow the call to succeed if the buffer is smaller than the size of the generated hash, but I could have chosen to fail if the buffer is too small.</p>
<p>All together now and with some <a href="http://www.stack.nl/~dimitri/doxygen/index.html">Doxygen</a> documentation thrown in:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#ifndef</span> GENERATESHA1HASH_H_
<span style="color: #da4832;">#define</span> GENERATESHA1HASH_H_

<span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">&lt;stdint.h&gt;</span>

<span style="color: green;">/// </span><span style="color: green;">
/// Generates a 20-byte SHA-1 hash for the provided string.</span><span style="color: green;">
///
</span><span style="color: green;">/// @param[in] data the string for which a hash will be generated.
</span><span style="color: green;">/// @param[out] hash a pointer to the buffer that will receive the hash
</span><span style="color: green;">/// @param[in] hashSize the size of the buffer
</span><span style="color: green;">///
</span><span style="color: green;">/// @return the actual number of bytes stored in </span><span style="color: gray;">&lt;em&gt;</span><span style="color: green;">hash</span><span style="color: gray;">&lt;/em&gt;</span><span style="color: green;"> on success,</span><span style="color: green;">
///  or -1 on failure.
</span><span style="color: green;">///
</span><span style="color: #cc7832;">int</span> GenerateSha1Hash(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data, int8_t** hash, <span style="color: #cc7832;">int</span> hashSize);

<span style="color: #da4832;">#endif</span> <span style="color: gray;">// GENERATESHA1HASH_H_</span></pre>
<p>That will do. Now all that is left is to actually implement the thing. But that’s for <a href="/2010/08/raii-by-example-implementing-generatesha1hash/">later</a>.</p>
<h4>Final thoughts</h4>
<p>“Who in their right mind would want to worry about the cost of returning an array of bytes?” one might say. And it’s an entirely valid opinion in my view. But there’s two important points to consider here:</p>
<p>Firstly, you don’t have to worry about it if you don’t want to. If this was a private function not meant for sharing among applications, and if my application was already using, for example, <strong>shared_array</strong> all over the place, I would certainly go ahead and just return a <strong>shared_array</strong> and be done with it.</p>
<p>Secondly, I would argue that having an eye for detail is always a good thing, regardless of programming language. For example, which would be better: A C# method that takes an input parameter of type <strong>List&lt;T&gt;</strong> or of type <strong>IList&lt;T&gt;</strong>? And wouldn’t an <strong>IEnumerable&lt;T&gt;</strong> be better yet?</p>
<p>Most developers these days don’t need to work with C++, and thank goodness for that. But tinkering with different programming languages is a lot like solving crossword puzzles: it keeps the mind active and (arguably) healthy. And C++ is, I think, <a href="http://en.wikipedia.org/wiki/The_New_York_Times_crossword_puzzle">The New York Times of cross puzzles</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/08/minimizing-header-bloat-in-c-an-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bad Assumptions: Hashing Algorithms</title>
		<link>http://shaderop.com/2010/08/bad-assumptions-hashing-algorithms/</link>
		<comments>http://shaderop.com/2010/08/bad-assumptions-hashing-algorithms/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 00:01:23 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[bad-assumptions]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[crc-32]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[sha-1]]></category>
		<category><![CDATA[sha1-saga]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=321</guid>
		<description><![CDATA[In which I play with SHA-1, MD5, and CRC32, and end up with some surprising conclusions (at least they were surprising me).]]></description>
			<content:encoded><![CDATA[<p>I’m doing some prep work for a little personal project I’m working on. For reasons that will hopefully become clear in future posts, I want to do the following:</p>
<p><em>Given the fully-qualified name of a type (whether it’s a </em>class<em>, </em>struct<em>, or </em>function<em>), generate a  unique identifier for it.</em></p>
<p>The identifier has to have the following properties:</p>
<ol>
<li>It should fit into 32 bits.</li>
<li>It should be unique only within the scope of a given program.</li>
<li>Speed isn’t a big factor since the results can be cached, but the faster it can be generated the better.</li>
</ol>
<p>Since the scope of uniqueness I care for is limited, I theorized that the first four bytes of a <a href="http://en.wikipedia.org/wiki/SHA-1">SHA-1</a> hash of the fully qualified name of the type <em>should</em> be unique enough in a given program space. It’s an assumption that sounds reasonable, but I had to test it before basing any code on it.</p>
<p>And while I was at it, I decided to test <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> as well because it’s supposedly faster. I also threw in <a href="http://en.wikipedia.org/wiki/CRC32">CRC32</a> just for laughs, because I reasoned while it would be the fastest of the three it would fail miserably at generating unique ID’s with a large enough set of types. Or so I thought.</p>
<p>Generating SHA-1 and MD5 using .NET is trivial thanks to the classes in the <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx">System.Security.Cryptography</a> namespace, but I had to look elsewhere for CRC32. I finally found an excellent and robust <a href="http://blogs.msdn.com/b/delay/archive/2009/01/14/free-hash-a-reusable-crc-32-hashalgorithm-implementation-for-net.aspx">implementation</a> on <a href="http://blogs.msdn.com/b/delay/">David Anson’s blog</a>.</p>
<p>To test all three I wrote a little C# console application that went through all the types in the <strong>System</strong> assembly, computed all three hashes, and took the first four bytes of the hash and stuffed them into a <strong>UInt32</strong>. The program ends by printing out the total number of types found and the total number of <em>unique</em> IDs generated.</p>
<p>Here’s the first run with just the types in the <strong>System</strong> assembly:</p>
<pre style="font-family: consolas;">Number of types         = 2779
Number of sha1 hashes   = 2779
Number of md5 hashes    = 2779
Number of crc32 hashes  = 2779</pre>
<p>That looks reasonable enough. CRC32 was doing pretty well. But I was sure it won’t take ling to break it.</p>
<p>Same test again, but this time with <strong>System</strong>, <strong>System.Xml</strong>, and <strong>System.Xml.Linq</strong></p>
<pre style="font-family: consolas;">Number of types         = 3740
Number of sha1 hashes   = 3740
Number of md5 hashes    = 3740
Number of crc32 hashes  = 3740</pre>
<p>I kept adding assemblies and rerunning the test, and <em>still </em>all hashing algorithms managed to produce unique ID’s. The final test I ran was this:</p>
<pre style="font-family: consolas;">Number of types         = 10183
Number of sha1 hashes   = 10183
Number of md5 hashes    = 10183
Number of crc32 hashes  = 10183</pre>
<p>10,000 types and still CRC32 was holding its ground. At that point I began to suspect that something was wrong with my code. More drastic testing was required.</p>
<p>So I downloaded <a href="http://www.gutenberg.org/etext/2701">Moby Dick</a>, modified the program to run through the file and store every line in a list while removing duplicates and trimming whitespaces, and then ran the hashing algorithms on each line in that list:</p>
<pre style="font-family: consolas;">Number of unique lines  = 18847
Number of sha1 hashes   = 18847
Number of md5 hashes    = 18847
Number of crc32 hashes  = 18847</pre>
<p>And again with <a href="http://www.gutenberg.org/etext/2600">War and Peace</a>:</p>
<pre style="font-family: consolas;">Number of unique lines  = 50605
Number of sha1 hashes   = 50604
Number of md5 hashes    = 50605
Number of crc32 hashes  = 50605</pre>
<p><span style="font-size: small;">Finally one algorithm produced a duplicate, but it’s SHA-1. CRC32 is still tugging along happily.</span></p>
<p>I wasn’t going to give up until the others broke, so I tried again with <a href="http://www.gutenberg.org/etext/1581">The Bible</a>:</p>
<pre style="font-family: consolas;">Number of unique lines  = 98377
Number of sha1 hashes   = 98377
Number of md5 hashes    = 98376
Number of crc32 hashes  = 98377</pre>
<p>The word of God did manage to shake MD5, but not the humble CRC32. Underestimating the meek is never a good idea.</p>
<p>All three books combined together now in one giant 10-megabyte file:</p>
<pre style="font-family: consolas;">Number of unique lines  = 167309
Number of sha1 hashes   = 167307
Number of md5 hashes    = 167305
Number of crc32 hashes  = 167307</pre>
<p>Finally CRC32 cracks, but not before finishing head to head with SHA-1.</p>
<h4>Conclusion</h4>
<p>I don’t know much about statistics and cryptography, so I don’t know what conclusions someone with better knowledge of the subject would draw from all of this. But in my case I’m now more than comfortable to assume the CRC32 will cover my needs quite adequately. I will probably still add some debug-only checks to my code to ensure that no duplicate ID’s are generated in my program, but otherwise, and contrary to my initial assumption, CRC32 are unique enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/08/bad-assumptions-hashing-algorithms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>To VB Programmers: An Apology and an Explanation</title>
		<link>http://shaderop.com/2010/08/to-vb-programmers-an-apology-and-an-explanation/</link>
		<comments>http://shaderop.com/2010/08/to-vb-programmers-an-apology-and-an-explanation/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 02:04:50 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[rants]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://shaderop.com/2010/08/to-vb-programmers-an-apology-and-an-explanation/</guid>
		<description><![CDATA[In which I try to explain the reasoning behind the often less-than-friendly attitude towards Visual Basic in the programming community.]]></description>
			<content:encoded><![CDATA[<p>Dear Visual Basic Programmers,</p>
<p>I’m one of those developers who prefer their braces curled, that is to mean that I’m partial to C-like languages. I started my career programming in C++ and then switched to C# later on.</p>
<p>And although I have worked with good old pre-.NET Visual Basic and VBScript on several projects, and although Visual Basic .NET is now nearly identical to C# in capabilities, I’m still reluctant to touch any VB code, .NET or otherwise, and I find myself overly more cautious when hiring or working with programmers who still call VB their primary programming language.</p>
<p>But I’m not alone. <a href="http://catb.org/~esr/">Eric S. Raymon</a>, author of <a href="http://www.catb.org/~esr/writings/cathedral-bazaar/cathedral-bazaar/">The The Cathedral and the Bazaar</a>, <a href="http://catb.org/esr/faqs/hacker-howto.html#closed_lang">singles out Visual Basic</a> in <a href="http://catb.org/esr/faqs/hacker-howto.html">How to Become a Hacker</a>:</p>
<blockquote><p>Also, like other Basics, Visual Basic is a poorly-designed language that will teach you bad programming habits. No, don&#8217;t ask me to describe them in detail; that explanation would fill a book. Learn a well-designed language instead.</p>
</blockquote>
<p>In a <a href="http://www.hanselman.com/blog/IsRootingForVisualBasicLikeRootingForTheRedSox.aspx">blog post about VB</a>, Scott Hanselman had this to say:</p>
<blockquote><p>Visual Basic programmers, historically, have tended to be a bit long suffering, patiently enduring the wrongs and difficulties of VB while being mocked by the C# folks. &quot;VB&#8217;s a toy.&quot; &quot;VB&#8217;s not performant.&quot; &quot;VB programmers aren&#8217;t real programmers.&quot;</p>
</blockquote>
<p>The above agrees with the general vibe that I’ve felt on the Internet and in the workplace over the years. Programmers, especially those working with C-like languages, look down on their VB brethren. </p>
<p>That attitude is often uncalled for. If I remember correctly, Jeff Atwood of <a href="http://www.codinghorror.com/">Coding Horror</a> and <a href="http://stackoverflow.com/">Stack Overflow</a> started his career as a Visual Basic programmer. <a href="http://www.lhotka.net/">Rockford Lhotka</a>, author of what I consider <a href="http://www.amazon.com/Expert-C-2008-Business-Objects/dp/1430210192">the best enterprise architecture book for .NET</a>, also started out as a Visual Basic coder. And I would love to be another Jeff Atwood or Rockford Lhotka.</p>
<p>Clearly a good programmer is a good programmer regardless of which programming language they use. So why pick on Visual Basic? </p>
<p>Maybe part of it is lingering envy. Back in the dark ages when it took 100 lines of C to display an empty window, a VB programmer could get the same by just creating a new project and not writing a single line of code. When Microsoft’s main web development offering was ASP sans the “.NET” part, VBScript was the de facto language for that platform. The closest thing C++ developers had was <a href="http://en.wikipedia.org/wiki/ATL_Server">ATL Server</a>, which I doubt anyone actually used.</p>
<p>I think there’s more to it than just sour grapes, however. But before I go on, I would like to offer two extreme yet relevant anecdotes from my own experience.</p>
<h4>Dim what now?</h4>
<p>I once worked for a company where I could have sworn that my managers (all three of them) were taking bets on how soon they could break me. Almost every project that was once abandoned found its way to me to shape up and ship out, and a lot of them were horrendous hacks. The final straw came when I was given a utility built in Visual Basic that needed some “very minor modifications” as manager #2 said.</p>
<p>I opened up the project and it contained a single form that looked something like the following:</p>
<p><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="A cluttered program window with too many controls on it" border="0" alt="A cluttered program window with too many controls on it" src="http://shaderop.com/wp-content/uploads/2010/08/image.png" width="393" height="224" /></p>
<p>Imagine it in Battleship grey and much, much more cluttered. It was an insult to both “theology and geometry” as <a href="http://en.wikipedia.org/wiki/Ignatius_J._Reilly#Ignatius_J._Reilly">Ignatius J. Reilly</a> would say. But let he who has not made an unusable mess of a UI cast the first LOL.</p>
<p>The real horror was those little aqua-colored rectangles next to the text boxes. Those turned out to be label controls that were invisible at runtime. The programmer—and I’m using that term generously—stored some data in the text fields of those labels. </p>
<p>Allow me to repeat: <strong>He used invisible label controls to store data.</strong></p>
<p>The only reasonable conclusion is that this person did <em>not</em> understand the concept of <em>variables</em>.</p>
<p>I gave up right there and then, went to manager #3 (since he was on paper my direct manager), and told him that I can’t work on this. They ended up contracting the programmer who originally wrote it to do the required work.</p>
<h4>You don’t need a textbox for that</h4>
<p>In the same company mentioned above, I was tasked with interviewing candidates for programming positions. I finally settled on a simple programming question: For your programming language of choice, write a subroutine that would take <strong>a single string</strong> as an argument and would <strong>reverse it in place</strong>.</p>
<p>Most people I interviewed didn’t know where to even start; the question simply seemed to make no sense to them. The remaining few who provided an answer got it wrong. I can remember only two people who eventually got it right after a nudge in the right direction.</p>
<p>But the most memorable interview was with someone who was <a href="http://en.wikipedia.org/wiki/Microsoft_Certified_Solution_Developer#Microsoft_Certified_Solution_Developer_.28MCSD.29">MSCD</a>-certified in Visual Basic. Unprovoked, he went on a monologue describing in detail his endless accomplishments and, his total awesomeness, and how unfair life was to him.</p>
<p>I had to interrupt him after fifteen minutes or so. I gave him a paper pad and a pen and asked him to write the subroutine described above.</p>
<p>Nothing came. And nothing continued to come for two minutes.</p>
<p>I offered to help. I wrote a skeleton of a Visual Basic subroutine for him and told him to fill in the rest.</p>
<p>Still nothing.</p>
<p>Finally, he began to write, and I quote:</p>
<blockquote><p><font color="#3e3e34">TextBox = </font></p>
</blockquote>
<p>“I don’t think you’ll need a textbox for this,” I said.</p>
<p>He crossed that out, wrote it again, and then we just sat there.</p>
<p>The way I had to end the interview was a sad affair that I will not recount here. </p>
<p>But here’s the thing: <strong>That person did not even comprehend that code could exist outside the scope of responding to UI events</strong>. Even the most basic structured programming concepts like functions and methods were alien to him.</p>
<h4>Of Goats and Sheep</h4>
<p>I believe that those two examples are just extreme cases of <a href="http://www.codinghorror.com/blog/2006/07/separating-programming-sheep-from-non-programming-goats.html">non-programming goats</a>. Actually, I believe that they are so far up the goat end of the sheep-goat spectrum to qualify as members of the Arian-goat race.</p>
<p>Yet they and people like them who are simply incapable of programming have managed to coast by, jumping from ship to ship taking away padded résumés and leaving unmaintainable rubbles of code in their wake. The saddest thing is that many of them end up becoming beneficiaries of the <a href="http://en.wikipedia.org/wiki/The_Dilbert_Principle">Dilbert Principle</a>, probably leading them to hire even more goats since they wouldn’t know any better, and the vicious cycle would continue.</p>
<p>Visual Basic and its ilk of <a href="http://en.wikipedia.org/wiki/Rapid_application_development">RAD</a> tools weren’t the only culprits that enabled the goats to masquerade as sheep, but they certainly helped.</p>
<p>But goats in sheep’s clothing aside, the damage was also extended to those who <em>could</em> actually program to save their lives. Most developers who stayed within the safety of the Visual Basic bubble were insulated from ever having to know about such trivia as the differences between the stack and the heap, how pointers work, the trade-offs between garbage collection and reference counting, or that reference counting even existed.</p>
<p>The problem is that these trivia <em>do</em> still matter: </p>
<blockquote><p>Life just gets messier and messier down here in byte-land. Aren&#8217;t you glad you don&#8217;t have to write in C anymore? We have all these great languages like Perl and Java and VB and XSLT that never make you think of anything like this, they just deal with it, somehow. But occasionally, the plumbing infrastructure sticks up in the middle of the living room, and we have to think about whether to use a String class or a StringBuilder class, or some such distinction, because the compiler is still not smart enough to understand everything about what we&#8217;re trying to accomplish and is trying to help us not write inadvertent Shlemiel the Painter algorithms.</p>
<p align="right">-Joel Spolsky, “<a href="http://www.joelonsoftware.com/articles/fog0000000319.html">Back to Basics</a>”</p>
</blockquote>
<p>Ignorance of byte-land isn’t something unique to Visual Basic, but there seems to be something about Visual Basic of old that encouraged this type of ignorance. Maybe it was because the <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">abstraction was near leak-free</a>, or maybe it was the IDE that tucked away the details too neatly, or maybe it was the poor design of the language itself, or maybe it was the tiering of developers into the component developers caste that did all the bit twiddling and the application developers caste that did all the dragging and dropping.</p>
<h4>In Conclusion</h4>
<p>Whatever the reasons were, and whatever the actual scope of this ignorance of byte-land was, it left a lasting impression among the curly-bracers that borders on being a full-blown stereotype: Visual Basic programmers don’t know the basics.</p>
<p>This stereotype is harsh, unfair, and is often too quickly applied to Visual Basic programmers in our community, and I would personally like to see much less of it. But maybe you can now better understand the reasoning behind it.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/08/to-vb-programmers-an-apology-and-an-explanation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Kindle DX Turns Into A Jackson Pollock</title>
		<link>http://shaderop.com/2010/08/my-kindle-dx-turns-into-a-jackson-pollock/</link>
		<comments>http://shaderop.com/2010/08/my-kindle-dx-turns-into-a-jackson-pollock/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 02:02:46 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Miscellanea]]></category>
		<category><![CDATA[gadgets]]></category>
		<category><![CDATA[rants]]></category>

		<guid isPermaLink="false">http://shaderop.com/2010/08/my-kindle-dx-turns-into-a-jackson-pollock/</guid>
		<description><![CDATA[In which I mourn the loss of a new friend (the aforementioned DX) right before going sour grapes on it.]]></description>
			<content:encoded><![CDATA[<p>After less than three months of ownership, my Kindle DX decided to die, but at least it has done it artfully:</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="My Kindle DX showing broken screen" border="0" alt="My Kindle DX showing broken screen" src="http://shaderop.com/wp-content/uploads/2010/08/KindDxJackPollock.jpg" width="484" height="364" /> </p>
<p>It isn’t quite dead. Rather it’s in a perpetual catatonic state. It will happily accept power and will turn its single, dimmed eye yellow until it had its fill. And it will sometimes show a glimmer of hope in the form of a flickering pixel or two (equivalent to <a href="http://en.wikipedia.org/wiki/Rapid_eye_movement_sleep">REM</a> I guess).</p>
<p>I wish I could send it back, but assuming that the shipping costs aren’t prohibitive, I’m not sure if the process would work for me, considering that I obtained it through a mail forwarding service.</p>
<p>Anyways, my Kindle 2 still survives. That’s some comfort.</p>
<h4>What was good</h4>
<p>The only reason I bought the DX was to read technical, large-format books on it, which proved to be a mixed bag.&#160; The instant gratification of wireless delivery was the biggest plus for me, especially considering that I have to wait an average of ten days for physical book shipments to actually arrive.</p>
<p>The convenience of carrying around a small library was also a big win. Reading <a href="http://www.amazon.com/The-Economist-US-Edition/dp/B0027VSU9S">The Economist</a> with the morning coffee, catching up on&#160; finishing <a href="http://www.amazon.com/ASP-NET-Framework-Second-Experts-Voice/dp/1430228865">Pro ASP.NET MVC 2</a> during breaks, and finally ending the day with some <a href="http://www.asimovs.com/">Asimov</a> after watching the semi-daily dose of <a href="http://www.thedailyshow.com/">The Daily Show</a>—all that was nothing but pure reading bliss.</p>
<h4>What was bad</h4>
<p>But the main reason I bought the DX was for programming books, which looked ridiculously ill-formatted on the diminutive Kindle 2. The DX works better, but not by much. I’ve tried books from <a href="http://oreilly.com/">O’reilly Media</a>, <a href="http://www.apress.com/">Apress</a>, and <a href="http://www.manning.com/">Manning</a>. And while the content was readable, the formatting was lacking. Code blocks are always rendered as pictures, which often look blurry and break the flow of normal text in weird ways.</p>
<p>Using PDFs instead of the Kindle’s native AZW files fixes all formatting issues, but even on the DX the text ends up looking like fine print: readable, but not without effort. And many charts and diagrams don’t render at all.</p>
<h4>Now what?</h4>
<p>I would have been tempted to get another Kindle DX had my experience been more positive. For technical books, it’s barely adequate. For everything else, its smaller sibling is a better fit, both in one’s budget and one’s messenger bag.</p>
<p>Now I’m stuck trying to find an alternative. Reading on my main computer at work or at home is not an option. If <a href="http://www.theshallowsbook.com/nicholascarr/The_Shallows.html">The Shallows</a> is to be believed (and it does offer some very compelling evidence), I fall into <a href="http://en.wikipedia.org/wiki/Attention-deficit_hyperactivity_disorder">ADHD</a> mode as soon as I’m seated in front of anything that has an email client and a browser.</p>
<p>My <strong>netbook</strong> is no good because I feel as if I’m viewing content through a peephole whenever I use it for more than half an hour.</p>
<p>All <strong>Tablet PCs</strong> that I have tried over the years—all three of them in fact—were power-challenged and carrying them in one hand for any extended amount of time required the stamina of a one-handed <a href="http://en.wikipedia.org/wiki/Special_Air_Service">SAS</a> operative <a href="http://www.youtube.com/watch?v=TzARLSbeCxM">trekking across the Falklands</a>. </p>
<p>Which leaves the <strong>iPad</strong>. More flexible, probably less ADHD-inducing than a full PC, has half-decent battery life, not <em>that</em> expensive compared to the DX,&#160; and—according to at least one <a href="http://www.useit.com/alertbox/ipad-kindle-reading.html">report</a>—offers a better reading experience than the Kindle.</p>
<p>But then again, I would be hovering dangerously close to becoming something like <a href="http://www.cracked.com/photoplasty_119_instructional-diagrams-people-who-suck-at-everyday-life_p18#4">this</a>.</p>
<p>Risky.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/08/my-kindle-dx-turns-into-a-jackson-pollock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using HtmlUnit to Test .NET Applications</title>
		<link>http://shaderop.com/2010/08/using-htmlunit-to-test-net-applications/</link>
		<comments>http://shaderop.com/2010/08/using-htmlunit-to-test-net-applications/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 22:38:16 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://shaderop.com/2010/08/using-htmlunit-to-test-net-applications/</guid>
		<description><![CDATA[I have been reading a lot about Behavior-Driven Development and test automation lately. Steven Sanderson’s blog proved to be a goldmine of practical information in that regard, specifically his post about BDD, SpecFlow, and ASP.NET MVC. From my limited understanding of the subject, an ideal BDD test should verify a feature by running a series [...]]]></description>
			<content:encoded><![CDATA[<p>I have been reading a lot about <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">Behavior-Driven Development</a> and test automation lately. <a href="http://blog.stevensanderson.com/">Steven Sanderson’s blog</a> proved to be a goldmine of practical information in that regard, specifically his post about <a href="http://blog.stevensanderson.com/2010/03/03/behavior-driven-development-bdd-with-specflow-and-aspnet-mvc/">BDD, SpecFlow, and ASP.NET MVC</a>.</p>
<p>From my limited understanding of the subject, an ideal BDD test should verify a feature by running a series of tests against a real system using a real web browser. With the help of a suitable library, like <a href="http://watin.sourceforge.net/">WatiN</a> for example, automating the web browser becomes a doable task. The problem is that as the number of tests increases, running them against a real browser becomes time consuming.</p>
<p>Mr. Sanderson lists another option in another <a href="http://blog.stevensanderson.com/2010/03/30/using-htmlunit-on-net-for-headless-browser-automation/">blog post</a>, and that is using a headless browser called <a href="http://htmlunit.sourceforge.net/">HtmlUnit</a>. It’s built with Java, but Mr. Sanderson has managed to get it running in .NET using <a href="http://www.ikvm.net/">IKVM</a>, and I was able to replicate his experience successfully.</p>
<h4>Close, But No Cigar</h4>
<p>When working on a feature, I would like to be able to:</p>
<ul>
<li>Use a real browser with BDD testing while developing the feature. This would allow me to actually <em>see</em> what’s going on and would make debugging easier.</li>
<li>Once I’m done working on it and the BDD tests are passing, I would like to switch to an headless browser for regression tests, since it would make the tests faster, and thus more likely to be maintained and ran with every build.</li>
<li>If a feature breaks during regression, I would like to switch back to using a real browser since, again, it would make debugging easier.</li>
</ul>
<p>Using <strong>HtmlUnit</strong> and some other browser automation framework (e.g. <a href="http://watin.sourceforge.net/">WatiN</a>) would mean that each test will have to be implemented twice, once for each framework, and that’s far from ideal.</p>
<h4>Enter Selenium’s WebDriver</h4>
<p>I have never used <a href="http://seleniumhq.org/">Selenium</a> before, so my knowledge of their range of products is peripheral at best. But apparently they are implementing a new browser automation API called “<a href="http://seleniumhq.org/docs/09_webdriver.html">WebDriver</a>,” which will be part of the upcoming Selenium 2.0 release. This new API should allow programmatic automation of IE, Chrome, Firefox, and HtmlUnit using a uniform set of interfaces.</p>
<p>The latest public release is Alpha 5, which can be downloaded at <a href="http://code.google.com/p/selenium/downloads/list">this link</a>. They have binaries for both Java and .NET, and the source code has libraries for Python and Ruby as well. I’ve only tried the .NET implementation, and found the Firefox driver to be quite solid, unlike the IE version, which was a bit too buggy for my taste. Hopefully it will all be sorted out by the final release.</p>
<p>Mr. Sanderson’s gives an example of how to use HtmlUnit in the <a href="http://blog.stevensanderson.com/2010/03/30/using-htmlunit-on-net-for-headless-browser-automation/">blog post mentioned earlier</a>. Here’s how the same example would be implemented in using WebDriver and Firefox:</p>
<pre style="font-family: consolas;">[<span style="color: #ffc66d;">TestFixture</span>]
<span style="color: #cc7832;">public</span> <span style="color: #cc7832;">class</span> <span style="color: #ffc66d;">GoogleSearchWithFirefox
</span>{
    <span style="color: #cc7832;">private</span> <span style="color: #6897bb;">IWebDriver</span> _driver;

    [<span style="color: #ffc66d;">SetUp</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> SetUp()
    {
        _driver = <span style="color: #cc7832;">new</span> <span style="color: #ffc66d;">FirefoxDriver</span>();
    }

    [<span style="color: #ffc66d;">TearDown</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> TearDown()
    {
        _driver.Close();
    }

    [<span style="color: #ffc66d;">Test</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> Can_Load_Google_Homepage()
    {
        _driver.Url = <span style="color: #a5c25c;">"http://www.google.com/"</span>;
        <span style="color: #ffc66d;">Assert</span>.That(<span style="color: #a5c25c;">"Google"</span>, <span style="color: #ffc66d;">Is</span>.EqualTo(_driver.Title));
    }

    [<span style="color: #ffc66d;">Test</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> Google_Search_For_AspNetMvc_Yields_Link_To_AspDotNet()
    {
        _driver.Url = <span style="color: #a5c25c;">"http://www.google.com/"</span>;
        _driver.FindElement(<span style="color: #ffc66d;">By</span>.Name(<span style="color: #a5c25c;">"q"</span>)).SendKeys(<span style="color: #a5c25c;">"asp.net mvc"</span>);
        _driver.FindElement(<span style="color: #ffc66d;">By</span>.Name(<span style="color: #a5c25c;">"btnG"</span>)).Click();

        <span style="color: gray;">// Should be on the results page now:</span>
        <span style="color: #cc7832;">var</span> resultLinks =
            <span style="color: #cc7832;">from</span> element <span style="color: #cc7832;">in</span> _driver.FindElements(<span style="color: #ffc66d;">By</span>.TagName(<span style="color: #a5c25c;">"a"</span>))
            <span style="color: #cc7832;">let</span> href = element.GetAttribute(<span style="color: #a5c25c;">"href"</span>)
            <span style="color: #cc7832;">where</span> href.StartsWith(<span style="color: #a5c25c;">"http://"</span>)
            <span style="color: #cc7832;">let</span> uri = <span style="color: #cc7832;">new</span> <span style="color: #ffc66d;">Uri</span>(href)
            <span style="color: #cc7832;">where</span> uri.Host.ToLower().EndsWith(<span style="color: #a5c25c;">"asp.net"</span>)
            <span style="color: #cc7832;">select</span> uri
            ;

        <span style="color: #ffc66d;">CollectionAssert</span>.IsNotEmpty(resultLinks);
    }
}</pre>
<p>I think the above code is self-explanatory and not too hard on the eyes. To run the same code against IE, it would be a simple matter of changing the following line:</p>
<pre style="font-family: consolas;">_driver = <span style="color: #cc7832;">new</span> <span style="color: #ffc66d;">FirefoxDriver</span>();</pre>
<p>To:</p>
<pre style="font-family: consolas;">_driver = <span style="color: #cc7832;">new</span> <span style="color: #ffc66d;">InternetExplorerDriver</span>();</pre>
<p>And everything <em>should</em> (at least once the final version of the IE driver is released) continue to work as before.</p>
<p>Now, as I have mentioned before, the WebDriver library does contain an implementation for an HtmlUnit-based driver, but unfortunately it’s only available in the Java version.</p>
<h4>My Modest Contribution</h4>
<p>So, I thought it would be a good idea to write a .NET implementation of the WebDriver API using HtmlUnit. I compiled the HtmlUnit JAR files to .NET assemblies, downloaded the WebDriver source code, and went to town. The WebDriver source code has about 340 unit tests. So far I’ve managed to get 303 of those to pass.</p>
<p>Using what I have so far, the above code could be rewritten as follows:</p>
<pre style="font-family: consolas;">[<span style="color: #ffc66d;">TestFixture</span>]<span style="color: #cc7832;">
public</span> <span style="color: #cc7832;">class</span> <span style="color: #ffc66d;">GoogleSearchWithHtmlUnit</span>
{
    <span style="color: #cc7832;">private</span> <span style="color: #6897bb;">IWebDriver</span> _driver;

    [<span style="color: #ffc66d;">SetUp</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> SetUp()
    {
        _driver = <span style="color: #cc7832;">new</span> <span style="color: #ffc66d;">HtmlUnitDriver</span>(<span style="color: #cc7832;">true</span>);
    }

    [<span style="color: #ffc66d;">TearDown</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> TearDown()
    {
        _driver.Close();
    }

    [<span style="color: #ffc66d;">Test</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> Can_Load_Google_Homepage()
    {
        _driver.Url = <span style="color: #a5c25c;">"http://www.google.com/"</span>;
        <span style="color: #ffc66d;">Assert</span>.That(<span style="color: #a5c25c;">"Google"</span>, <span style="color: #ffc66d;">Is</span>.EqualTo(_driver.Title));
    }

    [<span style="color: #ffc66d;">Test</span>]
    <span style="color: #cc7832;">public</span> <span style="color: #cc7832;">void</span> Google_Search_For_AspNetMvc_Yields_Link_To_AspDotNet()
    {
        _driver.Url = <span style="color: #a5c25c;">"http://www.google.com/"</span>;
        _driver.FindElement(<span style="color: #ffc66d;">By</span>.Name(<span style="color: #a5c25c;">"q"</span>)).SendKeys(<span style="color: #a5c25c;">"asp.net mvc"</span>);
        _driver.FindElement(<span style="color: #ffc66d;">By</span>.Name(<span style="color: #a5c25c;">"btnG"</span>)).Click();

        <span style="color: gray;">// Should be on the results page now:</span>
        <span style="color: #cc7832;">var</span> resultLinks =
            <span style="color: #cc7832;">from</span> element <span style="color: #cc7832;">in</span> _driver.FindElements(<span style="color: #ffc66d;">By</span>.TagName(<span style="color: #a5c25c;">"a"</span>))
            <span style="color: #cc7832;">let</span> href = element.GetAttribute(<span style="color: #a5c25c;">"href"</span>)
            <span style="color: #cc7832;">where</span> href.StartsWith(<span style="color: #a5c25c;">"http://"</span>)
            <span style="color: #cc7832;">let</span> uri = <span style="color: #cc7832;">new</span> <span style="color: #ffc66d;">Uri</span>(href)
            <span style="color: #cc7832;">where</span> uri.Host.ToLower().EndsWith(<span style="color: #a5c25c;">"asp.net"</span>)
            <span style="color: #cc7832;">select</span> uri
            ;

        <span style="color: #ffc66d;">CollectionAssert</span>.IsNotEmpty(resultLinks);
    }
}</pre>
<p>As you can see, only the <strong>SetUp</strong> method changes. Everything else stays the same, the tests pass, and the they take a lot less time to run compared to the Firefox version.</p>
<p>I have the code—including the examples above—to BitBucket at <a href="http://bitbucket.org/shaderop/htmlunitdriver">http://bitbucket.org/shaderop/htmlunitdriver</a>. I would appreciate any help that anyone reading this might offer, especially bug reports and patches. I know I’ll be using it myself at some point in the very near future, so I’ll try to work on it it as I go along.</p>
<p>Good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/08/using-htmlunit-to-test-net-applications/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>An Update and a Render</title>
		<link>http://shaderop.com/2010/05/an-update-and-a-render/</link>
		<comments>http://shaderop.com/2010/05/an-update-and-a-render/#comments</comments>
		<pubDate>Mon, 31 May 2010 18:57:40 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[archviz]]></category>
		<category><![CDATA[softimage]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=275</guid>
		<description><![CDATA[Actually this is just a thinly-disguised excuse to post a pretty picture. At least this author hopes it's pretty.]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I was made a committer on the <a href="http://www.subtextproject.com/">Subtext Project</a>, a blogging engine written in ASP.NET. I&#8217;m honored, and I would like to thank <a href="http://haacked.com/">Phil Haack</a> for entrusting me with this. I hope to be able to continue contributing to Subtext until it&#8217;s feature-rich enough for me to switch away from WordPress. Not that there is anything wrong with the current version of Subtext feature wise if I may add.</p>
<p>Version 2.5 of Subtext should be released in a week or so. I would like to think that I had a small part to play in making that possible. But unfortunately I couldn&#8217;t be there for the homestretch, as I had to help with this:</p>
<p style="text-align: center;"><a href="http://shaderop.com/wp-content/uploads/2010/05/Night_Persp.jpg"><img class="size-large wp-image-278 aligncenter" title="Night_Persp" src="http://shaderop.com/wp-content/uploads/2010/05/Night_Persp-600x337.jpg" alt="" width="600" height="337" /></a></p>
<p>My brother, who is studying architecture, is working on his graduation project, and he called upon me to help him out with some of the 3d rendering. That&#8217;s the result of our work so far. He did most of it. I just handled some of the grunt work for him.</p>
<p>I have learned a few things from all of this: <a href="http://www.autodesk.com/softimage">Softimage</a> is one fantastic piece of kit; <a href="http://en.wikipedia.org/wiki/Mental_ray">Mental Ray</a> <a href="http://forums.cgsociety.org/showthread.php?f=24&amp;t=881237#post6526973">sucks far too many hairy bananas</a>; moving 3d models from <a href="http://en.wikipedia.org/wiki/AutoCAD">Autocad</a> into other 3d programs is like trying to break a vertex out of <a href="http://en.wikipedia.org/wiki/Alcatraz_Island#Prison_history">Alcatraz</a>; and working on open source projects can do wonders to one&#8217;s knowledge, motivation, and self-respect.</p>
<p>May your renders be fast and your builds never break.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/05/an-update-and-a-render/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio Wallpapers: Full Resolution</title>
		<link>http://shaderop.com/2010/05/visual-studio-wallpapers-full-resolution/</link>
		<comments>http://shaderop.com/2010/05/visual-studio-wallpapers-full-resolution/#comments</comments>
		<pubDate>Mon, 10 May 2010 18:37:18 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[wallpaper]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=243</guid>
		<description><![CDATA[I&#8217;ve recently posted a few Visual Studio 2010 desktop wallpapers on Scott Hanselman&#8217;s Visual Studio 2010 Community Wallpapers site. Unfortunately, all the wallpapers there are scaled down to 1280 by 720 pixels (or thereabouts). So I thought I should repost them here in lossless, luscious 1080p resolution: Someone was kind enough to send me an [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently posted a few <em>Visual Studio 2010</em> desktop wallpapers on <a href="http://www.hanselman.com/">Scott Hanselman&#8217;s</a> <a href="http://vs2010wallpapers.com/">Visual Studio 2010 Community Wallpapers</a> site. Unfortunately, all the wallpapers there are scaled down to 1280 by 720 pixels (or thereabouts).</p>
<p>So I thought I should repost them here in lossless, luscious 1080p resolution:</p>
<div id="attachment_242" class="wp-caption aligncenter" style="width: 310px"><a href="http://shaderop.com/wp-content/uploads/2010/05/vs-banner-black-on-white.png"><img class="size-medium wp-image-242" title="vs-banner-black-on-white" src="http://shaderop.com/wp-content/uploads/2010/05/vs-banner-black-on-white-300x168.png" alt="" width="300" height="168" /></a><p class="wp-caption-text">Visual Studio MMX: Black on White</p></div>
<div id="attachment_241" class="wp-caption aligncenter" style="width: 310px"><a href="http://shaderop.com/wp-content/uploads/2010/05/vs-banner-white-on-black.png"><img class="size-medium wp-image-241" title="vs-banner-white-on-black" src="http://shaderop.com/wp-content/uploads/2010/05/vs-banner-white-on-black-300x168.png" alt="" width="300" height="168" /></a><p class="wp-caption-text">Visual Studio MMX: White on Black</p></div>
<div id="attachment_240" class="wp-caption aligncenter" style="width: 310px"><a href="http://shaderop.com/wp-content/uploads/2010/05/vs-banner-jolly-roger.png"><img class="size-medium wp-image-240" title="vs-banner-jolly-roger" src="http://shaderop.com/wp-content/uploads/2010/05/vs-banner-jolly-roger-300x168.png" alt="" width="300" height="168" /></a><p class="wp-caption-text">Visual Studio 2010: Jolly Roger Flag</p></div>
<div id="attachment_239" class="wp-caption aligncenter" style="width: 310px"><a href="http://shaderop.com/wp-content/uploads/2010/05/comp.png"><img class="size-medium wp-image-239 " title="comp" src="http://shaderop.com/wp-content/uploads/2010/05/comp-300x187.png" alt="Visual Studio 2010: God Rays" width="300" height="187" /></a><p class="wp-caption-text">Visual Studio 2010: God Rays</p></div>
<p>Someone was kind enough to send me an email asking me how I made them. I used <a href="http://www.autodesk.com/softimage">Autodesk Softimage</a> for 3d modeling and cloth simulation, <a href="http://www.3d-coat.com/">3d Coat</a> for some minor texture painting, and <a href="http://adobe.com/photoshop">Adobe Photoshop</a> for contrast correction and some other, minor tweaks.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/05/visual-studio-wallpapers-full-resolution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UV Geometry Constraint Goes To The Attic</title>
		<link>http://shaderop.com/2009/11/uv-geometry-constraint-goes-to-the-attic/</link>
		<comments>http://shaderop.com/2009/11/uv-geometry-constraint-goes-to-the-attic/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 17:23:13 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[plug-ins]]></category>
		<category><![CDATA[softimage]]></category>
		<category><![CDATA[xsi]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=169</guid>
		<description><![CDATA[Active development put on hold. World continues to turn, much to the bitter surprise of the author.]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago I proudly proclaimed to the world the release of <a href="/2009/10/uv-geometry-constraint-plug-in/">version 1.1 of the UV Geometry Constraint plug-in</a> for <a href="http://www.autodesk.com/softimage">Softimage</a>. Unfortunately, the <a href="http://forums.cgsociety.org/showpost.php?p=6139547&amp;postcount=37">great</a> <a href="http://forums.cgsociety.org/showpost.php?p=6140793&amp;postcount=41">enthusiasm</a> displayed by the community when I first announced that I had the idea for the plug-in failed to translate into any kind of meaningful interest when I actually made it. So far it has been downloaded a whopping twenty three times, and the related <a href="http://forums.cgsociety.org/showthread.php?f=24&amp;t=799632&amp;page=1&amp;pp=15">discussion thread</a> is slowly but surely heading towards archival.</p>
<p>I&#8217;m a bit disappointed but not really surprised. In my own experience with software development, users are quickly excited by new ideas, but are reluctant to adopt them once they&#8217;re turned into working solutions. It&#8217;s a complicated issue, and one that I might get around to musing about it in a future post.</p>
<p>Back to the plug-in at hand. It was fun making it, especially since it required getting re-acquainted with C++, which was my programming language of choice before the advent of .NET and C#. But it&#8217;s time to shelve it for the time being. I&#8217;ll keep the code in the <a href="http://bitbucket.org/shaderop/uvgeometryconstraint/">BitBucket repository</a>, where anyone is free to download it and tinker with it. I&#8217;m also more than happy to answer any questions anyone might have. Just hit the <a href="/contact/">contact form</a> on this website.</p>
<p>And that&#8217;s that.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2009/11/uv-geometry-constraint-goes-to-the-attic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>UV Geometry Constraint Plug-In</title>
		<link>http://shaderop.com/2009/10/uv-geometry-constraint-plug-in/</link>
		<comments>http://shaderop.com/2009/10/uv-geometry-constraint-plug-in/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 22:11:42 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[plug-ins]]></category>
		<category><![CDATA[softimage]]></category>
		<category><![CDATA[xsi]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=165</guid>
		<description><![CDATA[Shortly after releasing the geometry constraint plug-in, someone on the CGTalk forums asked if it was possible to extend it to allow of random or uniform placement of several objects on the surface of a polygonal mesh. After some head scratching and bouncing of ideas, I thought that the most flexible solution would be to [...]]]></description>
			<content:encoded><![CDATA[<p>Shortly after releasing the <a href="/softimage/geometry-constraint/">geometry constraint plug-in</a>, someone on the <a href="http://forums.cgsociety.org/showthread.php?f=24&amp;t=799632&amp;page=3#post6138467">CGTalk forums</a> asked if it was possible to extend it to allow of random or uniform placement of several objects on the surface of a polygonal mesh.</p>
<p>After some head scratching and bouncing of ideas, I thought that the most flexible solution would be to have something like the built-in surface constraint in Softimage, but one that works with polygonal meshes instead of NURBS surfaces.</p>
<p>After some more head scratching, SDK reading, and C++ reminiscing, I&#8217;ve managed to come up with a decent solution: <a href="/softimage/uv-geometry-constraint/">The UV Geometry Constraint plug-in</a>.</p>
<p>For maximum portability and hackability reasons, I would have preferred to write this in JavaScript. Unfortunately some key functionality that I needed was only accessible using the C++ API. So that&#8217;s what I had to use, and as a ramification, I&#8217;m only able to provide binary builds for both 32-bit and 64-bit flavors of Windows. Anyone wishing to use this on Linux will have to compile it from the <a href="http://bitbucket.org/shaderop/uvgeometryconstraint/src/">source code</a>.</p>
<p>Someone <a href="http://forums.cgsociety.org/showthread.php?f=24&amp;t=799632&amp;page=4#post6159752">already asked</a> if it&#8217;s possible to have this plug-in constrain the orientation of the constrained object in addition to the position. It is certainly possible, but it&#8217;s not trivial. And seeing that there doesn&#8217;t seem to be much interest in this plug-in so far, I&#8217;m inclined to leave it be for the time being.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2009/10/uv-geometry-constraint-plug-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
