<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8314956</id><updated>2011-07-08T08:33:48.157-07:00</updated><title type='text'>Analogous</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://analogous.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8314956.post-4599010561460072300</id><published>2009-06-01T15:26:00.000-07:00</published><updated>2009-06-01T16:14:56.951-07:00</updated><title type='text'>Duplication</title><content type='html'>Its been a while since ive encountered two weird C++ bugs in one week.&lt;br /&gt;&lt;br /&gt;The first was on OSX, where the compiler generated two copies of the same class which both contained a union but with different layouts.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;template &amp;lt;typename _Kind_&amp;gt;&lt;br /&gt;class Object&lt;br /&gt;{&lt;br /&gt;public:&lt;br /&gt;&lt;br /&gt; enum ObjectType&lt;br /&gt; {&lt;br /&gt;  TYPE_VOID,&lt;br /&gt;  TYPE_KIND,&lt;br /&gt; };&lt;br /&gt; &lt;br /&gt; union ObjectData;&lt;br /&gt; {&lt;br /&gt;  void * Void;&lt;br /&gt;  _Kind_ * Kind;&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt; ObjectType Type; &lt;br /&gt; ObjectData Data;&lt;br /&gt; &lt;br /&gt; void * operator &amp; () const&lt;br /&gt; {&lt;br /&gt;  switch(Type)&lt;br /&gt;  {&lt;br /&gt;  case TYPE_VOID: return (void*)Data.Void;&lt;br /&gt;  scase TYPE_KIND: return (void*)Data.Kind;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return 0;&lt;br /&gt; }&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The resulting bug was exposed when a non const version of the object was used to call the const operator.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int value = 8;&lt;br /&gt;&lt;br /&gt;Object&amp;lt;int&amp;gt; obj;&lt;br /&gt;obj.Type = Object&amp;lt;int&amp;gt;::TYPE_VOID;&lt;br /&gt;obj.Data.Kind = &amp;value;&lt;br /&gt;&lt;br /&gt;// This calls the const operator which may return an invalid &lt;br /&gt;// memory location. When dereferenced this will cause an error.&lt;br /&gt;&lt;br /&gt;int * result = &amp;obj;&lt;br /&gt;&lt;br /&gt;if (*result == 4)&lt;br /&gt;{&lt;br /&gt; // ...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Strange but true, obviously the code producing the bug was a little more complicated but semantically exactly the same as that above. &lt;br /&gt;&lt;br /&gt;The second bug was not quite so serious, but still very confusing as it relates to one of those obscure C++ standard "undefined behaviors". Sometimes i wish the standards body would realise that the language may actually benefit from having some consistency and predictability.&lt;br /&gt;&lt;br /&gt;This time the code was to perform some decoding from ascii to binary hex values on Windows and used a lookup table to convert 2 ascii bytes into 1 binary byte.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;char entry[] = {0x01,0x02,0x03,0x04,0x05};&lt;br /&gt;int table[] = {0x01,0x02,0x03,0x04,0x05};&lt;br /&gt;&lt;br /&gt;int decode[1];&lt;br /&gt;decode[0] = (table[*entry++]&lt;&lt;4)|(table[*entry++]);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;However the result was not what you would expect.  The assembly generated looked something like this.  With the result being that the two post increments did not &lt;br /&gt;occur until after the assignment.  Weird... ?&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;009603A7  mov         eax,dword ptr [entry] &lt;br /&gt;009603AA  movsx       ecx,byte ptr [eax] &lt;br /&gt;009603AD  movsx       edx,byte ptr table (0BC4A60h)[ecx] &lt;br /&gt;009603B4  shl         edx,4 &lt;br /&gt;009603B7  mov         eax,dword ptr [entry] &lt;br /&gt;009603BA  movsx       ecx,byte ptr [eax] &lt;br /&gt;009603BD  movsx       eax,byte ptr table (0BC4A60h)[ecx] &lt;br /&gt;009603C4  or          edx,eax &lt;br /&gt;009603C6  mov         byte ptr [decode],dl &lt;br /&gt;009603C9  mov         ecx,dword ptr [entry] &lt;br /&gt;009603CC  add         ecx,1 &lt;br /&gt;009603CF  mov         dword ptr [entry],ecx &lt;br /&gt;009603D2  mov         edx,dword ptr [entry] &lt;br /&gt;009603D5  add         edx,1 &lt;br /&gt;009603D8  mov         dword ptr [entry],edx &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;But it turns out there is an obscure C++ feature (see section 5, paragraph 4 of the ISO C++ standard) whereby you are not allowed to modify the same variable twice in a single expression. Doing so results in the above "undefined behavior".  Great.&lt;br /&gt;&lt;br /&gt;Why this needs to be so i dont really understand.  &lt;br /&gt;&lt;br /&gt;So there you go, two annoying bugs in one week.  Both required some pretty heavy problem solving to see. &lt;br /&gt;&lt;br /&gt;C++ is a hard language to love.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-4599010561460072300?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/4599010561460072300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/4599010561460072300'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2009/06/duplication.html' title='Duplication'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-3537350485638833541</id><published>2007-11-09T19:51:00.000-08:00</published><updated>2007-11-09T20:09:40.935-08:00</updated><title type='text'>Privateering</title><content type='html'>Nothing divides programmers more than coding style, in particular the representation of private member variables.&lt;br /&gt;&lt;br /&gt;Here are some of the options;&lt;br /&gt;&lt;br /&gt;&lt;font face="courier new" size="1"&gt;&lt;br /&gt;&lt;font color="gray"&gt;&lt;br /&gt;class Class&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m_member&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m_Member&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_member&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_Member&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;member&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Member&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The problem is, if you are like me you don't particularly like prefixes like m_ or _ and the problem with not using a prefix is that it can conflict with method names.&lt;br /&gt;&lt;br /&gt;Using prefixes unfortunately can create bad code since it encourages programmers not to think about naming or the number of members in a class.&lt;br /&gt;&lt;br /&gt;The solution can be to use a lower case member name and an upper case method name, but then there is no distinction between local variables and member variables within a method body.&lt;br /&gt;&lt;br /&gt;Another less common solution which i use sometimes, since i always prefer to use no prefixes and uppercase member names just like methods, is to wrap my private members in a struct to remove the naiming conflict but keep the cleanliness and readability.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="gray"&gt;&lt;br /&gt;class Class&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    struct PrivateMembers&lt;br /&gt;    {&lt;br /&gt;        Member;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    PrivateMembers Private;&lt;br /&gt;&lt;br /&gt;    void Member()&lt;br /&gt;    {&lt;br /&gt;        Private.Member; &lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-3537350485638833541?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/3537350485638833541'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/3537350485638833541'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2007/11/privateering.html' title='Privateering'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-3828039228641338072</id><published>2007-11-09T19:43:00.000-08:00</published><updated>2007-11-09T19:51:44.765-08:00</updated><title type='text'>Shifting</title><content type='html'>&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/dosdatetimetofiletime.asp"&gt;Dos Date Time&lt;/a&gt;&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Two's_complement"&gt;Two's Compliment&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I recently encountered this strange but simple bug in some code that i had written to convert from posix standard to dos standard time.  It was strange becuase it only shows up when you use dates and times which are earlier than a certain magic number.&lt;br /&gt;&lt;br /&gt;Dos uses a strange bit packed representation of date and time in two 16 bit words.  Within each word the various values occupy a set number of bits as follows.&lt;br /&gt;&lt;font face="courier new" size="1"&gt;&lt;br /&gt;&lt;font color="gray"&gt;&lt;br /&gt;Date Format&lt;br /&gt;&lt;br /&gt;0-4 Day of the month (1–31)&lt;br /&gt;5-8 Month (1 = January, 2 = February, and so on)&lt;br /&gt;9-15 Year offset from 1980 (add 1980 to get actual year)&lt;br /&gt;&lt;br /&gt;Time Format&lt;br /&gt;0-4 Second divided by 2&lt;br /&gt;5-10 Minute (0–59)&lt;br /&gt;11-15 Hour (0–23 on a 24-hour clock)&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;So assuming you have some simple integer representation of time and date, lets call it a Calendar you might use the following code to convert to and from the Dos format.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;font color="gray"&gt;&lt;br /&gt;&lt;font color="green"&gt;// Conversion to dos date and time&lt;/font&gt;&lt;br /&gt;Calendar calendar;&lt;br /&gt;short date = ((calendar.Year - 1980)&lt;&lt;9)&lt;br /&gt;  + ((calendar.Month)&lt;&lt;5) + calendar.Day;&lt;br /&gt;short time = ((calendar.Hour)&lt;&lt;11) &lt;br /&gt;  + ((calendar.Minute)&lt;&lt;5) + (calendar.Second/2);&lt;br /&gt;&lt;br /&gt;&lt;font color="green"&gt;// Conversion from dos date and time&lt;/font&gt;&lt;br /&gt;Calendar calendar;&lt;br /&gt;calendar.Year = ((date&gt;&gt;9)&amp;0x007F)+1980; &lt;font color="green"&gt;// 7 bits&lt;/font&gt;&lt;br /&gt;calendar.Month = ((date&gt;&gt;5)&amp;0x000F);  &lt;font color="green"&gt;// 4 bits&lt;/font&gt;&lt;br /&gt;calendar.Day = (date&amp;0x001F);   &lt;font color="green"&gt;// 5 bits&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;calendar.Hour = ((time&gt;&gt;11)&amp;0x001F);  &lt;font color="green"&gt;// 5 bits&lt;/font&gt;&lt;br /&gt;calendar.Minute = ((time&gt;&gt;5)&amp;0x003F);  &lt;font color="green"&gt;// 6 bits&lt;/font&gt;&lt;br /&gt;calendar.Second = (time&amp;0x001F)*2;  &lt;font color="green"&gt;// 5 bits&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So wheres the bug ?&lt;br /&gt;&lt;br /&gt;Well, the bug occurs when you use a year earlier than 1980, for example 1970.   What happens is that thanks to the subtraction of 1980 from the year you end up with a negative number which is left shifted 9 bits into the 16 bit word.&lt;br /&gt;&lt;br /&gt;Now usually when you do bit packing, its standard practice to mask the bits as you extract them by using an binary and after the right shift operation.  This ensures that the value you get is isolated from the other bits, especially those further to the left since usually you right shift until zero. &lt;br /&gt;&lt;br /&gt;In this case though the year is different from the other values in the date word.  Becuase the year occupies the left most bits, its two's compliment representation as a negative number is preserved all the way through the right shift operation until the binary and, where it is suddenly forced into being a positive integer with the same 7 bits, changing it from a small negative to a large positive number.&lt;br /&gt;&lt;br /&gt;The solution is to remove the binary and mask on the year.  For all other values the mask is still necessary, but due to the weird although perhaps purposeful design of the dos date and time format, the year is special.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-3828039228641338072?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/3828039228641338072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/3828039228641338072'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2007/11/shifting.html' title='Shifting'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-117099766930000600</id><published>2007-02-08T21:05:00.000-08:00</published><updated>2007-02-08T21:09:49.100-08:00</updated><title type='text'>Released</title><content type='html'>Finally after many sleepless nights, numerous relationships, two continents, and a lot of cold weather i have released some of my work into the wild under the GNU General Public License.&lt;br /&gt;&lt;br /&gt;Its a C++ framework called Reason, and represents about 40% of the code that ive produced in ecent years.  I hope you enjoy reading it, and maybe someone will even find it useful :)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://reasoning.info/examples.htm"&gt;http://reasoning.info/examples.htm&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-117099766930000600?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/117099766930000600'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/117099766930000600'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2007/02/released.html' title='Released'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-112242971029518327</id><published>2005-07-26T18:59:00.000-07:00</published><updated>2005-07-26T19:17:49.033-07:00</updated><title type='text'>Axioms</title><content type='html'>I was fumbling through some old code this evening after discovering that my original interpretaion of the slightly skewed BNF grammar used in &lt;a href="http://www.ietf.org/rfc/rfc2396.txt"&gt;FRC2396&lt;/a&gt; was incorrect.  Obviously despite creating the standard Sir Tim Burners Lee hadn't quite got the hang of hyperlinking yet, and so it is generally left up to the reader to discover the reference to &lt;a href="http://www.ietf.org/rfc/rfc822.txt"&gt;RFC822&lt;/a&gt; in the third paragraph of section 1.6, and hence forth seek out the correct interpretation.&lt;br /&gt;&lt;br /&gt;Speaking of hyperlinking, i found an amusing anecdote in my comments which i thought might be worth sharing.  It originates from the &lt;a href="http://www.w3.org/DesignIssues/Axioms.html"&gt;W3C&lt;/a&gt; axioms of web architecture, and pretty much states that the internet in its current form is a complete abomination.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;br /&gt;All the same, a word of caution is appropriate about the indiscriminate or deliberately misleading abuse of the identity of the object referred to by a URI. A web server is often in a position to know a lot of context about a request. This can include for example, the person who is asking, the document they were reading last from which they followed the link.  It is possible to use this information to ramatically change the content of the document referred to.  This undermines the concept of identity and of reference in general.  To do that without making it clear is misleading both to anyone who quotes the URI of a page or who follows the link.&lt;br /&gt;&lt;br /&gt;Unless it is clearly indicated on the page (or using a future protocol) , to return differing information for the same URI must be considered a form of deception.  It also of course messes up caches.&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Reflect on this next time you create some dynamic content...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-112242971029518327?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/112242971029518327'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/112242971029518327'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/07/axioms.html' title='Axioms'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-112217043341401687</id><published>2005-07-23T18:28:00.000-07:00</published><updated>2005-07-23T19:09:33.513-07:00</updated><title type='text'>Superstition</title><content type='html'>Sometimes, just sometimes the compiler is actually right.&lt;br /&gt;&lt;br /&gt;The problem is, not always.  Like every nascent computer user sometimes even seasoned programmers arbitrarily distrust the machine.  &lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Technofear.&lt;i&gt;n&lt;/i&gt; - The result of seemingly random &lt;br /&gt;consequences caused by the actions of inept users &lt;br /&gt;leeding to a deep sense of confusion and distrust &lt;br /&gt;for technology. i.e. &lt;i&gt;"programming the vcr"&lt;/i&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You see, as a programmer, even more so than a user it is essential to be able to trust the tools that you use to produce the results that you expect.  The programmer must have faith that the compiler tells the truth in all matters and all things.  &lt;br /&gt;&lt;br /&gt;For if the compiler lies, then the programmer is sure to spend many countless frustrating hours chasing bugs which don't exists.  And eventually their confidence will be eroded to the point that they will also waste countless hours swearing at the compiler for producing an error, which in fact is genuine.  &lt;br /&gt;&lt;br /&gt;Unfortunately its a bit of a &lt;i&gt;boy who cried wolf&lt;/i&gt; scenario, and the horrible truth and shame about the software industry, is that even today, all compilers lie.&lt;br /&gt;&lt;br /&gt;Anyone who has ever misplaced a curly brace or two and received a cascade of endless random errors from their compiler knows this shameful fact of life.&lt;br /&gt;&lt;br /&gt;So then, it is left to the programmer to develop their own intuitive understanding of the particular tools and compilers that they use, to the point that they know when an error is and error and when the machine just needs a good kick.  This truly is the art of programming, and perhaps more than you might think it can separate the good programmers from the great programmers, and at the very least, the frustrated from the productive.&lt;br /&gt;&lt;br /&gt;Ive just fixed a rather good example of such an error.  And fortunately for me, i chose wisely in trusting the compiler in what seemed like a perfect opportunity not too.  You see, my program was operating correctly in debug, but failing in release.&lt;br /&gt;&lt;br /&gt;An obvious problem you may suggest, but not quite.  You see, if i slowed the program down in release, it worked correctly.  Ahhh! i hear you say, &lt;i&gt;"clearly youve got a race condition between one or more threads"&lt;/i&gt;.  But alas, there is only a single thread and the timing is very subtle.  &lt;br /&gt;&lt;br /&gt;It sure did look like one of those occasions where one may be tempted to start looking under the rug for a wolf.  But i held fast and debugged the code into the long hours of the night.  And after much toiling, and tracing, and printing as there are only limited ways one can effectively debug release code, i started to narrow the trail. &lt;br /&gt;&lt;br /&gt;After two days of debugging i found the problem.&lt;br /&gt;&lt;br /&gt;It was caused by two objects being compared by memory addresses.  In the world of debug code, memory addresses are consecutive manufactured things, contrived to make the task of debugging easier for the programmer.  But in release code, memory addresses are random highly variable things.  So in debug, object A was less than object B, but in release where the memory was different, object B was less than object A.&lt;br /&gt;&lt;br /&gt;At its root was an innocuous problem, but it was burried deep in thousands of lines of code. Knowing when to trust and when to distrust your tools is critical, becuase sometimes...   &lt;br /&gt;&lt;br /&gt;The compiler is actually right.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-112217043341401687?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/112217043341401687'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/112217043341401687'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/07/superstition.html' title='Superstition'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-111393504629154081</id><published>2005-04-19T11:19:00.000-07:00</published><updated>2005-04-19T11:27:20.116-07:00</updated><title type='text'>Epoch</title><content type='html'>We are Microsoft, we will store dates with a different epoch and a different granularity to Unix.  &lt;br /&gt;&lt;br /&gt;An epoch of 01 Jan 1970 00:00:00 isn't good enough for us, no we want 01 Jan 1601, 00:00:00.  There much better.&lt;br /&gt;&lt;br /&gt;And whats this seconds rubbish, we want 100 nanosecond ticks thanks very much.  And we will call it file time so as to really confuse you.&lt;br /&gt;&lt;br /&gt;But its ok, well give you this really obvious conversion function so you can make all your code non standard. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include &lt;windows.h&gt;&lt;br /&gt;&lt;br /&gt;void TimetToFileTime( time_t t, LPFILETIME pft )&lt;br /&gt;{&lt;br /&gt;    LONGLONG ll = Int32x32To64(t, 10000000) &lt;br /&gt;    + 116444736000000000;&lt;br /&gt;    pft-&gt;dwLowDateTime = (DWORD) ll;&lt;br /&gt;    pft-&gt;dwHighDateTime = ll &gt;&gt;32;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Did you catch that ?&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;116444736000000000 / 10  = 11644473600000000 (Microseconds)&lt;br /&gt;11644473600000000 / 1000 = 11644473600000 (Milliseconds)&lt;br /&gt;11644473600000 / 1000  = 11644473600 (Seconds)&lt;br /&gt;11644473600 / 60  = 194074560 (Minutes)&lt;br /&gt;194074560 / 60   = 3234576 (Hours)&lt;br /&gt;3234576 /24   = 134774 (Days)&lt;br /&gt;134774 / 365   = ~369 (Years)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Which is roughly the interval between the two epochs, give or take a few leap years and leap seconds.  &lt;br /&gt;&lt;br /&gt;Oh yea, and then in .NET we will change it all again, just to be cool. Now we want the ultimate epoch - midnight on the 01 Jan A.D.  Beat that Richard Stallman.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-111393504629154081?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/111393504629154081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/111393504629154081'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/04/epoch.html' title='Epoch'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-111369735814616268</id><published>2005-04-16T11:52:00.000-07:00</published><updated>2005-04-16T17:28:26.543-07:00</updated><title type='text'>Multiplicity</title><content type='html'>Finally, someone in the C++ community that realises how far the language has fallen behind becuase its missing a decent library.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;"I'm going to speculate now, because I haven't been watching Java and I've just got my feet a little bit wet in .NET. But I would say that while the C++ community was focusing on templates, the STL, and exceptions—oddly enough the three are wrapped up together pretty closely—what they were not doing was component-based development. For example, there is no huge collection of class libraries for C++. The standard library for C++ is pretty impoverished. In the meantime, the rest of the world was busy creating huge class libraries that let you write all kinds of really neat applications without having to write very much code. Certainly Java is famous for its libraries. .NET has a huge number of libraries."&lt;/i&gt;&lt;br /&gt;- Scott Meyers&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.artima.com/intv/abcs.html"&gt;Abstract Base Classes&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I was reading the above article becuase ive just been using multiple inheritance in my own library.  The design forced me to use it and i wanted to validate some of my rational. Multiple inheritance allowed me to connect up the notion of streams with that of lower level IO like files and sockets.  So whilst i have a File class and a Socket class i also have a Stream class and an associated FileStream and SocketStream, each of which derive multiply from the base class of that type and implement the Stream interface.  The interesting part is that Stream implements two other interfaces, Reader and Writer.  File and Socket also implement Reader and Writer which define some standard contracts for reading and writing bytes.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class Reader&lt;br /&gt;{&lt;br /&gt;    ...&lt;br /&gt;    virtual int Read(char * data, int size)=0;&lt;br /&gt;    virtual int Read(Writer &amp; writer, int amount=0);&lt;br /&gt;    ...&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;class Writer&lt;br /&gt;{&lt;br /&gt;    ...&lt;br /&gt;    virtual int Write(char * data, int size)=0;&lt;br /&gt;    virtual int Write(Reader &amp; reader, int amount=0);&lt;br /&gt;    ...&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This is a very useful notion, it lets me have Stream's which i can use for efficient progressive reading, writing, and seeking, but i can also use these streams interact with their more primitive classes. For instance, to read from a SocketStream directly into a File i can do this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;SocketAddress socketAddress("www.google.com",80);&lt;br /&gt;SocketStream socketStream;&lt;br /&gt;socketStream.Connect(socketAddress);&lt;br /&gt;socketStream.Write("GET / HTTP/1.0\r\n\r\n");&lt;br /&gt;&lt;br /&gt;File file("/tmp/google.html");&lt;br /&gt;file.Write(socketStream);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And now i have a local copy of the html page.  It works becuase the Reader and Writer interfaces allow direct connections to one another.  This provides interoperability between streams and other forms of IO when i just want to do a simple redirection. But i can still harness all the pipelining capability of streams when i need it.&lt;br /&gt;&lt;br /&gt;The inheritance heirarchy of the FileStream class looks like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;FileStream&lt;br /&gt;[&lt;br /&gt;    File&lt;br /&gt;    [&lt;br /&gt;        Filesystem&lt;br /&gt;            [...]&lt;br /&gt;        Reader&lt;br /&gt;        Writer    &lt;br /&gt;    ]    &lt;br /&gt;    Stream    &lt;br /&gt;    [&lt;br /&gt;        Reader&lt;br /&gt;        Writer&lt;br /&gt;    ]&lt;br /&gt;]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Of course the FileStream can do everything that a File can do, but it also blends with it Stream semantics.  Multiple inheritance allows the common interfaces to be merged and the simple composition of two distinct usage patterns into a single class.&lt;br /&gt;&lt;br /&gt;Its nice when an intuitive design just jumps out at you, its a rare example of a truly useful piece of multiple inheritance.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-111369735814616268?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/111369735814616268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/111369735814616268'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/04/multiplicity.html' title='Multiplicity'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-110854965903262643</id><published>2005-02-16T01:26:00.000-08:00</published><updated>2005-02-16T08:10:02.530-08:00</updated><title type='text'>Maintainability</title><content type='html'>&lt;a href="http://www.acmqueue.com/modules.php?name=Content&amp;pa=showpage&amp;pid=240"&gt;OSS Should Strive for Even Greater Maintainability&lt;/a&gt;&lt;br /&gt;&lt;a href="http://developers.slashdot.org/developers/05/02/15/2031232.shtml?tid=156&amp;tid=8"&gt;Open Source Code Maintainability Analyzed&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.sei.cmu.edu/str/descriptions/halstead_body.html"&gt;Halstead Complexity Measures&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html"&gt;Cyclomatic Complexity&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;What makes software maintainable. Is it the number of lines of code, the formatting, the density of operators, the frequency of comments, the Halstead Volume, or perhaps the Cyclomatic Complexity ?&lt;br /&gt;&lt;br /&gt;I don't think so.  Theres only one way you can measure software maintainability, and that's how well it's been written.  The problem is there's no algorithm which can tell you how well written a piece of software is.&lt;br /&gt;&lt;br /&gt;Like a good movie script or a compelling novel, well written software this has less to do with metrics like the number of syllables or chapters and more to do with how the story is told.&lt;br /&gt;&lt;br /&gt;The problem with most software is that it is not written to be read or maintained, it is written to be compiled and run.  If the software industry understood even a little of the psychology involved in conveying information they would have realised long ago that like a novel, code needs to be presented in a readable format.&lt;br /&gt;&lt;br /&gt;Which brings us to the first important factor in maintainability. &lt;br /&gt;&lt;br /&gt;Understanding&lt;br /&gt;&lt;br /&gt;For code to be understandable it must be clear, unambiguous and easy to read.  This immediately excludes languages like Perl, frameworks like MFC, and standards like Hungarian Notation.  &lt;br /&gt;&lt;br /&gt;If you are an English speaker then it makes sense that the easiest thing for you to understand is "plain english", so it follows that code should be written in that form.  Intent and semantics are far more important than type and scope. Mixed case isMuchEasierToRead than singlecasewithnospaces, or single_case_with_underscores, and MUCH LESS OFFENSIVE THAN UPPERCASE. Understadable code is easier to maintain because it takes less time and effort to comprehend.&lt;br /&gt;&lt;br /&gt;Comprehension&lt;br /&gt;&lt;br /&gt;Once you understand the code, you can work out what its doing.  When you work out what its doing you can determine its purpose and therefore comprehend it.  When you can comprehend code you are well on your way to being able to make effective modifications.  Maintainable code should always be  easy to modify. &lt;br /&gt;&lt;br /&gt;But just comprehending the code is not enough.  You also need to be able to work within the confines of the implementation. &lt;br /&gt;&lt;br /&gt;Implementation&lt;br /&gt;&lt;br /&gt;There is no one way to implement an algorithm, a thousand people will implement the same code in a thousand different ways.  Implementations differ becuase different people have different priorities and ways of thinking about things.  Good implementations are those which don't just achieve the desired outcome, they follow principles.&lt;br /&gt;&lt;br /&gt;An implementation is a balance between performance, complexity, size, extensibility, and clarity.  It is a common misconception that high performance code necesitates a smaller and less extensible implementation whith reduced clarity and increased complexity. Just as it is also a common misconception that larger code with greater extensibility results in increased clarity and reduced complexity.&lt;br /&gt;&lt;br /&gt;The very best implementations are those which break down the problem and express it in terms of domain specific concepts and nomenclature, aiding in comprehension and making it easier to maintain. &lt;br /&gt;&lt;br /&gt;Expresive code which is separated into obvious concepts with logical relationships and sensible naming makes it easier for a developer to modify one area wihtout adversly impacting other unrelated areas.  It can also assist by removing complexity from otherwise monolithic implementations and encouraging re-use, without which maintenance would be a never ending and impossible task.&lt;br /&gt;&lt;br /&gt;Sometimes you may see an implementation which is so simple and understandable that you may wonder why it is that no one thought of it before.  Such implementations are akin to graphical proofs of mathematical theorems.  Powerful in their impact and yet obvious in their semantics.&lt;br /&gt;&lt;br /&gt;Semantics&lt;br /&gt;&lt;br /&gt;Possibly the most important aspect of maintainability and yet the least understood.  Semantics is the meaning conveyed by the code.  It influences how the developer interacts with and uses the libraries or environment with which the develop their software.&lt;br /&gt;&lt;br /&gt;Code which is written with good semantics is more likely to be used correctly and extended to produce better, more reliable software.  Code with bad semantics on the other hand is more likely to make it difficult for other programmers to use and will therefore result in more maintenance issues.&lt;br /&gt;&lt;br /&gt;The essential point here is that ambiguity of purpose leads to incorrect use, and incorrect use leads to bugs. &lt;br /&gt;&lt;br /&gt;Difference types of software require different approaches.  Writing a library or framework is has vastly different concerns than writing and application. &lt;br /&gt;&lt;br /&gt;Writing great software is an art not a technique, but by obeying common sense and using good principles you are more likely to succeed.&lt;br /&gt;&lt;br /&gt;My favorite is the principle of least surprise.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;br /&gt;There are a lot of common words for the concepts that ive described here which i have deliberately chosen not to use beucase of their ambiguous semantics.  Different people interpret things in different ways and often labeling concepts can result in the adoption of a false understanding of what they mean.&lt;br /&gt;&lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-110854965903262643?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110854965903262643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110854965903262643'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/02/maintainability.html' title='Maintainability'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-110705527641999328</id><published>2005-01-29T18:44:00.000-08:00</published><updated>2005-01-29T19:26:22.290-08:00</updated><title type='text'>Understanding</title><content type='html'>&lt;a href="http://www.joelonsoftware.com/items/2004/12/04.html"&gt;Vocabulary&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Ive just been reading through the archives in my aggregator and noticed an interesting point that Joel was trying to make but which he said he had trouble describing in english.&lt;br /&gt;&lt;br /&gt;The point is about the difference between the discovery and application of knowledge. I like to refer to this divide as the difference between a technician and a scientist.&lt;br /&gt;&lt;br /&gt;The technician is someone skilled in the art of applying knowledge. It is easy to learn and to use knowledge without necessarily understanding it.  Its just following a set of instructions, a method if you like.  The problem with method is that it does not allow you to solve problems or perform tasks which lie outside the rigid definition of what you have learnt.&lt;br /&gt;&lt;br /&gt;If on the other hand you are a scientist, then you are skilled in the art of learning.  The first thing a good scientist learns is how to learn.  A good scientist will always attempt to understand what it is that they are learning, not just memorise the formulae.  You see, the scientist recognises that it is not necessarily the knowledge of facts which is important, but rather the path by which those facts were derived. By understanding the path, you understand the reasoning.  And if you understand the reasoning which produced the knowledge then you yourself are more likely to be able to extend that knowledge.&lt;br /&gt;&lt;br /&gt;Everything has a context, and understanding that context allows us to move beyond mere recognition and application, it allows us to adapt and invent.  To draw on our in depth understanding of how the knowledge was acquired and use it to create something new.&lt;br /&gt;&lt;br /&gt;How many of use can truly say that we understand something  enough to be able to arrive at it by our own powers of reasoning and deduction ?&lt;br /&gt;&lt;br /&gt;Those that truly learn can.&lt;br /&gt;&lt;br /&gt;Try it for yourself, take something you know for granted and attempt to arrive at it from first principles.  Each time you do this, something in your mind will rewire itself, and your ability to do it again will improove until eventually you get to the point where nothing stands in your way.  This is learning how to learn, and is the only important and most valuable education you will ever need.&lt;br /&gt;&lt;br /&gt;Here are some examples to start you off, but remember that you don't necessarily have to succeed, you just have to understand how the knowledge was derived.&lt;br /&gt;&lt;br /&gt;* Proove pythagorases theorem, with a picture.&lt;br /&gt;* Create the basic components of a computer, with leggo.&lt;br /&gt;* Perform long division in base 2, 8, 10, and 16.&lt;br /&gt;* Proove that the earth is round, using two sticks. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.amasci.com/feynman.html"&gt;Richard P Feynman&lt;/a&gt; is famous for having always taught himself from first principles which is perhaps some of the reason why he became such a magician.&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;br /&gt;&lt;i&gt;&lt;br /&gt;"There are two kinds of geniuses: the 'ordinary' and the 'magicians'. An ordinary genius is a fellow whom you and I would be just as good as, if we were only many times better. There is no mystery as to how his mind works. Once we understand what they've done, we feel certain that we, too, could have done it. It is different with the magicians. Even after we understand what they have done it is completely dark. Richard Feynman is a magician of the highest calibre." - Mark Ka&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;&lt;/center&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-110705527641999328?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110705527641999328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110705527641999328'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/01/understanding.html' title='Understanding'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-110705192449333785</id><published>2005-01-29T18:23:00.000-08:00</published><updated>2005-01-29T18:25:24.493-08:00</updated><title type='text'>Method</title><content type='html'>&lt;a href="http://www.joelonsoftware.com/articles/fog0000000024.html"&gt;The moral of the story&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-110705192449333785?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110705192449333785'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110705192449333785'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/01/method.html' title='Method'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-110642141049964825</id><published>2005-01-22T11:05:00.000-08:00</published><updated>2005-01-22T11:18:41.073-08:00</updated><title type='text'>Devisive</title><content type='html'>&lt;a href="http://www.lysator.liu.se/c/duffs-device.html"&gt;Duffs device&lt;/a&gt;&lt;br /&gt;&lt;a href="http://groups-beta.google.com/group/net.lang.c/msg/66008138e07aa94c"&gt;The original usenet posting&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.google.com/googlegroups/archive_announce_20.html"&gt;Google 20 year usenet timeline&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I first saw this a few weeks back on googles 20 year usenet timeline.  Sometimes optimisation can be so cool. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;send(to, from, count)&lt;br /&gt;register short *to, *from;&lt;br /&gt;register count;&lt;br /&gt;{&lt;br /&gt;	register n=(count+7)/8;&lt;br /&gt;	switch(count%8){&lt;br /&gt;	case 0:	do{	*to = *from++;&lt;br /&gt;	case 7:		*to = *from++;&lt;br /&gt;	case 6:		*to = *from++;&lt;br /&gt;	case 5:		*to = *from++;&lt;br /&gt;	case 4:		*to = *from++;&lt;br /&gt;	case 3:		*to = *from++;&lt;br /&gt;	case 2:		*to = *from++;&lt;br /&gt;	case 1:		*to = *from++;&lt;br /&gt;		}while(--n&gt;0);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-110642141049964825?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110642141049964825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110642141049964825'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/01/devisive.html' title='Devisive'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-110642070596769337</id><published>2005-01-22T10:41:00.000-08:00</published><updated>2005-01-23T07:25:36.943-08:00</updated><title type='text'>Languages</title><content type='html'>&lt;a href="http://blog.metawrap.com/blog/Trackback.aspx?guid=9b7b5cb6-3e52-4431-8647-fab0cc8fc216"&gt;Things we tried 4 years ago&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.acmqueue.com/phpAdsNew-rc42/adclick.php?bannerid=177&amp;zoneid=20&amp;source=&amp;dest=http%3A%2F%2Fwww.ActiveState.com%2FQueue_PLSR"&gt;Enterprise support for dynamic languages&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.onboard.jetbrains.com/articles/04/10/lop/"&gt;Language oriented programming&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.intentsoft.com"&gt;Intentional software&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If only parsers and compilers were written in a modular fashion.  Then we could add anything we like at the abstract syntax tree level and have it compiled and targeted to our favorite binary format regardless of platform and language.&lt;br /&gt;&lt;br /&gt;The distinction between logic and code is clear, why are compilers so far behind.  This is the whole concept behind the push for intentional programming and domain specific languages.  I want to write a search engine, why can't i have my own language for that which compiles into nice fast x86 instructions using GCC, surely its just a transformation ?&lt;br /&gt;&lt;br /&gt;But instead i have to spend a lot of time writing good API's so that i can capture the knowledge which would be much better described in a domain specifc language...  the domain specific language is what you scrawl on the whiteboard, its what you can describe to a fellow programmer and have them instantly understand what you want to do.  And yet its so hard to put that down in syntax, thats because we have to do translation all the time.  Surely its better to speak and think in the native language and only do translation when we want the machine to run it...&lt;br /&gt;&lt;br /&gt;Theres such a huge gap between expressing an idea and implementing it.   But if we break the problem down so that we produce a grammar for description and then a translator for implementation it becomes much easier.  We no longer have to worry about bending our minds to describe and implement something at the same time.&lt;br /&gt;&lt;br /&gt;I don't think everyone will ever write code as XML, but it makes sense to store the logic in an independent form.  Programming should be more like mathematics with metadata.  Less focus on the specifics, more focus on the algorithm and the description of intent.&lt;br /&gt;&lt;br /&gt;Using a domain specific language is a natural thing for most programmers to do, after all we deal with abstraction every day.  The great thing about abstraction is that it separates the problem from the solution and the implementation.  We use interfaces to separate purpose from implementation, but we simply don't have the tools to separate language from logic...  &lt;br /&gt;&lt;br /&gt;What is required is good grammar processing and language description tools which can be combined with extensible editors and compilers.  I can't help but think that if programming were more of a science we would have learnt long ago to separate the modelling from the construction. &lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;br /&gt;Update: I noticed Jon Udell has some discussion on this topic, with a link to the same ACM Queue article that we've all read.  &lt;br /&gt;&lt;br /&gt;&lt;a href="http://weblog.infoworld.com/udell/2005/01/20.html#a1155"&gt;Deep structure redux&lt;/a&gt;&lt;br /&gt;&lt;a href="http://weblog.infoworld.com/udell/2005/01/19.html#a1154"&gt;The deep structure of code&lt;/a&gt;&lt;br /&gt;&lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-110642070596769337?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110642070596769337'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/110642070596769337'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2005/01/languages.html' title='Languages'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-109933891631161287</id><published>2004-11-01T11:52:00.000-08:00</published><updated>2004-11-01T11:57:43.843-08:00</updated><title type='text'>Inheritance</title><content type='html'>&lt;a href="http://geekbeers.blogspot.com/2004/10/delegate-now.html"&gt;Delegate Now&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Ouch.  Sometimes it scares me how easily programmers can confuse common knowledge.  This is one of those times.  &lt;br /&gt;&lt;br /&gt;The pattern goes like this. Someone takes a readily understood and accepted concept, removes it from its original context, confuses it thoroughly, gives different meaning to it, and then proceeded to extoll the virtues of the new found wisdom as though it were something other than misplaced enthusiasm.&lt;br /&gt;&lt;br /&gt;Every language deals with inheritance in a different way.  To examine the two mentioned above; Java uses single inheritance, public/private/protected access, abstract classes and interfaces as language constructs, but lacks a mechanism for composition via inheritance.  C++ provides multiple inheritance, public/private/protected access, abstract classes and interfaces (via pure virtual methods), but also provides for composition via inheritance and even virtual inheritance. C++ can also alter the access of a class at composition via inheritance using public/private/protected. &lt;br /&gt;&lt;br /&gt;In some cases these features are similar and in others they are vastly different.  The way C++ uses inheritance is not the same as the way Java uses inheritance.  In C++ it actually makes a lot more sense to do composition via inheritance, so inheriting implementation and behaviour is far more common.&lt;br /&gt;&lt;br /&gt;In fact, that is the essential point.  The issue at stake is not wether inheritance is good or bad, but wether you want to create a heirarchy of classes which share a common behaviour, or simply a common interface. &lt;br /&gt;&lt;br /&gt;The reason why most API's implement a Stack as a subclass of List is becuase it makes perfect sense to share the behaviour.  To misunderstand this is to misunderstand object oriented programming.  &lt;br /&gt;&lt;br /&gt;Clarifying what you want and understanding the concepts of type, composition, behaviour, interitance, polymorphism, and deligation are key to object oriented programming.  These constructs have not been arrived at without reason or purpose.&lt;br /&gt;&lt;br /&gt;Stating that implementation inheritance is bad and should be avioded altogether simly shows that you have not understood the issues at hand. There is no one size fits all solution. &lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-109933891631161287?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109933891631161287'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109933891631161287'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2004/11/inheritance.html' title='Inheritance'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-109624025706966441</id><published>2004-09-26T14:01:00.000-07:00</published><updated>2004-11-01T11:42:18.153-08:00</updated><title type='text'>Alignment</title><content type='html'>In the ancient world as in the modern one, some events are seen as important, and as such a special significant is associated with them.  Both science and psydo-science alike have established the importance of correct alignment.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.michielb.nl/maya/astro.html?adword=mayan_calendar"&gt;Myan Calendar&lt;/a&gt;&lt;br /&gt;&lt;a href="http://members.aol.com/jimb3d/stonehenge/starmap2.html"&gt;Stonehenge&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.nazca-lines.net/intro.php?screen=1024&amp;lang=EN"&gt;Nazca Lines&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.math.nus.edu.sg/aslaksen/gem-projects/hm/0102-1-pyramids/page1002.htm"&gt;Egyptian Pyramids&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.crystalinks.com/egyptastrology.html"&gt;Egyptian Astrology&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Computer science is no exception.  In modern processors memory alignment is important for performance reasons and for correctness.  The exact implications of alignment vary with processor architecture, but in general accessing data in memory which is aligned to a N byte boundary where N is the machine word size is faster than accessing that which is not aligned.  In older processors which were created when memory was limited alignment was not enforced becuase the performance gain was not as important as saving space.  In 64 bit processors alignment is given priority over space saving to maximuse performance.&lt;br /&gt;&lt;br /&gt;Becuase alignment is architecture dependent many implementations of system routines such as those found in the standard C runtime library are implemented with deliberately varying behaviour.  On Intel x86 processors alignment is not enforced, nor is it critical in all cases.  Despite this fact the standard C routines still check for alignment so as to avoid issues with page boundaries and portability.&lt;br /&gt;&lt;br /&gt;It is often claimed that accessing unaligned memory even on an x86 architecture incurs a massive performance hit.  Recent investigations that i have performed have led me to believe that this is a gross generalisation.  In fact i have found that in most cases reading 4 bytes of unaligned data in a single operation is still faster than reading 4 bytes individually.&lt;br /&gt;&lt;br /&gt;This contradicts many of the claims made about alignment performance, but can be explained on x86 architectures becuase the processor actually does the alignment correction in hardware.  Therefore accessing 2 blocks of memory as happens during an unaligned 4 byte access is still faster than accesing 4 bytes individually.&lt;br /&gt;&lt;br /&gt;The following code will perfom up to 200% faster than strcmp() in the general case due to the fact that strcmp() performes a single byte comparison whenever either parameter falls on an unaligned address.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int Compare(char * left, char * right)&lt;br /&gt;{&lt;br /&gt;	if (left == right) return 0;&lt;br /&gt;&lt;br /&gt;	while (*(long*)left == *(long*)right)&lt;br /&gt;	{&lt;br /&gt;		long mask = *(long*)left;&lt;br /&gt;		if ( 	(mask&amp;0xFF000000)==0 || &lt;br /&gt;			(mask&amp;0x00FF0000)==0 || &lt;br /&gt;			(mask&amp;0x0000FF00)==0 || &lt;br /&gt;			(mask&amp;0x000000FF)==0 ) return 0;&lt;br /&gt;		&lt;br /&gt;		left+=4;&lt;br /&gt;		right+=4;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	while(*left == *right)&lt;br /&gt;	{&lt;br /&gt;		if (*left==0)return 0;&lt;br /&gt;&lt;br /&gt;		++left;&lt;br /&gt;		++right;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	return *left-*right;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It is also highly optimised in the way it checks for null characters in either string.  Unfortunately i can't confirm if this code is safe in all circumstances though it does compile and run succesfully on both windows and linux.  There is potential danger particularly when either string falls on a page boundary in memory, an occurance which is statistically unlikely but non-the less possible.&lt;br /&gt;&lt;br /&gt;A similar algoirthm can however be employed as a replacement for memcmp() where the length is known without the uncertainty.  Such code performs much faster than memcmp() in the general case.  Following from this principle there is also an algorithm for rapid conversion from characters to numbers, i.e. atoi(), which can consume 4 bytes at a time.  This is somewhat more complicated but again involved masking in order to caplitalise on the values stored in the cpu registers and cache. &lt;br /&gt;&lt;br /&gt;Where does this leave us ?  &lt;br /&gt;&lt;br /&gt;Given that even with these optimisations aligned access still results in better performance by a factor of 2 it makes sense to ensure that as much memory is aligned as possible, particularly if you are using the standard C routines. &lt;br /&gt;&lt;br /&gt;Considder the implications of the following code.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;inline void * operator new (size_t size)&lt;br /&gt;{&lt;br /&gt;	char * address = (char *) malloc(size+4);&lt;br /&gt;	return (void *) (address + (((unsigned long)address)&amp;0x00000003));&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-109624025706966441?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109624025706966441'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109624025706966441'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2004/09/alignment.html' title='Alignment'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-109511422143083130</id><published>2004-09-13T15:08:00.000-07:00</published><updated>2004-09-13T15:27:38.090-07:00</updated><title type='text'>Promotion</title><content type='html'>Some recent ramblings from elsewhere on the net.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://binkley.blogspot.com/2004/09/more-on-domain-objects.html#109511306200965142"&gt;binkley.blogspot.com&lt;/a&gt;&lt;br /&gt;&lt;a href="http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&amp;ixPost=180782&amp;amp;ixReplies=24"&gt;discuss.fogcreek.com&lt;/a&gt;&lt;br /&gt;&lt;a href="http://blog.metawrap.com/blog/CommentView.aspx?guid=2b70bc77-0030-49c2-9c1c-8631d23ae42b"&gt;blog.metawrap.com&lt;/a&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-109511422143083130?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109511422143083130'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109511422143083130'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2004/09/promotion.html' title='Promotion'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-8314956.post-109511169968041427</id><published>2004-09-13T14:40:00.000-07:00</published><updated>2004-09-13T14:41:39.680-07:00</updated><title type='text'>Beginnings</title><content type='html'>All i wanted to do was post a comment, now look what ive gone and done.&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8314956-109511169968041427?l=analogous.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109511169968041427'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8314956/posts/default/109511169968041427'/><link rel='alternate' type='text/html' href='http://analogous.blogspot.com/2004/09/beginnings.html' title='Beginnings'/><author><name>Emerson</name><uri>http://www.blogger.com/profile/00096723206012357237</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
