<?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-8875558920754897232</id><updated>2011-12-15T10:50:36.070-08:00</updated><category term='logging'/><category term='jokes'/><category term='protocol'/><category term='tools'/><category term='memory management'/><category term='books'/><category term='compilers'/><category term='C'/><category term='singleton'/><category term='file i/o'/><category term='interfaces'/><category term='api'/><category term='project: Itsy Bitsy Bag'/><category term='scoping'/><category term='inheritance'/><category term='Collections'/><category term='TDD'/><category term='code coverage'/><category term='configuration'/><category term='DSL'/><category term='frustration'/><category term='eclipse'/><category term='project: Run-time JUnit Ignore'/><category term='project: First-class MBean names'/><category term='project: Smart Factory'/><category term='MS Office'/><category term='code generation'/><category term='lectures'/><category term='java'/><category term='paradox'/><category term='confidence'/><category term='mistakes'/><category term='optimizing'/><category term='macros'/><category term='build system'/><category term='brute force'/><category term='OpenOffice'/><category term='algorithm'/><category term='links'/><category term='concurrency'/><category term='big vs. small'/><category term='boring'/><category term='people'/><category term='annotation'/><category term='Self'/><category term='dumb-it-down'/><category term='errors'/><category term='project: Cava'/><category term='patters'/><category term='design'/><category term='regular expressions'/><category term='fun'/><category term='testing'/><category term='evaluate'/><category term='factory'/><category term='java beans'/><category term='talks'/><category term='lazy evaluation'/><category term='prototype'/><category term='LISP'/><category term='D language'/><category term='ruby'/><category term='mail'/><category term='reflection'/><category term='Microsoft'/><category term='skills'/><category term='contracts'/><category term='debugging'/><category term='dynamic'/><category term='DRY'/><category term='social'/><category term='environment'/><category term='solutions'/><category term='Static Python'/><category term='C++'/><category term='project: Easy Configuration'/><category term='pair-programming'/><category term='look-up tables'/><category term='memories'/><category term='python'/><category term='zen'/><category term='annoy'/><category term='Fit'/><category term='JUnit'/><category term='laws'/><category term='crazy stuff'/><category term='learning'/><category term='naming'/><category term='project: Mim'/><category term='code review'/><category term='linux'/><category term='dependency injection'/><category term='file convertion'/><category term='programming'/><category term='static'/><category term='communication'/><category term='opinions'/><category term='treasures'/><category term='meta'/><category term='compile-time evaluation'/><category term='stack'/><category term='commodore'/><category term='project: Dynamic Scoping'/><category term='functional programming'/><category term='languages'/><category term='exception'/><category term='approximate'/><category term='type-checking'/><category term='user interfaces'/><category term='parser'/><category term='fear'/><category term='rambling'/><category term='TED'/><category term='reuse'/><category term='object-orientation'/><title type='text'>Programmatically Speaking</title><subtitle type='html'>My projects, ideas, and mistakes: considered useless by many, interesting by some, and humorous by few, respectively.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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>89</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3398342455987230976</id><published>2011-12-11T04:13:00.001-08:00</published><updated>2011-12-15T10:50:36.078-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='static'/><category scheme='http://www.blogger.com/atom/ns#' term='type-checking'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><category scheme='http://www.blogger.com/atom/ns#' term='Static Python'/><category scheme='http://www.blogger.com/atom/ns#' term='compile-time evaluation'/><title type='text'>A potpourri on static vs dynamic</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Sa-potpourri.jpg/320px-Sa-potpourri.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="137" src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Sa-potpourri.jpg/320px-Sa-potpourri.jpg" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;Static typing is great for more reasons that checking that the program doesn't do (too) crazy things at run-time. On the other hand, dynamic typing let's you (the programmer) do a lot of crazy things that you &lt;i&gt;know&lt;/i&gt; will be correct.&lt;br /&gt;&lt;br /&gt;The problem with dynamic typing starts popping up as the project grows. This is when static typing starts to be useful, because the compiler keeps you from doing type-unsafe tricks and other black magic.&lt;br /&gt;&lt;br /&gt;However, there will always be things -- even in multi-million line projects -- that you brain easily can type-check, but the compiler can't. For instance, interactions between functions within a class or a small source file.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lambda-the-ultimate.org/node/1707"&gt;&lt;i&gt;Gradual typing&lt;/i&gt;&lt;/a&gt; is a typing strategy that might solve such problems.&lt;br /&gt;&lt;br /&gt;With gradual typing you can (manually) infer more and more types as the project evolves and more performance, compiler-checked interfaces, etc, is needed.&lt;br /&gt;&lt;br /&gt;I find &lt;a href="http://www.python.org/dev/peps/pep-3107/"&gt;&lt;i&gt;function annotations&lt;/i&gt;&lt;/a&gt; in python a very interesting start for something that could let python grow into a type-checked language. I've fiddled around with this idea &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/01/statically-typed-and-compiled-python.html"&gt;before&lt;/a&gt;, however, I'm not skilled enough, nor do I have time enough, to develop a compiler for a full-fledge "static python" language.&lt;br /&gt;&lt;br /&gt;Personally I find the research done in the type-checking field very interesting, and even though I'm not formally trained I have played around with different type-checking ideas. One was based on the idea that abstract types are simply a group of concrete types with a common set of behavior; another was an attempt on doing duck-typing at compile-time and inferring interfaces (Java sense of the word).&lt;br /&gt;&lt;br /&gt;Compile-time evaluation is another concept that are (distantly) related to static typing that I'm a bit fan of. In a language that have compile-time evaluation, it is possible to for instance generate classes, populate data structures, etc, that will appear to the compiler to be equivalent to manually written classes and data structures.&lt;br /&gt;&lt;br /&gt;There are a lot of things to say about gradually moving type-checking to compile-time and execute code at compile-time, but I'll try to end here with the following, slightly confusing, note.&lt;br /&gt;&lt;br /&gt;Typing is a fuzzy concept in the sense that &lt;i&gt;static&lt;/i&gt; and &lt;i&gt;dynamic &lt;/i&gt;all depends on you point of view. If you implement a compiler &lt;i&gt;A&lt;/i&gt; that have compile-time evaluation, then you need to implement an dynamically type-checking interpreter which evaluates the code at compile-time (unless, of course, you write a compiler for it... ehm, wait... omg, recursion!). On the other hand, if your writing a just-in-time compiler &lt;i&gt;B&lt;/i&gt; you're writing a compiler for the compiler is run at run-time -- if such compiler do typing, is it really static-typing? Is it dynamic typing? I don't know... Making it even more convoluted -- what if compiler &lt;i&gt;A&lt;/i&gt; is used as a just-in-time compiler by a virtual machine for a script language -- is it still static-typing?&lt;br /&gt;&lt;br /&gt;As always, the world is neither white nor black. In fact, it's not even gray -- it is in some quantum state, being white and black at the same time.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3398342455987230976?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3398342455987230976/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3398342455987230976' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3398342455987230976'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3398342455987230976'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/12/potpourri-on-static-vs-dynamic.html' title='A potpourri on static vs dynamic'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1020344099778618245</id><published>2011-11-23T08:42:00.001-08:00</published><updated>2011-11-23T09:00:11.754-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='code review'/><title type='text'>Test the source code, not the assembly</title><content type='html'>Unit tests are great. Integration tests are great. Regression tests are great. User tests are great. The problem with these kinds of test is that they test the compiled source code -- the assembly -- not the source code itself.&lt;br /&gt;&lt;br /&gt;Why is this a problem, you ask? The problem is that the source code, which (should) communicates intention to other programmers is not tested. This aspect of the source code is not tested.&amp;nbsp;It is very hard to test this aspect, though. The only way I know of is Checkstyle (and similar lint tools), and code reviews. The former has the advantage of being possible to automate, the latter has the advantage of being correct (which the former is not :)).&lt;br /&gt;&lt;br /&gt;I use &lt;i&gt;correct &lt;/i&gt;here in the sense that it actually reflect the opinions of by the target audience -- the programmer. The primary user of source code is the programmer, not the computer. The primary user of the compiled code is the computer (which doesn't have strongs opinions on how structure of the code should by, by the way).&lt;br /&gt;&lt;br /&gt;The idea I'm trying to express here is that source code can be functionally perfect in the sense that millions and millions of testcases verify every tiny aspect of the code. Yet the source code will fail miserably on a core review. Testing is not enough if the source code is supposed to live for long.&lt;br /&gt;&lt;br /&gt;Is code review the only tool for making sure that source code does not degenerate into a huge ball of spaghetti/mud mixture? Refactoring is often mentioned in discussions like this. However, in my mind this is is a tool for un-mudding the ball into something nicer. It is not a tool for identifying what to refactor.&lt;br /&gt;&lt;br /&gt;It kind of sucks, because core review are boring like hell, but what other solution is there to this problem? I would be happy to know of any other than code reviews.&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1020344099778618245?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1020344099778618245/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1020344099778618245' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1020344099778618245'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1020344099778618245'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/11/test-source-code-not-assembly.html' title='Test the source code, not the assembly'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2526714714065394022</id><published>2011-11-04T09:18:00.000-07:00</published><updated>2011-11-04T09:18:49.757-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><title type='text'>RIP C and LISP</title><content type='html'>Two very influential profiles in the computer science community and in software engineering has passed away.&lt;br /&gt;&lt;br /&gt;On the 12th of October the father of C, &lt;a href="http://en.wikipedia.org/wiki/Dennis_Ritchie"&gt;Dennis Ritchie&lt;/a&gt; passed away; and on the 24th the father of LISP, &lt;a href="http://en.wikipedia.org/wiki/John_McCarthy_%28computer_scientist%29"&gt;John McCarthy&lt;/a&gt;, passed away. I encourage you to read up on their achievements during their life-time.&lt;br /&gt;&lt;br /&gt;Imagine a world without C -- the worlds first really portable language.&lt;br /&gt;&lt;br /&gt;Imagine a would without LISP -- the Maxwell's equations of computer science.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2526714714065394022?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2526714714065394022/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2526714714065394022' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2526714714065394022'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2526714714065394022'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/11/rip-c-and-lisp.html' title='RIP C and LISP'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3853218799022971547</id><published>2011-10-20T02:17:00.000-07:00</published><updated>2011-10-20T02:17:32.062-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='file i/o'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>std::fstream -- please leave!</title><content type='html'>During the last half year, there has been several bugs related to C++ file streams, e.g., std::ifstream. Most bugs have been resolved by adding calls to &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ios::clear&lt;/span&gt; to clear the error state of the stream, or similar fixes. Other bugs were fixed by using C &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;FILE&lt;/span&gt; instead.&lt;br /&gt;&lt;br /&gt;Why, why, why, is file I/O so hard to implement in a reliable way in C++? I haven't done much work with files in C, but the things I've done work well. Same thing with python. Java's file I/O is just a joke (why do I need three classes to read a file?), but at is more reliable than C++.&lt;br /&gt;&lt;br /&gt;Think twice before I use std::fstream again. You code might be fine on implementation of the C++ standard library, but will fail on another. Sad.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3853218799022971547?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3853218799022971547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3853218799022971547' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3853218799022971547'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3853218799022971547'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/10/stdfstream-please-leave.html' title='std::fstream -- please leave!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5012327793978877126</id><published>2011-09-10T00:49:00.000-07:00</published><updated>2011-09-10T00:49:14.672-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='logging'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='prototype'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Dynamic Scoping'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Dynamic scoping in C++</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/5/50/Klingle_road.May2008.websize.JPG?uselang=sv" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://upload.wikimedia.org/wikipedia/commons/5/50/Klingle_road.May2008.websize.JPG?uselang=sv" width="132" /&gt;&lt;/a&gt;&lt;/div&gt;I've had ideas for &lt;a href="http://en.wikipedia.org/wiki/Scope_%28computer_science%29#Dynamic_scoping"&gt;dynamic scoping&lt;/a&gt; &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/dynamic-scoping-as-alternative-to.html"&gt;before&lt;/a&gt;. There are pros and cons with dynamic scoping, as is explained at the &lt;a href="http://www.emacswiki.org/emacs/DynamicBindingVsLexicalBinding"&gt;emacs wiki&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Last time I implemented it in Java, this time I'm trying to get something more primitive (compared to the Java implementation) working in C++ (it should be straight-forward to port to C).&lt;br /&gt;&lt;br /&gt;The use-case I had in mind when implementing this was how to distribute loggers or hierarchical configurations to every part of an application. In such code is is usually required to pass a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Logger&lt;/span&gt; or a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Configuration&lt;/span&gt; around to basically every function. However, with the approach described here, there is no need to pass stuff around like that, instead one uses stack-searching technique.&lt;br /&gt;&lt;br /&gt;This little demo demonstrates the idea:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;int func1() {&lt;br /&gt;&amp;nbsp; TaggedInt i1(1);&lt;br /&gt;&amp;nbsp; printf("i1 = %d\n", findClosestTaggedInt()); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int func0() {&lt;br /&gt;&amp;nbsp; printf("i0 = %d\n", findClosestTaggedInt()); &lt;br /&gt;&amp;nbsp; func1();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;&amp;nbsp; TaggedInt i0(0);&lt;br /&gt;&amp;nbsp; func0();&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;When run, the following is printed:&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;i0 = 0&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;i1 = 1&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Ok, what happens here? It's pretty simple. The class &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;TaggedInt&lt;/span&gt; is a class that holds two values. A tag and an &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt;. The function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;findClosestTaggedInt&lt;/span&gt; searches backwards through the stack until it finds the tag of a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;TaggedInt&lt;/span&gt; object and returns the corresponding &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt; of that object.&lt;br /&gt;&lt;br /&gt;In a real application the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt; would probably be replaced by something else, like a pointer to a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LogConfig&lt;/span&gt; object or something similar. Further more, the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;findClosestTaggedInt&lt;/span&gt; would be replaced by &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;findClosestTaggedLogConfig&lt;/span&gt; and probably not be used explicitly like in this demo, but rather implicitly in a constructor of some other object such as &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Logger&lt;/span&gt;. For instance:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;void func() {&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp; Logger logger; // findClosestTaggedLogConfig is called by the ctor.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp; logger.warning("Something is happening!");&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;int main() {&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp; LogConfig config(std::cerr); // This is a tagged object.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;&amp;nbsp; func();&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;Now, there is a problem with this approach. Searching through the stack is an expensive operation, and not something you should do often. That implies that &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;findClosetsTaggedXYZ&lt;/span&gt; should only be rarely called, or when the program is not in a time-critical phase. However, since &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;findClosestTaggedXYZ&lt;/span&gt; is only used when &lt;i&gt;distributing an object throughout the application&lt;/i&gt; (not when the object is used) this can usually be done once at application start-up and never again.&lt;br /&gt;&lt;br /&gt;To sum up, here's a the rest of the code for running the demo above:&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;#include &amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;#include &amp;lt;assert.h&amp;gt;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;#include &amp;lt;inttypes.h&amp;gt;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;static const unsigned TAG_SIZE = 16;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;static const char TAG[TAG_SIZE] = "my magic string";&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: x-small;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// A tagged int is an magic tag followed by an int. The tag&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// *must* be at zero&lt;/span&gt;&amp;nbsp;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt; offset relative to the 'this' pointer.&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;class TaggedInt {&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; char tag[TAG_SIZE + sizeof(void*)];&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;public:&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; int variable;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; TaggedInt(int variable) : variable(variable) {&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Setup the tag.&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; memcpy(tag, TAG, TAG_SIZE);&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TaggedInt* self = this;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; memcpy(&amp;amp;tag[TAG_SIZE], &amp;amp;self, sizeof(self));&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;};&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// Find which direction the stack grows.&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int stackDirection() {&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; int first;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; int second;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; int diff = intptr_t(&amp;amp;second) - intptr_t(&amp;amp;first);&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; return diff &amp;gt; 0 ? 1 : -1;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;}&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// The maximum number of bytes to search backwards through&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// the stack for the&lt;/span&gt;&amp;nbsp; &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;tagged int before giving up.&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;static const unsigned STACK_SIZE = 1024 * 1024;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// Search backwards through the stack for the first tag.&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// Note: this might be possible to optimize by a large factor&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;// by knowing the&lt;/span&gt;&amp;nbsp; &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;alignment of stack allocated variables.&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int findClosestTaggedInt() {&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; static const unsigned ALIGMENT = 1;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; int direction = stackDirection();&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; char start;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; for (char* ptr = &amp;amp;start; ptr != &amp;amp;start - STACK_SIZE;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: x-small;"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ptr -= direction * ALIGMENT) {&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (0 == strcmp(TAG, ptr)) {&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TaggedInt* tagged;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; memcpy(&amp;amp;tagged, &amp;amp;ptr[TAG_SIZE], sizeof(tagged));&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return tagged-&amp;gt;variable;&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; assert(false);&lt;/span&gt;&lt;br style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;" /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;With this code you should have a fairly good start for implementing something more useful, such as a distributor of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Logger&lt;/span&gt;s or &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Configuration&lt;/span&gt;s.&lt;br /&gt;&lt;br /&gt;One last thing, I would be really interested in hearing from any anyone that figures out how to compute the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ALIGMENT&lt;/span&gt; constant in &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;findClosestTaggedInt&lt;/span&gt; to increase performance while still making sure no tag is missed.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5012327793978877126?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5012327793978877126/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5012327793978877126' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5012327793978877126'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5012327793978877126'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/09/dynamic-scoping-in-c.html' title='Dynamic scoping in C++'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7118458533310179004</id><published>2011-09-04T15:11:00.000-07:00</published><updated>2011-09-04T15:11:17.163-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reuse'/><category scheme='http://www.blogger.com/atom/ns#' term='object-orientation'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='type-checking'/><title type='text'>Types are smart, inheritance is plain old code-resue</title><content type='html'>I've been programming object-oriented languages for at least ten years, yet the paper &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CBcQFjAA&amp;amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.102.8635%26rep%3Drep1%26type%3Dpdf&amp;amp;rct=j&amp;amp;q=inheritance%20is%20not%20subtyping&amp;amp;ei=iYRjTtnAMonxsgaCp5CICg&amp;amp;usg=AFQjCNGY37nS8Vu9UyYm6wsptkDUcq9EEw&amp;amp;sig2=Q-RMlLJJGs4bvbC9ti16HQ&amp;amp;cad=rja"&gt;Inheritance is not subtyping&lt;/a&gt; didn't make any sense to me at all the first time I read it. (If you haven't read, I recommend you to do so.) When I started to think about type-checking in my own terms and concepts, and implemented a type-system using those concepts, then I think I got it. I think...&lt;br /&gt;&lt;br /&gt;I guess the cause for me not understanding it was that my understanding of types in programming languages was (and still are, but to a lesser degree) very ad-hoc. For instance, in C++ is &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt; a subtype of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;std::complex&amp;lt;int&amp;gt;&lt;/span&gt;. Why isn't it? After all, the set of of non-complex integers is a subset of the set of complex integers, isn't it?&lt;br /&gt;&lt;br /&gt;And that's just what &lt;i&gt;type&lt;/i&gt; or a variable is -- the set of possible values of that variable. So, why isn't &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt; a subtype of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;std::complex&amp;lt;int&amp;gt;&lt;/span&gt; in C++ then? Well, it's simple: because the type-system doesn't allow it. And why doesn't the type-system allow it? Because subtyping in (most? all?) statically typed OO languages is tied to inheritance. This is the error that crippled the ability  to reason about types of so many programmers brains.&lt;br /&gt;&lt;br /&gt;The more I read and the more I program, the more I realize that inheritance is just another tool for code-reuse; not so much different from how functions are a tool for code-reuse. It should have been decoupled from the type-system when it was designed though. Instead there should be a separate construct for defining subtype relationship. &lt;br /&gt;&lt;br /&gt;But let me get back to what the type of a variable is. If your brain, like mine, has been crippled by C++'s or Java's types-systems, then the phrase "the type of a variable is the set of possible values for that variable" isn't as eye opening as it should be. Consider the following C program:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;1&amp;nbsp; int func(int a) {&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;2&amp;nbsp; &amp;nbsp;&amp;nbsp; if (a % 2 == 0) {&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return a - 2;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return a - 1;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;5&amp;nbsp; }&lt;/div&gt;What can we say about the type of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a&lt;/span&gt; here? The brain-crippled programmer would say something like &lt;i&gt;"it's an int, which it either 16, 32, or 64 bits signed integer, depending on for which the architecture it's compiled."&lt;/i&gt; But let's dig deeper...&lt;br /&gt;&lt;br /&gt; Let us take the role of a type-system and let's write down as much as we can possible know about &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a&lt;/span&gt; at every line of this program.&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;1&amp;nbsp; int func(int a) { // Don't know anything yet.&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;2&amp;nbsp; &amp;nbsp;&amp;nbsp; if (a % 2 == 0) { // If true, then a is even.&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return a - 2; // a is even; a - 2 is even&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return a - 1; // a is odd; a - 1 is even.&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;5&amp;nbsp; }&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;There are type-system that does precisely &lt;a href="http://stackoverflow.com/questions/3210025/what-is-typestate"&gt;this&lt;/a&gt;; the type of a variable may change each time the variable is used. The reason for this is that every time a variable is used, there is (potentially) more information about the value of the variable. Thus, following the definition &lt;i&gt;the type of a variable is the set of possible values for that variable,&lt;/i&gt; the type of a variable changes as the type-system derives different set of possible values at different parts of the program. For example, at line 1 the type of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a&lt;/span&gt; is &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt;, and at line 3 the type of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a&lt;/span&gt; is &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;even int&lt;/span&gt;.&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;Why did I just discuss that? Because I wanted to illustrate how distant the concept &lt;i&gt;subtype&lt;/i&gt; and &lt;i&gt;inheritance&lt;/i&gt; is. It is unfortunate that these two concepts are blended together in mainstream languages.&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;What makes the problem even worse is that inheritance is not a guarantee for proper subtyping, as the &lt;a href="http://en.wikipedia.org/wiki/Circle-ellipse_problem"&gt;circle vs. ellipse problem&lt;/a&gt; illustrates.&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;(Please note that I'm not a type-system guy; I have no formal training in this area. This is just my take at the inheritance vs. subtyping problem.)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7118458533310179004?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7118458533310179004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7118458533310179004' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7118458533310179004'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7118458533310179004'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/09/types-are-smart-inheritance-is-plain.html' title='Types are smart, inheritance is plain old code-resue'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6830893169450593117</id><published>2011-08-31T13:55:00.000-07:00</published><updated>2011-08-31T13:55:24.725-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='object-orientation'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>What OOP's jargons and complexities?</title><content type='html'>I was introduced to functional programming in an introductory programming course at the university. I had experience with imperative languages and a bit of OO from before. My immediate impression of the functional programming style was simply &lt;i&gt;"this is really peculiar".&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;However, after a few labs in Scheme, I was stuck. The ideas was clean yet powerful. Everything made sense.&lt;br /&gt;&lt;br /&gt;The reason I write about is because I just read &lt;a href="http://xahlee.org/Periodic_dosage_dir/t2/oop.html"&gt;Programing: What are OOP's Jargons and Complexities&lt;/a&gt; and I recalled those introductory lectures into functional programming. It's a great read if you are interested in how languages and programming styles relate to each other.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6830893169450593117?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6830893169450593117/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6830893169450593117' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6830893169450593117'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6830893169450593117'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/08/what-oops-jargons-and-complexities.html' title='What OOP&apos;s jargons and complexities?'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-9196019615907665452</id><published>2011-08-17T14:46:00.000-07:00</published><updated>2011-08-17T14:46:41.054-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='type-checking'/><category scheme='http://www.blogger.com/atom/ns#' term='compile-time evaluation'/><title type='text'>Type-checking is compile-time evaluation</title><content type='html'>I've been thinking a bit about type-checking the last days -- especially how type-checking corresponds to compile-time evaluation (CTE). In C++ templates are often used to implement CTE -- it's a messy way of achieving it but it works.&lt;br /&gt;&lt;br /&gt;The idea is a language &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;L&lt;/span&gt; where the type-checking is implemented purely by CTE. When a compiler for such language compiles a function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;f&lt;/span&gt; it generates (in addition to compiling the function into executable code) a function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;fs&lt;/span&gt; that type-checks each call to &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;f&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The fact the that type-checking is a function make it possible to do all kinds of fun things, like verifying that an argument is an even integer. All that is needed to do this is to create a function (like &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;fs&lt;/span&gt;) that checks whatever one wish to check. For instance, such function can be implemented in &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;L&lt;/span&gt; itself (or a subset thereof).&lt;br /&gt;&lt;br /&gt;Of course, to be able to type-check this way a lot of type-information needs to be available for the type-checking functions (like &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;fs&lt;/span&gt;). For providing that kind of information, the compiler needs to (in addition to compiling) know how various kinds of type-information propagate through the program. For instance, if &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a&lt;/span&gt; is an even integer and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;b&lt;/span&gt; is an odd integer, is &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a * b&lt;/span&gt; even?&lt;br /&gt;&lt;br /&gt;It is more less straight-forward to figure out how such type-information propagates through arithmetic functions, like &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;a * b&lt;/span&gt;. What complicates things are conditionals, but those can be solved too. The real problem is &lt;i&gt;loops&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;And that's were I'm stuck right now -- with loops. Not that I've implemented a full compiler, but I've played around with a instruction set for a stack-based VM and how type-information propagates through those instructions. So far the only compiler is my head, though, but I'm using very mechanical steps to compile.&lt;br /&gt;&lt;br /&gt;Anyway, as I said, the problem is &lt;i&gt;loops&lt;/i&gt;. Generally, as soon as there is a loop in a program very little type-information can be propagated. However, it is my hope that most practical uses of loops can be expressed in a way such that a reasonable amount of type-information can be propagated through functions containing loops. This might imply that there is a need for other kinds of loops that is traditionally used: &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;for&lt;/span&gt;, &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;while&lt;/span&gt;, and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;do-while&lt;/span&gt;. Or, that only a certain form of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;for&lt;/span&gt; loops are allowed.&lt;br /&gt;&lt;br /&gt;This is where I currently spend my thinking.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-9196019615907665452?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/9196019615907665452/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=9196019615907665452' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/9196019615907665452'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/9196019615907665452'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/08/type-checking-is-compile-time.html' title='Type-checking is compile-time evaluation'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7951115136564526289</id><published>2011-07-28T13:05:00.000-07:00</published><updated>2011-07-28T13:05:57.039-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='jokes'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='laws'/><title type='text'>Andersson's Law</title><content type='html'>&lt;a href="http://research.microsoft.com/en-us/um/people/toddpro/papers/law.htm"&gt;Proebsting's law&lt;/a&gt; states &lt;i&gt;compiler advances double computing power every 18 year&lt;/i&gt; --- a pretty depressing fact.&lt;br /&gt;&lt;br /&gt;Another depressing fact is that the &lt;a href="http://langpop.com/"&gt;most used&lt;/a&gt; language appeared to the public in 1973 -- almost 40 years ago.&lt;br /&gt;&lt;br /&gt;The second most used language is essentially a combination of language features developed in the 70th and 80th -- 30 to 40 years ago. This language appeared in 1995 -- 16 years ago.&lt;br /&gt;&lt;br /&gt;The third most used language is 30 years old and is based on a 40 years old language with some added features developed 40 years ago.&lt;br /&gt;&lt;br /&gt;And the list goes on... Here is a compilation of the ages of the top 10 most used programming languages:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;38 (C)&lt;/li&gt;&lt;li&gt;16 (Java)&lt;/li&gt;&lt;li&gt;28 (C++)&lt;/li&gt;&lt;li&gt;16 (PHP)&lt;/li&gt;&lt;li&gt;16 (JavaScript)&lt;/li&gt;&lt;li&gt;20 (Python)&lt;/li&gt;&lt;li&gt;10 (C#)&lt;/li&gt;&lt;li&gt; 24 (Perl)&lt;/li&gt;&lt;li&gt;37 (SQL)&lt;/li&gt;&lt;li&gt;16 (Ruby)&lt;/li&gt;&lt;/ol&gt;Of course, just because a language is old doesn't mean it bad. Not at all. On the contrary, an old language is proven to be good (in some regard).&lt;br /&gt;&lt;br /&gt;What bothers me though, is the "new" languages, e.g., Java, C#, or Ruby, which don't really add any kind of innovation except new syntax and more libraries to learn. Come on, there are &lt;a href="http://lavape.sourceforge.net/doc/html/Unsolved.htm"&gt;tonnes&lt;/a&gt; of more interesting problems to solve... There is still no way of automatically parallelize a sequential program for instance. &lt;br /&gt;&lt;br /&gt;There seems to be a new law lurking in programming language development... I call it Andersson's Law: &lt;i&gt;Modulo syntax, innovation in new programming languages approaches zero.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;And here's the "proof"&lt;i&gt;:&lt;/i&gt;&lt;br /&gt;Every year there are new programming languages, however, a wast majority of those are merely reiterations of features found in previous languages (except syntax). Thus, the number of unique features per  new language approaches zero for each year, that is, innovation approaches zero.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7951115136564526289?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7951115136564526289/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7951115136564526289' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7951115136564526289'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7951115136564526289'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/07/anderssons-law.html' title='Andersson&apos;s Law'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-926261941653930684</id><published>2011-06-26T12:59:00.000-07:00</published><updated>2011-06-26T12:59:17.423-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='environment'/><title type='text'>I wish for a green compiler</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/3B2_model_400_sitting_on_grass.jpg/800px-3B2_model_400_sitting_on_grass.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="212" src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/3B2_model_400_sitting_on_grass.jpg/800px-3B2_model_400_sitting_on_grass.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;I like programming languages and I like compilers. I especially like how the less-is-more rule applies to programming languages in the sense that what is not available to the programming, the compiler and the run-time environment can do what ever it want with. For example, a language with pointer arithmetic can not (easily) have a compacting garbage collector, while a language without pointer arithmetic can. This can also be said for what optimizations the compiler can perform.&lt;br /&gt;&lt;br /&gt;In a language where the memory layout is implementation defined, the compiler can optimize the memory layout of data structures such that they are ideal for the application. For instance, using run-time profiling information, the compiler can opt for a tree-base map instead of a hash-based map; or select an array of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;bool&lt;/span&gt;s instead of a array of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt;s addressed bit-wise.&lt;br /&gt;&lt;br /&gt;However, this requires us programmers to let go of some of our control -- if we want to have the memory layout optimized by the compiler, then the programmer can't have control over it. In theory, the resulting application should be slower, since the programmer usually know more about the application than the compiler. However, in practice I believe the application will perform better, because the programmer rarely take advantage of his/her knowledge for improving performance (it takes to much time and effort).&lt;br /&gt;&lt;br /&gt;So, if we accept the fact that programmers are lazy (and compilers aren't), and designed a language with this in mind we would see applications with higher performance. Especially considering that programmers should &lt;i&gt;&lt;a href="http://c2.com/cgi/wiki?MakeItWorkMakeItRightMakeItFast"&gt;make it work, make it right, make it fast&lt;/a&gt;&lt;/i&gt; (in that order) applications rarely reach the &lt;i&gt;make it fast phase&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;If you would ask me a few years ago, I wouldn't think that performance is such a big issue -- there is enough computation power in an average computer anyway. But considering that everything is turning into a computer application (I can book laundry slots using my remote control for my TV), it is nothing other that &lt;i&gt;waste&lt;/i&gt; to not use that computing power properly.&lt;br /&gt;&lt;br /&gt;I use the word &lt;i&gt;waste&lt;/i&gt; here in an environmental way. For instance, assume an application that runs on several million computers over 5 year had 25% better cache performance -- how much less energy would that consume? Also, assume that those computers are mostly servers located in huge server rooms -- how much less cooling would be needed? Maybe it would be possible to have fewer computers running in the server room, such that the building containing the computers could be smaller...&lt;br /&gt;&lt;br /&gt;Seeing how more and more is driven by software, we should look more into how we use the available computing power. We don't want to waste such incredible resource.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-926261941653930684?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/926261941653930684/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=926261941653930684' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/926261941653930684'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/926261941653930684'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/06/i-wish-for-green-compiler.html' title='I wish for a green compiler'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8677933289745704352</id><published>2011-05-21T05:43:00.000-07:00</published><updated>2011-05-21T05:43:26.705-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='brute force'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='dumb-it-down'/><title type='text'>Dumbing it down, intelligently</title><content type='html'>&lt;div style="font-family: inherit;"&gt;There are problems that seems to be solvable only by throwing more and more code at it -- we call it the &lt;i&gt;brute force&lt;/i&gt; method. It works in most situations, but it rarely produces elegant code that survives bit rotting. The &lt;i&gt;brute force&lt;/i&gt; method is actually surprisingly common -- how many times haven't developers solved bugs by adding yet another if-statement?&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;Luckily, there is another way of solving such problems that result in simple and (potentially) elegant code -- here, I call it the &lt;i&gt;dumb-it-down&lt;/i&gt; method. The &lt;i&gt;dumb-it-down&lt;/i&gt; method achieves simplicity by solving a similar, more general, problem.&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;While the &lt;i&gt;brute force&lt;/i&gt; method result in masses of code that covers every possible corner case of a very specific problem, the &lt;i&gt;dumb-it-down&lt;/i&gt; method results in much less, more general, and simpler code. Let's take an example to clarify what I mean.&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #666666; font-family: inherit;"&gt;Let's assume there is a function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;has_python()&lt;/span&gt; that checks if python is installed on a machine. This function is used for making sure that a certain Python script can be executed. How is such function implemented? It needs to check permissions, check paths, check the python version, etc. There is a lot of intelligence needed to be implemented to make sure that the script can be executed, right? This is the &lt;i&gt;brute force&lt;/i&gt; method.&lt;/div&gt;&lt;div style="color: #666666; font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #666666; font-family: inherit;"&gt;Ok, now let's rewrite the problem slightly. Remember that &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;has_python()&lt;/span&gt;'s purpose in life is to make sure that a certain python script can be executed. So, we can just as well write a function that executes the python script and return whether or not it succeeded, right? As it turns out, that function is called &lt;a href="http://www.gnu.org/s/hello/manual/libc/Executing-a-File.html" style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;exec&lt;/a&gt; and is built into the standard C library. No need to write a single line of code!&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: inherit;"&gt;In the above example we rewrote the problem slightly but kept the intention: execute the script only if it can be executed. This is the pattern; this is the idea behind the &lt;i&gt;dumb-it-down&lt;/i&gt; method -- look at what the code tries to do on a more general level and find the a simple (or 'dumb' if you want) way of implementing that.&lt;br /&gt;&lt;br /&gt;I think the &lt;i&gt;dump-it-down&lt;/i&gt; method is an umbrella term covering many design strategies which aim are to produce simple long-living code. I've previously &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/06/only-design-pattern-is-small-solutions.html"&gt;discussed&lt;/a&gt; this here.&lt;br /&gt;&lt;br /&gt;It seems like software is in a never-ending spiral of complexity: software needs to grow more complex to manage other complex software. Why is an &lt;a href="http://www.eclipse.org/"&gt;IDE&lt;/a&gt; larger than the operating systems from last decade? Are today's IDEs solving more complex problems than the operating systems did? How can the plug-in APIs of said editor be more complex than the C standard library? Aren't the APIs supposed to make it &lt;i&gt;easy &lt;/i&gt;to implement plugins? &lt;br /&gt;&lt;br /&gt;&lt;/div&gt;I've mentioned it before in this blog, but it needs to be repeated. We software developers, need to start looking at our work (and the result of out work) differently. Our skills are not measured by the complexity of the programs we write, but the simplicity of them (assuming equal functionality).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8677933289745704352?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8677933289745704352/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8677933289745704352' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8677933289745704352'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8677933289745704352'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/05/dumbing-it-down-intelligently.html' title='Dumbing it down, intelligently'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8738483085986569625</id><published>2011-05-14T02:55:00.000-07:00</published><updated>2011-05-14T02:55:09.570-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='look-up tables'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='treasures'/><title type='text'>Forgotten treasures I: Look-up tables</title><content type='html'>Think fast: how would you convert a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;bool&lt;/span&gt; to a string, &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;"TRUE"&lt;/span&gt; or &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;"FALSE"&lt;/span&gt;, in C? Most of us probably answers "with an if-statement", and when asked to show some code doing it end up with something like:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;const char* theString;&lt;br /&gt;if (theBool)&lt;/code&gt;&lt;code&gt;theString&lt;/code&gt;&lt;code&gt; = "TRUE";&lt;br /&gt;else&lt;/code&gt;&lt;code&gt;theString&lt;/code&gt;&lt;code&gt; = "FALSE";&lt;/code&gt;&lt;/pre&gt;or some similar code using the &lt;a href="http://msdn.microsoft.com/en-us/library/e4213hs1%28VS.71%29.aspx"&gt;conditional operator&lt;/a&gt; as follows:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;const char* theString = (theBool ? &lt;/code&gt;&lt;code&gt;"TRUE" : &lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;"FALSE");&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;But there is at least one more way of doing it, which in some cases gives more readable code. I'm speaking of &lt;span style="font-style: italic;"&gt;look-up tables&lt;/span&gt;. The code above can be expressed by look-up tables like this:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;static const char* options[] = { "FALSE", "TRUE" };&lt;br /&gt;const char* theString = options[theBool];&lt;/code&gt;&lt;/pre&gt;which is much less code compare to the above if-then-else version. It's slightly more code, though, compared to the conditional operator-version. However, I personally find code using the conditional operator hard to read. I know perfectly well how the conditional operator works, but I still need a few more brain cycles to read such code.&lt;br /&gt;&lt;br /&gt;On the other hand, I have no problems at all reading code using look-up tables since code like &lt;span style="font-family: courier new;"&gt;options[theBool]&lt;/span&gt; looks very similar to a normal function call. Of course, there are down-sides to using look-up tables; the most important one is that the result is undefined if you index outside the range of the table.&lt;br /&gt;&lt;br /&gt;Another way of using arrays like this is when you need to decode a tree structure. Let's say we have three &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;int&lt;/span&gt;s which represents a path in a tree.&lt;br /&gt;&amp;nbsp;&amp;nbsp; Node root = { "Root", layer1Nodes[node1], 0 };&lt;br /&gt;&amp;nbsp;&amp;nbsp; Node layer1Nodes[] = { { "L1A", layer1ANodes[node1]&amp;nbsp; } };&lt;br /&gt;&lt;br /&gt;During the last year I've gone from &lt;span class="long_text" id="result_box"&gt;&lt;span style="background-color: white; color: black;" title=""&gt;primarily&lt;/span&gt;&lt;/span&gt; developing Java to write C++, C, and assembler. I have to admit, there are a few idioms that I have rediscovered in this transition to C and assemble. Look-up tables are on such treasure.&lt;br /&gt;&lt;br /&gt;Of course, there is now real reason why look-up tables can't be used in, e.g., Java, but it seems like the coding traditions in Java discourage such code. Other languages encourage such&amp;nbsp; code; for instance &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;list&lt;/span&gt;s and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;dict&lt;/span&gt;s are very common in Python as a tool for simplifying expressions. It's a bit funny what different communities value in their code.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8738483085986569625?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8738483085986569625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8738483085986569625' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8738483085986569625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8738483085986569625'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/05/forgotten-treasures-i-look-up-tables.html' title='Forgotten treasures I: Look-up tables'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-4915741311896179497</id><published>2011-04-29T13:48:00.000-07:00</published><updated>2011-04-29T13:48:29.445-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='static'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='code coverage'/><title type='text'>The problem with code coverage and why static is good</title><content type='html'>How can&amp;nbsp; you automatically determine how well a test-case tests some piece of code? What metric should you use? What instrument should you use to measure it? As I'm sure you know, these questions are not easy to answer. Measuring the quality of testing is not a simple thing to do.&lt;br /&gt;&lt;br /&gt;Normal &lt;a href="http://emma.sourceforge.net/"&gt;code coverage&lt;/a&gt; tools are basically worthless for measuring the quality of the test-cases. All they measure is if a line of code has been executed or not; not if the test case verified the result produced by that line. Tools like&amp;nbsp;&lt;a href="http://jumble.sourceforge.net/"&gt;Jumble&lt;/a&gt; are much better, but much harder to use.&lt;br /&gt;&lt;br /&gt;However, code coverage is actually a much better&amp;nbsp; measurement of test-case quality if you language is very static, declarative, and the code hard to extend. Language like that may sound not sound very useful, but they are. They really are. There are circumstances when you really need their static, declarative and precise semantics. When you design hardware, or software for running on resource limited hardware, then you really need precise semantics with regards to the &lt;a href="http://en.wikipedia.org/wiki/Digital_logic"&gt;combinatoric depth&lt;/a&gt;, stack usage, the position of code in memory, etc. Higher level language, by their very definition, do not make you worry about those "petty details", which is good unless the "petty details" are what makes you application actually function as it should.&lt;br /&gt;&lt;br /&gt;High level languages come with a cost, though. The more dynamic a language gets, the less you know what actually will happen when you run the program. Thus, a test-case exercising the code is further from how the code actually will be run. In fact the more dynamic a language gets, the less you can tell what any piece of code written in that language does; it all depends on how it is run. For example, what does this python function do?&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;def magic(string, filter):&lt;br /&gt;   return [c.swapcase() for c in string if c in filter]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;You cannot say for sure, because it all depends on the types of 'string' and 'filter'. Furthermore, when you see the following code:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;print magic("Hello, world", "aeiou")&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;You cannot say for sure what it means either (even though you know the types of the arguments), because 'magic' might have been redefined to do something entirely different.&lt;br /&gt;&lt;br /&gt;Consequently, the value of the test-case is much smaller in dynamic languages; thus, more test-cases are needed. This is nothing new, but it is worth considering before saying "I'm so much more productive in Language X than in Language Y". You didn't forget testing did you?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-4915741311896179497?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/4915741311896179497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=4915741311896179497' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4915741311896179497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4915741311896179497'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/04/problem-with-code-coverage-and-why.html' title='The problem with code coverage and why static is good'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8479811765663248301</id><published>2011-04-16T09:39:00.000-07:00</published><updated>2011-04-16T09:39:38.775-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='solutions'/><category scheme='http://www.blogger.com/atom/ns#' term='contracts'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><title type='text'>Separating responsibilities using channels</title><content type='html'>Imagine the following situation. Three developers that are responsible for three separate (but connected and interacting) subsystem discuss which subsystem that should be responsible for a certain feature.&lt;br /&gt;&lt;br /&gt;Developer A: I don't care if Subsystem B or C is responsible for producing Information  Y; but Subsystem A needs Information Y to support Feature  X.&lt;br /&gt;&lt;br /&gt;Developer B: Well, Subsystem B shouldn't be responsible for Information Y because Subsystem B's responsibility is Z and only Z!&lt;br /&gt;&lt;br /&gt;Developer C: Subsystem C shouldn't provide Subsystem A with information Y, because Subsystem C should never communicate subsystem A directly!&lt;br /&gt;&lt;br /&gt;Feels familiar? It sure does for me.&lt;br /&gt;&lt;br /&gt;Basically the problem is that the architecture (which say what subsystems that should interact with each other directly) and the responsibilities (of those subsystem), does not allow the some information (needed by one of those subsystem) to flow as needed.&lt;br /&gt;&lt;br /&gt;What is the solution here then? Should poor Developer B be forced to make Subsystem B take on responsibilities beyond Z? Or should Subsystem A and Subsystem C be allowed to communicate directly? Non of these two alternatives are ideal, is there another option?&lt;br /&gt;&lt;br /&gt;Of course there are! And I'm sure that you have already figured it out: simply let Subsystem B provide a channel that let's other subsystems provide Subsystem A with information. Subsystem B never knows what information is sent over the channel and Subsystem A never know that the information's source actually is some other subsystem than Subsystem B. In other words, no architectural rule should be broken by introducing a channel.&lt;br /&gt;&lt;br /&gt;If you have looked into layered systems, such as communication stacks, this is nothing new. The insight (if there is any for you to get here) should be that any&amp;nbsp; kind of system can provide a channel without that conflicting with it's other responsibilities. For instance, a system that decompresses files can have a channel that allows other systems to attach meta-data of the decompressed file.&lt;br /&gt;&lt;br /&gt;Of course, the channel doesn't have to be a channel in the sense of sending messages back and forth. For instance, a channel can be implemented simply by a std::map&amp;lt;std::string, boost::any&amp;gt;.&lt;br /&gt;&lt;br /&gt;A problem that might appear is that the channel is misused and unspecified, informal protocols/contracts starts to grow. But that's a problem that isn't connected to channels, but rather any kind of communication (be it messages, method calls, or file formats).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8479811765663248301?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8479811765663248301/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8479811765663248301' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8479811765663248301'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8479811765663248301'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/04/separating-responsibilities-using.html' title='Separating responsibilities using channels'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-9180866893972700815</id><published>2011-01-12T14:33:00.000-08:00</published><updated>2011-01-13T00:08:48.930-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='solutions'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>When all magic goes wrong: std::vector of incomplete type</title><content type='html'>I have recently been working on an API. I put great effort into separating the implementation from the interface, which in this case means that the header file of the API strictly contains declarations. No executable code at all. This makes it easier to hide implementation details, which is something we should always aim for, especially for APIs.&lt;br /&gt;&lt;br /&gt;In C++ there are several ways to hide implementation. One way is to forward declare types and simply use pointers and references to those types in header files. However, when you need to use a type by-value it is not possible to use a forward declared. For example:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;class CompleteType { }; &lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;class IncompleteType;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;class HoldsTheAboveTypes {&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; CompleteType value0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Ok. &lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; IncompleteType* pointer; // Ok.&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; IncompleteType value1;&amp;nbsp;&amp;nbsp; // Compilation error!&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;}; &lt;/div&gt;In my experience, there are usually ways to avoid having types by-value that are implementation details. Usually its a matter of thinking hard about the life-time or ownership of an object. However, when I implemented the API mentioned above I ran into a problem that seemed to be unsolvable.&lt;br /&gt;&lt;br /&gt;I needed to have a class with a field of type &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;std::vector&lt;/span&gt; of an incomplete type, that is:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;class StdVectorOfIncompleteType {&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; std::vector&amp;lt;IncompleteType&amp;gt; value; &lt;/incompletetype&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;};&lt;/div&gt;This code fails to compile, though, giving some error message about "invalid use of incomplete type" (just as the code above). However, &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;IncompleteType&lt;/span&gt; isn't used anywhere! So it should compile, shouldn't it?&lt;br /&gt;&lt;br /&gt;(Well, I guess you could argue that it should compile if C++ would be designed properly, but it not so let's not go into that...)&lt;br /&gt;&lt;br /&gt;The reason the above code doesn't compile is because the following special methods are automagically generated by the compiler:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;zero-argument constructor&lt;/li&gt;&lt;li&gt;copy constructor&lt;/li&gt;&lt;li&gt;destructor &lt;/li&gt;&lt;li&gt;assignment operator&lt;/li&gt;&lt;/ul&gt;The default implementations of these special methods are nice to have in most cases. However, in the example with &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;std::vector&amp;lt;&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;IncompleteType&amp;gt;&lt;/span&gt; above these default implementation doesn't work at all. It is these default implementation that causes the compilation error, which is very much non-obvious. All (auto-)magic goes wrong.&lt;br /&gt;&lt;br /&gt;So to fix the compilation error given above, we simply need to declare these special methods, and provide an implementation to them in a separate .cc-file, where the declaration of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;IncompleteType&lt;/span&gt; is available.&lt;br /&gt;&lt;br /&gt;I've been fiddeling with programming for more 15 years (professionally much shorter, though) and I've run into this problem several times before but never tried to understand the cause for it. Today I did.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-9180866893972700815?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/9180866893972700815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=9180866893972700815' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/9180866893972700815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/9180866893972700815'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/01/when-all-magic-goes-wrong-stdvector-of.html' title='When all magic goes wrong: std::vector of incomplete type'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3633190236586434648</id><published>2011-01-09T14:48:00.000-08:00</published><updated>2011-01-09T14:54:53.842-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='opinions'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='books'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><title type='text'>Design patterns are to software development what McDonald's is to cooking</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Dsg_UF_French_Fries_Sculpture_Shadows_20050507.jpg/800px-Dsg_UF_French_Fries_Sculpture_Shadows_20050507.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="240" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Dsg_UF_French_Fries_Sculpture_Shadows_20050507.jpg/800px-Dsg_UF_French_Fries_Sculpture_Shadows_20050507.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;I remember reading the &lt;a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612"&gt;GoF design patterns&lt;/a&gt; book and thinking &lt;i&gt;gosh, this is really good stuff. Now I can write program like a real master!&lt;/i&gt; I liked the whole idea so much that I went on reading the &lt;a href="http://www.amazon.co.uk/xUnit-Test-Patterns-Refactoring-Signature/dp/0131495054/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1278483579&amp;amp;sr=1-1"&gt;xUnit Patterns&lt;/a&gt; book, and a few more like &lt;a href="http://www.amazon.co.uk/Refactoring-Patterns-Addison-Wesley-Signature-Kerievsky/dp/0321213351/ref=sr_1_1?s=books&amp;amp;ie=UTF8&amp;amp;qid=1278483502&amp;amp;sr=1-1"&gt;Refactoring     to Patterns&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;Looking back on these books now and what I learned from them, I realize that it's not the patterns described in the books that I value the most. It's the reason for their existents; the motivation for using them. For example, the &lt;a href="http://en.wikipedia.org/wiki/Factory_object"&gt;Factory&lt;/a&gt; pattern exists because it's often desirable to separate object construction from domain logic. Why? Because it reduces coupling, which means code are easier to enhance, reuse, extend, and test. So when you understand why a pattern exists, then you know when to use and when not to use it.&lt;br /&gt;&lt;br /&gt;The problem is that you don't need to understand why a design pattern is needed in order to use a design pattern in you code. Code with a misused design pattern is worse than code without that pattern. As an example, here is some code taken basically directly from an application I worked with:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;Thing t = new ThingFactory().create(arg);&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;with &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ThingFactory&lt;/span&gt; defined as &lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;class ThingFactory {&lt;br /&gt;  Thing create(int arg) { return new Thing(arg); }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;This is a prime example of code that misuses a design pattern. Clearly, (s)he who wrote this code did not understanding why and when a Factory should be used, (s)he simply used used a Factory without thinking. Probably because (s)he just read some fancy-named design-pattern book.&lt;br /&gt;&lt;br /&gt;This is one big problem I see with design patterns. It makes it easy to write code that looks good and professional, when in fact it's horribly bad and convoluted. The Design Patterns book is the software equivalent to MacDonald's Big Book of Burgers: you need to be a good cook/developer already in order to learn anything that will actually make you burgers/software skills better. A less-than-good cook/developer will only learn how to make burgers/software that &lt;i&gt;look&lt;/i&gt; good on the &lt;i&gt;surface&lt;/i&gt;. &lt;br /&gt;&lt;br /&gt;I recently read &lt;a href="http://www.amazon.com/Object-Oriented-Design-Heuristics-Arthur-Riel/dp/020163385X"&gt;Object-Oriented Design Heuristics&lt;/a&gt; by Arthur J. Riel, and I must say that this book is much better than the Design Patterns book. First of all, it more than just a dictionary of patterns, it's actually a proper book you can read (without being bored to death). Second, the design rules (what the author calls "heuristics") are much more deep and applicable than design patterns. These rules are like Maxwell's equations for good software design. Understand and apply them, and your software will be well designed.&lt;br /&gt;&lt;br /&gt;Let me illustrate with an example how I think Riel is different from GoF. Where GoF say &lt;i&gt;"this is a hammer, use it on nails"&lt;/i&gt;, Riel says &lt;i&gt;"to attach to wooden objects you can either use hammer+nails or screwdriver+screws, both has pros and cons."&lt;/i&gt; Sure GoF is easier to read and you'll learn some fancy word you can say if you running out of buzz-words, but Riel actually makes you understand. Understanding is underrated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But let's give the GoF book et. al, some slack. To be honest I actually did learn something useful and important from those books, and I couldn't do my daily programming work properly without that knowledge:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;favor composition over inheritance,&lt;/li&gt;&lt;li&gt;separating construction logic from domain logic, and&lt;/li&gt;&lt;li&gt;code towards an interface, not an implementation.&lt;/li&gt;&lt;/ul&gt;To me, these three ideas are the essence of most design pattern that are worth knowing and if you keep those three ideas in you head all the time you won't screw up to much.&lt;br /&gt;&lt;br /&gt;Oh, there is actually one more &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/06/only-design-pattern-is-small-solutions.html"&gt;idea&lt;/a&gt; you should keep in your head or you will definitely screw up big time (literately).&lt;br /&gt;&lt;ul&gt;&lt;/ul&gt;But of course there is one more important thing about knowing design patterns. It helps you communicating with your colleagues. Design patterns defines a pattern language, so instead of saying "...you know that class that instantiates a bunch of classes an connects them..."you can say "...you know that builder class...". Honestly, for me this language is way more important than the patterns themselves.&lt;br /&gt;&lt;br /&gt;Actually, there is one thing that I learned from all those design-patterns books (not including Riel). They taught me something  important that I could only have learned from few other places: I learned  that if an author tries hard enough,  (s)he can write a 500 pages book  consisting of some common sense and two  or three good ideas repeated  over and over again. The xUnit Patterns book is the prime example of  this. Don't read it. Read Riel.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3633190236586434648?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3633190236586434648/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3633190236586434648' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3633190236586434648'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3633190236586434648'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/01/design-patterns-are-to-software.html' title='Design patterns are to software development what McDonald&apos;s is to cooking'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6232544555300014311</id><published>2011-01-06T07:15:00.000-08:00</published><updated>2011-01-06T07:15:22.461-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='contracts'/><category scheme='http://www.blogger.com/atom/ns#' term='memory management'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='singleton'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>Global contracts: mutexes, new/delete, singeltons</title><content type='html'>I had lunch with a former collegue some time ago. We discussed several things, I would like to write about one idea in particular: global contracts.&lt;br /&gt;&lt;br /&gt;A &lt;i&gt;global contract&lt;/i&gt; is an agreement that every part of your code needs to adhere to. A good example of a global contract is &lt;i&gt;locks&lt;/i&gt;. Locks are used in multi-threaded applications to synchronize threads. The problem is that there is no way to enforce that threads take the locks when they should, nor to release the lock when they should. Application code is free to access memory/object with or with out locks. &lt;i&gt;Correct&lt;/i&gt; application code, though, have to take locks, access the memory/object, and the release the lock.&lt;br /&gt;&lt;br /&gt;A similar but more common global contract is Singletons. Every part of the application must know how to get the singleton instance, and if the singleton has mutable state every part of the application must also know how to mutate that state correctly.&lt;br /&gt;&lt;br /&gt;An even more common global contract is memory management in application without garbage collection. In such applications, the knowledge of how to allocate and deallocate instances of objects are distributed. There are ways to &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/04/unit-testing-makes-manually-managed.html"&gt;design&lt;/a&gt; your application such that the &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/05/design-for-optimizability.html"&gt;global contract is encapsulated&lt;/a&gt;, but in general the detailed knowledge of the contract is spread out all over the application.&lt;br /&gt;&lt;br /&gt;Ok, global contracts are bad for the application design, but how do we get rid of them? We can't simply ignore locks or memory management, so how do we handle them? I see the following possibilities:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Encapsulate the details of the global contract, or&lt;/li&gt;&lt;li&gt;Make the global contract into a local contract.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;The first alternative means that you accept that contract is global, but you make sure that it's simple to adhere to it. An example of this is to use &lt;a href="http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/smart_ptr.htm"&gt;smart pointers&lt;/a&gt;, or to hide that there is a singleton within a single class, which also holds state, e.g.,&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;class SingletonHider {&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; Singleton singleton = Singleton::instance();&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; int intState;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; void changeState(int i) { intState = i; }&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; void doit() { singleton.doit(i); }&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;}&lt;/div&gt;And every time some part of the application needs to use the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Singleton&lt;/span&gt; class it instantiates a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;SingletonHider&lt;/span&gt;, which holds all the state needed. The precise behavior (or any state that absolutely needs to be global) is handled by the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Singleton&lt;/span&gt; class.&lt;br /&gt;&lt;br /&gt;The second alternative is to make the global contract into a local contract. How is this possible? Well, whether or not a contract is global or local is entirely a question of perspective. If you look a memory management from a operating system's perspective, the details of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;new&lt;/span&gt; and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;delete&lt;/span&gt; are definitely  a local contract of that application; it's not a operating system-global contract. So the idea for making global contract local is to implement a class that acts like a  overseer of the rest of the code. This class encapsulates all the details of the contract and provides a simple interface. This is exactly what a garbage collector is. Let's take Java's garbage collector as an example:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;it oversees: inspects the running application and determines when a piece of memory is garbage&lt;/li&gt;&lt;li&gt;simple interface: every piece of memory is allocated by &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;new&lt;/span&gt;. The details of where the memory is allocated (constant pool, generation 1, stack, etc) is encapsulated.&lt;/li&gt;&lt;/ul&gt;I'm sure there are more examples and more ways how to make a global contract local, but the approaches outlined here should give a few ideas the next time you which to refactor away a singleton, or fix &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;new-delete&lt;/span&gt; coupling.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6232544555300014311?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6232544555300014311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6232544555300014311' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6232544555300014311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6232544555300014311'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2011/01/global-contracts-mutexes-newdelete.html' title='Global contracts: mutexes, new/delete, singeltons'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7578589866453759150</id><published>2010-11-21T15:19:00.000-08:00</published><updated>2010-11-22T01:47:59.562-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Mim'/><category scheme='http://www.blogger.com/atom/ns#' term='user interfaces'/><category scheme='http://www.blogger.com/atom/ns#' term='tools'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy stuff'/><category scheme='http://www.blogger.com/atom/ns#' term='errors'/><category scheme='http://www.blogger.com/atom/ns#' term='build system'/><title type='text'>Integrating invisible tools with the shell</title><content type='html'>I've been working on a build system called &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/09/mim-build-system-you-always-wanted.html"&gt;Mim&lt;/a&gt; for some time. It has some interesting features, the most interesting being that files/artifacts are implicitly built when they are read. In other words, there is never a file on disk that is out of date.&lt;br /&gt;&lt;br /&gt;That's pretty neat, but what happens if a file cannot be built for some reason? What happens it there is a syntax error in a .c file, for instance? Since Mim runs in the background, how should such errors be reported? Simply writing them to a log file might suffice, but the experience for the end-user is not very nice. Imagine having to open a file every time a file fails to compile... Horrible!&lt;br /&gt;&lt;br /&gt;This is not a unique problem for Mim. All tools that runs in the background have problem with reporting errors, and most solve this by writing to a log. However, for Mim I chose a totally other way of doing it.&lt;br /&gt;&lt;br /&gt;Most shell utilities, e.g. &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cat&lt;/span&gt;, uses functions in the standard library for printing, open files, etc, but more importantly (for this post) for reporting errors. The &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;error&lt;/span&gt; function is used to report errors.&lt;br /&gt;&lt;br /&gt;That means that we can control how errors are printed, by &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;LD_PRELOAD&lt;/span&gt;ing a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;.so&lt;/span&gt;-file with another implementation of the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;error&lt;/span&gt; function. This is what I did to implement error reporting for Mim.&lt;br /&gt;&lt;br /&gt;This is how Mim works now; if you try to open a file that Mim cannot build because of a build error (e.g., compile error), you'll get the following printed to the console:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;$ cat generated-file.txt&lt;/div&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cat: Mim: error 512 when building artifact&lt;/span&gt;&lt;br /&gt;which is much better than the old error message which just was a generic message indicating that the file could not be opened.&lt;br /&gt;&lt;br /&gt;But there is still room for improvement! It's nice to get a error message that says that the file couldn't be built, but it would be even nicer if the message told us &lt;i&gt;why&lt;/i&gt; the artifact couldn't be built. I haven't implemented that yet, but I'm working on it. I imagine something like this:&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;$ cat generated-file.txt&lt;/div&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cat: Mim: error 512 when building artifact generated-file.txt, because:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; input-file.cc failed to build, because:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input-file.cc:53: Syntax error.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;$ emacs input-file.cc&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt; &lt;br /&gt;That will require some more work though as I said, but it's definitely possible and it's technically not hard.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7578589866453759150?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7578589866453759150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7578589866453759150' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7578589866453759150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7578589866453759150'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/11/integrating-invisible-tools-with-shell.html' title='Integrating invisible tools with the shell'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2115552896217026763</id><published>2010-09-04T00:41:00.000-07:00</published><updated>2010-09-05T04:37:32.297-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='social'/><category scheme='http://www.blogger.com/atom/ns#' term='solutions'/><category scheme='http://www.blogger.com/atom/ns#' term='rambling'/><category scheme='http://www.blogger.com/atom/ns#' term='interfaces'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><title type='text'>The firge, the door and the fire alarm</title><content type='html'>Today when I prepared breakfast  I realized that the fridge wasn't properly closed. It had been a small opening the whole night. That's not particularly good.&lt;br /&gt;&lt;br /&gt;But what's worse is that the fridge did not make any noise indicating that the door was open. In fact, it did the opposite: it turned off the only indication there was that the door wasn't properly closed -- it turned of the light inside the fridge. If that light wasn't turned off it would be easier to spot that the door was open.&lt;br /&gt;&lt;br /&gt;Turning the light off would not be a big problem &lt;i&gt;if &lt;/i&gt;there was some way the fridge alarmed when the door was open. Let's say making a noise when it had been open for more than 1 minute.&lt;br /&gt;&lt;br /&gt;I'm sure that it's a feature that the fridge turns off the light by it self when it has been on for too long. But I can't the use-case when it would be a useful. There must be a timer somewhere that turns off the light. That timer should instead trigger an alarm.&lt;br /&gt;&lt;br /&gt;Actually there is an noise-making-device in the fridge. But that only used when the temperature in the fridge is above a certain temperature. That noise-making-devices should be triggered by that timer. Why didn't it?&lt;br /&gt;&lt;br /&gt;I don't know.&lt;br /&gt;&lt;br /&gt;Recently I've noticed more and more weird design choices in everyday things. Like having the handle of a door be shaped as a pipe when you should push the door, and having the handle be shaped like a flat surface when you should pull the door (think about it, a surface is easy to &lt;i&gt;push&lt;/i&gt; and a pipe is easy to &lt;i&gt;pull&lt;/i&gt;).&lt;br /&gt;&lt;br /&gt;Even worse, I've experienced that fire alarms sounds very very much like the break-in alarm.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2115552896217026763?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2115552896217026763/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2115552896217026763' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2115552896217026763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2115552896217026763'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/09/firge-door-and-fire-alarm.html' title='The firge, the door and the fire alarm'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5059190134484396728</id><published>2010-09-03T07:31:00.000-07:00</published><updated>2010-09-03T07:31:07.564-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='project: Mim'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='build system'/><title type='text'>Mim: the build system you always wanted</title><content type='html'>The word &lt;i&gt;mim&lt;/i&gt; means any of the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;an incorrect way of writing 1999 in roman numbers (as in "we gonna party like it's 1999"), &lt;/li&gt;&lt;li&gt;the Madame Mim from the Disney movie Sword in the Stone,&lt;/li&gt;&lt;li&gt;Swedish for "mime" meaning "imitating"&lt;/li&gt;&lt;li&gt;a figure in Norse mythology renowned for his wisdom&lt;/li&gt;&lt;li&gt;an acronym for "Mim Isn't Make"&lt;/li&gt;&lt;/ul&gt;it's also the name of the build system I've been thinking and working of for a while. This post is about what Mim is &lt;i&gt;now&lt;/i&gt; and what it can &lt;i&gt;become&lt;/i&gt;. &lt;span style="color: #990000;"&gt;Text in red describes stuff that isn't implemented yet&lt;/span&gt;, &lt;span style="color: #b45f06;"&gt;text in yellow is things that are kind-of implemented&lt;/span&gt;, and text in black describes implemented features. First, I'll describe how Mim is different from other build systems and why it's better.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Problem&lt;/span&gt;&lt;br /&gt;The problems with traditional build system are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;make sure the dependencies are correct and built in the right order (think: make depend &amp;amp;&amp;amp; make &amp;amp;&amp;amp; make install),&lt;/li&gt;&lt;li&gt;be sure of that every built file you see is up to date,&lt;/li&gt;&lt;li&gt;hard understand how a software project is built (what are the artifacts? where are they stored? what are the dependencies?),&lt;/li&gt;&lt;li&gt;hard to understand which variables a build have (e.g., make DEBUG=1)&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold;"&gt;Solution&lt;/span&gt;&lt;br /&gt;The solution to these problem is (of course) Mim&lt;span style="font-style: italic;"&gt;.&lt;/span&gt; With Mim you clearly see all artifact the build system produces, how they are built, and when they need to be built. In fact, you can see the artifacts, e.g., by doing &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ls&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;before they are built&lt;/span&gt;. In other words, using your normal command line tools you can browse the project file tree to find the artifact you need (to run, to view, to copy, etc.) &lt;span style="font-style: italic;"&gt;without building anything&lt;/span&gt;. Sounds like magic? Keep reading...&lt;br /&gt;&lt;br /&gt;When when you found the program you need to get built you simple execute it. No need to issue any command yourself to build it. The program will be &lt;span style="font-style: italic;"&gt;automatically built&lt;/span&gt; for you and then executed. Similarly, if you have a tool that generates a text file as part of the build, you can do &lt;span style="font-family: courier new;"&gt;emacs file.txt&lt;/span&gt;. The file will be &lt;span style="font-style: italic;"&gt;automatically generated&lt;/span&gt; and then opened in emacs.&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;If you need to get more information about a file you can do &lt;span style="font-family: courier new;"&gt;mim ls filename&lt;/span&gt;. You'll get something like this written to the console:&lt;code&gt;&lt;br /&gt;-r-xr-xr-x you users filename (g++ filename.cc -o filename)&lt;/code&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;that is, you can easily discover to the access rights, the owner, the group, and how the file is built, of any artifact simply by locating it in the file system.&lt;/div&gt;&lt;br /&gt;This is especially powerful when you work with project of which source code generation is part of the building process. Understanding what files are generated, how they are generated, can be very hard. With Mim, however, this is very easy, because every file (generated or not) looks the same to you when you browse the file tree. Additionally, you can ask Mim for additional information about a certain artifact file, e.g., get the command for building the file by doing &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;mim cmd file&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Furthermore, getting the dependencies right when the build involves several steps (generating .d files, generating source code, generating .d files for the generated code, compiling the source code, linking the source code against libraries (which themselves has to be built, including its generated source code)) is also very very hard. And understanding it a few month later is even harder. Mim solves all these problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;Let's take an example. Let's assume we have a directory called hello with a file hello.cc contaning an implementation of &lt;a href="http://en.wikipedia.org/wiki/Hello_world_program"&gt;Hello, World&lt;/a&gt;. There is also a file called Mimfile that tells Mim how to build this program. Doing &lt;span style="font-family: courier new;"&gt;mim ls&lt;/span&gt; in &lt;span style="font-family: courier new;"&gt;hello&lt;/span&gt; gives you:&lt;/div&gt;&lt;pre style="color: #990000;"&gt;&lt;code&gt;greeting.txt&lt;br /&gt;hello.cc     codegen -lang en greeting.txt -o hello.cc&lt;br /&gt;hello-en     g++ hello.cc -o hello-en&lt;br /&gt;Mimfile&lt;/code&gt;&lt;/pre&gt;&lt;div style="color: #990000;"&gt;which tells you that there is only one physical file, &lt;span style="font-family: courier new;"&gt;greetings.txt&lt;/span&gt;, in this directory (except the obligatory &lt;span style="font-family: courier new;"&gt;Mimfile&lt;/span&gt;) and two artifact files, &lt;span style="font-family: courier new;"&gt;hello.cc&lt;/span&gt; and &lt;span style="font-family: courier new;"&gt;hello-en&lt;/span&gt;, of which the latter is an executable. You can also see that there is some code generation involved by looking at the command line for &lt;span style="font-family: courier new;"&gt;hello.cc&lt;/span&gt;.  You can tell all this by issuing one simple command, &lt;span style="font-family: courier new;"&gt;mim ls&lt;/span&gt;, without building anything at all.&lt;/div&gt;&lt;br /&gt;Let's say you wish to take a peek at the hello.cc file. All you need to do then is to do &lt;span style="font-family: courier new;"&gt;cat hello.cc&lt;/span&gt;. No need to worry that the code you see is out of date because Mim makes sure it's up to date before it's opened.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Being lazy&lt;/span&gt;&lt;br /&gt;Being lazy is a good attribute; as long as you're not &lt;span style="font-style: italic;"&gt;too &lt;/span&gt;lazy. Being lazy in the context of a build system means that you only rebuild a file if is inputs have changed. How do Mim achieve this?&lt;br /&gt;&lt;br /&gt;Mim woks by intercepting all read and write accesses to the file system. This means that Mim has perfect knowledge of 1) which files are read when a given artifact if built, and 2) which files that have been updated since the artifact was last built. This means that artifacts are only rebuilt when needed.&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;This information makes it possible for Mim to easily construct a graph dependencies and what needs to be built. This implies that Mim start building the leaves of the graph as early as possible to reduce the build time.&lt;/div&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;&lt;span style="font-weight: bold;"&gt;Instant messaging&lt;/span&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;In large project there is usually variables that controls how the project is built. &lt;span style="font-style: italic;"&gt;Mim&lt;/span&gt; supports such variables in a nice way. To list the variables simply do&lt;span style="font-family: courier new;"&gt; mim conf&lt;/span&gt;, you'll get something like this printed to the console:&lt;/div&gt;&lt;pre style="color: #990000;"&gt;&lt;code&gt;lang      en   en|fr|swe  Controls in which language "Hello, World" is printed.&lt;br /&gt;debug     off  on|off     Compile with or without debug information.&lt;br /&gt;optimize  0    1|2|3      Optimization level as defined by gcc:s -O flag.&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div style="color: #990000;"&gt;The first column is the the name of the variable, second column is its current value, third is its possible value, and last column is a textual description of it.&lt;/div&gt;&lt;div style="color: #990000;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;Changing the value of the variable &lt;span style="font-family: courier new;"&gt;lang&lt;/span&gt; to &lt;span style="font-family: courier new;"&gt;swe&lt;/span&gt; is done by doing &lt;span style="font-family: courier new;"&gt;mim conf lang=swe&lt;/span&gt;. Setting a variable have instant effect. For example, if a variable affects which files are built, you can see the updated list of files immediately by doing &lt;span style="font-family: courier new;"&gt;ls&lt;/span&gt; without building anything.  Example:&lt;/div&gt;&lt;pre style="color: #990000;"&gt;&lt;code&gt;$ ls hello*&lt;br /&gt;hello.cc  hello-en&lt;br /&gt;&lt;/code&gt;&lt;code&gt;$ mim lang=swe&lt;br /&gt;$ ls&lt;br /&gt;hello.cc  hello-swe&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div style="color: #990000;"&gt;That is, by changing the variable &lt;span style="font-family: courier new;"&gt;lang&lt;/span&gt; from &lt;span style="font-family: courier new;"&gt;en&lt;/span&gt; to &lt;span style="font-family: courier new;"&gt;swe&lt;/span&gt; we get an artifact file called &lt;span style="font-family: courier new;"&gt;hello-swe&lt;/span&gt; instead of &lt;span style="font-family: courier new;"&gt;hello-en&lt;/span&gt; like we got before.&lt;/div&gt;&lt;div style="color: #990000;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;To aid discoverability even more, you can do &lt;span style="font-family: courier new;"&gt;mim ls hello* lang==*&lt;/span&gt; to list all files matching &lt;span style="font-family: courier new;"&gt;hello*&lt;/span&gt; for all values of &lt;span style="font-family: courier new;"&gt;lang&lt;/span&gt;. For the &lt;span style="font-family: courier new;"&gt;hello&lt;/span&gt; example this outputs:&lt;/div&gt;&lt;pre style="color: #990000;"&gt;&lt;code&gt;hello-en   &lt;/code&gt;&lt;code&gt;lang=en   &lt;/code&gt;&lt;code&gt;g++ hello.cc -o hello-en&lt;br /&gt;&lt;/code&gt;&lt;code&gt;hello-fr   &lt;/code&gt;&lt;code&gt;lang=fr   &lt;/code&gt;&lt;code&gt;g++ hello.cc -o hello-fr&lt;br /&gt;&lt;/code&gt;&lt;code&gt;hello-swe  &lt;/code&gt;&lt;code&gt;lang=swe  &lt;/code&gt;&lt;code&gt;g++ hello.cc -o hello-swe&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div style="color: #990000;"&gt;All these features helps you in learning how an a project is built, how to use its build system, how to change it, how those changes affect the build system, and more.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;The Tracker&lt;/span&gt;&lt;br /&gt;Since Mim intercepts all accesses to the file system, it can help you when you do potentially bad things, for example, if you delete a file that is needed to build an artifact. &lt;span style="color: #b45f06;"&gt;Mim also updates your Mimfiles automatically when you do non-destructive changes to your source tree. For instance, when you rename an input file to gcc as the following example illustrates this:&lt;/span&gt;&lt;br /&gt;&lt;pre style="color: #b45f06;"&gt;&lt;code&gt;$ mim ls hello*&lt;br /&gt;hello.cc  codegen -lang en greeting.txt -o hello.cc&lt;br /&gt;hello-en  g++ hello.cc -o hello-en&lt;br /&gt;$ mv hello.cc hi.cc&lt;br /&gt;$ mim ls hello*&lt;br /&gt;hi.cc     codegen -lang en greeting.txt -o hi.cc&lt;br /&gt;hello-en  g++ hi.cc -o hello-en&lt;/code&gt;&lt;/pre&gt;&lt;div style="color: #b45f06;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #b45f06;"&gt;Mim can do this automatic update of Mimfiles correctly in many cases, but it's impossible to get it right every time since that would require deep knowledge of all possible build tools (e.g., gcc, ld, m4, etc). However, Mim makes certain (reasonable) assumption on how the build tools behave, and as long as the build tools' conform to these assumptions Mim gets it right most of the time. Anyway, to check that Mim got it right, you simple build of application so this isn't a big problem in practice.&lt;/div&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;&lt;span style="font-weight: bold;"&gt;Getting general&lt;/span&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;Mim is built around the concepts of events and actions triggered by event. So far we have implicitly discussed the event &lt;span style="font-style: italic;"&gt;open artifact for reading&lt;/span&gt; and the action was &lt;span style="font-style: italic;"&gt;update artifact&lt;/span&gt;. The possible events that can trigger actions are:&lt;/div&gt;&lt;ul style="color: #990000;"&gt;&lt;li&gt;open physical of artifact file for reading (&lt;span style="font-style: italic;"&gt;before-read&lt;/span&gt;),&lt;br /&gt;&lt;/li&gt;&lt;li&gt;closing a physical or artifact file after write (&lt;span style="font-style: italic;"&gt;after-write&lt;/span&gt;),&lt;/li&gt;&lt;li&gt;deleting physical or artifact file or directory (&lt;span style="font-style: italic;"&gt;delete-file&lt;/span&gt;),&lt;/li&gt;&lt;li&gt;creating a physical file or directory (&lt;span style="font-style: italic;"&gt;create-file&lt;/span&gt;), and&lt;br /&gt;&lt;/li&gt;&lt;li&gt;moving a physical or artifact file (&lt;span style="font-style: italic;"&gt;move-file&lt;/span&gt;).&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="color: #990000;"&gt;Since arbitrary commands can be executed as the result of an event, you can do what ever you heart desire when a file is moved, delete, created, etc.  However, actions that interact with Mim would be hard to implement ourself and are thus built into Mim:&lt;/div&gt;&lt;ul style="color: #990000;"&gt;&lt;li&gt;update an artifact (without reading the artifact file)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;log to the Mim log (e.g., an error message)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;reject event (e.g., reject opening a file).&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style="color: #990000;"&gt;The &lt;span style="font-style: italic;"&gt;reject event&lt;/span&gt; action is useful, e.g., if you need to verify the consistency of a save file: if the file is consistent it cannot be saved.&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Playing nice&lt;/span&gt;&lt;br /&gt;So far so good. But what about integrating with other non-&lt;span style="font-style: italic;"&gt;mim&lt;/span&gt;ing tools like, say, &lt;span style="font-family: courier new;"&gt;make&lt;/span&gt;. Since all Mim does to build an artifact files is to execute a command line in the shell, Mim can easily be used as a front-end to &lt;span style="font-family: courier new;"&gt;make&lt;/span&gt;. &lt;span style="color: #990000;"&gt;Here is an example of doing &lt;/span&gt;&lt;span style="color: #990000; font-family: courier new;"&gt;mim ls&lt;/span&gt;&lt;span style="color: #990000;"&gt; in a directory of a project that does just that:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;main.cc&lt;br /&gt;hello     make hello&lt;br /&gt;Makefile&lt;br /&gt;Mimfile&lt;/code&gt;&lt;/pre&gt;Furthermore, any program can use Mim for building files, because all the program need to do to build a file is to (try to) open it for reading. Mim will then kick in and build the file, without the other program ever nothing anything special. This is very powerful, because every program you have on you computer, cat, emacs, Mozilla, etc, can build any file using Mim.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Saving the environment&lt;/span&gt;&lt;br /&gt;Do you have &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;./configure &amp;amp;&amp;amp; make &amp;amp;&amp;amp; sudo make install&lt;/span&gt; &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/10/alias-norrissudo.html"&gt;aliased&lt;/a&gt; to &lt;a href="http://www.google.se/images?q=nike%20just%20do%20it&amp;amp;oe=utf-8&amp;amp;rls=org.mozilla:en-US:official&amp;amp;client=firefox-a&amp;amp;um=1&amp;amp;ie=UTF-8&amp;amp;source=og&amp;amp;sa=N&amp;amp;hl=sv&amp;amp;tab=wi"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;nike&lt;/span&gt;&lt;/a&gt; on you system? If you do you're not using Mim.&lt;br /&gt;&lt;br /&gt;One reason to use autoconf (i.e., the ./configure script above) is to find the paths to all the necessary tools, libraries, headers, etc, needed to build your project. With autoconf this involves generating &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Makefile&lt;/span&gt;s that are specifically targeted at your system. The problem with autoconf and the generated &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Makefile&lt;/span&gt;s is that you have to be a genius to understand what the heck is going on.&lt;br /&gt;&lt;br /&gt;So what do Mim do instead? Easy, you have an artifact file for each tool, library, header, etc, that is needed by your build. Those artifacts are configured (e.g., using autoconf) to &lt;i&gt;link to&lt;/i&gt; a specific file on your system. These tools (or, ) are normally placed in a directory called &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ext&lt;/span&gt; (for "external"); thus, to list the dependencies to external tools simply do &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;find ext&lt;/span&gt; or &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ls -R ext&lt;/span&gt;. Every file listed is an external dependency. Example:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;$ cd my-project/ext&lt;br /&gt;$ ls -R *&lt;br /&gt;ext/bin:&lt;br /&gt;g++  ld  python&lt;br /&gt;&lt;br /&gt;ext/include:&lt;br /&gt;3rdparty.h&lt;br /&gt;&lt;br /&gt;ext/lib:&lt;br /&gt;3rdparty.so&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;In this example we have configured the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ext&lt;/span&gt; directory to contain a compiler, linker, and a python interpreter. So, when we need to compile a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;.cc&lt;/span&gt; file we have the following command line in the Mimfile &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ext/bin/g++ file.cc&lt;/span&gt;. The first time this command line is executed, the artifacts (&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ext/bin/g++&lt;/span&gt;, etc) are not yet configured, so Mim launches the configuration tool (&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;autoconf&lt;/span&gt;). After that, the artifacts in the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ext&lt;/span&gt; directory point to the correct files on the local system and can be executed easily. &lt;br /&gt;&lt;br /&gt;A way of thinking of this is that Mim creates a virtual environment (directory structure) that is ideal(ized) for you specific project. All the tools and libraries you need are right there in the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ext&lt;/span&gt; directory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Deep understanding&lt;/span&gt;&lt;br /&gt;Mim really, &lt;i&gt;really&lt;/i&gt;, understands mimfiles. If a mimfile is updated, artifacts potentially need to be rebuilt. However, Mim will only rebuild artifact which command lines has changed or if their dependencies' command lines are changed. Have you ever experienced that your entire project needs to be rebuilt just because you added an a comment to a makefile? That will never happen with Mim.&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;Mim has even support for refactoring &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;mimfile&lt;/span&gt;s via your filesystem! In the hidden directory &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;.mimfile&lt;/span&gt; the artifact files' &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;mimfile&lt;/span&gt;-data is represented as files. So, if you want to change the name of an artifact file simply do &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;mv .mimfile/old-name .mimfile/new-name&lt;/span&gt;; to create a new artifact file that is identical to an existing one do &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cp .mimfile/original .mimfile/copy&lt;/span&gt;; to delete an entry from the mimfile do &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;rm .mimfile/remove-me&lt;/span&gt;. Finally, to edit the command line for an artifact you simply edit the corresponding file under &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;.mimfile&lt;/span&gt;, e.g., &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;echo 'gcc input-file.c -o %output' &amp;gt; .mimfile/file-to-be-edited&lt;/span&gt;.&lt;/div&gt;&lt;br /&gt;&lt;span style="color: #990000;"&gt;As mentioned earlier, Mim also has variables that can be used to configure how your project is built. These variables are also represented as files under the directory &lt;/span&gt;&lt;span style="color: #990000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;.mimvars&lt;/span&gt;&lt;span style="color: #990000;"&gt;. Here you can easily browse the variables using &lt;/span&gt;&lt;span style="color: #990000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;ls&lt;/span&gt;&lt;span style="color: #990000;"&gt;, &lt;/span&gt;&lt;span style="color: #990000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;find&lt;/span&gt;&lt;span style="color: #990000;"&gt;, &lt;/span&gt;&lt;span style="color: #990000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;grep&lt;/span&gt;&lt;span style="color: #990000;"&gt;, etc, or even your graphical file browser. The get the current value of a variable, simply do &lt;/span&gt;&lt;span style="color: #990000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cat .mimvars/the-variable&lt;/span&gt;&lt;span style="color: #990000;"&gt; and to set the value do something like &lt;/span&gt;&lt;span style="color: #990000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;echo 'new value' &amp;gt; .mimvars/the-variable&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #990000;"&gt;&lt;span style="font-weight: bold;"&gt;When thing go wrong&lt;/span&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;Ok, so Mim is nice, I think I've &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/03/most-important-skill.html"&gt;communicated&lt;/a&gt; that by now. But sometimes the world is very very unnice. Sometimes the world don't want to compile your code. Then you need to either 1) fix the world, or 2) fix your code. To my experience the latter is slightly easer than the former, although your mileage may vary.&lt;/div&gt;&lt;div style="color: #990000;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;To fix your code you need to know what the compiler complains about, but how do you do this when the files is compiled behind the scenes? Simply do &lt;span style="font-family: courier new;"&gt;mim log errors&lt;/span&gt; to print the error messages to the console, fix the error, and run the program again.&lt;/div&gt;&lt;div style="color: #990000;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: #990000;"&gt;Doing &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;mim log error&lt;/span&gt; after every build error gets annoying very fast. Thus there is a variant, &lt;span style="font-family: courier new;"&gt;mim log errors -f&lt;/span&gt;, that is useful when you wish to get continuous feedback. Given the -&lt;span style="font-family: courier new;"&gt;f&lt;/span&gt; flag Mim will run in the background and print the error message to the console as soon as they appear. So, assuming hello.cc contains an error, trying to execute hello will print the flowing:&lt;/div&gt;&lt;pre style="color: #990000;"&gt;&lt;code&gt;$ mim errors -f&lt;br /&gt;$ ./hello&lt;br /&gt;hello/hello.cc: In function ‘int main()’:&lt;br /&gt;hello/hello.cc:3: error: ‘printf’ was not declared in this scope&lt;/code&gt;&lt;/pre&gt;&lt;div style="color: #990000;"&gt;which is is just what gcc outputs, thus, any tool that parses the output from gcc will understand the error output from Mim as well. This is of course also applicable for other tools like make.&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Summing up&lt;/span&gt;&lt;br /&gt;I've described the ideas behind Mim, its current state, and possible future features (that, by that way should be very reasonable to implement :)). You can find Mim &lt;a href="http://gitorious.org/mim"&gt;here&lt;/a&gt;. You'll need Intel &lt;a href="http://www.threadingbuildingblocks.org/"&gt;Threading Building Blocks&lt;/a&gt; and &lt;a href="http://www.lua.org/"&gt;Lua 5.1&lt;/a&gt;. I've developed using Ubuntu and also compiled it on Suse 11.2. The instructions for how to build Mim itself is currently horribly missing, so please contact me if you like to try Mim out.&lt;br /&gt;&lt;ul&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5059190134484396728?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5059190134484396728/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5059190134484396728' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5059190134484396728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5059190134484396728'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/09/mim-build-system-you-always-wanted.html' title='Mim: the build system you always wanted'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3592551889543918228</id><published>2010-07-09T11:30:00.000-07:00</published><updated>2010-07-09T11:42:15.814-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='api'/><category scheme='http://www.blogger.com/atom/ns#' term='logging'/><category scheme='http://www.blogger.com/atom/ns#' term='DSL'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>DSL: Domain Specific Logging</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/9/9a/Log_train05.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="142" src="http://upload.wikimedia.org/wikipedia/commons/9/9a/Log_train05.jpg" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;A few years ago, I was asked to to design a logging framework for our new Product X 2.0. Yeah, I know, "design a logging framework?". That's what I thought too. So I didn't really do much design; all I did was to say "let's use &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;java.util.logging&lt;/span&gt;", and then I was done designing. Well kind of anyway...&lt;br /&gt;&lt;br /&gt;I started to think back on my first impression of the logging framework in Product X 1.0. I remembered that I had a hard time understanding how it was (supposed) to be used from a developer's point-of-view. That is, when faced with a situation where I had to log, I could not easily answer:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;to which severity category does the entry belong (FINEST, FINER, FINE, INFO, WARNING, SEVERE, ERROR, etc)&lt;/li&gt;&lt;li&gt;what data should be provided in the entry and how it should be formatted, and&lt;/li&gt;&lt;li&gt;how log entries normally is expressed textually (common phrases, and other conventions used).&lt;/li&gt;&lt;/ul&gt;In other words, it was too general (i.e., complex) with too much freedom to define your own logging levels, priorities, etc.&lt;br /&gt;&lt;br /&gt;That's why I proposed to make logging easier, thus, the logging framework in Product X 2.0 has very few severity levels. This is implemented by simply wrapping a &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;java.util.logging.Logger&lt;/span&gt; in a new class that only has the most necessary methods.&lt;br /&gt;&lt;br /&gt;This simplification reduced the code noise in the logger interface, which made it a lot easier to choose log-level when writing a new log message in the source. This had an important implication (that we originally didn't think of): given that a certain message had to be logged, we (the developers) agreed more on which level that message should be logged. No more, "oh, this is a log message written by Robert -- he always use high log level for unimportant messages".&lt;br /&gt;&lt;br /&gt;Of course, you could still here comments like "oh, this log message is written by John -- he always use ':' instead of '=' when printing variables". This isn't much of an issue if an somewhat intelligent being is reading the logs, e.g., a human. However, if the logs are read by a computer program, this can be a big problem -- this was the case for Product X 2.0.&lt;br /&gt;&lt;br /&gt;This variable-printing business could be solved quite easily; we simply added a method to the logger class (&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;MyLogger&lt;/span&gt;) called &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;MyLogger variable(final String name, final Object value)&lt;/span&gt; that logged a variable. Example:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;logger.variable("result", compResult).low("Computation completed.");&lt;/code&gt; &lt;/pre&gt;which would result in the following log message:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;2008-apr-12 11:49:30 LOW: Compuation completed. [result = 37289.32];&lt;/code&gt;&lt;/pre&gt;When I did this, I began to think differently about log messages. It put me into a new mind-set -- the pattern matching mind-set. Patterns in log messages started to appear almost everywhere. Actually, most of our logs followed one of the following patterns: &lt;br /&gt;&lt;ul&gt;&lt;li&gt;Started X&lt;/li&gt;&lt;li&gt;Completed X&lt;/li&gt;&lt;li&gt;Trying to X&lt;/li&gt;&lt;li&gt;Failed to X&lt;/li&gt;&lt;li&gt;Succeeded to X&lt;/li&gt;&lt;/ul&gt;Here is an example:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;try {&lt;br /&gt;  myLogger.tryingTo("connect to remote host").variable(&lt;br /&gt;                    "address", remoteAddress).low();&lt;br /&gt;  // Code for connecting.&lt;br /&gt;  myLogger.succeededTo("connect to remote host").variable(&lt;br /&gt;                       "address", remoteAddress).low();&lt;br /&gt;} catch (final IOException e) {&lt;br /&gt;  myLogger.failedTo("connect to remote host").cause(e).medium();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;To take this even further, it is possible to only allow certain combinations of &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;succeededTo&lt;/span&gt;, &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;tryingTo&lt;/span&gt;, &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cause&lt;/span&gt;, etc, by declaring them in different interfaces. Let's assume that it should only be possible to use &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;cause&lt;/span&gt; if the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;failedTo&lt;/span&gt; method have been called. Here's the code for doing that:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;interface MyLogger {&lt;br /&gt;  // ... other logging methods.&lt;br /&gt;  CauseLogger failedTo(String msg);&lt;br /&gt;}&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;interface CauseLogger {&lt;br /&gt;  LevelChooser cause(Throwable cause);&lt;br /&gt;  LevelChooser cause(String message);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;interface LevelChooser {&lt;br /&gt;  void low();&lt;br /&gt;  void medium();&lt;br /&gt;  void high();&lt;br /&gt;} &lt;/code&gt;&lt;/pre&gt;This is essentially what's called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. It complicates the design of the logging framework, but also makes it impossible to misuse it. Good or bad? It's a matter of taste, I think, but it's a good design strategy to have up your sleeve.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3592551889543918228?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3592551889543918228/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3592551889543918228' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3592551889543918228'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3592551889543918228'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/07/dsl-domain-specific-logging.html' title='DSL: Domain Specific Logging'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5459083497054383728</id><published>2010-06-17T11:34:00.000-07:00</published><updated>2010-06-17T11:34:00.665-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='big vs. small'/><category scheme='http://www.blogger.com/atom/ns#' term='solutions'/><category scheme='http://www.blogger.com/atom/ns#' term='talks'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><title type='text'>The only design pattern is small solutions</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/071018ImresoltBurjDubai.jpg/386px-071018ImresoltBurjDubai.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/071018ImresoltBurjDubai.jpg/386px-071018ImresoltBurjDubai.jpg" width="128" /&gt;&lt;/a&gt;&lt;/div&gt;I just saw &lt;a href="http://www.ted.com/talks/rory_sutherland_sweat_the_small_stuff.html"&gt;this TED talk&lt;/a&gt; (which you &lt;span style="font-style: italic;"&gt;need&lt;/span&gt; to see too!). It's essentially about how we prefer big complex solutions to any problem we face. Why? Because it makes us feel smart and important. As my head is filled software thoughts, I started to think how this it relates to software design. We software developers &lt;i&gt;really!&lt;/i&gt;, &lt;i&gt;really!&lt;/i&gt;, &lt;i&gt;really!&lt;/i&gt;, like big solutions to small problems: "Oh, you got a program that needs to store some data? You better get yourself a dedicated database machine, a persistence layer, and define a XML schema for communication data format."&lt;br /&gt;&lt;br /&gt;We don't need big solutions to small problems. &lt;a href="http://www.c2.com/cgi/wiki?TimeToMakeItShort"&gt;Big solutions&lt;/a&gt; are easy to find. Big solutions need man-hours but no understanding. We need small solution to big problems. Small solutions are hard to find. Small solutions need insight into the &lt;span style="font-style: italic;"&gt;actual&lt;/span&gt; problem we're solving. The actual problem is what's left when we remove all accidental complexity, marketing buzz-words, etc, and think &lt;span style="font-style: italic;"&gt;clearly&lt;/span&gt; about the original problem.&lt;br /&gt;&lt;br /&gt;Small solutions are orthogonal to each other; big solutions are not, they interact in non-obvious ways. Thus, big solutions creates more problems, or as the american journalist Eric Sevareid, said: &lt;br /&gt;&lt;blockquote&gt;The chief cause of problems is solutions&lt;/blockquote&gt;which is more true in software development than in most other areas. Implement small solutions to problems and your future self will thank you. Implement big solutions and you fall for the sirens' calls of the marketeers, or your own wishes to do seemingly cool stuff while looking smart doing it. Do you &lt;span style="font-style: italic;"&gt;really&lt;/span&gt; need a DSL? A database? Web interface? Reflection? Operator overloading? Meta-programming? Code generation? Ruby? SOAP?&lt;br /&gt;&lt;br /&gt;Thinking small have big impact.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5459083497054383728?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5459083497054383728/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5459083497054383728' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5459083497054383728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5459083497054383728'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/06/only-design-pattern-is-small-solutions.html' title='The only design pattern is small solutions'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2837769316159916963</id><published>2010-06-14T09:33:00.000-07:00</published><updated>2010-06-14T09:33:00.951-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='opinions'/><category scheme='http://www.blogger.com/atom/ns#' term='social'/><category scheme='http://www.blogger.com/atom/ns#' term='people'/><category scheme='http://www.blogger.com/atom/ns#' term='skills'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='confidence'/><title type='text'>If it hurts do it more often</title><content type='html'>&lt;a href="http://3.bp.blogspot.com/_WTvaZ7cJV5k/S-WTkVvqp1I/AAAAAAAAADo/k4lbiYd6_xA/s1600/automotivator%282%29.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" id="BLOGGER_PHOTO_ID_5468939575032981330" src="http://3.bp.blogspot.com/_WTvaZ7cJV5k/S-WTkVvqp1I/AAAAAAAAADo/k4lbiYd6_xA/s320/automotivator%282%29.jpg" style="cursor: pointer; float: left; height: 300px; margin: 0pt 10px 10px 0pt; width: 280px;" /&gt;&lt;/a&gt;As a kid, most of what you did was related to learning. Some call it "playing" or "being curious", but whatever you call it, it's learning in one form or another. And learning hurts. A lot.&lt;br /&gt;&lt;br /&gt;So you start going to school to make learning not hurt so much. As a 7-8 year-old you enjoy school, because it makes learning much easier. Also, you can play with your class mates, like say, play soccer. Oh, by the way, playing soccer makes you run faster longer, gives you better balance, gives you better feeling of how flying objects behaves (like a ball), better timing your own movements to others (like your team mates). It also makes you better winning and loosing (you aren't a bad looser, are you? How about a bad winner?), and it makes you better at focusing on a particular task, and making other (your team mates) focusing on the same task as you. It's all learning.&lt;br /&gt;&lt;br /&gt;As you grow older you become more comfortable with most things you do because you know them. When you finally leave school to start working as a programmer you lost all will to do things that hurts... because you lost your will to learn (in comparison to how much you wanted to learn stuff as a kid). You lost your will to discover new things.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Stupidity hurts&lt;/h4&gt;So, when stated with a problem that forces you to do something you don't know or something you really don't like, you stall. Stall, stall, stall, because you don't want to do it. You want to do something fun. Something you know how to do. Something that makes you feel secure  and comfortable. Something that makes you feel less &lt;span style="font-style: italic;"&gt;stupid&lt;/span&gt;!&lt;br /&gt;&lt;br /&gt;Yes, you are stupid. I'm stupid. We are all stupid. No one know everything, so everyone is stupid at something. And we will remain stupid at those things unless we learn how to do them better. But... Ouch! Remember? Learning &lt;span style="font-style: italic;"&gt;hurts&lt;/span&gt;! So we think to yourselfs &lt;span style="font-style: italic;"&gt;"Ooh, I don't want to do that!"&lt;/span&gt;, or put slightly different &lt;span style="font-style: italic;"&gt;"I may get hurt doing that! I'd rather stay stupid!"&lt;/span&gt;. Imagine if you thought that way as a 8-year-old kid in school... or at the playground, or at the soccer field. We wouldn't get many marathon runners or Nobel prize winners that way, would we?&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The cure&lt;/h4&gt;It's really easy: dare to be stupid. Dare to ask stupid questions. After a while, you'll notice that you start to ask really hard questions, and before you know it you'll ask questions no one (you know) have answers to. And then, as it happens, people will ask you instead. If they dare to of course (hint: they should).&lt;br /&gt;&lt;br /&gt;In fact, I think that people will be more likely to ask you (stupid) questions because they'll seen or heard (of) you ask (stupid) questions. And you know what, people do as other people do. Especially like people they look up to, and since you know so much (from asking stupid questions) they look up to you. Isn't that neat?&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The workplace&lt;/h4&gt;If you have colleagues who don't mind potentially looking stupid by asking a question, you've work at a good company, I think. But I'm not really someone who read or thinks about this a lot, so I may be wrong. There may be effect here that I'm clueless about. Anyway, my feelings are that I'd be more at home at a company like that than a company that encourages silence and really intelligent questions only. But I'm just me. You are you, and you may feel entirely different about this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2837769316159916963?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2837769316159916963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2837769316159916963' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2837769316159916963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2837769316159916963'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/06/if-it-hurts-do-it-more-often.html' title='If it hurts do it more often'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_WTvaZ7cJV5k/S-WTkVvqp1I/AAAAAAAAADo/k4lbiYd6_xA/s72-c/automotivator%282%29.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8783752951400272488</id><published>2010-06-10T12:53:00.000-07:00</published><updated>2010-06-10T12:53:00.503-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='opinions'/><category scheme='http://www.blogger.com/atom/ns#' term='social'/><category scheme='http://www.blogger.com/atom/ns#' term='D language'/><category scheme='http://www.blogger.com/atom/ns#' term='rambling'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><title type='text'>Features are social processes</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Sign_language_C.svg/200px-Sign_language_C.svg.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 200px; height: 217px;" src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Sign_language_C.svg/200px-Sign_language_C.svg.png" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;For a while now I've been thinking about how human languages &lt;span style="font-style: italic;"&gt;evolve&lt;/span&gt; compared to how computer languages are &lt;span style="font-style: italic;"&gt;designed&lt;/span&gt; and how that relates to the features of the respective languages. In this post I will ramble at bit about how the meaning of software related terms is defined. I'll also discuss &lt;span style="font-style: italic;"&gt;hard&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;soft&lt;/span&gt; features in programming languages and how the community surrounding the language affects, and is affected by, those features.&lt;br /&gt;&lt;br /&gt;This is mostly a bunch of ideas and observations that I'm trying to put into words to 1) make me understand them better, and 2) make sure I don't forget them. If you expect a scientific survey, then I'm sorry to disappoint you. Maybe, though, you'll find food for your own thought and ideas.&lt;br /&gt;&lt;h3&gt;What's a language?&lt;/h3&gt;As I see it, languages (be it the human or programming kind) are mutual agreement between the communicating parts of what a statement means. If everyone have a different opinion of what the following statement means, then it effectively &lt;i&gt;&lt;span style="font-style: italic;"&gt;doesn't have any&lt;/span&gt; meaning at all&lt;/i&gt; since we can't use it to communicate with anyone:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;You need wood from a forrest to create a fire.&lt;/code&gt;&lt;/pre&gt;or in computer-speak:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;Fire fire = new Fire(forrest.getWood());&lt;/code&gt;&lt;/pre&gt;On the other hand, when we agree upon what this phrase mean, then we can do all kinds of things: discuss its correctness, use it in another context, abstract it to cover more and general cases, write a compiler for it, etc.&lt;br /&gt;&lt;br /&gt;For example, the common breed of C compiler &lt;a href="http://www.ioccc.org/main.html"&gt;accepts&lt;/a&gt; a lot of code most C programmers won't. It's the users of the C language that defines the subset of allowed-by-the-compiler-C that is acceptable for us to use. In other words, the C standard can say all it wants; its the C users who in the end defines the (practical) C language. It a bit like if physics would say "Our universe have 11 dimensions. Go use all of them!", but all known users of the universe are three-dimensional beings, thus, the accepted subset of the universe is three-dimensional. Sorry to break it to you, physics, but that's how it is.&lt;br /&gt;&lt;br /&gt;Language features are all about what the community around the language make of the language. In a way, language features are just as much a technical aspect of the language as a social aspect of it. Example: is a language feature that's not accepted by the community really a &lt;span style="font-style: italic;"&gt;feature&lt;/span&gt;, or is it just useless language complexity? More extreme example: a language without users but with extremely powerful features; effectively, does that language have any features at all?&lt;br /&gt;&lt;br /&gt;I would also say that anyone aiming to develop a successful programming language (without the backing of a &lt;span style="font-size:78%;"&gt;&lt;span style="font-style: italic;"&gt;*caugh* Sun&lt;/span&gt;&lt;/span&gt; huge &lt;span style="font-size:78%;"&gt;&lt;span style="font-style: italic;"&gt;*caugh* Microsoft&lt;/span&gt;&lt;/span&gt;  corporation) needs to have equally good eye for the technical aspect as well as the social aspect. (S)he needs to understand the social processes involved for getting a community of users who agree (hopefully with the language designer) on how the language should be used. (I think python is good example of such community, by the way).&lt;br /&gt;&lt;h3&gt;What about software?&lt;/h3&gt;Developing software is also a social process. For example, you get requirements from your customer, you discuss the requirements in order to understand them, and you implement them. Implementing requirement are also a social process: you design the code by discussing it with your colleagues. And what words do you use for doing that?&lt;br /&gt;&lt;br /&gt;You use words like object, generic, sort, inheritance, stack, tree, operation, method, message, reuse, client, algorithm, allocation, port, framework, mapping, service, channel, process, decoupled, assumption, resource, provider, input, interface... I could go on forever, but the point is that none of these words really mean anything if we humans don't agree on what they mean. The computer, framework, or programming language has no opinion on what "decouple the client's mapping algorithm from the port allocation" means, but programmers do. It's important it means that same to all programmers involved.&lt;br /&gt;&lt;h3&gt;Soft and hard&lt;/h3&gt;How does this relate to programming language features? I think there two different kinds of features: &lt;span style="font-style: italic;"&gt;hard&lt;/span&gt; features that was (deliberately) designed into the language, and &lt;span style="font-style: italic;"&gt;soft&lt;/span&gt; features that are concepts and idioms that have evolved from using the language.&lt;br /&gt;&lt;br /&gt;Hard features are concrete. You can make a list of hard features by reading the language specification. Soft features, on the other hand, are not. They are embedded in the community and to enumerate them you need to vibe with it for a while. Hard features are taught in classes and in books; soft features are learned by hacking, hacking, living and breathing, and some more hacking.&lt;br /&gt;&lt;br /&gt;Example: C++ templates. Originally intended to provide better type-safety when writing generic code, like &lt;span style="font-family:courier new;"&gt;std::vector&lt;/span&gt;. The C++ community has then discovered that templates can be used for much, much more (like Boost.Spirit). There are a lot of template code written to implement various kinds of abstract features, e.g., compile-time if, compile-time strings, domain specific languages, etc. The hard feature is "write type-safe generic code". The soft features are "compile-time if", "embedded DSL", and even "it's hard, but you can evaluate &lt;a href="http://ompf.org/forum/viewtopic.php?f=8&amp;amp;t=1556"&gt;everything&lt;/a&gt; at compile-time".&lt;br /&gt;&lt;br /&gt;The D language took these soft features of C++ templates (e.g., compile-time if, embedded DSL) and integrated them into the core language. Thus, enabled more programmers to use them, because of easier syntax, error messages, compiler support, documentation, etc.&lt;br /&gt;&lt;br /&gt;So when a C++ programmer talks about enabling or disabling some piece of code (s)he needs to think about some abstract concept like the enable-if template, while a D programming just thinks "static if". In fact, I don't think the D programmer even thinks "static if" because it's so natural to them, just as the more common "dynamic if" is so natural to all of us. The D programmer probably thinks in entirely other abstract concepts because his/her mind is free from the horrible details of C++ templates meta-programming.&lt;br /&gt;&lt;br /&gt;You may argue that our mind is very good at abstracting and that this isn't a problem in practice, but I don't think that's true at all. Example: very few C++ programmer have every done something like an template computing the sinus of an angle, so when they're told to optimize a piece of code doing trigonometry what they'll do is to use some kind of table look-up. A D programmer, on the other hand, will simply slap together a sinus function that can be evaluated statically by the compiler because compile-time evaluation is nothing magic to her/him. (In fact, in &lt;a href="http://www.digitalmars.com/d/2.0/features2.html"&gt;D 2.0&lt;/a&gt; match function will automatically be evaluated at compile-time if their arguments are constants).&lt;br /&gt;&lt;br /&gt;What I'm saying here is that compile-time evaluation is a (hard) language feature of D but not of C++ (where it is an soft feature). Sure, you &lt;span style="font-style: italic;"&gt;can in theory&lt;/span&gt; do compile-time evaluation of &lt;span style="font-style: italic;"&gt;everything&lt;/span&gt; you need in C++ (templates are &lt;a href="http://en.wikipedia.org/wiki/Template_metaprogramming"&gt;Turing complete&lt;/a&gt;), but not in practice because it's so hard that you actively avoid it. Thus, in most programmers conscious mind, C++ does not have compile-time evaluation. Similarly, you can do object-oriented programming in C, but you probably don't because it's hard and littered with horrible details. Thus, C is in most programmers mind not a object-oriented language. It's possible to do structured programing in Commodore BASIC, but most of us don't think of it like that. Got my point yet? Good.&lt;br /&gt;&lt;h3&gt;Ok, so what?&lt;/h3&gt;By now you are probably thinking "that's all very interesting, but how is this useful?". Well, I did say that this post was a rambling, didn't I? :)&lt;br /&gt;&lt;br /&gt;Seriously though, I don't really think any of this will make you a better software developer, but I think it could be useful if you are developing an tool and there's a community of users around it. Be sure to note what kinds of words the users uses, what features they ignore, what idioms they invent, etc. Integrate the words and idioms into the tool and it's documentation to make the experience for a new user of the application more consistent.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8783752951400272488?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8783752951400272488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8783752951400272488' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8783752951400272488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8783752951400272488'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/06/features-are-social-processes.html' title='Features are social processes'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-4856155553152718136</id><published>2010-06-09T16:30:00.000-07:00</published><updated>2010-06-10T07:25:48.052-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><title type='text'>Design for optimizability</title><content type='html'>Only optimize when you found a bottleneck by measuring. That's what we all have been taught, right? It's a good rule. But is it really right? What if you're designing a performance critical application? Should you really be ignorant about performance until you measured and found a bottleneck?&lt;br /&gt;&lt;br /&gt;I say: &lt;span style="font-style: italic;"&gt;don't optimize, but plan for optimizability&lt;/span&gt;. I think Dwight D. Eisenhower said it best, though&lt;br /&gt;&lt;blockquote&gt;Plans are nothing; planning is everything.&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;And now a disclaimer. I am not encouraging over-design in this post. Neither am I proposing that you should analyze every silly detail of your application domain before you start coding. I encourage you to start hacking on you application without &lt;span style="font-style: italic;"&gt;worrying&lt;/span&gt; about performance, because you know (if you follow the tips that follows and use your brain) that you can optimize it later. Knowing that you can rewrite that slow and memory-hungry Ruby script into a snappy C program (that don't use any heap memory at all) makes it so much easier to sleep at night. Believe me.&lt;br /&gt;&lt;br /&gt;Ok, now let's get going!&lt;br /&gt;&lt;h3&gt;Caring about planning&lt;/h3&gt;Planning for optimizability means that you make sure that:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;seams&lt;/span&gt; are placed to allow optimizing,&lt;/li&gt;&lt;li&gt;&lt;span style="font-style: italic;"&gt;system-wide concerns&lt;/span&gt; are encapsulated,&lt;/li&gt;&lt;li&gt;it's possible to do &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt; later on,&lt;/li&gt;&lt;li&gt;algorithmic decisions are delayed,&lt;/li&gt;&lt;li&gt;intelligence can be moved from data to code to data,&lt;/li&gt;&lt;li&gt;it's possible to move run-time aspects to compile-time,&lt;/li&gt;&lt;li&gt;you know your tools.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;We will now go through the first three of these bullets one by one and discuss them in detail. I will discuss the remaining four in a later post.&lt;br /&gt;&lt;h3&gt;Seams and optimization borders&lt;/h3&gt;&lt;span&gt;A &lt;/span&gt;&lt;span style="font-style: italic;"&gt;seam&lt;/span&gt;s in software design is a some-what vaguely defined concept. If two components of a software design are loosely &lt;a href="http://en.wikipedia.org/wiki/Coupling_%28computer_science%29"&gt;coupled&lt;/a&gt;, we say that there is a &lt;span style="font-style: italic;"&gt;seam&lt;/span&gt; between them. In other words, seams separate the design into parts that can vary independently of each other. Inheritance and &lt;a href="http://www.codeproject.com/KB/architecture/DependencyInjection.aspx"&gt;dependency injection&lt;/a&gt; is often used to achieve this in object-oriented systems; function pointers play a similar role in procedural programming.&lt;br /&gt;&lt;br /&gt;Since planning for optimizability means that we wish to make it easy to replace slow code with faster, we thus should place seams in the design such that they surround potentially slow code. This implies that design seams also are &lt;span style="font-style: italic;"&gt;optimization borders&lt;/span&gt;; borders over which optimizations cannot easily be done without affecting other parts of the system.&lt;br /&gt;&lt;br /&gt;This doesn't mean that it's impossible to optimize over &lt;span style="font-style: italic;"&gt;optimization borders&lt;/span&gt;, just that other parts of the system will be affected by it. Thus, optimizing over &lt;span style="font-style: italic;"&gt;optimization boarders&lt;/span&gt; is harder than optimizing within them. Let's take an example.&lt;br /&gt;&lt;br /&gt;Assume that we iterate over a  a large list in a  performance critical part of our application:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;void Foo::iterateOverLargeList(std::list&amp;lt;Thing&amp;gt; l) {&lt;br /&gt; for (int i = 0; i &amp;lt; l.size(); i++)&lt;br /&gt;   l[i].doEasyCalculation();&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;Where should we place the design seams in this code? Or, more concretely, how do we use virtual functions to enable us to optimize this piece of code as much as possible without affecting the surrounding code? As I see it, we have the following alternatives:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;make &lt;span style="font-family:courier new;"&gt;Thing::doEasyCalculation&lt;/span&gt; virtual,&lt;br /&gt;&lt;/li&gt;&lt;li&gt;make &lt;span style="font-family:courier new;"&gt;Foo::iterateOverLargeList&lt;/span&gt; virtual, or&lt;br /&gt;&lt;/li&gt;&lt;li&gt;encapsulate &lt;span style="font-family:courier new;"&gt;std::list&amp;lt;Thing&amp;gt;&lt;/span&gt; in a new class &lt;span style="font-family:courier new;"&gt;ThingList&lt;/span&gt;, move iterateOverLargeList to &lt;span style="font-family:courier new;"&gt;ThingList&lt;/span&gt;, and make it virtual.&lt;/li&gt;&lt;/ol&gt;Of these three alternatives 3) gives us the most freedom to increase the performance of the loop, because it allows us to do not only optimize how we iterate, but also change &lt;span style="font-style: italic;"&gt;the underlying data structure&lt;/span&gt;. For instance, changing from &lt;span style="font-family:courier new;"&gt;std::list&lt;/span&gt; (a linked list) to &lt;span style="font-family:courier new;"&gt;std::vector&lt;/span&gt; (essentially a linear array) will make the access pattern linear, thus the (L1, L2, L3) cache can do a better job.&lt;br /&gt;&lt;br /&gt;However, 3) is also the option that is the hardest to refactor to, because we have to change every part of the application that uses the &lt;span style="font-family:courier new;"&gt;std::list&amp;lt;Thing&amp;gt;&lt;/span&gt; list. So, if our application is large, this may involve a lot of refactoring work. Thus, if we choose 3) early in the development we don't have to refactor so much later. In other words, early understanding of the domain, the application's&lt;a href="http://en.wikipedia.org/wiki/Use_case"&gt; use-cases&lt;/a&gt;, and the application design, is important for us when designing for optimizability.&lt;br /&gt;&lt;br /&gt;This example touches on another important tool when designing for optimizability: encapsulation. We will now discuss this in greater detail.&lt;br /&gt;&lt;h3&gt;Concerning encapsulation&lt;/h3&gt;One of the worst kind of optimizations you can do is to optimize something that affect the entire system, e.g., the data flow. A global property like is a &lt;span style="font-style: italic;"&gt;system-wide concern&lt;/span&gt; and having the system's performance depend on such property is very very bad. We need to encapsulate these global properties to make sure that there are &lt;span style="font-style: italic;"&gt;design seams&lt;/span&gt; that enable us to optimize them when we need to.&lt;br /&gt;&lt;br /&gt;This means that  we need to design our code such that &lt;span style="font-style: italic;"&gt;if  &lt;/span&gt;we need to optimize something, that something is encapsulated into a single class (or few classes). This does not mean that we should encapsulate every little petty detail, though. It does mean, however, that we need to put a bit more effort in understanding our application, and then think about its design, data flow, etc. This is actually something we should do anyway even if we're not designing for optimizability, because understand the current design of the application make it easier to improve it.&lt;br /&gt;&lt;br /&gt;When we got a firm understanding of the application and how it will be used, then we can identify the &lt;span style="font-style: italic;"&gt;system-wide concerns&lt;/span&gt;, and only then can we start to encapsulate them to make them possible to optimize in the future.&lt;br /&gt;&lt;h4&gt;Avoid memory copying&lt;/h4&gt;Reading data from memory is one of the most  costly thing a modern CPU can do. If the data is in (L1) cache reading  memory is fast (3-5 cycles or so), but if the data needs to be read from  main memory it takes 100 times longer. That's right: 500 cycles to read  a single bit of data. Writing data to main memory is also very costly  operation, thus, copying is extremely costly operation.&lt;br /&gt;&lt;br /&gt;With this  in mind, it may be tempting to return a pointer to a shared data buffer  instead of returning a copy of that data. For example:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;class Thingie {&lt;br /&gt; int* stuff;&lt;br /&gt; // constructor that initializes 'stuff'.&lt;br /&gt; int* getStuff() { return stuff; }&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;This  may look efficient since there is no memory copying going on. However,  this design is horrible with regard to encapsulation. For instance, what  if we need to modify the returned data in some other part of the  program without affecting the &lt;span style="font-family:courier new;"&gt;Thing&lt;/span&gt;  instance? In that case we need to make a copy and modify the copied  data. But what if that copied data is returned just like the &lt;span style="font-family:courier new;"&gt;int* stuff&lt;/span&gt; above? And what if it needs to be  modified in some other part of the program? Then we need to make &lt;span style="font-style: italic;"&gt;another&lt;/span&gt; copy!&lt;br /&gt;&lt;br /&gt;In situations  like this, you need to take a step back and look at the data flow  through your application and then try to optimize it to minimize the  number of copies. This is not an easy task, so we really wish to avoid doing it, or at least make it easier for us to do. Is there some way to design our application (with little extra effort) from the start to make optimizations like this possible (without huge  redesigns)?&lt;br /&gt;&lt;br /&gt;In the example above, the &lt;span style="font-style: italic;"&gt;system-wide concern&lt;/span&gt; is  how the &lt;span style="font-family:courier new;"&gt;int* stuff&lt;/span&gt; data is passed around in the application and how to make sure  that it is not copied unnecessarily.&lt;br /&gt;&lt;br /&gt;Let's rewrite this example a bit into:&lt;pre&gt;&lt;code&gt;class Thingie2 {&lt;br /&gt; MyBuffer stuff;&lt;br /&gt; // constructor that initializes 'stuff'.&lt;br /&gt; MyBuffer getStuff() { return stuff; }&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;Since the data now is encapsulated in &lt;span style="font-family:courier new;"&gt;MyBuffer&lt;/span&gt; (and the entire application uses &lt;span style="font-family:courier new;"&gt;MyBuffer&lt;/span&gt; to refer the the data), we can now implement a lot of optimizations that affect the data-flow of the application. For instance, we can implement copy-on-write  semantics and reference counting; this would be extremely hard to do without using &lt;span style="font-family:courier new;"&gt;MyBuffer&lt;/span&gt; to encapsulate the data-flow, which is a &lt;span style="font-style: italic;"&gt;system-wide concern&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Design lowering&lt;/h3&gt;The term &lt;span style="font-style: italic;"&gt;instruction lowering &lt;/span&gt;is an optimization technique used in compilers to generate more efficient code. For example, the C code &lt;span style="font-family:courier new;"&gt;a = 16 * b;&lt;/span&gt; can be implemented (in pseudo-assembler) by a multiplying operation &lt;span style="font-family:courier new;"&gt;A = MULT(16, B)&lt;/span&gt; but also by a simpler and faster shift operation &lt;span style="font-family:courier new;"&gt;A = SHL(4, B)&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;With the term &lt;span style="font-style: italic;"&gt;design lowering &lt;/span&gt;I mean a similar concept: reimplementing the design in a simpler and faster language or platform, or using faster language constructs or data structures. Examples of such &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt; include:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;rewrite Python code in C,&lt;/li&gt;&lt;li&gt;replace reflection-heavy Java code with simpler non-reflection code,&lt;/li&gt;&lt;li&gt;using a simple array instead of a hash map or a linked list,&lt;/li&gt;&lt;li&gt;using the memory layout of C structs as data format instead of XML in files, over network, etc&lt;br /&gt;&lt;/li&gt;&lt;li&gt;use hardware acceleration like graphics cards.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-style: italic;"&gt;Design lowering&lt;/span&gt; can easily increase the performance 10-100 times or even much more. Lets take an example of this.&lt;br /&gt;&lt;h4&gt;A story of left and right&lt;/h4&gt;Imagine two small program &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;right&lt;/span&gt;; &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; generates data and sends it to &lt;span style="font-family:courier new;"&gt;right&lt;/span&gt;, which sums the received data. Here is the first version of this pair of programs implemented in Python:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;--- left.py ---&lt;br /&gt;a = { "first":[1, 123], "middle":[1, 2, 456], "last":[1, 2, 3, 789] }&lt;br /&gt;for i in xrange(0, 1000000):&lt;br /&gt; print a&lt;br /&gt;&lt;br /&gt;--- right.py ---&lt;br /&gt;import sys&lt;br /&gt;total = 0&lt;br /&gt;for line in sys.stdin:&lt;br /&gt; a = eval(line)&lt;br /&gt; total = total + sum(a["first"]) + sum(a["middle"]) + sum(a["last"])&lt;br /&gt; print total&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;Translating this Python code into C++ idioms this becomes:&lt;pre&gt;&lt;code&gt;--- left.cc ---&lt;br /&gt;struct Data {&lt;br /&gt; Data() {&lt;br /&gt;   first[0] = 1; first[1] = 123;&lt;br /&gt;   middle[0] = 1; middle[1] = 2; middle[2] = 456;&lt;br /&gt;   last[0] = 1; last[1] = 2; last[2] = 3; last[3] = 789;&lt;br /&gt; }&lt;br /&gt; int first[2];&lt;br /&gt; int middle[3];&lt;br /&gt; int last[4];&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt; Data data;&lt;br /&gt; for (size_t a = 0; a &amp;lt; 1000000; a++) {&lt;br /&gt;   for (size_t b = 0; b &amp;lt; sizeof(Data); b++)&lt;br /&gt;     putchar(reinterpret_cast&amp;lt;char*&amp;gt;(&amp;amp;data)[b]);&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;--- right.cc ---&lt;br /&gt;struct Data {&lt;br /&gt; int first[2];&lt;br /&gt; int middle[3];&lt;br /&gt; int last[4];&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt; Data data;&lt;br /&gt; long total = 0;&lt;br /&gt; for (size_t a = 0; a &amp;lt; 1000000; a++) {&lt;br /&gt;   for (size_t b = 0; b &amp;lt; sizeof(Data); b++)&lt;br /&gt;     reinterpret_cast&amp;lt;char*&amp;gt;(&amp;amp;data)[b] = getchar();&lt;br /&gt;   total += data.first[0] + data.first[1] + data.middle[0] + data.middle[1] +&lt;br /&gt;   data.middle[2] + data.last[0] + data.last[1] + data.last[2] + data.last[3];&lt;br /&gt; }&lt;br /&gt; printf("%ld\n", total);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;And running them we get:&lt;pre&gt;&lt;code&gt;$ time ./left.py | ./right.py&lt;br /&gt;1378000000&lt;br /&gt;&lt;br /&gt;real 1m3.284s&lt;br /&gt;user 1m14.057s&lt;br /&gt;sys 0m0.256s&lt;br /&gt;$ g++ -O2 right.cc -o right &amp;amp;&amp;amp; g++ -O2 left.cc -o left &amp;amp;&amp;amp; time ./left | ./right&lt;br /&gt;1378000000&lt;br /&gt;&lt;br /&gt;real 0m0.763s&lt;br /&gt;user 0m1.292s&lt;br /&gt;sys 0m0.088s&lt;br /&gt;$ echo 74.067/1.292 | bc&lt;br /&gt;57&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; Thus, the C++ version of these little programs are 57 times faster than the equivalent Python code. But this is not the neat thing... Let's get back to discussing &lt;span style="font-style: italic;"&gt;design lowering.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Where to do design lowering&lt;/h4&gt;The neat thing is that you can implement &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;right&lt;/span&gt; in Python without worrying about performance because they are easily &lt;span style="font-style: italic;"&gt;design lowered&lt;/span&gt; to C++. I can say this because &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;right&lt;/span&gt; comes in pair; if you redesign one side you also redesign the other. In other words, the details of the implementations are free to change in ways to make them execute more efficient. Thus, it's easy to do &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;However, this would not be the case if &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; sent data to an unknown part via a predefined protocol. If this was so, then reimplementing &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; in C++ would probably be considerably harder. In other words, &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt; &lt;span style="font-family:courier new;"&gt;left&lt;/span&gt; would be less feasible. It would still be possible to do, though.&lt;br /&gt;&lt;br /&gt;In summary, &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt; can be used as a plan for optimizability when the design can vary in ways that make it feasible to implement faster in another language (or data structure, etc). How the design can vary depends on many things, though, thus &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt; is not always easy to do without first refactoring the design.&lt;br /&gt;&lt;br /&gt;As before, &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt; is not something you get for free, though: you need to ha good understanding of how/where the application will be used. When you understand your application, you're more likely to make correct assumptions of what parts of that can be design lowered and which parts that can't.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Conclusions&lt;br /&gt;&lt;/h3&gt;Just like optimizing, designing for optimizability requires us to &lt;span style="font-style: italic;"&gt;understand&lt;/span&gt; the problem. We cannot escape it: to do something good, we need to know what "good" is, and to know what "good" is we need to understand the domain. Beware of over-analyzing though: concentrate on &lt;span style="font-style: italic;"&gt;system-wide concerns&lt;/span&gt; that are likely to affect performance, and know how the design can vary to enable &lt;span style="font-style: italic;"&gt;design lowering&lt;/span&gt;. Also, make sure that design seams don't hinder optimizability.&lt;br /&gt;&lt;br /&gt;There are still much more to discuss on this topic, so stay tuned!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-4856155553152718136?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/4856155553152718136/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=4856155553152718136' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4856155553152718136'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4856155553152718136'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/design-for-optimizability.html' title='Design for optimizability'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-491987780082251251</id><published>2010-05-29T06:20:00.000-07:00</published><updated>2010-05-29T06:20:17.880-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='talks'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>LLVM talks</title><content type='html'>A few very interesting &lt;a href="http://llvm.org/devmtg/2009-10/"&gt;presentations&lt;/a&gt; of LLVM from the LLVM developer's meeting 2009.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-491987780082251251?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/491987780082251251/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=491987780082251251' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/491987780082251251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/491987780082251251'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/llvm-talks.html' title='LLVM talks'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6792168787069467491</id><published>2010-05-26T08:04:00.000-07:00</published><updated>2010-06-17T01:39:46.996-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Find parent pid given a pid</title><content type='html'>&lt;a href="http://upload.wikimedia.org/wikipedia/commons/3/33/Mother_hen_with_chicks02.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" src="http://upload.wikimedia.org/wikipedia/commons/3/33/Mother_hen_with_chicks02.jpg" style="cursor: pointer; float: left; height: 240px; margin: 0pt 10px 10px 0pt; width: 360px;" /&gt;&lt;/a&gt;&lt;br /&gt;A few days ago I needed to get the parent process id (pid) of a given pid on Linux. There are shell commands for that, .e.g, &lt;span style="font-family: courier new;"&gt;ps -o ppid= p &lt;span style="font-style: italic;"&gt;the-pid&lt;/span&gt;&lt;/span&gt;, but I needed to get the pid programmatically from C.&lt;br /&gt;&lt;br /&gt;For some reason the &lt;a href="http://www.opengroup.org/onlinepubs/009695399/functions/getppid.html"&gt;&lt;span style="font-family: courier new;"&gt;getppid&lt;/span&gt;&lt;/a&gt; function can only return the parent pid of the &lt;span style="font-style: italic;"&gt;current &lt;/span&gt;process, not &lt;span style="font-style: italic;"&gt;any&lt;/span&gt; process. I searched the web after an alternative solution but I found nothing.&lt;br /&gt;&lt;br /&gt;So I took a peek at the source for the &lt;span style="font-family: courier new;"&gt;ps&lt;/span&gt; command, which revealed that there is a function called &lt;span style="font-family: courier new;"&gt;get_proc_stats&lt;/span&gt; defined in &lt;a href="http://sysinf0.klabs.be/usr/include/proc/readproc.h?dist=;arch="&gt;&lt;span style="font-family: courier new;"&gt;proc/readproc.h&lt;/span&gt;&lt;/a&gt; that (among other things) returns the parent pid of a given pid. You need to do install &lt;a href="http://www.rpmfind.net/linux/RPM/mandriva/2009.1/x86_64/media/main/release/procps-devel-3.2.7-8mdv2009.1.x86_64.html"&gt;&lt;span style="font-family: courier new;"&gt;libproc-dev&lt;/span&gt;&lt;/a&gt; to get this function. You can then do:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#include &amp;lt;proc/readproc.h&amp;gt;&lt;br /&gt;void printppid(pid_t pid) {&lt;br /&gt;proc_t process_info;&lt;br /&gt;get_proc_stats(pid, &amp;amp;process_info);&lt;br /&gt;printf("Parent of pid=%d is pid=%d\n", pid, process_info.ppid);&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;and compile it with &lt;span style="font-family: courier new;"&gt;gcc the-file.c -lproc&lt;/span&gt;. I hope you find this useful.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6792168787069467491?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6792168787069467491/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6792168787069467491' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6792168787069467491'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6792168787069467491'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/find-parent-pid-given-pid.html' title='Find parent pid given a pid'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1575696004145371665</id><published>2010-05-16T04:12:00.000-07:00</published><updated>2010-05-16T06:29:16.558-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='people'/><category scheme='http://www.blogger.com/atom/ns#' term='skills'/><category scheme='http://www.blogger.com/atom/ns#' term='memories'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='confidence'/><title type='text'>A story of being humble with confidence</title><content type='html'>I just read &lt;a href="http://www.skorks.com/2010/05/who-deserves-the-credit-for-software-craftsmanship-and-great-design/"&gt;a post&lt;/a&gt; over at &lt;a href="http://www.skorks.com/"&gt;SKORKS&lt;/a&gt;, a blog I read every now and then. He writes a bit about &lt;a href="http://www.faqs.org/docs/artu/"&gt;The Art Of Unix Programming&lt;/a&gt;, which got me reminded of when I first heard of that book. I then remembered reading &lt;a href="http://www.lifehack.org/articles/communication/the-art-of-humble-confidence.html"&gt;The Art of Humble Confidence&lt;/a&gt; and I felt that I really had to write something along those lines. Here goes.&lt;br /&gt;&lt;br /&gt;It was 2006 and I was working with a handful experienced colleagues on a project trying to radically increase the usefulness two large applications by making them work together. This was an extremely interesting time for me and they taught me a lot that I today value very highly.&lt;br /&gt;&lt;br /&gt;One day one of my colleagues who was sitting next door to my office knocked gently on my open door to get my attention. He said, "Sorry, am I interrupting you? Do you have a moment?" He was humble as always, speaking slowly and silently to make sure that I wouldn't be anxious about what he'd say next.&lt;br /&gt;&lt;br /&gt;"Do you remember what we talked about before?" Even though I wasn't really sure to what he was referring I replied "sure", thinking that I'd get what he means when he starts to talk about it.&lt;br /&gt;&lt;br /&gt;While he slowly pulled up a chair and sat down next to me he said, "did you consider what we said about the &lt;span style="font-family:courier new;"&gt;MessageReceiver&lt;/span&gt; class?" I now realized that he was referring to our discussion over a coffee they day before. I nodded, remembering that he didn't really liked how I designed some part of the system we were working on.&lt;br /&gt;&lt;br /&gt;Though I couldn't really understand his argument from yesterday I had redesigned it anyway to be more in line with his suggestions. Making a poor design made me feel a bit bad and not &lt;span style="font-style: italic;"&gt;understanding&lt;/span&gt; why it was bad made me feel worse. But I didn't want to ask him explaining it (again) because I didn't want to look (more) stupid. That would be even worse. Or so I thought.&lt;br /&gt;&lt;br /&gt;I guess he realized my anxiety about not properly understanding his design, because he next said "I did a pretty crappy job explaining why that class needed to be changed, right?" He smiling and chuckled, "I was always better at instructing computers than people." We laughed.&lt;br /&gt;&lt;br /&gt;"Anyway", he said, "I read this book a bit yesterday and I think chapter 10 explains what I mean much better than I ever can." He handed me the book and said "you can borrow it if you like." He laughed again and added "but not for too long. I need to read it soon again since you ask so incredibly interesting and hard questions." He got up from the chair and said "let's go and get some coffee." He smiled and added "I'm 'starving'".&lt;br /&gt;&lt;br /&gt;I grabbed my cup and we walked over to our colleagues offices and asked them to joined us. As we walked to the coffee machine I felt like I was in the greatest group of developers there was. Everyone was working for our mutual goal while having fun, learning, and teaching at the same time.&lt;br /&gt;&lt;br /&gt;My colleague had basically just asked me questions, yet managed to &lt;span style="font-style: italic;"&gt;tell me something&lt;/span&gt;. Yes, he evened managed to &lt;span style="font-style: italic;"&gt;tell me what to do&lt;/span&gt;. But more importantly, he taught me that you will never know everything and that working in software is a constant process of learning.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1575696004145371665?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1575696004145371665/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1575696004145371665' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1575696004145371665'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1575696004145371665'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/story-of-being-humble-with-confidence.html' title='A story of being humble with confidence'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1948214023214112210</id><published>2010-05-11T05:19:00.000-07:00</published><updated>2010-05-11T05:30:28.441-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='debugging'/><category scheme='http://www.blogger.com/atom/ns#' term='mistakes'/><title type='text'>My worst mistakes (since two days)</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Interdit_forbidden.svg/200px-Interdit_forbidden.svg.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 200px; height: 141px;" src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Interdit_forbidden.svg/200px-Interdit_forbidden.svg.png" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;I just spent two days on writing, testing, rewriting, testing, debugging, etc, a piece of code only to find the error to be five misplaces pixels. Learn from my mistake: &lt;span style="font-weight: bold;"&gt;never&lt;/span&gt; use &lt;span style="font-family:courier new;"&gt;i&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;j&lt;/span&gt; as index in nested loops.&lt;br /&gt;&lt;br /&gt;What made this problem worse than it should have needed to be was that the erroneous code was in a test-case. Learn from my mistake: &lt;span style="font-weight: bold;"&gt;never&lt;/span&gt; write complex control flow in your test code.&lt;br /&gt;&lt;br /&gt;What made it take longer than it should have take for me to find this mistake was that I didn't assert my assumptions. Learn from my mistake: &lt;span style="font-weight: bold;"&gt;always &lt;/span&gt;assert, &lt;span style="font-weight: bold;"&gt;never&lt;/span&gt; assume anything of any piece of code when you're chasing weird behavior.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1948214023214112210?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1948214023214112210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1948214023214112210' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1948214023214112210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1948214023214112210'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/my-worst-mistakes-since-two-days.html' title='My worst mistakes (since two days)'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6110197613904112393</id><published>2010-05-07T11:19:00.001-07:00</published><updated>2010-05-07T11:25:09.155-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='talks'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>Threading is not a model</title><content type='html'>I just saw the &lt;a href="http://python.mirocommunity.com/video/1600/pycon-2010-threading-is-not-a-"&gt;Threading is not a model&lt;/a&gt; talk by Joe Gregori from PyCon 2010, which I found very interesting. It has some history about programming language development, and some discussion about design of every-day physical stuff and every-day programming language stuff. I especially find the idea of sufficient irritation very funny and interesting. :)&lt;br /&gt;&lt;br /&gt;The main part of the talk is about different ways of implementing concurrency, mainly CSP (Communicating Sequential Processes) and Actors. Interesting stuff presented by a good speaker.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6110197613904112393?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6110197613904112393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6110197613904112393' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6110197613904112393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6110197613904112393'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/threading-is-not-model.html' title='Threading is not a model'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2282194556600658005</id><published>2010-05-07T07:29:00.000-07:00</published><updated>2010-05-07T12:06:18.645-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Must-know tricks for safer macros</title><content type='html'>&lt;a href="http://4.bp.blogspot.com/_WTvaZ7cJV5k/S97waeCKNXI/AAAAAAAAADQ/XSPVGU0TZlU/s1600/HPIM0896-289x1941.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" id="BLOGGER_PHOTO_ID_5467071335203747186" src="http://4.bp.blogspot.com/_WTvaZ7cJV5k/S97waeCKNXI/AAAAAAAAADQ/XSPVGU0TZlU/s200/HPIM0896-289x1941.jpg" style="margin: 0pt 10px 10px 0pt; cursor: pointer; float: left; height: 134px; width: 200px;" border="0" /&gt;&lt;/a&gt;Almost every developer knows to &lt;a href="http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.4"&gt;stay away&lt;/a&gt; from (C-style) macros unless it's absolutely necessary to use them, or that is saves &lt;span style="font-style: italic;"&gt;a lot!&lt;/span&gt; of code to use them. And when they do use them, they make sure to write them &lt;a href="http://www.ebyte.it/library/codesnippets/WritingCppMacros.html"&gt;properly&lt;/a&gt;. I won't go into the details of the traditional ways of writing good macros (as in "harder to misuse, but still possible") because there are several pages like that already (see the links above). Instead, I'll discuss an entirely different way of making macros more safe.&lt;br /&gt;&lt;h4&gt;Why macros are hard&lt;/h4&gt;Let's describe the problems with macros with an example. This simple macro definition multiplies the provided argument with it self to yield the square of the input:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define SQUARE(x) x * x&lt;/code&gt;&lt;/pre&gt;Looks simple right? Too bad, because its not that simple. For example, what happens when the following code is evaluated:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;int a = 2;&lt;br /&gt;int b = SQUARE(a + 1);&lt;/code&gt;&lt;/pre&gt;I tell you what happens: all hell breaks loose! The above code is expanded into:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;int a = 2;&lt;br /&gt;int b = a + 1 * a + 1;&lt;/code&gt;&lt;/pre&gt;thus, &lt;span style="font-family:courier new;"&gt;b&lt;/span&gt; will equal 2 + 1 * 2 + 1 = 5. Not quite what we expected by looking at the code &lt;span style=";font-family:&amp;quot;;" &gt;SQUARE(a + 1)&lt;/span&gt;, right? All in all, macros looks simple and harmless enough, but are not simple at to get right. And definitely not harmless, on the contrary, it's extremely easy to get bitten horribly bad. We are now going to discuss how to make macros a bit more safe to work with.&lt;br /&gt;&lt;h4&gt;Making them softer: check your typing&lt;/h4&gt;Types are an important part of the C language and even more so of C++ with all its classes, templates, and function overloading. Macros, though, are simple text substitution without knowledge of types, so macros fits very badly in the normal type-centric C/C++ world we are used to work with.&lt;br /&gt;&lt;br /&gt;For example, you give a function the wrong types as argument. What do you get? A type error. No code is emitted by the compiler. This is good. On the other hand, if you give a macro&lt;span style="font-style: italic;"&gt; &lt;/span&gt;the wrong types as argument, what do you get? If you're lucky some kind of error; maybe a syntax error, maybe a semantic error. If you're unlucky, though, you won't get any error at all. The compiler will just silently emit ill-behaving code into the &lt;span style=";font-family:&amp;quot;;" &gt;.o&lt;/span&gt;-file. This is extremely bad because we're fooled into believing our code works as we expects it to.&lt;br /&gt;&lt;br /&gt;Luckily, there is a way of making macros more safe in this regard. Let's take simple, yet illustrative example: a macro called &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; that takes one argument, which is a variable, and sets it to &lt;span style="font-family:courier new;"&gt;0&lt;/span&gt;. The first version looks like this:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define ZERO(variable) variable = 0;&lt;/code&gt;&lt;/pre&gt;and is intended to be used inside a function like this:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;void func() {&lt;br /&gt;int i;&lt;br /&gt;ZERO(i);&lt;br /&gt;// more code here...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;Simple but not safe enough for our tastes. For example, this macro can be called as &lt;span style="font-family:courier new;"&gt;ZERO(var0 += var1)&lt;/span&gt; and it will produce code the compiler accepts, but that code does not have the behavior the macro was intended to have. The macro will expand this code to &lt;span style="font-family:courier new;"&gt;var0 += var1 = 0&lt;/span&gt;, which (I think) is equivalent to &lt;span style="font-family:courier new;"&gt;var1 = 0; var0 += 0&lt;/span&gt;. Whatever the expanded code does, its not what we intended &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; to do. In fact, &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; was never designed to handle this kind of argument and should thus reject it with a compilation error. We will now discuss how to reject such invalid argument. Here goes...&lt;br /&gt;&lt;h4&gt;Halt! Identify yourself!&lt;br /&gt;&lt;/h4&gt;To make sure that the compiler emits an error when the &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; macro is given a non-variable as argument, we rewrite it to:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define ZERO(variable) \&lt;br /&gt;{ enum variable { }; } \&lt;br /&gt;v&lt;/code&gt;&lt;code&gt;ariable&lt;/code&gt;&lt;code&gt; = 0;&lt;/code&gt;&lt;/pre&gt;That is, an enumeration is declared with the same name as argument inside a new scope. This makes sure that the argument is a valid identifier and not just any expression, since an expression can't be used as a name for an enumeration. For example, the previously problematic code, &lt;span style="font-family:courier new;"&gt;ZERO(var0 += var1)&lt;/span&gt;, will expand to:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;{ enum var0 += var1 { }; } var0 += var1 = 0;&lt;/code&gt;&lt;/pre&gt;which clearly won't compile. On the other hand given correct argument, e.g., the code &lt;span style="font-family:courier new;"&gt;ZERO(var0)&lt;/span&gt;, we get&lt;br /&gt;&lt;pre&gt;&lt;code&gt;{ enum var0 { }; } var0 = 0;&lt;/code&gt;&lt;/pre&gt;which compiles and behaves as we expect &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; to behave. Neat! Even neater, the compiler won't emit any code (in the resulting &lt;span style=";font-family:&amp;quot;;" &gt;.o&lt;/span&gt;-file) for the extra "type-checking code" we added, because all it does is to declare a type, and that type is never used in our program. Awesomeness!&lt;br /&gt;&lt;br /&gt;So we now have a pattern for making sure that a macro argument is a variable: declare a enumeration (or a class or struct) inside a new scope with the same name as the variable. We can encapsulate this pattern in a macro &lt;span style="font-family:courier new;"&gt;VARIABLE&lt;/span&gt; and rewrite &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; using it&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define VARIABLE(v) { enum v { }; }&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;#define ZERO(variable) \&lt;br /&gt;VARIABLE(variable) \&lt;br /&gt;variable = 0;&lt;/code&gt;&lt;/pre&gt;Note that with a bit of imagination, the definition of &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; can be read as the signature (&lt;span style="font-family:courier new;"&gt;VARIABLE(variable)&lt;/span&gt;) followed by the macro body (&lt;span style="font-family:courier new;"&gt;variable = 0;&lt;/span&gt;), making macros look more like function definitions that we are familiar with. This wraps up our discussion about variables as macro argument. But read on, there's more!&lt;br /&gt;&lt;h4&gt;Constants&lt;/h4&gt;Let's assume that we wish to generalize &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt; into another macro called &lt;span style="font-family:courier new;"&gt;ASSIGN&lt;/span&gt; that sets the provided variable to any &lt;span style="font-style: italic;"&gt;constant integer expression&lt;/span&gt; not just zero. For example, &lt;span style="font-family:courier new;"&gt;1&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;2&lt;/span&gt;, and &lt;span style="font-family:courier new;"&gt;39 + 3&lt;/span&gt; are valid arguments, but &lt;span style="font-family:courier new;"&gt;i + 2&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;1.0&lt;/span&gt;, and &lt;span style="font-family:courier new;"&gt;f()&lt;/span&gt; are not because those are not constant integers. One way of defining such macro is as follows:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define ASSIGN(variable, value) \&lt;br /&gt;VARIABLE(variable) \&lt;br /&gt;variable = value;&lt;/code&gt;&lt;/pre&gt;that is, we simply added an argument &lt;span style="font-family:courier new;"&gt;value&lt;/span&gt; that &lt;span style="font-family:courier new;"&gt;variable&lt;/span&gt; is assigned to. Simple, but as usual with macros, very easy to misuse. For example &lt;span style="font-family:courier new;"&gt;ASSIGN(myVar, myVar + 1)&lt;/span&gt; will assign &lt;span style="font-family:courier new;"&gt;myVar&lt;/span&gt; to a non-constant value, which is precisely what we didn't want &lt;span style="font-family:courier new;"&gt;ASSIGN&lt;/span&gt; to do.&lt;br /&gt;&lt;br /&gt;To solve this problem, we recall that an enumerator (a member of an enumeration) can be assign a constant integer value inside the enumeration declaration. This is exactly the kind of values we wish &lt;span style="font-family:courier new;"&gt;ASSIGN&lt;/span&gt; to accepts, thus, we rewrite it into the following code:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define ASSIGN(variable, value) \&lt;br /&gt;VARIABLE(variable) \&lt;br /&gt;{ enum { E = value }; } \&lt;br /&gt;variable = value;&lt;/code&gt;&lt;/pre&gt;This version of &lt;span style="font-family:courier new;"&gt;ASSIGN&lt;/span&gt; only accepts variables names for its first argument and constant integers for its second argument. Note, that the constant can be a constant &lt;span style="font-style: italic;"&gt;expression&lt;/span&gt;, so things like &lt;span style="font-family:courier new;"&gt;ASSIGN(var0, 1 + C * D)&lt;/span&gt; will work as long as &lt;span style="font-family:courier new;"&gt;C&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;D&lt;/span&gt; are &lt;span style="font-family:courier new;"&gt;static const int&lt;/span&gt;'s. If we extract the pattern for checking that an argument is a constant integer int &lt;span style="font-family:courier new;"&gt;CONST_INT&lt;/span&gt;, we get the following two definitions:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define CONST_INT(v) { enum { E = v }; }&lt;br /&gt;#define ASSIGN(variable, value) \&lt;br /&gt;VARIABLE(variable) CONST_INT(value) \&lt;br /&gt;variable = value;&lt;/code&gt;&lt;/pre&gt;As for the final version of &lt;span style="font-family:courier new;"&gt;ZERO&lt;/span&gt;, the definition of &lt;span style="font-family:courier new;"&gt;ASSIGN&lt;/span&gt; can be read as the signature of &lt;span style="font-family:courier new;"&gt;ASSIGN&lt;/span&gt; followed by the body of it.&lt;br /&gt;&lt;h4&gt;Types&lt;/h4&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_WTvaZ7cJV5k/S-LPh6su-oI/AAAAAAAAADc/zlKbE3OSfmI/s1600/Metal_movable_type.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img src="http://1.bp.blogspot.com/_WTvaZ7cJV5k/S-LPh6su-oI/AAAAAAAAADc/zlKbE3OSfmI/s200/Metal_movable_type.jpg" border="0" height="180" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;Now we will modify &lt;span style=";font-family:&amp;quot;;" &gt;ASSIGN&lt;/span&gt; into &lt;span style=";font-family:&amp;quot;;" &gt;DECLARE&lt;/span&gt;; a macro that declares a variable of some type, which is provided as argument to &lt;span style=";font-family:&amp;quot;;" &gt;DECLARE&lt;/span&gt;. Similar to &lt;span style=";font-family:&amp;quot;;" &gt;ASSIGN&lt;/span&gt;, &lt;span style=";font-family:&amp;quot;;" &gt;DECLARE&lt;/span&gt; initializes the variable to the provided constant integer expression. Our first implementation of such macro is:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define DECLARE(type, variable, value) \&lt;br /&gt;&lt;/code&gt;&lt;code&gt;VARIABLE(variable) CONST_INT(value) \&lt;br /&gt;type variable = value;&lt;/code&gt;&lt;/pre&gt;However, the compiler will accept code like &lt;span style=";font-family:&amp;quot;;" &gt;DECLARE(int i =, j, 0)&lt;/span&gt; (assuming &lt;span style=";font-family:&amp;quot;;" &gt;j&lt;/span&gt; is delcared variable and &lt;span style=";font-family:&amp;quot;;" &gt;i&lt;/span&gt; is not). So following our habit from previous examples, we wish to make it a bit safer by making sure the &lt;span style=";font-family:&amp;quot;;" &gt;type&lt;/span&gt; argument actually is a type, e.g., &lt;span style=";font-family:&amp;quot;;" &gt;int&lt;/span&gt;, &lt;span style=";font-family:&amp;quot;;" &gt;MyClass&lt;/span&gt;, or &lt;span style=";font-family:&amp;quot;;" &gt;MyTemplate&amp;lt;MyClass&amp;gt;&lt;/span&gt;.  We do this by having the macro using &lt;span style=";font-family:&amp;quot;;" &gt;type&lt;/span&gt; as a template argument, as follows:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;template&amp;lt;typename TYPE&amp;gt; class TypeChecker { };&lt;br /&gt;#define DECLARE(type, variable, value) \&lt;br /&gt;VARIABLE(variable) CONST_INT(value) \&lt;br /&gt;{ class Dummy : TypeChecker&amp;lt;type&amp;gt; { }; } \&lt;br /&gt;type variable = value;&lt;/code&gt;&lt;/pre&gt;This definition is much more safe from misuse than the previous; code like &lt;span style=";font-family:&amp;quot;;" &gt;DECLARE(int i =, j, 0)&lt;/span&gt;won't compile. If we extract the argument-checking code into a separate macro, &lt;span style=";font-family:&amp;quot;;" &gt;TYPE&lt;/span&gt;, we get:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;template&amp;lt;typename TYPE&amp;gt;&lt;/code&gt;&lt;code&gt; class TypeChecker { };&lt;br /&gt;#define TYPE(t) { class Dummy : TypeChecker&amp;lt;t&amp;gt; { }; }&lt;br /&gt;#define DECLARE(type, variable, value) \&lt;br /&gt;&lt;/code&gt;&lt;code&gt;TYPE(type) &lt;/code&gt;&lt;code&gt;VARIABLE(variable) CONST_INT(value) \&lt;br /&gt;type variable&amp;gt; = value;&lt;/code&gt;&lt;/pre&gt;As before, note that we can read this definition as two parts: first the macro signature and then the macro body. Compiler enforced documentation FTW!&lt;br /&gt;&lt;h4&gt;Everyone's unique&lt;/h4&gt;To not make this post too long, I'll stop giving background and reasons for the rest of the type-checking macros I'll present from now on. I'll just briefly describe what they do.&lt;br /&gt;&lt;br /&gt;The following macro makes sure that a list of identifiers only contains unique identifiers:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define UNIQUE(is...) { enum { is }; }&lt;/code&gt;&lt;/pre&gt;Note that this macro requires that the compiler supports &lt;a href="http://www.emerson.emory.edu/services/gcc/html/Macro_Varargs.html"&gt;macro varargs&lt;/a&gt;. It used as &lt;span style=";font-family:&amp;quot;;" &gt;UNIQUE(name1, name2, name3)&lt;/span&gt;, or &lt;span style=";font-family:&amp;quot;;" &gt;UNIQUE(name1, name2, name1)&lt;/span&gt; where former is ok, but the latter will emit an error.&lt;br /&gt;&lt;h4&gt;Comparison&lt;/h4&gt;These macros compares constant integer expressions in various ways. The basic idea here is that the size of an array must not be negative and that the boolean value &lt;span style=";font-family:&amp;quot;;" &gt;true&lt;/span&gt; is converted into &lt;span style=";font-family:&amp;quot;;" &gt;1&lt;/span&gt; in a integer context and &lt;span style=";font-family:&amp;quot;;" &gt;false&lt;/span&gt; is converted to &lt;span style=";font-family:&amp;quot;;" &gt;0&lt;/span&gt;. We use this to implement the macro &lt;span style=";font-family:&amp;quot;;" &gt;IS_TRUE&lt;/span&gt; as follows:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define IS_TRUE(a) { struct _ { int d0[!(a)]; int d1[-!(a)]; }; }&lt;/code&gt;&lt;/pre&gt;Many comparison macros are then trivially implemented using &lt;span style=";font-family:&amp;quot;;" &gt;IS_TRUE&lt;/span&gt;, for example:&lt;br /&gt;&lt;pre&gt;&lt;code&gt;#define LESS_THAN(a, b) IS_TRUE(a &amp;lt; b)&lt;br /&gt;#define EQUAL(a, b) IS_TRUE(a == b)&lt;br /&gt;#define SMALL_TYPE(t) IS_TRUE(sizeof(t) &amp;lt; 8)&lt;/code&gt;&lt;/pre&gt;You may ask yourself why such macro is needed. Shouldn't templates be used here instead? I agree, but there are some of us who is (un-)lucky enough to use C and not C++...&lt;br /&gt;&lt;h4&gt;Let's get general&lt;/h4&gt;The general idea we've used so far is to have two parts of the macro. One part that implements the desired (visible) behavior, and another part that works like type-checking code. The type-checking code is implemented by having short trivial side-effect free pieces of code that only will compile under the assumptions you make on the argument. For example, the argument is a variable, or the argument is a constant integer expression.&lt;br /&gt;&lt;br /&gt;Of course, it may still be possible to fool the "type-checking" code, but its much less likely to happen indeliberately, which is the most important cases to find.&lt;br /&gt;&lt;h4&gt;Descriptive error message?&lt;/h4&gt;In short: no. The error messages got from any of these type-checking macros are extremely non-descriptive. However, &lt;i&gt;any&lt;/i&gt; error message, even weird and non-descriptive ones, is still better than &lt;i&gt;no&lt;/i&gt; error message at all and &lt;i&gt;ill-behaving&lt;/i&gt; binary code.&lt;h4&gt;The sum of all fears&lt;/h4&gt;Does the approach described here solve all problems with macros? No, it does not. It does however,  make it less of an issue. It is possible to write macros that are type-safe and behaves in a good way (by which I mean: either compiles into correct code or does not compile at all). However, I'm pretty sure that are uses for macros that cannot be covered with this approach.&lt;br /&gt;&lt;br /&gt;Despite this, I highly recommend to use this idea when you write macros. It will make the macro better in most way possible, e.g., safer and better documented. Compiler-enforced documentation, even! Just like type declarations in all your other C/C++ code. Neat, don't you think?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2282194556600658005?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2282194556600658005/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2282194556600658005' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2282194556600658005'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2282194556600658005'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/must-know-tricks-for-safer-macros.html' title='Must-know tricks for safer macros'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_WTvaZ7cJV5k/S97waeCKNXI/AAAAAAAAADQ/XSPVGU0TZlU/s72-c/HPIM0896-289x1941.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-4777536805175001148</id><published>2010-05-02T05:56:00.000-07:00</published><updated>2011-05-14T02:07:35.323-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dependency injection'/><category scheme='http://www.blogger.com/atom/ns#' term='D language'/><category scheme='http://www.blogger.com/atom/ns#' term='memory management'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Beautiful Dependency Injection in C++</title><content type='html'>&lt;a href="http://upload.wikimedia.org/wikipedia/commons/a/a7/El_acueducto_de_Los_Milagros.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" src="http://upload.wikimedia.org/wikipedia/commons/a/a7/El_acueducto_de_Los_Milagros.jpg" style="cursor: pointer; float: left; height: 220px; margin: 0pt 10px 10px 0pt; width: 300px;" /&gt;&lt;/a&gt;&lt;br /&gt;Dependency injection is a very nice way of making classes testable and more reusable. An instance of a class &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; that a class &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; depends on are simply injected into &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;'s constructor. That is, the client of &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; (the code instantiating it) controls how &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; instances are created, thus, &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; is more decoupled from the rest of the system. Why? Because &lt;span style="font-style: italic;"&gt;any&lt;/span&gt; object that is a &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; can be used with &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;: subclasses of &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt;, instances shared other objects, or a &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; created in a special way (e.g., a singelton or proxy to a remote object). Compare this to the traditional non-dependency injection way, where &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; is instantiated by &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;, thus making it impossible for the client to control what kind of instance of &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; that is used by &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Object life-time management complicated&lt;/h4&gt;Dependency injection is  straight forward to do (correctly) in languages with automatic memory management like Java and Python, but it's much harder to get right in languages like C++ which forces you to manage memory manually. Of course, it possible to do simply delete the injected object in &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;'s destructor; like:&lt;br /&gt;&lt;pre style="font-family: courier new;"&gt;&lt;code&gt;class NeedsFoo {&lt;br /&gt;Foo* foo;&lt;br /&gt;public:&lt;br /&gt;NeedsFoo(Foo* foo) : foo(foo) { }&lt;br /&gt;~NeedsFoo() { delete foo; }&lt;br /&gt;// Methods using foo that needs to be tested.&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;However, this is far from an optimal solution because now &lt;span style="font-style: italic;"&gt;all&lt;/span&gt; objects given to &lt;span style="font-style: italic;"&gt;any&lt;/span&gt; instance of &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; must be heap allocated because we &lt;span style="font-family: courier new;"&gt;delete&lt;/span&gt; them in the destructor. So even when &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; is stack allocated (e.g., for performance reasons), its &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; object must be heap allocated. Besides being hard to get right, heap allocation is extremely costly compared to stack allocation. So if we want performance we're screwed, and since we're using C++ I assume that performance is important. If it's not, we could just as well use some other language.&lt;br /&gt;&lt;br /&gt;Another reason, arguably more important, for that doing &lt;span style="font-family: courier new;"&gt;delete&lt;/span&gt; in the destructor is &lt;span style="font-style: italic;"&gt;bad! bad! bad!&lt;/span&gt; is that the injected object cannot be easily share with some other part of the application: who knows when (and if) the object should be deleted...? &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; don't know it, that's for sure.&lt;br /&gt;&lt;br /&gt;How do we fix this? Well, we could simply remove the &lt;span style="font-family: courier new;"&gt;delete&lt;/span&gt; from the destructor:&lt;br /&gt;&lt;pre style="font-family: courier new;"&gt;&lt;code&gt;class NeedsFoo {&lt;br /&gt;Foo* foo;&lt;br /&gt;public:&lt;br /&gt;NeedsFoo(Foo* foo) : foo(foo) { }&lt;br /&gt;// Methods using foo that needs to be tested.&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;Easy to test, right? Yes. Easy to use in production code? Well, kind of. Easy to use &lt;span style="font-style: italic;"&gt;correctly&lt;/span&gt; in production code? Definitely not!&lt;br /&gt;&lt;br /&gt;Why? Because, as said before, C++ lacks garbage collection thus the injected object needs&lt;br /&gt;to be managed manually somehow. For example using referenced counting pointers, or making sure that &lt;span style="font-family: courier new;"&gt;foo&lt;/span&gt; are has the same life time as the &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; instance. We will focus on the latter for the remaining of this post. But first a short discussion why it is common that the injected objects (e.g., a &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; instance) should be destroyed at the same time as the object they are injected into (e.g., a &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; instance).&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Let them here on be joined and never parted&lt;/h4&gt;&lt;a href="http://upload.wikimedia.org/wikipedia/commons/2/28/Small_aquarium.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" src="http://upload.wikimedia.org/wikipedia/commons/2/28/Small_aquarium.jpg" style="cursor: pointer; float: left; height: 225px; margin: 0pt 10px 10px 0pt; width: 300px;" /&gt;&lt;/a&gt;Dependency injection is used a lot to make code more testable. However, in my experience it is often the case that if it wasn't for testing, dependency injection wouldn't be needed because its always the same kind if objects that are injected (e.g., the &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; instance is always instantiated the same way for all &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; instances). In a way using dependency injection is a kind of over-design: the code is more generic than it needs to be, thus, more complex that it needs to be.&lt;br /&gt;&lt;br /&gt;Despite this, we still do dependency injection and we still consider it part of a good design. Why? Because, as said, it makes the code testable. We consider testable code to be more important than simple design. I can accept that for languages with garbage collector, but when you do the memory management manually the design get &lt;span style="font-style: italic;"&gt;much! much! much&lt;/span&gt;!&lt;span style="font-style: italic;"&gt; &lt;/span&gt;more complex. Not good, not good. Infact it's terrible. The reason it's terrible is that memory management is a program global problem that isn't easy to encapsulate.&lt;br /&gt;&lt;br /&gt;For example, if I call &lt;span style="font-family: courier new;"&gt;malloc&lt;/span&gt; in my code and pass the pointer to a function defined in a library, how do I know if that memory is deallocated by the library? I can't. Not without manually inspecting the source code of the library. How about using smart pointers? Doesn't help much. The library is most probably a C library. Crap.&lt;br /&gt;&lt;br /&gt;So, how can we do dependency injection to simplify testing while keeping memory management simple? This is what we will discuss now, my friend. Let's go!&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Object life-time management simplified&lt;/h4&gt;One way making sure that a bunch of objects is created and destroyed at the same time is to stuff them into a class:&lt;br /&gt;&lt;pre style="font-family: courier new;"&gt;&lt;code&gt;class Concrete : public NeedsFoo {&lt;br /&gt;Foo foo;&lt;br /&gt;public:&lt;br /&gt;Concrete() : NeedsFoo(&amp;amp;foo) { }&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;Here, an instance (actually an instance of a sub-type) of &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; is created and injected with a &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; object. Since both object are held in a &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt; instance, the &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; and the &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; instances will be destroyed at the same time. Sweetness!&lt;br /&gt;&lt;br /&gt;This approach works well under the assumption we never need to inject a subclass of &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; to &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;. (Well, of course it's still possible to inject a subclass to &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;, but if we do that we are back to square one since we need to manage those objects' lifetime manually; &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt; is only usable for &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; objects not subtypes of &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;So, for classes that injected with the same kind of instances, this approach solves the memory management problem. Goody-goody!&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Lies, damn lies, and untested source code&lt;/h4&gt;Wait, what's that? Do you the cries? &lt;span style="font-style: italic;"&gt;Breaking news! Lies on the Internet! Read all about it!&lt;/span&gt; It sad, but true. And I'm a bit ashamed to tell the truth.. but here goes.&lt;br /&gt;&lt;br /&gt;In the code for &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt; above, the injected object &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; is not constructed when it's injected into &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;'s constructor. Why? Because the constructor of base classes are called before the constructor of derived classes. In other words, &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;'s constructor is called before &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt;'s, thus, before &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt;'s constructor. Similarly, when &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;'s destructor is called, &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; has already been destroyed. Why? Because &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;'s destructor is called after &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt;'s.&lt;br /&gt;&lt;br /&gt;So what does this mean for us? Is everything we done here useless? Fortunately, it is not that bad. All we need to do is make sure &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; isn't used in the constructor or destructor of &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;. More precisely, we must not in any way touch the memory where &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; will be/was constructed. In fact this is good rule in any case, because constructors shouldn't do any work any way. Constructors should ideally just initialize the class' members. Destructors shouldn't do any work either (except for destroying object, closing sockets, etc., of course).&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Making it testable (my favourite pass-time)&lt;/h4&gt;Let's take a step back before we continue and look at the piece we have. Note that &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; uses &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; which is a concrete class, not an interface. Thus, there is no way to inject an object that does not share any implementation with &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; (e.g., because &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt;'s constructor is called even for derived classes). But this is precisly what we need for testing! Crap! How to solve this? Templates to the rescue!&lt;br /&gt;&lt;br /&gt;I know what you think: what a stupid template-loving masochistic guy. Yeah, you're half-right. I'm a guy. But not a template-loving masochistic one. Just a guy, who actually, do think templates can be put to good use (as in: &lt;span style="font-family: courier new;"&gt;std::vector&lt;/span&gt;). However, templates can also be used in way they weren't designed for (as in: most of Boost). There are better ways of doing so-called "template meta-programming"(affectionately called so by the Boost guys; disgustedly called so by sane people&lt;span style="text-decoration: underline;"&gt;[1]&lt;/span&gt;). The &lt;a href="http://www.digitalmars.com/d/"&gt;D&lt;/a&gt; language is a good example of this. Check out this &lt;a href="http://vimeo.com/4333802"&gt;talk&lt;/a&gt; by the inventor of D, Walter Bright, about how meta-programming should be done.&lt;br /&gt;&lt;br /&gt;Anyway, if we use templates we get:&lt;br /&gt;&lt;pre style="font-family: courier new;"&gt;&lt;code&gt;template&amp;lt;class TFOO&amp;gt;&lt;br /&gt;class NeedsFoo {&lt;br /&gt;TFOO foo;&lt;br /&gt;public:&lt;br /&gt;NeedsFoo(TFOO foo) : foo(foo) { }&lt;br /&gt;// Methods using foo that needs to be tested.&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;and the derived class that manages the injected objects life-time becomes:&lt;br /&gt;&lt;pre style="font-family: courier new;"&gt;&lt;code&gt;class Concrete : public NeedsFoo&amp;lt;Foo*&amp;gt; {&lt;br /&gt;Foo foo;&lt;br /&gt;public:&lt;br /&gt;Concrete() : NeedsFoo&amp;lt;Foo*&amp;gt;(&lt;/code&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;code&gt;foo) { }&lt;br /&gt;};&lt;/code&gt;&lt;/pre&gt;So, now we have a class that contains the code with interesting behavior (&lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;) and a class that contains the construction and life-time management of the injected objects (&lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt;). Furthermore, &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; can be instantiate with any kind of class that &lt;span style="font-style: italic;"&gt;looks like&lt;/span&gt; a &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; class, e.g., a stub implementation of &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt;.  This means that the interesting code can be tested easily because of dependency injection, while still being easy to instantiate and pass around.&lt;br /&gt;&lt;br /&gt;Also, note that the template part of the class is never visible in the production source code. That is, all other parts of the production code uses &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt;, not &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;. Furthermore, the implementation of &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; does not need to be given in the header file as is traditional for template classes. The reason is that we know all possible types &lt;span style="font-family: courier new;"&gt;TFOO&lt;/span&gt; will refer to. Thus, the build-time will no increase significantly using this approach compared to a initial code we had.&lt;br /&gt;&lt;br /&gt;Another important thing to note is that this style of dependency injection is in some ways more powerful than the Java-style dito, because &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; can actually instantiate the template parameter class &lt;span style="font-family: courier new;"&gt;TFOO&lt;/span&gt; itself. Of course, it requires  &lt;span style="font-family: courier new;"&gt;TFOO&lt;/span&gt; to a have a matching constructor, though. In Java, you would need to write a factory to achieve something roughly equivalent.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Pros and Cons&lt;/h4&gt;There are several things to consider with the code we started with and what we ended up with. First of all, the code for &lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt; has become more complex. This is bad, but I would argue that the test-cases for the production code will be much simpler. Thus, the total complexity (of production code + test-cases) is less for the templated code, than for the code we started with. This is good.&lt;br /&gt;&lt;br /&gt;Second, the production code we ended up with is more generic yet maintains a its original simple interface. I say this because the main implementation (&lt;span style="font-family: courier new;"&gt;NeedsFoo&lt;/span&gt;) is now a template, thus, any class that &lt;span style="font-style: italic;"&gt;looks&lt;/span&gt;&lt;span style="font-style: italic;"&gt; like&lt;/span&gt; &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; can be used instead of &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt;. Yet the interface is simple, since the most common case (when &lt;span style="font-family: courier new;"&gt;Foo&lt;/span&gt; is used) is still simple because of the &lt;span style="font-family: courier new;"&gt;Concrete&lt;/span&gt; helper-class. This is good.&lt;br /&gt;&lt;br /&gt;Third, the performance of the templated code should be the same as the original code, because there are no unnecessary virtual calls. Virtual methods are traditionally used for making a class testable, but we managed to avoid those here. Instead we use templates, which means that methods calls can be statically bound to a concrete method implementation. Thus, the compiler can do a better job at optimizing the code. This is good.&lt;br /&gt;&lt;br /&gt;The major drawback with this approach is that the error messages emitted by the compiler usually gets harder to read. This is common for templated code though, since the error messages contain a bunch of references to template and the types of the template parameters (here &lt;span style="font-family: courier new;"&gt;FOO&lt;/span&gt;). For someone who is a bit familiar with the compiler's error messages this should be very hard to figure out what the error messages mean, though. However, compared to a non-templated solution, the error messages are much harder to understand for someone not used to them.&lt;br /&gt;&lt;br /&gt;I recommend you to try this way of doing dependency injection in C++. I'm not saying its the best way of doing under all circumstances, but I am sying its good card to have in your sleeve. A card that may come handy sometime.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 85%;"&gt;[1] The Boost guys have my fullest respect for what they are doing. It is a really impressive project that has helped me more time than I can count. But that doesn't change the fact that what they are doing is insane.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-4777536805175001148?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/4777536805175001148/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=4777536805175001148' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4777536805175001148'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4777536805175001148'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/04/beautiful-dependency-injection-in-c.html' title='Beautiful Dependency Injection in C++'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-913807574302299240</id><published>2010-05-01T13:37:00.000-07:00</published><updated>2010-05-01T13:52:27.606-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fun'/><category scheme='http://www.blogger.com/atom/ns#' term='TED'/><category scheme='http://www.blogger.com/atom/ns#' term='talks'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>Best TED-talk ever</title><content type='html'>&lt;a href="http://www.ted.com"&gt;TED&lt;/a&gt; is an extremely good place to find interesting (short) talks about various topics. You can literally spend days there watching really high quality presentations ranging from musical performances to science talks. Today, though, I saw something that bets every other TED-talks I've ever seen before.&lt;br /&gt;&lt;br /&gt;It was short and witty, it was science and humor, and it was refreshingly different. And best of all, you actually learnt something useful! Stop wasting your time here. Get over &lt;a href="http://www.ted.com/talks/lies_damned_lies_and_statistics_about_tedtalks.html"&gt;there&lt;/a&gt; and see it now!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-913807574302299240?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/913807574302299240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=913807574302299240' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/913807574302299240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/913807574302299240'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/05/best-ted-talk-ever.html' title='Best TED-talk ever'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-352766841486629499</id><published>2010-04-24T01:30:00.000-07:00</published><updated>2010-04-29T06:35:33.130-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lazy evaluation'/><category scheme='http://www.blogger.com/atom/ns#' term='jokes'/><title type='text'>Lambda the Ultimate Joker</title><content type='html'>From &lt;a href="http://lambda-the-ultimate.org/node/3922#comment-59005"&gt;Lambda the Ultimate Weblog&lt;/a&gt;:&lt;blockquote&gt;All people are lazy, but very eager when evaluated.&lt;/blockquote&gt;Funny stuff for all us language nerds. :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-352766841486629499?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/352766841486629499/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=352766841486629499' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/352766841486629499'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/352766841486629499'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/04/lambda-utlimate-joker.html' title='Lambda the Ultimate Joker'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5055038453911394080</id><published>2010-04-18T09:55:00.000-07:00</published><updated>2010-04-18T23:01:52.068-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='DSL'/><category scheme='http://www.blogger.com/atom/ns#' term='memories'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='mistakes'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>Testing generated code</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/c/ce/SWTPC_Function_Generator.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 260px; height: 230px;" src="http://upload.wikimedia.org/wikipedia/commons/c/ce/SWTPC_Function_Generator.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;I started to write this post roughly one and a half years ago. I never finished writing it until now for whatever reason. Here's the post.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;lt;one and a half years ago&amp;gt;&lt;/span&gt;&lt;br /&gt;At work I'm currently devloping a little tool that generates Java code for decoding messages (deeply nested data structures) received over a network. To be more precise, the tool generates code that wraps a third-party library, which     provides generic access to the messages' data structures. Slightly amusing is that the library consists of generated Java code.&lt;br /&gt;&lt;br /&gt;The third-party library is... clumpsy to use, to say the least. Its basic problem is that is so generic/general that it's feels like programming in assembler when using it. Ok, I'm exaggerating a bit, but you get the point: its API is  so general it fits no one.&lt;br /&gt;&lt;br /&gt;There are several reasons for hiding a library like this:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;simplifynig the API such that it fits your purpose;&lt;/li&gt;&lt;li&gt;removing messy boilerplate code that's hard to read, understand, and  test;&lt;/li&gt;&lt;li&gt;less code to write, debug, and maintain;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;introducing logging, range checks, improved exception handling, Javadoc, etc, is cheap;&lt;/li&gt;&lt;li&gt;removing the direct dependency to      the third-party library; and &lt;/li&gt;&lt;li&gt;you  get a warm fuzzy feeling knowing that 3 lines of trivial DSL code corresponds to something like 60-80 lines of messy Java code.&lt;/li&gt;&lt;/ul&gt;I'm developing the code generator using Ruby, which have had its pros and cons. The good thing with Ruby is that is easy to prototype things and  ou can do fairly complex stuff really easy.&lt;br /&gt;&lt;br /&gt;On the bad side is that I reqularly get confused about what types go where. This isn't to much of a problem from a bug point-of-view since test-cases catches the mistakes I make, but it's a bit of an annoyance if you're used to developing Java with Eclipse like I am. The Ruby IDE I'm using (Eclipse RDT) is not nearly as nice as Eclipse JDT, which is natural since  an IDE for a dynamic language has less information available  for refactorings, content assist, etc.&lt;br /&gt;&lt;br /&gt;I've discovered that a functional style is really the way to go when writing code generators, especially when there  are no requirements on performance. This is a nice fit, beacuse Ruby encurage a functional programming style -- or rather, it encurage   me.&lt;br /&gt;&lt;br /&gt;What keeps biting me when it come to code generators is how to test them. Sure, the intermediate steps are fairly easy to test, that is, while things are still objects and not  just a chunk of characters. But how is the output tested?&lt;br /&gt;&lt;br /&gt;Usually I test the output (the generated code) by matching it against a few &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/zen-of-regular-expressions.html"&gt;regual expressions&lt;/a&gt; or similar, but this isn't a very good solutions as the test-cases are hard to read. Also, if an assertion fails the error message isn't very informative (e.g., &lt;i&gt;&amp;lt;true&amp;gt; was not &amp;lt;false&amp;gt;&lt;/i&gt;). Furthermore, test-cases like these are still just an approximation how the code really should look like. For example, I've found no general and simple way of testing that all used classes (and no other classes) are imported. So, it is possible that a bug such as:&lt;br /&gt;&lt;code&gt;import tv.muppets.DanishChef;&lt;/code&gt;&lt;br /&gt;wouldn't be found by my test-cases, even though the   resulting code wouldn't even compile (do'h! The chef's from Sweden not Denmark!).&lt;br /&gt;&lt;br /&gt;Ok, this could be fixed by having some test-cases that actually compiles the output by invoking a Java compiler. Not very beautiful but still possible. Even better, the generated-and-compiled code could be tested with a few hand-written test-cases. These test-cases would test the generated code, alright, but would not document the code at all (since "the code" here means the uby code that genreated the executed Java code). This problem, I'm sad to say, I think is impossible to solve with reasonable effort.&lt;br /&gt;&lt;br /&gt;This approach is good and covers a lot of pitfalls. However, it misses one important aspect: generated documentation. The code generator I wrote generated Javadoc for all the methods and classes, but how should such documentation be tested? The generated documentation is definitely an important part of the output since it's basically the only human-readable part of it (in other words, the generated code that  implements methods are really hard to read and understand).&lt;br /&gt;&lt;br /&gt;Another approach is to simply compared the output with the output from a previous "Golden Run" that is known to be good. The problem here is, of course, to know what is 'good'. Also, when the Golden Run needs to be updated, the entire output of the (potential) new Golden Run has to   be manually inspected to be sure it really is  good/a Golden Run. The good thing with a "Golden Run" is that documentation in the generated code is also tested and not only the generated code.&lt;br /&gt;&lt;br /&gt;The approach I'm using is to have a lot of simple test-cases that exercises the entire  code generator (from input to output). Each of these test-cases verifies a certain aspect of the generated code, for instance:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;number of  methods;&lt;/li&gt;&lt;li&gt;number of public methods;&lt;/li&gt;&lt;li&gt;name of the methods;&lt;/li&gt;&lt;li&gt;a bunch of key code sequences exists  in   method implementations (e.g., &lt;span style="font-family:courier new;"&gt;&lt;span&gt;foo.doIt()&lt;/span&gt;&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;&lt;span&gt;new Bar(beer)&lt;/span&gt;&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;&lt;span&gt;baz.ooka(boom)&lt;/span&gt;&lt;/span&gt;, etc);&lt;/li&gt;&lt;li&gt;for a certain method the code  looks exactly like a certain string (e.g.,&lt;span&gt; &lt;span style="font-family:courier new;"&gt;return (Property) ((SomethingImpl) s).propertyOf(obj);&lt;/span&gt;&lt;/span&gt;);&lt;/li&gt;&lt;li&gt;name of the class;&lt;/li&gt;&lt;li&gt;name of the implemented interfaces;&lt;/li&gt;&lt;li&gt;number of imports;&lt;/li&gt;&lt;li&gt;that used classes are imported;&lt;/li&gt;&lt;li&gt;key frases exist in Javadoc.&lt;/li&gt;&lt;/ul&gt;In addition to these test-cases, there is are test-cases that simply generates a lot of Java classes and compiles them. The test-cases pass if everything compiles. Finially, some of these generated classes are tested using hand-written JUnit test-cases. For me, this approach worked good and I didn't experience any problems (meaning more problem than normally) with testing the generated code. For the generated documentation, however, there were much more bugs. These bugs didn't get fixed either because it wasn't high priority to fix them. Too bad.&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;lt;/one and a half years ago&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Looking back at this story now, I would have done things differently. First of all I would not use Ruby. Creating (internal) DSLs with Ruby is really easy and most of the time turn out really nice. I've tried doing similar things with Python, Java and C++, but the syntax of these languages just isn't as forgiving as Ruby's. However, the &lt;a href="http://www.martinfowler.com/bliki/DomainSpecificLanguage.html"&gt;internal DSL&lt;/a&gt; I created for this tool never used the power of Ruby, so it just be an &lt;span style="font-style: italic;"&gt;external&lt;/span&gt; DSL instead. An external DSL could just as well be implemented in some other language than Ruby.&lt;br /&gt;&lt;br /&gt;Second, I would consider just generating helper methods instead of entire classes and packages of classes. So instead of doing:&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;void setValueFrom(Message m) {&lt;br /&gt;this.value = new GeneratedMessageWrapper(m).fieldA().fieldB().value();&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt; you would do&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;void setValueFrom(Message m) {&lt;br /&gt;   this.value = getValue(m);&lt;br /&gt;}&lt;br /&gt;@MessageDecoder("Message.fieldA.fieldB.value")&lt;br /&gt;int getValue(Message m) {&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;where the code for the  &lt;span style="font-family:courier new;"&gt;getValue&lt;/span&gt; method would be generated and inserted into the file when the class is built. This way, much less code would be needed to be generated, no DSL would be needed (annotations are used instead), and things like name collisions would not be such a big problem as it was (believe it or not).&lt;br /&gt;&lt;br /&gt;At any rate, writing this tool was a really valuable experience for meany reasons that I wouldn't wish to have undone. Considering how good (meaning how much code it saved us from writing) it was a sucess. On the other hand, considering how much time we spent on getting it to work it was a huge failure. As it turns out, this tool has been replaced with a much simpler variant written entierly in Java. Although simpler, it gives the user much more value, though, so its arguable much better then that stuff I wrote 1,5 years ago. My mistake.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5055038453911394080?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5055038453911394080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5055038453911394080' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5055038453911394080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5055038453911394080'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/06/testing-generated-code.html' title='Testing generated code'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5492727746597182808</id><published>2010-03-31T23:38:00.000-07:00</published><updated>2010-05-02T11:26:26.418-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='meta'/><title type='text'>Happy two year!</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/d/dd/Birthday_candles.jpg"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 350px; height: 116px;" src="http://upload.wikimedia.org/wikipedia/commons/d/dd/Birthday_candles.jpg" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;One &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/04/happy-new-year.html"&gt;year ago&lt;/a&gt; this blog celebrated its first birthday. And today, one year later, it celebrates its &lt;span style="font-style: italic;"&gt;second&lt;/span&gt; birthday! Gasp! Oh, arithmitic, you gotta love it.&lt;br /&gt;&lt;br /&gt;During the last year I've written about some &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/09/using-office-as-ide-was-making-word.html"&gt;crazy stuff&lt;/a&gt;, some &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/04/unit-testing-makes-manually-managed.html"&gt;testing&lt;/a&gt; &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/06/missing-level-of-testing-for-software.html"&gt;stuff&lt;/a&gt;, &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/06/give-me-smarter-compiler-how-heres-how.html"&gt;a lot&lt;/a&gt; &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/11/state-of-art-c-compiler-optimization.html"&gt;of&lt;/a&gt; &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/01/your-decompiler-is-my-compiler.html"&gt;compiler&lt;/a&gt; stuff, a few things &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/02/unskilled-and-unaware-of-it.html"&gt;about&lt;/a&gt; &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/03/most-important-skill.html"&gt;learning&lt;/a&gt;, and even a &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/10/alias-norrissudo.html"&gt;joke&lt;/a&gt;!&lt;br /&gt;&lt;br /&gt;I hope some of these posts have been useful for you and that you continue to stop by every now and then to read some of them. Now I'm looking forward to another year of crazy, and (I hope) few useful, ideas and hacks.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5492727746597182808?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5492727746597182808/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5492727746597182808' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5492727746597182808'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5492727746597182808'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/03/happy-two-year.html' title='Happy two year!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6975528968709916783</id><published>2010-03-30T09:41:00.000-07:00</published><updated>2010-03-30T09:51:45.789-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Self'/><category scheme='http://www.blogger.com/atom/ns#' term='lectures'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>Designing (your)Self</title><content type='html'>I just saw a very interesting &lt;a href="http://www.youtube.com/watch?v=3ka4KY7TMTU"&gt;lecture&lt;/a&gt; by Davig Unger. Its main focus is the programming language &lt;span style="font-style:italic;"&gt;Self&lt;/span&gt;, which is a prototyped-based object-oriented language. He also talks about other things: getting the right people to work with you, some of his guiding principles, and even a few comments on getting to know yourself. &lt;br /&gt;&lt;br /&gt;David Ungar received the Dahl-Nygaard Senior Prize in 2009 for his work in the field of object-orientation, most notably for Self. He currenlty works at IBM Research.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6975528968709916783?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6975528968709916783/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6975528968709916783' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6975528968709916783'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6975528968709916783'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/03/designing-yourself.html' title='Designing (your)Self'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6497427517404909458</id><published>2010-03-15T10:32:00.000-07:00</published><updated>2010-03-15T11:01:18.064-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fun'/><category scheme='http://www.blogger.com/atom/ns#' term='commodore'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>Commodore 64 Live!</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/2/23/CommodoreMAX_isolated.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 300px; height: 135px;" src="http://upload.wikimedia.org/wikipedia/commons/2/23/CommodoreMAX_isolated.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Do you remember those old arcade machines where, when no one played them, the "computer" would play the games? Kind of like a demonstration of the game instead showing some fancy graphics. I came across the übergeeky page &lt;a href="http://www.c64-longplays.de/"&gt;C64 Longplays&lt;/a&gt; a few days ago and I feel that I must do my small part to spread the word through the world.&lt;br /&gt;&lt;br /&gt;It's basic like those computer-played arcade machines, with the difference that it's actually someone who plays through the entire game. Didn't you solve the third level of Boulder Dash or did you think no one ever cared to finish Ghostbusters? Everything is on C64 Longplays for you to watch.&lt;br /&gt;&lt;br /&gt;I personally liked the video of Yie Ar Kung Fu... some fights was really close... the energy bar was down to the last pixel!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6497427517404909458?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6497427517404909458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6497427517404909458' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6497427517404909458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6497427517404909458'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/03/do-you-remember-those-old-arcade.html' title='Commodore 64 Live!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1766901956332136196</id><published>2010-03-05T11:51:00.000-08:00</published><updated>2010-08-09T13:04:57.389-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='people'/><category scheme='http://www.blogger.com/atom/ns#' term='skills'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='mail'/><category scheme='http://www.blogger.com/atom/ns#' term='communication'/><title type='text'>The most important skill</title><content type='html'>&lt;a href="http://www.flickr.com/photos/tonythemisfit/2360241678/" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" src="http://farm4.static.flickr.com/3198/2360241678_139770f679.jpg" style="cursor: pointer; float: left; height: 230px; margin: 0pt 10px 10px 0pt; width: 310px;" /&gt;&lt;/a&gt;&lt;br /&gt;As a software developer you need to have large skill set: knowing the right tools, languages, frameworks, platforms, and so on. To complicate things you need to keep every piece of knowledge updated since the field of computing is changing so rapidly. But what is the most important skill among all these? I argue that it's not knowledge of some key technology that make you more than an average programmer, nor is it how many &lt;a href="http://en.wikipedia.org/wiki/Fixed_point_combinator"&gt;fixed point combinators&lt;/a&gt; you dreamt up, how many open-source project you've started, or how fast you type. If non of these, what is the most important skill?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;First let me note that with &lt;span style="font-style: italic;"&gt;developer&lt;/span&gt; I do not mean someone like &lt;a href="http://abstrusegoose.com/strips/alice_and_bob.JPG"&gt;Alice&lt;/a&gt; who write &lt;a href="http://www.commandlinefu.com/"&gt;shell one-liners&lt;/a&gt; for her husband &lt;a href="http://en.wikipedia.org/wiki/Alice_and_Bob"&gt;Bob&lt;/a&gt; to search his photo collection of dogs with hats. I don't mean teenanger Charlie who uses barbed wire and duct tape to hack together scripts to play suitably random mp3s to match his &lt;a href="http://xkcd.com/177/"&gt;parents&lt;/a&gt;' photo slide show from their &lt;a href="http://www.campgonetothedogs.com/html/home.html"&gt;vacation&lt;/a&gt; last summer. What I mean is someone who write code that other developers will maintain, test, read, enhance, etc. In essens I mean developers that work with other developers either directly or through their code.&lt;br /&gt;&lt;br /&gt;So, the important part here is &lt;span style="font-style: italic;"&gt;work with other developers&lt;/span&gt;, which leads me to the heart of this post: the most important skill for a developer is &lt;span style="font-style: italic;"&gt;communication&lt;/span&gt;. Err, I'm sorry, let me correct that: the most important skill for every developer is &lt;span style="font-style: italic;"&gt;effective communication&lt;/span&gt;.  By communication I mean source code and comments, documentation and specifications, power-point presentations and white-board explanations, mail and phone calls, and so on. I could continue, but I think you've got the point.&lt;br /&gt;&lt;br /&gt;A small disclaimer before we continue. I assume that the ones you communicate with &lt;span style="font-style: italic;"&gt;want&lt;/span&gt; to understand what you say or write. If that's not the case then you have an entierly different &lt;a href="http://fvives.free.fr/Calvin/CH940127.JPG"&gt;problem&lt;/a&gt;, and I don't think much in this post can help you. Sorry. Ok, that's that, let's continue.&lt;br /&gt;&lt;br /&gt;I've met many very technically skilled developers that, when I tell them &lt;span style="font-style: italic;"&gt;I don't understand what you mean&lt;/span&gt;, literally repeated what they just said (only louder)&lt;span style="font-style: italic;"&gt;.&lt;/span&gt; Is this good communication? I don't think so. I've also got mails from otherwise good programmers which basically required mind reading ability in order to be understood. Even worse, the mails were sent to 10-50 people, meaning&lt;span style="font-family: trebuchet ms;"&gt; (10 to 50) * (mail length) / (reading speed) * (average speed reduction factor due to lack of mind reading ability)&lt;/span&gt; man-hours was spent reading that mail. Effective communication? No.&lt;br /&gt;&lt;br /&gt;I think there are two kinds of communication: one-way and two-way. Explaning some intricate technical detail to you pair-programming collaegue is a two-way communication since you (should) instantly realize if you collaegue understands you (by her facial expressions and body language). Phone calls are also two-way since the vocal intonation (should) indicate directly to you whether or not your explanation is good enough.&lt;br /&gt;&lt;br /&gt;On the other hand, I consider mail to be a one-way communication (especially if sent to many people) since you don't get any indications when you write the mail if it will be understood or not when received. The same is true for documentation, specifications, code comments and source code. All these kinds of communication is one-way since it takes long time until you get any feedback on if your colleagues understand it or not.&lt;br /&gt;&lt;br /&gt;So, how to become a better developer? How to communicate more effectively? For two-way communication I suggest to practice. For one-way communication I suggest... practice, as well actually. That is. Move along, nothing more to see here. Hit the back-button on you &lt;a href="http://www.microsoft.com/presspass/press/2009/jul09/07-24statement.mspx"&gt;favourite&lt;/a&gt; browser.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/karynsig/3781963038/" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" src="http://farm3.static.flickr.com/2453/3781963038_2b746dd77a.jpg" style="cursor: pointer; float: left; height: 165px; margin: 0pt 10px 10px 0pt; width: 250px;" /&gt;&lt;/a&gt;Just kidding. Well, kind of any way. I do think that practice is the one thing will make any noticable difference, so my suggestion is to try to explain whatever technical work you're currently doing to your aunt Mathilda who lives in that small red cottage and knits &lt;a href="http://whipup.net/wp-content/images/hat_dog_flickr_macky%5C%27shat.jpg"&gt;hats&lt;/a&gt; all day long. Oh, it's impossible you say? Your aunt has never heard of numerical stability? She thinks &lt;a href="http://en.wikipedia.org/wiki/Monte_Carlo_method"&gt;monte carlo&lt;/a&gt; simulations has something to do with &lt;span style="font-style: italic;"&gt;SimCity?&lt;/span&gt; That's doesn't mean you can't make her understand the ideas behind your work. It only means that you have to try harder to explain and simplify it &lt;span style="font-style: italic;"&gt;using her language and concepts&lt;/span&gt;! Remember that the whole idea with this exercise is to make &lt;span style="font-style: italic;"&gt;you&lt;/span&gt; better at explaning to other people, not make your aunt capable of challenging the next Turing award winner. To be blunt, she is just an exercise tool. A excerise tool that makes extremely good blueberry pies,  though. Yum.&lt;br /&gt;&lt;br /&gt;In fact, you could do this exercise by explaning anything. For instance, describe the story of the last book you read, a &lt;a href="http://blogg.aftonbladet.se/11171"&gt;news&lt;/a&gt; article, or the story behind the &lt;a href="http://activerain.com/image_store/uploads/3/6/2/0/4/ar117587968640263.jpg"&gt;best photo&lt;/a&gt; you ever took. But remember, you're not just telling the story, you're &lt;span style="font-style: italic;"&gt;explaning what happend&lt;/span&gt;. You should "listen" (with your ears and eyes) to your audience to make sure they understand every word you say.&lt;br /&gt;&lt;br /&gt;Some simple concrete tips:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Be clear about the background. Just because you remember the context, does not mean every one else do. Start the explanation by giving the relevant context. If you have discussed it before with the audience, refer to that. If you haven't, start by giving a hand-waving overview of the problem.&lt;/li&gt;&lt;li&gt;Use you hands. If you talk about many different objects, place them on different places in the air and "hold" the object you talk about by making a fist on that spot. Much less confusing for those who listen to you. Also, if you need to move your arms around to refer to different objects, you will probably speak more slowly which is also makes it easier to take in everything you say.&lt;/li&gt;&lt;li&gt;Pop the stack explicitly. Often in a discussion you need to get back to something you talked about earlier. In that case, say "as I said before" before you start talking about it to indicate to your audience that you pop the conversation stack. If you just change the subject people could get a bit confused and miss some parts of your explanation.&lt;/li&gt;&lt;li&gt;Be utterly clear when something is important. Say "this is the key point", or "this is important".  Use short sentences. Repeat yourself. Use short sentences and repeat yourself. Make a pause to let your audience take in what you just said and let them think about it. Use some uncommon phrase or a silly name when explaning this key point that you can use later on to refer to it.&lt;/li&gt;&lt;li&gt;Speak clearly or stay quiet. If you don't know what to say, don't mumble something incomprehensible. Just shut up. Scratch your head a bit if you feel akward to let the audience know that you're thinking about what to say. People won't mind anyway, unless you do it twice every minute. They will probably thank you for the moment of mental relief. In fact, you can make pauses like this work for your advantage. (Read the bullet above one more time if you don't understand why. (You still don't get it? I'm refering to the 6th sentence, silly.))&lt;/li&gt;&lt;/ul&gt;That it for this post. But what about the one-way communications? How to improve those? I'll get back to that in my next post on this subject. Until then, if you have any other tips please let me know by leaving a comment!&lt;br /&gt;&lt;br /&gt;And oh, before I forget, say hello to aunt Mathilda for me!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1766901956332136196?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1766901956332136196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1766901956332136196' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1766901956332136196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1766901956332136196'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/03/most-important-skill.html' title='The most important skill'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://farm4.static.flickr.com/3198/2360241678_139770f679_t.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1805608978268057035</id><published>2010-03-05T08:33:00.000-08:00</published><updated>2010-03-05T08:37:00.713-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fun'/><category scheme='http://www.blogger.com/atom/ns#' term='jokes'/><title type='text'>How browsers work</title><content type='html'>A former colleague sent me this very sweet &lt;a href="http://pics.kuvaton.com/kuvei/how_browsers_work.jpg"&gt;explanation&lt;/a&gt; of how browsers work today. Can you say &lt;span style="font-style: italic;"&gt;awww&lt;/span&gt;?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1805608978268057035?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1805608978268057035/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1805608978268057035' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1805608978268057035'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1805608978268057035'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/03/how-browsers-work.html' title='How browsers work'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1015716379309554715</id><published>2010-02-25T09:20:00.000-08:00</published><updated>2010-02-25T21:47:45.673-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='lectures'/><category scheme='http://www.blogger.com/atom/ns#' term='DSL'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>Talks by Herb Sutter</title><content type='html'>I wish to recommend two very good talk by Herb Sutter; &lt;a href="http://video.google.com/videoplay?docid=-4714369049736584770#"&gt;Machine Architecture: Things Your Programming Language Never Told You&lt;/a&gt; and &lt;a href="http://video.google.com/videoplay?docid=-4714369049736584770#docid=7625918717318948700"&gt;Concur ad C++ Futures&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1015716379309554715?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1015716379309554715/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1015716379309554715' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1015716379309554715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1015716379309554715'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/02/i-wish-to-recommend-two-very-good-talk.html' title='Talks by Herb Sutter'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-4672833656977969214</id><published>2010-02-19T07:21:00.000-08:00</published><updated>2010-03-06T03:08:45.701-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='people'/><category scheme='http://www.blogger.com/atom/ns#' term='skills'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>Unskilled and unaware of it</title><content type='html'>I just read the &lt;a href="http://successfulsoftware.net/2010/02/16/unskilled-and-unaware-of-it/"&gt;Unskilled and unaware of it&lt;/a&gt; post at &lt;span style="font-style: italic;"&gt;Successful Software&lt;/span&gt;. It's a good post that (should) make you think about how good you consider yourself in various fields such as software development and driving.&lt;br /&gt;&lt;br /&gt;I find it a bit funny that there are some skills that you probably don't overestimate your own skill level. Have you ever heard of someone saying he/she is a great gardener when in fact every flower they touch die?&lt;br /&gt;&lt;br /&gt;Update: See &lt;a href="http://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect"&gt;Dunning-Kruger effect&lt;/a&gt; on Wikipedia.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-4672833656977969214?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/4672833656977969214/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=4672833656977969214' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4672833656977969214'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4672833656977969214'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/02/unskilled-and-unaware-of-it.html' title='Unskilled and unaware of it'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5229844030450582963</id><published>2010-01-25T10:33:00.000-08:00</published><updated>2010-04-26T09:22:58.769-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='pair-programming'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy stuff'/><category scheme='http://www.blogger.com/atom/ns#' term='memory management'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='stack'/><title type='text'>Detecting function inlining</title><content type='html'>By definition, compiler optimizations should only affect the performance of the application. The behavior of the optimized code should be the same as the non-optimized code. If this is the case, how is it possible to detect whether a function has been inlined or not? Dear reader, read on (that's what readers do, right?).&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Before we continue, let's take a step back and ask ourselves why it would ever be useful to detect function inlining. Let's take an example. Imagine the following code is part of a large application developed at company where we pair program. The function &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;createObject()&lt;/span&gt; allocates an object on the stack (that is, it's a local variable) and returns a pointer to it:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;int function() {&lt;br /&gt;Object* o = createObject();&lt;br /&gt;return o-&gt;field;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;although it's bad practice to return a pointer to a local variable, &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;function&lt;/span&gt; works and does precisely what you'd expect: it creates an object and returns one of its fields. As developers with good taste, we decide to clean up the code a bit by introducing a getter for &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;field&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;int function() {&lt;br /&gt;Object* o = createObject();&lt;br /&gt;return o-&gt;getField();&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;We compile the code and run the tests. There is a green light. Everything is good. We commit the code to the repository. &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Not so fast! After a while the automated build system indicates a whole lot of failing tests. What happend? &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here's what happend: since we introduced a function call to &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;getField&lt;/span&gt; we changed how the stack looks when reading the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;field&lt;/span&gt; field. And since the object is stack allocated, the object is overwritten by the call to &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;getField&lt;/span&gt;. Good team players as we are, we do not want to have failing builds for long, so we revert our last check-in and start to inspect the problem closer.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We quickly determine that the only failing builds were the non-release builds, that is, those builds that failed were those that did not optimize the code. We stare into the air and ask ourselves... why?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;At this point one of us remembers a &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/01/detecting-function-inlining.html"&gt;blog&lt;/a&gt; he had read the night before about function inlining. He explains "if the call to &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;createObject&lt;/span&gt; is inlined into &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;function&lt;/span&gt;, then the object is allocated as a local variable in &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;function&lt;/span&gt; instead of in &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;createObject&lt;/span&gt;. This means that when &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;getField&lt;/span&gt; is called, the object is not overwritten. It's not overwritten because its allocated in a different place on the stack. In a different &lt;a href="http://en.wikipedia.org/wiki/Call_stack"&gt;stack frame&lt;/a&gt;, to more precise."&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The other developer thinks about this for a few seconds and replies "ok, then let's just allocate the object on the heap instead of the stack". Before the blog-reading developer can say "memory leak" he adds "and, of course, delete it when we're done with it".&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After a few minutes of coding and half an hour of waiting for the compiler to finish, we have a working solution that works on both optimized builds and on non-optimized builds. We commit the code and leave for the day.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;When we get back the morning after, we have an angry mail in out inbox. Apparently our little fix made the performance of the application degrade horrendously -- we had placed a call to &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;new&lt;/span&gt; inside an extremely tight loop. Doh! Stupid us. A better solution is needed. In order to understand the problem properly the pair programmers summarize:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;For optimized builds the function is inlined, thus stack allocation work fine. With stack allocation the performance of the application is acceptable.&lt;/li&gt;&lt;li&gt;On non-optimized builds the function is not inlined, thus stack allocation does not work and we have to resort to heap allocation. However, heap allocation is not acceptable for performance. &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;When the problem is so clearly stated, the blog-reading developer exclaims "let's allocate on the stack if the function is inlined, and allocate on the heap otherwise!"&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ok, now let's leave those two developers, I think they can handle it by themselves now. I'm now going to explain how to do the trick the blog-reading developer suggested.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For every function that is called there is a stack frame which contains the function's local variables. Every stack frame has an address, which can be accessed via a register called &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;EBP&lt;/span&gt; (extended base pointer, I think). This register can be read using the following code:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;void* stack_frame() {&lt;br /&gt;register void* ebp asm("ebp");&lt;br /&gt;return ebp;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;This function returns the pointer to the stack frame that is created when entering the function. So, as you can see, it's really simple to get the pointer to the current stack frame. Let's make another function and provide it with the pointer the stack frame of its caller:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;bool is_inlined(void* callers_ebp) {&lt;br /&gt;register void* my_ebp asm("ebp");&lt;br /&gt;return my_ebp == callers_ebp;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;This function is kind of magic because it will return true if its inlined and false otherwise. Using this we can write a function that allocates on the stack if the function is inlined, and allocates on the heap otherwise:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;Object* createObject(void* callers_ebp) {&lt;br /&gt;register void* my_ebp asm("ebp");&lt;br /&gt;if (my_ebp == callers_ebp)&lt;br /&gt;return &amp;amp;Object();&lt;br /&gt;else&lt;br /&gt;return new Object();&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Pretty sweet.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Note that the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;EBP&lt;/span&gt; register is not used when compiling with &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;-O2&lt;/span&gt; or &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;-O3&lt;/span&gt; unless you use &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;-fno-omit-frame-pointer&lt;/span&gt;. It should be possible to do the same trick without &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;EPB&lt;/span&gt; by using the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;ESP&lt;/span&gt; register instead, but I haven't tested that.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;(Disclaimer: I'm not suggesting that this is a good way for solving the problem the two developers in the story faced. What I do say it that in those rare circumstances when the behavior of a function need to change when its inlined, then this is one way to do it.)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5229844030450582963?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5229844030450582963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5229844030450582963' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5229844030450582963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5229844030450582963'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/detecting-function-inlining.html' title='Detecting function inlining'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5455393004810161798</id><published>2010-01-24T13:11:00.000-08:00</published><updated>2010-02-24T04:01:16.783-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy stuff'/><category scheme='http://www.blogger.com/atom/ns#' term='memory management'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><title type='text'>Faster and optimization friendly: speculative stack allocation</title><content type='html'>I've have had an idea for a memory allocation algorithm for some time. It's based on the idea behind &lt;a href="http://en.wikipedia.org/wiki/Escape_analysis"&gt;escape analysis&lt;/a&gt;. Escape analysis analyzes the source code and determines the dynamic scope of an allocated object. For example, in the following Java code:&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;void printNumber(int i) {&lt;br /&gt;System.out.print(new Formatter(i).hex());&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;escape analysis can help improve performance by determining that the instance of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Formatter&lt;/span&gt; cannot escape the &lt;a href="http://en.wikipedia.org/wiki/Call_stack"&gt;stack frame&lt;/a&gt; it was allocated in. Thus, the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Formatter&lt;/span&gt; instance can be allocated on the stack instead, which (greatly) improves performance. Since Java 6u14 there has been &lt;a href="http://java.dzone.com/articles/escape-analysis-java-6-update"&gt;experimental escape analysis&lt;/a&gt; in the JVM.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For languages such as C++, which rely on static compilation and optimization, escape analysis make it possible to do even more optimization. Consider the following C++ code:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;pre&gt;int two() {&lt;br /&gt;Cons second(2, NULL);&lt;br /&gt;Cons first(1, &amp;amp;first);&lt;br /&gt;return first.cdr()-&gt;car();&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;which creates a linked list of cons cells &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;(1 (2 NULL))&lt;/span&gt; and returns the first value of the second cons cell. That is, the function returns 2. GCC (and most other compilers as well (I will use the name &lt;i&gt;GCC &lt;/i&gt;in this post meaning &lt;i&gt;your favourite C++ compiler&lt;/i&gt;)) will gladly optimize this to simply return 2, it doesn't even bother to create the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Cons&lt;/span&gt; objects at all (unless there are some kind of side-effect in the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Cons&lt;/span&gt; constructor, like printing). Furthermore, since GCC now knows that this function simply returns 2, GCC can replace all calls to &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;two&lt;/span&gt; with the value 2. Thus, the overall performance will increase even more.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;However, what if we cannot allocate the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Cons&lt;/span&gt; cell on the stack... what if we must allocate on the heap. What kind of code will GCC generate then? I'll spare you the list of assembler code here, but let me tell you it isn't pretty. Its a far cry from the assembler equivalent of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;return 2;&lt;/span&gt;. Also, allocating on the heap is an extremely costly operation in comparison to stack allocation. So, not only is heap allocation expensive, it also stops GCC from performing any kind of smart optimizations. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So what has escape analysis to do with the idea I have for memory allocation? The idea is to perform escape analysis at run-time instead of at compile-time. That is, determine at run-time if an object is reachable from outside its stack frame; and if it is move it to the heap.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This boils down to insert checks at certain places in the source code, e.g., when returning a pointer from a function. These check determines if the object (which is returned) will be reachable from a stack frame with longer lifespan than the stack frame in which the object was allocated.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now, &lt;i&gt;inserting checks at certain places&lt;/i&gt; may not sound like a very good idea, but the application I'm thinking of for this right now is not manually written code; it's &lt;a href="http://programmaticallyspeaking.blogspot.com/search/label/code%20generation"&gt;generated code&lt;/a&gt;. A proper implementation of this idea would only (explicitly) allocate object on the stack and then (implicitly) move object to the heap when needed. Thus the code for allocating an object always looks the same; it does not depend on the lifetime of the allocated object. Code that look the same is easier to generate, so this is a good thing for code generators. Furthermore, extra checks that I mentioned above should be easily generated by a code generator since those checks also always look the same. Pretty neat.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Another thing that is important to note is that the algorithm uses data that is only available at run-time when deciding if an object should be moved to the heap or not. This means that this algorithm is independent on the complexity of the code or knowledge of data. On the other hand, normal (static) escape analysis may fail if the code is very complex, e.g., a lot of execution paths that depends on statically unknown data.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;There is one more thing with this approach that is important: it plays well with GCCs optimizations. Since every object is stack allocated (until the run-time system notices that it needs to be moved to the heap), optimization like the one discussed above for function &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;two&lt;/span&gt; can be done by GCC.  That's pretty neat as well.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I've implemented a prototype of this idea &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/spy/alloc.cc"&gt;here&lt;/a&gt;. It's not at all well-tested and I haven't tried it on any other machine than my laptop, so there are probably a lot of problems with it. The idea, however, I think is not entirely insane and could be useful.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-5455393004810161798?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/5455393004810161798/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=5455393004810161798' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5455393004810161798'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/5455393004810161798'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/faster-and-optimization-friendly.html' title='Faster and optimization friendly: speculative stack allocation'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2180995446253700421</id><published>2010-01-15T09:58:00.000-08:00</published><updated>2010-05-16T06:32:29.831-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><title type='text'>Your decompiler is my compiler</title><content type='html'>Last night while brushing my teeth I thought about a recent project I've worked with, namely compiling Java to native code. The basic idea is the same as my other project &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/01/statically-typed-and-compiled-python.html"&gt;compiling python statically&lt;/a&gt;: translate the byte code into C/C++ and then use any C/C++ compiler to compile it to native code.&lt;br /&gt;&lt;br /&gt;There is a neat tool called &lt;span style="font-family:courier new;"&gt;javap&lt;/span&gt; that prints a bunch of interesting stuff of a Java class: the byte code of all its methods, and the type of its fields, among other things. I simply translated the sequence of byte code into something gcc could compile, and I got a primitive Java compiler that compiles to native code. Well, right now it only supports a small subset of Java... and it probably will stay that way too. :) I have a good habit of starting many project and a bad habit of never finishing them.&lt;br /&gt;&lt;br /&gt;As I've discussed &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/05/java-compatible-syntax-for-c.html"&gt;before&lt;/a&gt; Java and C++ are textually quite similar, for instance is the following code Java or C++?&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if (var &gt;= 0) return new ArrayList&amp;lt;String&amp;gt;&lt;string&gt;(“hello”);&lt;/string&gt;&lt;/span&gt;&lt;br /&gt;(I intended it to be Java, but the only reason for it to be more Java-ish than C++-ish is the name of the types: &lt;span style="font-family:courier new;"&gt;ArrayList&lt;/span&gt; instead of &lt;span style="font-family:courier new;"&gt;list&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;String&lt;/span&gt; instead of &lt;span style="font-family:courier new;"&gt;string&lt;/span&gt;.)&lt;br /&gt;As you probably know, there are several decompilers for Java. Just google it and you'll find some. What these decompiler do is to print the Java code that behave exactly as the original byte code. No surprise there; that's what decompilers do!&lt;br /&gt;&lt;br /&gt;Since Java and C++ are such similar languages textually, much more similar than Java and C or Java and Ada, its pretty straight-forward to turn a Java decompiler into a bytecode-to-C++ compiler.  What I just said may sound like it turns the decompiler inside-out, but the only difference are (comparatively) small changes to the textual output.&lt;br /&gt;&lt;br /&gt;Of course, such compiler will not support any of Java's good stuff, such as reflection, but that's not the point here. The point is that it's possible to make a compiler for Java by hacking an existing decompiler, and doing so is fairly easy because of the reuse of a C++ compiler.&lt;br /&gt;&lt;br /&gt;Total awesomeness.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2180995446253700421?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2180995446253700421/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2180995446253700421' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2180995446253700421'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2180995446253700421'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/your-decompiler-is-my-compiler.html' title='Your decompiler is my compiler'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8811727839997847223</id><published>2010-01-06T12:04:00.000-08:00</published><updated>2010-01-06T12:38:03.960-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='Static Python'/><title type='text'>Print sucks, tuples rocks: disassembling Python</title><content type='html'>I &lt;a href="http://programmaticallyspeaking.blogspot.com/2010/01/statically-typed-and-compiled-python.html"&gt;recently&lt;/a&gt; worked on compiling Python by translating it to C++. I did this using the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;dis&lt;/span&gt; module, which prints the bytecode of a Python function. Note the word &lt;i&gt;prints&lt;/i&gt;. There is no (simple) way of getting the bytecode of a function in a list or similar; just having it printed to the console.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;At first I parsed the textual output that &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;dis&lt;/span&gt; gives, but this turned out to not work for more complex stuff. Basically, the textual output did not contain all the information I needed (printing &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;repr(obj)&lt;/span&gt; throws away information for some values). What to do?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Well, luckily Python is distributed with the source code of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;dis&lt;/span&gt;, so it was easy to create a copy of the function that disassembles Python functions into bytecode and rewrite it a bit. Essentially, instead of calling print, everything is stuffed into a list. Thus, you'll get a list of tuples instead of a textual output to the screen. Each tuple holds the source code line, the byte code line, the opcode and the argument to the opcode, making it easy to access them programmatically. If you would like to see the source look &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/spy/spy.py?spec=svn249&amp;amp;r=249"&gt;here&lt;/a&gt; (the first 60 lines).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;More importantly though, this patch does not just makes the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;dis&lt;/span&gt; module a bit easier to use. As I said earlier, the textual output of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;dis&lt;/span&gt; throws away certain important information. Howerver, this patch retains the actual values instead of converting them to strings using &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;repr&lt;/span&gt;. This is really important for me because it makes it possible to translate certain code which otherwise would have been impossible. More precisely, it makes it possible to translate lambdas, list comprehensions, etc, from Python to C++. The reason is that instead of getting &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;repr(function_object)&lt;/span&gt; I now get &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;function_object&lt;/span&gt; itself. Awesomeness!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Oh, did I mention that Python rocks?&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8811727839997847223?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8811727839997847223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8811727839997847223' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8811727839997847223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8811727839997847223'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/print-sucks-tuples-rocks-disassembling.html' title='Print sucks, tuples rocks: disassembling Python'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6497238179696163758</id><published>2010-01-06T09:19:00.000-08:00</published><updated>2010-02-19T07:31:34.326-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fun'/><category scheme='http://www.blogger.com/atom/ns#' term='jokes'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>Of banks and doorbells</title><content type='html'>This is an amusing kafkaesque &lt;a href="http://neil.fraser.name/news/2009/04/01/"&gt;story&lt;/a&gt; form real life by Niel Fraser at Google.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6497238179696163758?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6497238179696163758/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6497238179696163758' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6497238179696163758'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6497238179696163758'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/of-banks-and-doorbells.html' title='Of banks and doorbells'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8226207101616645876</id><published>2010-01-05T07:59:00.000-08:00</published><updated>2010-01-05T08:10:56.650-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft'/><title type='text'>History of Microsoft</title><content type='html'>I just saw a few episodes of web show &lt;a href="http://channel9.msdn.com/shows/History/"&gt;The History of Microsoft&lt;/a&gt; on Channel 9, which I thought was quite interesting. Channel 9 is web site by Microsoft so I assume that the show focuses on the good parts, but it's still a good watch.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8226207101616645876?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8226207101616645876/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8226207101616645876' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8226207101616645876'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8226207101616645876'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/history-of-microsoft.html' title='History of Microsoft'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2106053333701443678</id><published>2010-01-03T04:37:00.000-08:00</published><updated>2010-01-03T08:56:10.915-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='parser'/><category scheme='http://www.blogger.com/atom/ns#' term='Fit'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><category scheme='http://www.blogger.com/atom/ns#' term='Static Python'/><title type='text'>Statically typed and compiled Python</title><content type='html'>It was a while since I wrote something here. In spring last year, that is 2009, I started on a really fun project: writing a compiler for a language I invented myself (previously mentioned &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/08/code-generation-c-vs-java.html"&gt;here&lt;/a&gt;). Ok, the "compiler" only compiled to C++-code, but still...&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The language is called &lt;i&gt;Fit&lt;/i&gt; and is a compiled (of course :)) statically-typed &lt;a href="http://en.wikipedia.org/wiki/Prototype-based_programming"&gt;prototype-based&lt;/a&gt; language. It's heavily inspired by Java and Ruby, has first-class functions, &lt;a href="http://en.wikipedia.org/wiki/Generator_(computer_science)"&gt;generators&lt;/a&gt;, list literals, and &lt;a href="http://en.wikipedia.org/wiki/String_literal#Variable_interpolation"&gt;string interpolation&lt;/a&gt;, among other things. All these things aren't supported completely by the compiler; there is only a rudimentary (by which I mean &lt;i&gt;buggy&lt;/i&gt;) implementation. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;I worked alot on this project. Many late nights. Four hours of sleep before getting up to work wasn't unusual. Naturally I got really tired and when the compiler was able to compile a decent subset of the language I originally imagined, I stopped working on it. It just wasn't fun anymore.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I learnt alot from this project. Writing parsers, writing efficient C++ code,  optimization tricks, how to implement closures, and... oh yeah, I learnt Python. Why? The compiler is written in Python.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here's the funny part: after working on Fit and its compiler for a while, I realized that the language I was trying to create essentially was a statically typed Python with braces. And after writing Python for a few weeks I was ready to throw away the braces. What's left is a statically typed Python (take a look at &lt;a href="http://boo.codehaus.org/"&gt;Boo&lt;/a&gt; if this kind of language sounds interesting). The rest of this post is about creating a compiler for a statically typed Python with minimal effort.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I actually tried writing a parser for such a language, but I failed miserably so I dropped the idea. However, I have recently realized that it is not necessary to write a parser to create a statically typed Python. Here's why: in Python 3 it is possible to &lt;a href="http://www.python.org/dev/peps/pep-3107/"&gt;annotate&lt;/a&gt; functions to indicate the types of the arguments. Example:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;def plus(i: int, j: int) -&gt; int:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__return i + j&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;These annotations provide the type information needed to convert Python to C++ code, which then can be statically type-checked and compiled. The first step for this is to get the signature for the C++ function. The following code returns the C++ function signature corresponding to a given Python function:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;def signature(func):&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;__import inspect&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;__spec = inspect.getfullargspec(func)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;__c_args = [spec.annotations[arg].__name__ + " " + arg for arg in spec.args]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;__return_type = spec.annotations['return'].__name__ &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;__return return_type + " " + func.__name__ + "(" + ", ".join(c_args) + ")" &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt; &lt;/div&gt;&lt;div&gt;For example, calling &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;signature(plus)&lt;/span&gt; will result in the string &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;int plus(int i, int j)&lt;/span&gt;, which of course is the signature of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;plus&lt;/span&gt; in C-speak. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ok, so how is this useful you may wonder. We need to convert the body of a function not just its signature. Well, here's where another great Python module comes into action, namely &lt;a href="http://docs.python.org/library/dis.html"&gt;dis&lt;/a&gt;, which returns the &lt;a href="http://en.wikipedia.org/wiki/Bytecode"&gt;bytecode&lt;/a&gt; of a &lt;a href="http://www.python.org/doc/2.5.2/lib/bytecodes.html"&gt;Python&lt;/a&gt; function. For example, when called with &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;plus&lt;/span&gt; as argument, &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;dis&lt;/span&gt; get us:&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;  1           0 LOAD_FAST                0 (i) &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;              3 LOAD_FAST                1 (j) &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;              6 BINARY_ADD           &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;              7 RETURN_VALUE    &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I won't go into details here, but it fairly easy to translate this bytecode sequence into C++ code. I've written a translator that supports only a few bytecodes (LOAD_FAST, LOAD_CONST, STORE_FAST, BINARY_ADD, BINARY_MULTIPLY, and RETURN_VALUE) and with limited support for types. On the other hand, I only spent a few hours on it. That's the "minimal effort" I was talking about earlier. :) The following Python code is small example of what the translator currently supports:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;def testing(i: int, j: str) -&gt; int:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__text = "hello"&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__dog = 9.9&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__donald = i&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__goofy = dog&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__donald = donald + 100 &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__daisy = donald * 4 + donald&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__return daisy * 223&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And this is the corresponding C++ code:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;int testing(int i, str j) {&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__str text = "hello";&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__float dog = 9.9;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__int donald = i;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__float goofy = dog;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__donald = (donald + 100);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__int daisy = ((donald * 4) + donald);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;__return (daisy * 223);&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As you can see in this example, the C++ code matches the Python code quite well. Also, note that local variables are declared and typed automatically. Awesome! The best part is that the whole translator is only 100 lines of Python! Of course it doesn't support all bytecodes and does not have proper &lt;a href="http://en.wikipedia.org/wiki/Type_inference"&gt;type inference&lt;/a&gt; but it definitely works as a proof-of-concept. As I said, I only spent a few hours on the translator.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So, assuming this translator ever gets finished, what could it be used for? Anything, I would say. As a general purpose compiled language just like C++ or Java (if you consider that compiled), or as a replacement for normal Python when performance or memory consumption is an issue. I also find it really neat that you have an interpreted language and a compiled language in one; the same language can be used to hack together a small prototype and to write a large well-specified application. In other words, you can stay in the same language and simply "refactor" your hacky prototype into a not-so-hacky application. I find this a very attractive idea.&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2106053333701443678?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2106053333701443678/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2106053333701443678' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2106053333701443678'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2106053333701443678'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2010/01/statically-typed-and-compiled-python.html' title='Statically typed and compiled Python'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7496449786154015138</id><published>2009-11-09T12:25:00.000-08:00</published><updated>2009-11-09T12:35:47.476-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='C'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><title type='text'>State of the art C compiler optimization tricks</title><content type='html'>I found this via Lambda the Ultimate: &lt;a href="http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf"&gt;State of the art C compiler optimization tricks&lt;/a&gt;. It's a good read, though I would really like to see the presentation rather than just reading the slides. Also, is this really 'state of the art'? Sure, some of the optimizations the compiler does is pretty impressive, but some are... well... not. Is &lt;a href="http://en.wikipedia.org/wiki/Common_subexpression_elimination"&gt;common subexpression elimination&lt;/a&gt; really state of the art? Seems like 1970-ish to me...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7496449786154015138?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7496449786154015138/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7496449786154015138' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7496449786154015138'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7496449786154015138'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/11/state-of-art-c-compiler-optimization.html' title='State of the art C compiler optimization tricks'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1913056257205422000</id><published>2009-10-29T11:53:00.001-07:00</published><updated>2009-10-29T11:53:46.898-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='jokes'/><title type='text'>alias norris=sudo</title><content type='html'>Joke of the day. :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1913056257205422000?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1913056257205422000/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1913056257205422000' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1913056257205422000'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1913056257205422000'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/10/alias-norrissudo.html' title='alias norris=sudo'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1375408938127345847</id><published>2009-10-22T09:51:00.000-07:00</published><updated>2009-10-22T09:55:50.559-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lectures'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><title type='text'>Lecture from MIT: Cache oblivious algorithm</title><content type='html'>I just watched a lecture about cache oblivious algorithm from MIT's course &lt;span style="font-style:italic;"&gt;Introduction to Algorithms&lt;/span&gt;. It's really nice that things like this is free on the net. I found it via &lt;a href=" http://www.catonmat.net/blog/mit-introduction-to-algorithms-part-fourteen/"&gt;Good coders code, great reuse&lt;/a&gt;. Check it out!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1375408938127345847?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1375408938127345847/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1375408938127345847' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1375408938127345847'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1375408938127345847'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/10/lecture-from-mit-cache-oblivious.html' title='Lecture from MIT: Cache oblivious algorithm'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-521466483921076349</id><published>2009-08-30T05:13:00.000-07:00</published><updated>2009-08-30T06:01:46.175-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='Fit'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>Code generation: C++ vs. Java</title><content type='html'>Code generation can be very useful by reducing the amount of code that is needed to be written, tested, debugged, maintained, etc. But if you develop the code generation yourself, code generation can be a nightmare. Getting the code generator right can be very hard, especialley so if the generator needs to support more than one target language (the output language).&lt;br /&gt;&lt;br /&gt;The last code generator I wrote was for a private project. I've always wanted to design and implement my own programming language, and this spring I finally started to implement a compiler (actually it was source-to-source translator that outputted C++ code) for my language, which I call &lt;span style="font-style:italic;"&gt;Fit&lt;/span&gt;. This was the first translator/code generator I've written that generates C++ code.&lt;br /&gt;&lt;br /&gt;I'm not a big fan of C++, I think it's a too complex language that forces you to think about low-level implementation details. Thus, since my brain is limited, I have less brain-cycles to think about the high-level design of the application.&lt;br /&gt;&lt;br /&gt;So why did I choose to translate &lt;span style="font-style:italic;"&gt;Fit&lt;/span&gt; to C++ if C++ is so hard? Well, my other alternaive was Java, but since I already had written a couple of generator for Java I thought that a generator for C++ would be a fun experience.&lt;br /&gt;&lt;br /&gt;Actually it was a really nice experience! Having access to all the low level details (e.g., pointer arithmitics and goto) and the high-level constructs (e.g., operator overloading) made it relatively easy to translate &lt;span style="font-style:italic;"&gt;Fit&lt;/span&gt; to C++. When implementing the Fit-to-C++ translator I really saw the difference in power between C++ and Java. &lt;br /&gt;&lt;br /&gt;Java, although a good language, is not a powerful language. In Java you can do certain very easy, but as soon you need to do things the language wasn't designed for you will have a bad experience. Ever tried to write bit manipulations in Java? It's not fun I tell you. In C++ at least you have the option to implement it using operator overloading and templates to make is less of a burden. I Java, that option is not available.&lt;br /&gt;&lt;br /&gt;To take a concrete example from the &lt;span style="font-style:italic;"&gt;Fit&lt;/span&gt; compiler/translator: &lt;span style="font-style:italic;"&gt;yield&lt;/span&gt; is translated to (efficient) C++ using a &lt;span style="font-style:italic;"&gt;switch&lt;/span&gt; and a &lt;span style="font-style:italic;"&gt;goto&lt;/span&gt;. I'm not sure how to implement it in Java, but I guess you would have to do some work-around using a &lt;span style="font-style:italic;"&gt;do-while&lt;/span&gt; construct.&lt;br /&gt;&lt;br /&gt;So what am I'm saying? Is C++ a good language? Well, no it's not good if a human being writes it, but it is actually good if a code generator writes it. All the low-level stuff actually simplifies the generator.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-521466483921076349?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/521466483921076349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=521466483921076349' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/521466483921076349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/521466483921076349'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/08/code-generation-c-vs-java.html' title='Code generation: C++ vs. Java'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1763674547135509643</id><published>2009-08-13T08:36:00.000-07:00</published><updated>2009-08-13T08:44:02.798-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>Everything you ever need to know about concurrent programming</title><content type='html'>I usually don't link to other blogs without adding some of my thought on the topic, but this time I make an exception. &lt;a href="http://en.wikipedia.org/wiki/Herb_Sutter"&gt;Herb Sutter&lt;/a&gt; has written 25+ articles in Dr. Dobb's Journal, all about different aspects of cuncurrent programming. &lt;a href="http://herbsutter.wordpress.com/2009/08/11/effective-concurrency-design-for-manycore-systems/"&gt;Check them out&lt;/a&gt;. They are a good read even if you're not really into concurrency.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1763674547135509643?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1763674547135509643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1763674547135509643' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1763674547135509643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1763674547135509643'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/08/everything-you-ever-need-to-know-about.html' title='Everything you ever need to know about concurrent programming'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-795083272530479942</id><published>2009-06-07T05:25:00.000-07:00</published><updated>2009-06-07T06:20:37.983-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='compilers'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy stuff'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Give me a smarter compiler! How? Here's how!</title><content type='html'>In dark ages writing code and compiling code was two separate processes. As we get better languages and faster hardware and compilers, writing code and compiling code becomes more and more integrated. For instance, when programming Java in Eclipse the source is compiled in the background every time the file is saved. Very nice because it gives fast feedback!&lt;br /&gt;&lt;br /&gt;This means that since the code is compiled so often there usually only a small difference between the previous compiled version of the code and the current. This got me thinking: would it be possible to improve the error messages from the compiler by comparing the current code with the previously compiled code?&lt;br /&gt;&lt;br /&gt;Let's take an example. Assume that there is an interface &lt;span style="font-family:courier new;"&gt;I&lt;/span&gt; with a method &lt;span style="font-family:courier new;"&gt;void m(int)&lt;/span&gt; that is implemented by a class &lt;span style="font-family:courier new;"&gt;C&lt;/span&gt;. The code compiles without errors. Then you change the signature of &lt;span style="font-family:courier new;"&gt;m&lt;/span&gt; in &lt;span style="font-family:courier new;"&gt;C&lt;/span&gt; to &lt;span style="font-family:courier new;"&gt;void m(long)&lt;/span&gt;, which breaks the code of course. In this case the compiler could give an error message something like &lt;em&gt;"Changing C.m(int) to C.m(long) makes non-abstract class C abstract, because I.m(int) is no longer implemented"&lt;/em&gt; instead of the error message given by today's compilers which is something like &lt;em&gt;"Non-abstract class C is missing implementation for I.m(int)"&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;For this small example it may seem like a small improvement, but I think that if the compiler has access to the latest working code, then it can help in several way. Better error messages is only one improvement. Other improvements could be to give warnings when the semantics of the code has changed in an uncommon way. Let's take an example of this.&lt;br /&gt;&lt;br /&gt;Let's say an instance of the &lt;span style="font-family:courier new;"&gt;java.lang.Thread&lt;/span&gt; class is created and its &lt;span style="font-family:courier new;"&gt;void start()&lt;/span&gt; method is called. During refactoring, the call to &lt;span style="font-family:courier new;"&gt;start&lt;/span&gt; is removed by mistake. The compiles knows that its a common pattern to instantiate a &lt;span style="font-family:courier new;"&gt;java.lang.Thread&lt;/span&gt; and then call &lt;span style="font-family:courier new;"&gt;start&lt;/span&gt; on it, thus, the compiler gives a warning indicating the (potential) problem. For instance something like: &lt;span style="font-style:italic;"&gt;"Thread is created but not started because the call to Thread.start() has be removed between the two latest version of the code."&lt;/span&gt; It's almost like having a conversation with the compiler.&lt;br /&gt;&lt;br /&gt;Another nice thing with this kind of warning is that it only appears when the code has changed. In other words, it is the &lt;em&gt;difference&lt;/em&gt; between two consecutive versions of the code that triggers the warning, not the individual versions.&lt;br /&gt;&lt;br /&gt;If the programmers intent is that the thread &lt;em&gt;should not&lt;/em&gt; be started, then the warning can be ignored and the next time the code is compiled the compiler will not warn about the "missing" call to &lt;span style="font-family:courier new;"&gt;Thread.start()&lt;/span&gt;, because from the compiler's perspective the call is not missing anymore.&lt;br /&gt;&lt;br /&gt;This idea can of course be useful for static code analyzers, such as FindBugs, as well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-795083272530479942?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/795083272530479942/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=795083272530479942' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/795083272530479942'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/795083272530479942'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/06/give-me-smarter-compiler-how-heres-how.html' title='Give me a smarter compiler! How? Here&apos;s how!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6846280734460097876</id><published>2009-06-02T09:18:00.000-07:00</published><updated>2010-05-11T06:45:30.659-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reuse'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy stuff'/><category scheme='http://www.blogger.com/atom/ns#' term='dependency injection'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>The missing level of testing for software reuse</title><content type='html'>I've noticed a pattern. For software projects that start doing unit testing, unit testing is often paired with (manual) system testing only. Here's the pattern I've notices a few times:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Project members think unit testing and (traditional manual) system testing is good enough.&lt;/li&gt;&lt;li&gt;Project members want the simplicity of unit testing for system testing.&lt;/li&gt;&lt;li&gt;Project members try to automate system testing, but realize its hard or impossible to come close to the simplicty of unit tests.&lt;/li&gt;&lt;li&gt;Project members realize there is a missing level of testing between unit testing and system testing.&lt;/li&gt;&lt;/ol&gt;Here &lt;em&gt;project members&lt;/em&gt; means developers, architects, testers, managers, etc.&lt;br /&gt;&lt;br /&gt;The funny thing with this is that the missing level of testing often has been mentioned in discussions several time before in the project, but said to be impossible to implement because there is not time to do it. However, after being bitten by bugs found late in the project work on it is finally started.&lt;br /&gt;&lt;br /&gt;This missing level of testing tests such a large part of the application that it can be run indepenently. However, hardware and OS depenent parts are stubbed, configuration is (thus) simplified, user interaction is replaced with test-case input and assertions, and so on. There are several names for this level of testing: subsystem testing, multi-component testing, module testing, etc.&lt;br /&gt;&lt;br /&gt;There is a important difference between unit tests and system tests: unit tests live inside the code, while system tests live outside the code. When you write a unit test you write the code in parallel, you rewrite to code to make it testable, you refactor the test code and the production code at the same time. System tests, on the other hand, is often written in a completely other language (if automatic at all).&lt;br /&gt;&lt;br /&gt;This missing level of testing I'm talking about here also lives inside the code. Those tests are also refactored when the production code are, for instance. This is important. Being inside the code means these tests are easy to run, update, and write. Being inside the code is the thing that make this level of testing work.&lt;br /&gt;&lt;br /&gt;Essentially, these tests are unit tests in many aspects except that they test much larger chunks than a 'unit' (which is often said to be a single class).&lt;br /&gt;&lt;br /&gt;If done well, I think there is an interesting side-effect of this level of testing: it's easier to adapt larger chunks of code to work under different environments or assumptions (this can be seen for unit-tested classes. but for smaller chunks). If unit testing encourage interfaces and dependency injection, then this level of testing encourage a similar mind-set on larger chunks of code. For instance, configuration could be done in such as way that it easy to configure the application to use some kind of stub (e.g., saying &lt;span style="font-family:courier new;"&gt;PROTOCOL_TO_USE=TCP&lt;/span&gt; instead of &lt;span style="font-family:courier new;"&gt;USE_TCP=TRUE&lt;/span&gt;, because then it's simple to add a stub protocol)&lt;br /&gt;&lt;br /&gt;Seeing how much code is written that essentially reimplements existing application just because some small part of the application does not meet some requirement, this style of testing (if it improves reuseability, as I think it does) can be worth doing for more reasons than quality.&lt;br /&gt;&lt;br /&gt;Is testability what we should really aim at if we wish to make our code reusable? If so, then we need to test code in chunks that we think is valuble for reuse. In other words, the levels of testing we have defines the chunks of code that can be (easily) reused.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6846280734460097876?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6846280734460097876/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6846280734460097876' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6846280734460097876'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6846280734460097876'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/06/missing-level-of-testing-for-software.html' title='The missing level of testing for software reuse'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8874526966219025447</id><published>2009-05-11T13:19:00.000-07:00</published><updated>2009-05-11T11:18:16.984-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MS Office'/><category scheme='http://www.blogger.com/atom/ns#' term='crazy stuff'/><category scheme='http://www.blogger.com/atom/ns#' term='DSL'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='OpenOffice'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><category scheme='http://www.blogger.com/atom/ns#' term='file convertion'/><title type='text'>Using Office as an IDE (was: Making word documents machine-readable)</title><content type='html'>Ok, I admit it: this is a bit crazy. :) I think &lt;a href="http://www.geocities.com/krishna_kunchith/misc/bscs.html"&gt;this&lt;/a&gt; is a very good quote:&lt;blockquote&gt;Get your data structures correct first, and the rest of the program will write itself.&lt;br /&gt;David Jones&lt;/blockquote&gt;&lt;p&gt;especially in the context of this post.&lt;/p&gt;&lt;p&gt;That enough fluff, now let's get to the stuff.&lt;/p&gt;&lt;p&gt; Let's assume that you get some kind of specification in a simple computer readable format, e.g., comma-seperated-values or XML. There are several things that you can do when provided with such specification:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;generate code, e.g., interfaces or test-cases&lt;/li&gt;&lt;li&gt;automatically check you code (e.g., the states of a finite state machine handles the events they should and no more)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;automatic formal analysis of the specification (e.g., the finite state machine does not have any unreachable states)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;make sure the user documentation contains all parts of the documentation (e.g., a chapter for each &lt;span style="font-style: italic;"&gt;foo&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;bar&lt;/span&gt; specified).&lt;/li&gt;&lt;/ul&gt;Nice, stuff. But what if you get the specification in a less computer friendly format like MS Word format? Luckily, OpenOffice can read Word files and convert those to an easier format like HTML or plain text format (this should be possible to do via command line according to &lt;a href="http://www.oooninja.com/2008/02/batch-command-line-file-conversion-with.html"&gt;this&lt;/a&gt;, although I haven't tried it). OpenOffice can also do similar thing with Excel files.&lt;br /&gt;&lt;br /&gt;Ok, now you've got the Word or Excel file converted to plain text, now it's time to write that anayzer, code generator, or whatever you need. Code on!&lt;br /&gt;&lt;br /&gt;Actually, when thinking about this I realized that a very cool thing to do would be to &lt;a href="http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html"&gt;trigger&lt;/a&gt; the &lt;span style="font-style: italic;"&gt;convert-to-text-and-generate-code&lt;/span&gt; process when the Word/Excel document is saved. This way, whoever is updating the Word/Excel file will immediately know if the document is consistent (if the process analyzed its content) or if the tests passed (of the process generated test-cases). Would that be awesome?&lt;br /&gt;&lt;br /&gt;In some sense, this is using MS Office (or OpenOffice for that matter) like an IDE. Sound like madness to me, but perhaps its useful someone. Although I have to say that writing some document in MS Office, saving it, and a few seconds later getting an indication saying "&lt;span style="font-style: italic;"&gt;document analysis result: 1 warning: no test-case for feature 'Foo is sent to Bar'.&lt;/span&gt;" would be really cool. Or when saving an Excel file getting an indication saying "&lt;span style="font-style: italic;"&gt;Data in cell (5, 4) made test-case 'Foo can hold configured number of Bar:s' fail.&lt;/span&gt;"... that would be awesome.&lt;br /&gt;Why? Dunno, it just would. :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8874526966219025447?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8874526966219025447/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8874526966219025447' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8874526966219025447'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8874526966219025447'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/09/using-office-as-ide-was-making-word.html' title='Using Office as an IDE (was: Making word documents machine-readable)'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8973063915777850949</id><published>2009-05-04T11:33:00.000-07:00</published><updated>2009-05-04T12:27:31.852-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='parser'/><category scheme='http://www.blogger.com/atom/ns#' term='tools'/><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Cava'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='prototype'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>Java-compatible syntax for C++</title><content type='html'>Since I first realized how much more productive you are in Java compared to C++, it has bugged me that the syntactic difference is so small. Take this code as an example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class A {&lt;br /&gt;public void a() { }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Is that C++ or Java? (Hint: add ":" and ";" and it becomes another language). The difference is syntactically tiny, but huge when you think of all the things you get from Eclipse when using Java.&lt;br /&gt;&lt;br /&gt;So, this the idea: express C++ with a syntax that is compatible with Java. This Java-compatible syntax (&lt;span style="font-style: italic;"&gt;JCS&lt;/span&gt; from now on), of course, requires a program to translate it to C++, but it will make it possible to use a number of tools currently only available for Java. Refactoring and code browser (which is reliable), for example.&lt;br /&gt;&lt;br /&gt;Yeah, I hear you, &lt;span style="font-style: italic;"&gt;"you can't express advanced-feature-X and meta-programming-feature-Y using a JCS"&lt;/span&gt;. You're right; macros and advanced template-programming is far beyond the reach of a JCS, but that's not my point. My point is that the majority of C++ code could easily be expressed with a JCS. If 95% of your code could be refactored or browsed using Eclipse, thats much better than if 0% of your code could be refactored properly.&lt;br /&gt;&lt;br /&gt;Actually, I think that the alot of the C++ language is an example of &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/03/pythonic-parsing-and-keeping-corner.html"&gt;not keeping the corner-cases in the corner&lt;/a&gt;. Simple things like writing a script to list all defined functions in a source file is (in the general case) impossible because macros can possibly redefine the language... (thus all #include:ed header files has to be parsed, thus the entire build system with all its makefiles has to be known to the scrip.).&lt;br /&gt;&lt;br /&gt;I know Bjarne Stroustrup had reasons for doing this (backwards compatability with C), but I think this was more of marketing reason than a technical reason. His new language could have been compatible with C (being able to call it, and be called from it, etc) without the new language having to be a &lt;span style="font-style: italic;"&gt;syntactial&lt;/span&gt; superset of C. Anyway, back to JSC for C++.&lt;br /&gt;&lt;br /&gt;Friends and colleagues have told me that the new CDT for Eclipse gives you refactoring, code completion, and browsing, but it works poorly from my experience. Perhaps I've failed to configure Eclipse correctly, or I'm using a crappy indexer to index my C++ code... but I can't refactor my C++ code the way I can with Java code. (Compare the number of available refactorings in Eclipse for Java and C++ if you like to have an objective measurement).&lt;br /&gt;&lt;br /&gt;I've implemented a prototype that proves that it is possible to create a JCS that covers the most common part of C++. It works by traversing the Java AST (abstact syntax tree) and translates relevant nodes to its C++ representation. Example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class A extends B implements C {&lt;br /&gt;public int foo(@unsigned int[] a, boolean b) {&lt;br /&gt;  if (b) return a[1];&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;translates to&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class A : public B, public C {&lt;br /&gt;public: virtual int foo(unsigned int* a, bool b) {&lt;br /&gt;  if (b) return a[1];&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;There are very much that's not covered with this prototype, and it's probably riddled with bugs... but it fulfills its purpose perfectly: proving that expressing C++ using a JCS is possible. The prototype is available &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/cava"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I'd love to make a real-world worthy implementation of this idea, but I'm afraid it will take up my entire spare-time... I have other things to think about! :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8973063915777850949?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8973063915777850949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8973063915777850949' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8973063915777850949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8973063915777850949'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/05/java-compatible-syntax-for-c.html' title='Java-compatible syntax for C++'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7077115025413668595</id><published>2009-04-22T10:19:00.000-07:00</published><updated>2010-01-24T15:13:55.173-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='contracts'/><category scheme='http://www.blogger.com/atom/ns#' term='memory management'/><category scheme='http://www.blogger.com/atom/ns#' term='factory'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><category scheme='http://www.blogger.com/atom/ns#' term='mistakes'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Unit testing makes manually managed memory simple</title><content type='html'>I recently started working on a project that does test-first development in C++. I have mostly done TDD in Java, but I've done a lot of C++ before that so neither the language nor methodology is new to me. However, after two-three years of unit testing in Java and a few scripting languages, I have (luckily) learned a few things. So this time around I hope to avoid one of the problems I had earlier with C++: the problem with ownership of &lt;span style="font-family:courier new;"&gt;new&lt;/span&gt;ed objects, that is, &lt;span style="font-style: italic;"&gt;which object owns another dynamically allocated object?&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;When I was rewriting some classes to be testable I, as you usually do, introduced interfaces, used factories, injected dependencies, etc. In one case I rewrote the class under test (CUT) to use a factory instead of calling &lt;span style="font-family:courier new;"&gt;new&lt;/span&gt; directly. This made it easy to test that the CUT allocated an object correctly. But how to test that the CUT &lt;span style="font-family:courier new;"&gt;delete&lt;/span&gt;s that allocated object?&lt;br /&gt;&lt;br /&gt;Well, it was quite simple: simply add a method to the factory called &lt;span style="font-family:courier new;"&gt;destroy&lt;/span&gt; that takes one argument which is the object to be destroyed. The method &lt;span style="font-family:courier new;"&gt;destroy&lt;/span&gt; is used to tell the factory &lt;span style="font-style: italic;"&gt;I'm done with this object, do what ever you like with it... delete it it you want to&lt;/span&gt;. The &lt;span style="font-family:courier new;"&gt;destroy&lt;/span&gt; method corresponds to the &lt;span style="font-family:courier new;"&gt;make&lt;/span&gt; method, which of course allocates an object.&lt;br /&gt;&lt;br /&gt;This is probably not some great new discovery I've made; most of you who have done test-first development in languages with manual memory management have probably already done this kind of thing. This was a new thought for me, though.&lt;br /&gt;&lt;br /&gt;I also realized how clear the ownership of the object (created by the factory) had become. It was obvious from reading the production code and/or test-case that it was the factory that owned the object; all it did was to &lt;span style="font-style: italic;"&gt;lend&lt;/span&gt; the object to some other class until &lt;span style="font-family:courier new;"&gt;destroy&lt;/span&gt; was called. This was an insight for me.&lt;br /&gt;&lt;br /&gt;The fact that the factory owns the objects it creates means that it is trivial to replace the memory allocation scheme used by the factory. Only the factory is needed to be changed, e.g., if there is a need for pooling objects. Awesomeness.&lt;br /&gt;&lt;br /&gt;I guess there are cases when this approach is not possible to use, but in cases where it is I think its a good pattern to use.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7077115025413668595?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7077115025413668595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7077115025413668595' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7077115025413668595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7077115025413668595'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/04/unit-testing-makes-manually-managed.html' title='Unit testing makes manually managed memory simple'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8296715416790006167</id><published>2009-04-08T10:34:00.000-07:00</published><updated>2009-04-08T11:48:33.189-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='api'/><category scheme='http://www.blogger.com/atom/ns#' term='protocol'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='patters'/><title type='text'>Movable proxy: a design pattern for hiding dirty secrets</title><content type='html'>The &lt;a href="http://en.wikipedia.org/wiki/Proxy_pattern"&gt;&lt;span style="font-style: italic;"&gt;Proxy&lt;/span&gt;&lt;/a&gt; design pattern is a useful pattern. For my style of programming it's quite common, although I seldom actually name the proxy-class &lt;span style="font-style: italic;"&gt;SomethingProxy&lt;/span&gt;. Instead I try to come up with a name for its actual role or responsibility. An example of this kind of proxy could be the services provided by a package in some Java code. All classes of the package is package private (default visibility) except for one, which is the proxy towards the package (there are of course a bunch of public interfaces).&lt;br /&gt;&lt;br /&gt;Traditionally, though, a proxy is an object used to accessing something complex in a easier way. For instance a proxy for sending messages to, or retrieving the state of, a remote process. At work, where I'm working with a distributed Java application, we have a bunch of such proxies for accessing the various distributed subsystems/processes. All these proxies have some protocol for talking to the remote subsystem, e.g., homebrew protocol &lt;span style="font-style: italic;"&gt;N&lt;/span&gt;, standard protocol &lt;span style="font-style: italic;"&gt;M&lt;/span&gt;, etc.&lt;br /&gt;&lt;br /&gt;This is all good, except for the fact that both the client subsystem and the server subsystem need to share a dirty secret: which protocol that is used to communicate. A sane design will hide this dirty secret inside some class (e.g., a proxy!), such that the bulk of the code doesn't need to know the secret. But still, some part of the client subsystem &lt;span style="font-style: italic;"&gt;must&lt;/span&gt; know it, otherwise it cannot establish a connection to the server.&lt;br /&gt;&lt;br /&gt;Or does it?&lt;br /&gt;&lt;br /&gt;Wouldn't it be great if it was possible for the client to ask the server for the proxy instance? That is instead of doing:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;final Proxy forTalkingToServer = new ServerProxy();&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;the client does:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;final Proxy forTalkingToServer = server.proxyForTalkingToServer();&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Ok, that sound like a nice idea. But wait, how is the &lt;span style="font-family:courier new;"&gt;proxyForTalkingToServer() &lt;/span&gt;method implemented? Doesn't the implementation of that method need to communicate with the server? Well, yes. But this bootstrap problem is easily solved by having a standardized protocol for sending java objects between different Java systems, e.g., RMI.&lt;br /&gt;&lt;br /&gt;The sequence is something like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;      Client                      Server&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;      ------                      ------&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        |    Proxy request [RMI]    |&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        |--------------------------&gt;|&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;        |                           |&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        |    Proxy confirm [RMI]    |&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        |&lt;--------------------------|&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        |                           |&lt;br /&gt;+------------------+                |&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;| client has proxy |                |&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;+------------------+                |&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="font-family:courier new;"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;   |                           |&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;    |    Whatever request [?]   |&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;    |--------------------------&gt;|&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;   &lt;/span&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt; |                           |&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;    |    Whatever confirm [?]   |&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt; &lt;/span&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;   |&lt;--------------------------| &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Where &lt;span style="font-style: italic;"&gt;Proxy request &lt;/span&gt;is what happens when the &lt;span style="font-family:courier new;"&gt;proxyForTalkingToServer() &lt;/span&gt;method is called, and &lt;span style="font-style: italic;"&gt;Proxy confirm&lt;/span&gt; is the methods' return value, that is, the proxy the client should use for talking with the server. The &lt;span style="font-style: italic;"&gt;Whatever request&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;Whatever &lt;/span&gt;&lt;span style="font-style: italic;"&gt;confirm&lt;/span&gt; messages are messages sent using some protocol (symbolized by &lt;span style="font-style: italic;"&gt;[?]&lt;/span&gt;) that the server decided to use.&lt;br /&gt;&lt;br /&gt;In addition to having the client completely unaware of the actual protocol used for communicating with the server, the proxy can be pass around (using, e.g., RMI) the entire distributed system and it will still be possible to access the server using the proxy. It is also possible to update the protocol without updating the clients, or to let the proxy instance implement multiple interfaces (one new-and-improved and one legacy for backwards compatibility). It is also great for testability/debugging: you can easily replace a troublesome protocol with a simpler variant.&lt;br /&gt;&lt;br /&gt;Neat.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8296715416790006167?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8296715416790006167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8296715416790006167' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8296715416790006167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8296715416790006167'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/04/movable-proxy-design-pattern-for-hiding.html' title='Movable proxy: a design pattern for hiding dirty secrets'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6853452436216237976</id><published>2009-04-01T10:42:00.001-07:00</published><updated>2009-04-01T11:00:19.898-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='meta'/><title type='text'>Happy new year!</title><content type='html'>One year ago I wrote my &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/programatically-speaking.html"&gt;first&lt;/a&gt; post for this blog! I've been writing about Java and testing, published a few small programming project, and screamed in &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/03/its-2009-and-you-cant-read-forwarded.html"&gt;frustration&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;There have also been some (slightly) philosophical &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/zen-of-regular-expressions.html"&gt;posts&lt;/a&gt;, seemingly &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/09/thoughts-on-naming-methods-and-classes.html"&gt;trivial&lt;/a&gt; ideas, my old sweatheart &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/06/lisps-mapcar-for-java-onall-collect.html"&gt;Lisp&lt;/a&gt;, and my views on &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/inheritance-is-overrated.html"&gt;OO&lt;/a&gt;. I have also faced one of my many &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/08/fear-and-loathing-in-parse-vegas.html"&gt;fears&lt;/a&gt; and helped you making your faviourite IDE a bit &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/10/junit-45-in-eclipse-34.html"&gt;better&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;At late, I've even had some &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/03/contracts-test-driven-insight.html"&gt;insights&lt;/a&gt; and publishing one of my coolest &lt;a href="http://programmaticallyspeaking.blogspot.com/2009/03/code-generation-using-reflection.html"&gt;ideas&lt;/a&gt; (with a crappy implementation) :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6853452436216237976?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6853452436216237976/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6853452436216237976' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6853452436216237976'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6853452436216237976'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/04/happy-new-year.html' title='Happy new year!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1522615073124443161</id><published>2009-03-23T10:51:00.000-07:00</published><updated>2009-03-23T12:36:49.478-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><title type='text'>Code generation using reflection</title><content type='html'>A couple of month ago, in late November I think, I got an idea when I was riding my bike home from work: using code generation to optimize code that normally relies on reflection. (To understand this post, you should at least now the basics basics of dynamic proxies. Check &lt;a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Proxy.html"&gt;this&lt;/a&gt; out if you don't.)&lt;br /&gt;&lt;br /&gt;A common pattern for me is to have an interface and generating the class(es) that implements it at runtime using a Dynamic Proxy. The behavior of the class defined by code that is parameterize by annotations on the interface. Example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;interface CommandLineArguments {&lt;br /&gt;&lt;br /&gt;@ArgumentName({"c", "config"})&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;String configFileName();&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;@Argument({"x", "max")&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;String maxValue();&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;@Argument({"n", "min"})&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;String minValue();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The annotations on the methods in the interface defines what they should do simply by giving the name of the corresponding command line switch. Extremely terse, readable, and flexible code. If you ask me, this (annotated interfaces + dynamic proxies) is one of the best thing in the Java language.&lt;br /&gt;&lt;br /&gt;The code that actually gets executed when the methods in the interface is called often rely heavily on reflection. Reflection, as everyone know who have ever used it, is very powerful but can also be very slow. Is there some way to make it a bit faster? Yes, there certainly is: code generation.&lt;br /&gt;&lt;br /&gt;Lets take a simple but realistic example: for any interface, create a wrapper that print the name of the method and delegates to some other class that implements the same interface. The code for doing this look something like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.lang.reflect.InvocationHandler;&lt;br /&gt;&lt;br /&gt;class InvocationPrinter implements InvocationHandler {&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;private Object delegateTo;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;InvocationPrinter(Object delegateTo) {&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;this.delegateTo = delegateTo;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;Object invoke(Object proxy, Method method, Object[] args) throws Throwable {&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;System.out.println(method.getName() + " called.");&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;return method.invoke(delegateTo, args);&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This is a general but, unfortunately, slow. It is trivial to speed up, but this requires us to hand-write every method for every interface we wish to use this way. Another way, which gives the same speed-up, is to generate the same code dynamically. Literally the same code (except for indentation and such). Then using &lt;a href="http://www.csg.is.titech.ac.jp/%7Echiba/javassist/"&gt;Javassist&lt;/a&gt;, this code can be compiled to a class at run-time, resulting in bytecode with the same performance as your hand-written code.&lt;br /&gt;&lt;br /&gt;I have prototyped this approach for generating code by providing wrapper classes that looks and behaves just like &lt;span style="font-style: italic;"&gt;java.lang.reflect.Method&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;java.lang.reflect.Constructor, &lt;/span&gt;etc, except that they also stores how they were used. For example, the class (called &lt;span style="font-style: italic;"&gt;BIMethod&lt;/span&gt;) that corresponds to &lt;span style="font-style: italic;"&gt;java.lang.reflect.Method stores&lt;/span&gt; the arguments used when invoking it and the returned object. By doing this you can write normal Java code that uses reflection (via these provided wrappers), but also generate (at run-time) the Java code that implements the same functionality. In fact, since the wrappers keep track of returned values, and created object (via &lt;span style="font-style: italic;"&gt;Constructor.newInstance&lt;/span&gt;) it is possible to do fairly complex stuff like:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void doSomeReflectionStuff(Object[] args) {&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;DummyInterface obj = null;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt; for (final BIConstructor c : factory.constructors(Dummy.class)) {&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;try {&lt;br /&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;obj = (DummyInterface) c.newInstance(args[1], args[2]);&lt;br /&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;break;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;} catch (final Exception e) {&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;}&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;}&lt;br /&gt;&lt;/code&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;final BIMethod someMethod = getSomeMethod();&lt;br /&gt;&lt;/code&gt;&lt;code&gt;  &lt;/code&gt;&lt;code&gt;return someMethod.invoke(obj, 0, args[0]);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;That is, you reflectively invoke an object created using reflection. In addition, a constructor matching the arguments (the &lt;span style="font-style: italic;"&gt;Object[] args&lt;/span&gt;) is found automatically by checking if the constructor threw an exception of not. The generated Java code for this will look something like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Dummy variable0 = new Dummy(arg1, arg2);&lt;br /&gt;return variable0.theChosenMethod(0, arg0);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;If you wish to take a peek at the prototype, &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/Burn-in/"&gt;just go ahead&lt;/a&gt;. Be aware, though, that is is probably the least tested stuff I'll written in quite a while... there a probably heaps of bugs... :) Despite this, I think it's worth taking a look at if you need to get more performance out of your reflection-based code. Please contact me if you have any questions or ideas.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1522615073124443161?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1522615073124443161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1522615073124443161' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1522615073124443161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1522615073124443161'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/03/code-generation-using-reflection.html' title='Code generation using reflection'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3538065698525554304</id><published>2009-03-20T07:59:00.000-07:00</published><updated>2009-03-31T07:35:09.915-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rambling'/><category scheme='http://www.blogger.com/atom/ns#' term='mail'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='mistakes'/><category scheme='http://www.blogger.com/atom/ns#' term='frustration'/><title type='text'>It's 2009 and you can't read a forwarded mail</title><content type='html'>&lt;span style="font-family:arial;"&gt;The other day, a college sent me a mail that I simply forwarded from Outlook to my Gmail. Today, when I finally had time to read it I opened it in Gmail. What do you think the mail contains? Nothing, except an attached file called smime.p7m. This file contains the &lt;span style="font-style: italic;"&gt;encrypted&lt;/span&gt; mail, apparently, so I can't read it.&lt;br /&gt;&lt;br /&gt;Oh, please! Come on! Why is a simple thing like this so hard?! Really... seriously, I'm failing to forwardning an e-mail...? Are we really making progress?&lt;br /&gt;&lt;br /&gt;Yeah, I know that I should have forwarded it without encryption. But why is this something I need to know about? The mail client should tell me that the receiver won't be able to read the mail... It's freaking 2009! Not 1979!&lt;br /&gt;&lt;br /&gt;Who knows... in 3009, perhaps we humans have evolved enough to have figured out and understand this whole &lt;span style="font-style: italic;"&gt;send plain stupid text to another person&lt;/span&gt;-thingie. It's apparently too advanced to grasp for the current generation of humans.&lt;br /&gt;&lt;br /&gt;(I have high hopes for the next-gen humans, though... No, really. I do!)&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="font-family:georgia;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3538065698525554304?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3538065698525554304/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3538065698525554304' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3538065698525554304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3538065698525554304'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/03/its-2009-and-you-cant-read-forwarded.html' title='It&apos;s 2009 and you can&apos;t read a forwarded mail'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2586976204860477324</id><published>2009-03-14T06:48:00.000-07:00</published><updated>2009-03-14T08:23:16.242-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='opinions'/><category scheme='http://www.blogger.com/atom/ns#' term='pair-programming'/><category scheme='http://www.blogger.com/atom/ns#' term='contracts'/><category scheme='http://www.blogger.com/atom/ns#' term='rambling'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Contracts? Test-driven? Insight!</title><content type='html'>The other day I pair-programmed with a new guy at work. He showed my a class and its tests he and another guy had written a few week earlier. I don't recall exactly what the class did, but it was quite simple, hence the tests was short and simple. Overall well written tests if you ask me.&lt;br /&gt;&lt;br /&gt;As we looked at the tests we had the following conversation, which afterwards gave me an new insight to an idea I've had a long while: &lt;span style="font-style: italic;"&gt;tests are contracts&lt;/span&gt;. &lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;He&lt;span style="font-style: italic;"&gt;: Most of these tests are for testing that the class logs correctly&lt;/span&gt;...&lt;br /&gt;Me: Y&lt;span style="font-style: italic;"&gt;es&lt;/span&gt;,&lt;span style="font-style: italic;"&gt; is that a problem?&lt;br /&gt;&lt;/span&gt;He&lt;span style="font-style: italic;"&gt;: Well, I know TDD says that you need to write a failing test before you're allowed to write any production code. But isn't testing logging a bit over-kill?&lt;br /&gt;&lt;/span&gt;Me:&lt;span style="font-style: italic;"&gt; I understand what you mean. What kind of logging is this? Why does the class need to log?&lt;/span&gt;&lt;br /&gt;He:&lt;span style="font-style: italic;"&gt; We needed it to understand the code. For debugging.&lt;/span&gt;&lt;br /&gt;Me:&lt;span style="font-style: italic;"&gt; Is the logging needed now? Is there some script that parses these logs, for example?&lt;br /&gt;&lt;/span&gt;He: &lt;span style="font-style: italic;"&gt;No, it's not needed any more.&lt;br /&gt;&lt;/span&gt;Me:&lt;span style="font-style: italic;"&gt; In that case I'd say that these tests isn't needed.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;I could go even further and say that those tests shouldn't be written at all and should be removed. I actually think that tests like these are more confusing then anything else. I'm not saying those two guys who wrote these tests did anything wrong; they were doing TDD and was doing TDD right.&lt;span style="font-style: italic;"&gt; &lt;/span&gt;What I'm saying is that, in my opinion, TDD isn't ideal.&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Yeah, I hear you're cries: &lt;span style="font-style: italic;"&gt;What?! Heresy! &lt;/span&gt;Calm down. I'll try to explain&lt;span style="font-style: italic;"&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;My opinion is that a class' tests should define what the class has to fulfill to be considered correct. To be precise, with 'correct' I mean 'what makes all things that depend on the class behave correctly'. (I realize that this is an recursive definition of 'correct', but you're a human being so you can handle it. :))&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Recall that my pair-programming partner said that the logging wasn't needed any more. This means that we could remove the part of the code that logs and the class would still be correct according to the definition above. However, the class would not pass its tests because of the tests that tests the logging. This means that the class is over-specified. This is bad. The solution? Ditch the logging tests!&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;And so my fellow pair-programmer did.&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;I often say that test-methods should be named&lt;span style="font-style: italic;"&gt; shouldFoo&lt;/span&gt; since it makes you focus on the behavior that is tested instead of the part that is tested (the tested method for example)&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;I'm thinking of extending this tip to nameing test-methods as &lt;span style="font-style: italic;"&gt;shouldFooBecauseBar. &lt;/span&gt;If this convention was followed, the the test-methods that tests the logging whould be named &lt;span style="font-style: italic;"&gt;shouldLogBecauseWeNeedItforDebugging.&lt;/span&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;That name sound a bit silly, doesn't it?&lt;span style="font-style: italic;"&gt; &lt;/span&gt;That because it is silly to test it!&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;As I said&lt;span style="font-style: italic;"&gt;, &lt;/span&gt;a class' tests defines what the class has to fulfill to be correct. In other words, the tests is the &lt;span style="font-style: italic;"&gt;contract&lt;/span&gt; that the class must fulfill. Having tests that define contracts is much better, I think, than having tests for every little piece of functionality a class have (i.e., TDD). One reason is that it makes it easier to understand how you can change the class without breaking anything.&lt;br /&gt;&lt;br /&gt;Now don't get me wrong, TDD is great, really great. But is it perfect? Of course not, that would be very naive to think (not to mention boring: &lt;span style="font-style: italic;"&gt;Nothing more to do here, we've find the ideal solution! Now, let's drink tea all day!&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;Is contracts ideal? Probably not. It contracts better? Yes, I think so.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2586976204860477324?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2586976204860477324/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2586976204860477324' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2586976204860477324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2586976204860477324'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/03/contracts-test-driven-insight.html' title='Contracts? Test-driven? Insight!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7007662483512437640</id><published>2009-03-07T04:40:00.000-08:00</published><updated>2009-03-07T10:01:12.603-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='parser'/><category scheme='http://www.blogger.com/atom/ns#' term='api'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><title type='text'>Pythonic parsing and keeping corner-cases in the corners</title><content type='html'>I've been fiddeling with Python for a while, especially a nice library called &lt;a href="http://pyparsing.wikispaces.com/"&gt;Pyparsing&lt;/a&gt;. I have posted some stuff about parsers &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/08/fear-and-loathing-in-parse-vegas.html"&gt;before&lt;/a&gt;, and I have tried ANTL for a private project for parsing and translating Java code. Anyway, Pyparsing has to be the most intuitive and easy to use parser library I have used.&lt;br /&gt;&lt;br /&gt;In my opinion, a common problem with many libraries, programming languages, etc., is that they are not opted for the most common, simple, cases. Rather, they make the most common cases just as hard as the most weird corner-cases you can possibly think of. Take this Java code for reading an entire file into a String:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;FileInputStream fstream = new FileInputStream("filename.txt");&lt;br /&gt;DataInputStream in = new DataInputStream(fstream);&lt;br /&gt;BufferedReader br = new BufferedReader(new InputStreamReader(in));&lt;br /&gt;String strLine;&lt;br /&gt;StringBuffer content = new StringBuffer();&lt;br /&gt;while ((strLine = br.readLine()) != null)   {&lt;br /&gt;content.append(strLine);&lt;br /&gt;}&lt;br /&gt;in.close();&lt;br /&gt;return content.toString();&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Why, oh why, do I have to write all this code when all I want is (in Python):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;return open('filename.txt', 'r').read()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or (in Perl):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;open FILE, "filename.txt";&lt;br /&gt;$string = &amp;lt;file&amp;gt;;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The Java API for opening and reading files seems to be focused on covering all possible use-cases. Covering all use-cases is of course a good thing, &lt;span style="font-style: italic;"&gt;but not&lt;/span&gt; on the expense of common simple cases. It is trivial for me to add a few convenience methods/classes to cover the common cases. But why aren't these methods/classes in the API from the beginning?&lt;br /&gt;&lt;br /&gt;There are several other examples of this &lt;span style="font-style: italic;"&gt;screw-the-common-cases-and-make-the-api-super-generic&lt;/span&gt;-mentality. Reflection in Java throws a gazillion exceptions, for example, and in most cases you don't need to know &lt;span style="font-style: italic;"&gt;what&lt;/span&gt; went wrong, only that it &lt;span style="font-style: italic;"&gt;did&lt;/span&gt; go wrong.&lt;br /&gt;&lt;br /&gt;So, anyway, let's get back to the Pyparsing library. As I said, it is very easy to use and the common cases are straight-forward to implement. For example, there are helper classes/methods for parsing a string while ignoring up-/downcase, for matching one (and only one) of a set of grammar rules, etc. In addition to this the +, ^, | operators, etc, are overloaded so a grammar rule normally looks something like &lt;a href="http://pyparsing.wikispaces.com/file/view/greeting.py"&gt;this&lt;/a&gt;:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;greet = Word( alphas ) + "," + Word( alphas ) + "!"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;a href="http://abstrusegoose.com/81"&gt;Awesomeness&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So, what is this post all about? Pyparsing or bad libraries? Both. There are so many bad libraries out there that aren't ment to be used by human programmers. That is, the most simple things in the library are hard to use just becase the harders things are har to use. Pyparsing, on the other hand, is a joy to use. I was suprised how often I thought &lt;span style="font-style: italic;"&gt;oh, it would be nice to have such-and-such helper function now&lt;/span&gt; and after looking in the Pyparsing documentation though &lt;span style="font-style: italic;"&gt;yey, there it is!&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7007662483512437640?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7007662483512437640/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7007662483512437640' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7007662483512437640'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7007662483512437640'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2009/03/pythonic-parsing-and-keeping-corner.html' title='Pythonic parsing and keeping corner-cases in the corners'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8723790929110252402</id><published>2008-11-09T05:39:00.000-08:00</published><updated>2010-05-11T06:46:00.113-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Smart Factory'/><category scheme='http://www.blogger.com/atom/ns#' term='dependency injection'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><title type='text'>One factory to bring them all, and at run-time bind them</title><content type='html'>Most of us know that to call &lt;tt&gt;new&lt;/tt&gt; makes code hard to test because the piece of code calling &lt;tt&gt;new&lt;/tt&gt; is &lt;i&gt;statically&lt;/i&gt; bound to another class. In other words, there is no way to isolate the class under test such that its interactions with other classes can be tested. There is of course a solution to this: factories and/or dependency injection (with or without a framework).&lt;br /&gt;&lt;br /&gt;I personly prefer to manually inject the needed dependencies, and seldomly use DI frameworks such as &lt;a href="http://code.google.com/p/google-guice/"&gt;Guice&lt;/a&gt;. Why? Well, call my old fashioned, but I find it easier to read and understand non-DI framework code. So this leaves me with writing factories.&lt;br /&gt;&lt;br /&gt;I don't know about you, but I find it boring to write these lousy factories (also, how can they be tested?). All they do is calling &lt;tt&gt;new&lt;/tt&gt;; should there be some better way of doing it? Of course there should, and in fact, there are (for some definition of "better" :)). I've implemented a generic factory that can be used to instantiate most classes.&lt;br /&gt;&lt;br /&gt;The factory uses some reflection magic and does a fair amount of work at run-time instad of compile-time. Actually, the factory is on the edge of being a primitive DI framework. :) As I usually do in my post I cut to the chase and give a few code examples:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;final Factory factory = Factory.of(ConcreteClass.class, AnotherConcreteClass.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This creates a new factory that can create two concrete classes. Let's say that these classes implement the interfaces &lt;tt&gt;Interface&lt;/tt&gt; and &lt;tt&gt;AnotherInterface&lt;/tt&gt; respectiveley. To get an instance of the &lt;tt&gt;Interface&lt;/tt&gt; the factory is called like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;final Interface instance = factory.create(Interface.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;When the &lt;tt&gt;create&lt;/tt&gt; method is called, the factory finds the class that implements the provided interface (in this case this is the &lt;tt&gt;ConcreteClass&lt;/tt&gt; class). Then the factory instantiates that class and returns the instance. This means that&lt;p&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;the factory can be used for creating different kinds of objects as long as they implement different interfaces,&lt;/li&gt;&lt;li&gt;the client of the factory is unaware of the concrete class implementing the interface, and&lt;br /&gt;&lt;/li&gt;&lt;li&gt;the same &lt;tt&gt;Factory&lt;/tt&gt; class can be used in test-cases testing the client code (the difference is only how the factory instance is created).&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;I illustrait the last bullet with an example, This code is part of a test-case testing a class which use the factory to create an &lt;tt&gt;Interface&lt;/tt&gt; instance:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;final Factory stubbedFactory = Factory.of(Stub.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;where, of course, &lt;tt&gt;Stub&lt;/tt&gt; implements &lt;tt&gt;Interface&lt;/tt&gt; (compare to the example above where the factory is created using &lt;tt&gt;ConcreteClass&lt;/tt&gt; and &lt;tt&gt;AnotherConcreteClass&lt;/tt&gt;).&lt;br /&gt;&lt;br /&gt;Ok, so far so good. But how to instantiate a class that need some parameters to be instantiated, i.e., only has a non-default constructor? Parameters that the factory needs in order to create objects are provided by calling the &lt;tt&gt;using&lt;/tt&gt; method:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;final DependencyUser user = factory.using(new Dependency()).create(DependencyUser.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Here, the concrete class implementing &lt;tt&gt;DependencyUser&lt;/tt&gt; is instantiated with the &lt;tt&gt;Dependency&lt;/tt&gt; instance as argument to the class' constructor. Any number of parameters can be given to the &lt;tt&gt;using&lt;/tt&gt; method as long as they have different (run-time) type. The factory will use those parameters that are needed for instantiating the class that the client requests.&lt;br /&gt;&lt;br /&gt;The &lt;tt&gt;using&lt;/tt&gt; method returns a new &lt;tt&gt;Factory&lt;/tt&gt; instance that "know about" the parameters provided to &lt;tt&gt;using&lt;/tt&gt; (and any parameters known by the factory instance which &lt;tt&gt;using&lt;/tt&gt; was called on). This may sound a bit strange, but it makes it possible to add parameters as they become available in the program flow. For instance, it's quite common that some parameters are only know when the factory is used. Example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Outside client code. An instance of 'Two' is not available.&lt;br /&gt;final Factory factory = Factory.of(ClassUsingOneAndTwo.class).using(new One());&lt;br /&gt;&lt;br /&gt;// Inside client code. An instance of 'Two' is now available.&lt;br /&gt;final OneAndTwoUser user = factory.using(two).create(OneAndTwoUser.class);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;(&lt;tt&gt;ClassUsingOneAndTwo&lt;/tt&gt; is a class implementing the &lt;tt&gt;OneAndTwoUser&lt;/tt&gt; interface and its constructor takes an &lt;tt&gt;One&lt;/tt&gt; instance and a &lt;tt&gt;Two&lt;/tt&gt; instance).&lt;br /&gt;&lt;br /&gt;That is! That's the mostly simple, almost generic, factory I hacked together... pardon me ...developed this afternoon. The source is available &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/SmartFactory/src/programmatically/speaking/smartfactory/Factory.java"&gt;here&lt;/a&gt;. There is also a few test-cases in that Eclipse project.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8723790929110252402?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8723790929110252402/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8723790929110252402' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8723790929110252402'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8723790929110252402'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/11/one-factory-to-bring-them-all-and-at.html' title='One factory to bring them all, and at run-time bind them'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8944113091833580766</id><published>2008-10-23T09:05:00.000-07:00</published><updated>2008-11-04T04:26:34.841-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='JUnit'/><title type='text'>JUnit 4.5 in Eclipse 3.4</title><content type='html'>&lt;p&gt;I was quite disappointed when I realized that Eclipse 3.4 (Ganymede) still uses JUnit 4.3. JUnit 4.4 was released last summer so it shouldn't be rocket science to it a part of Ganymede, which was released &lt;span style="font-style:italic;"&gt;this&lt;/span&gt; summer. Well, I guess it all comes down to lack of time and resources. Of course, it could also be because of compatabilty issues between JUnit versions, or something similar.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Luckely, it quite easy to use a newer version of JUnit in Eclipse. By simply adding the new JUnit JAR to the classpath of an Eclipse project and removing the JUnit 4 &lt;i&gt;Library&lt;/i&gt; (that is, the entry in 'Java Build Path' with an icon that looks like a pile of books), the project will run the test-case with the new JUnit. Of course, your other projects will still use JUnit 4.3. We have done this at work with JUnit 4.5 and it works really good.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;I recommend you to do this, since there are some really nice features in JUnit 4.5; the possibility do distinguish between &lt;em&gt;assumptions&lt;/em&gt; and &lt;em&gt;assertions&lt;/em&gt;, and better descriptions of failed assertions, for instance.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8944113091833580766?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8944113091833580766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8944113091833580766' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8944113091833580766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8944113091833580766'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/10/junit-45-in-eclipse-34.html' title='JUnit 4.5 in Eclipse 3.4'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8685615255977433490</id><published>2008-10-17T07:38:00.000-07:00</published><updated>2008-10-17T09:28:54.708-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><category scheme='http://www.blogger.com/atom/ns#' term='DRY'/><title type='text'>Don't Repeat Yourself - what does 'repeat' really mean?</title><content type='html'>&lt;p&gt;It is not uncommon to have some kind of script (e.g., a &lt;span style="font-family:courier new;"&gt;.sh&lt;/span&gt;-script) to start an application. For example, a start script for an Java application could check that the correct version of the JRE is installed, set up the classpath, and then start the application by executing &lt;span style="font-family:courier new;"&gt;java -cp [classpath] [mainclass]&lt;/span&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;In a case like this, the start script contains some information that is already embedded in the source code, e.g., the name of the class containing the &lt;span style="font-family:courier new;"&gt;static void main(String[])&lt;/span&gt; method. Is this a violation of the DRY principle? I certainly think so.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;However, you could argue that source code is filled with this kind of violation (refering to something by its textual name) since classes/types are referred to by name everywhere, for example when instantiating a new object in most OO languages. I don't consider this to be a violation of DRY, though.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Why? Because with modern IDEs classes can be renamed/moved and all references to the class will be updated. Thus, effectively, there is no repetition (since you don't manually handle it). So, no violation of the DRY principle.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;However, if the application uses reflection, or something similar, then the IDE can't safely handle it. Consequently, you have to handle these repetions manually with IDE support. In other words, the DRY principle is violated.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The impact of these violations can be minimzed by having a good test-suit. This way, if you fail to update the code correctly the tests will tell you so. Reflection-heavy code is not different than any other code in this sense.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Ok, so let's get back to the original example: the start script and the reference to the main class of the application. This is a violation of the DRY principle since the IDE does not update the script's references to classes. But not only that, in most cases it does not have any test-cases. This is very bad, because you'll get no indication that something has gone wrong. (You could argue that you shouldn't rename the main class, but that's beside the point I'm making).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So, how to fix this? Simple. Either&lt;/p&gt;&lt;ol&gt;&lt;li&gt;unit test the start-script (run it, or use some kind of pattern matching), or&lt;/li&gt;&lt;li&gt;generate the test-script by a well-tested generator.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;All executable parts of the application it should be possible to test; but how about the non-executable parts? How about documentation, e.g., user guides? I don't have a good answer to this besides &lt;em&gt;generate what can be generated&lt;/em&gt; but this is hard in practice. If you have a good soluation, please let me know...&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8685615255977433490?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8685615255977433490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8685615255977433490' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8685615255977433490'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8685615255977433490'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/10/dont-repeat-yourself-what-does-repeat.html' title='Don&apos;t Repeat Yourself - what does &apos;repeat&apos; really mean?'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8297326768584664715</id><published>2008-10-13T08:36:00.000-07:00</published><updated>2008-10-23T09:53:35.441-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Run-time JUnit Ignore'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='JUnit'/><title type='text'>A run-time equivalent to JUnit's @Ignore</title><content type='html'>It's been some time since I wrote about things that annoy me. Now it's time again. The pain-in-the-lower-back this time is: why isn't there a (good) way to ignore a JUnit test-case based on a piece of information that only awailable at run-time? In short: dynamically ignore a test-case.&lt;br /&gt;I think a "good way" to solve this should fulfill the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;a dynamically ignore test-case should marked as "ignored" in the JUnit test-run,&lt;/li&gt;&lt;li&gt;possible search for dynamically ignored test-cases in the IDE.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;There is a very simple way to ignore a test-case base on run-time information:&lt;br /&gt;&lt;code&gt;@Test&lt;br /&gt;public void testIt() {&lt;br /&gt;  if (shouldIgnore())&lt;br /&gt;    return;&lt;br /&gt;  // ... the rest of the test-case.&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;However, this solution does not fulfill the above requirements at all. Something better is needed.&lt;/p&gt;&lt;p&gt;JUnit uses a &lt;span style="font-family:courier new;"&gt;org.junit.runner.Runner&lt;/span&gt; to run test-cases. Since JUnit 4.0 it's possible to define which such &lt;span style="font-family:courier new;"&gt;Runner&lt;/span&gt; that should be used to run a set of test-cases. The @RunWith annotation does just this. Here is an example:&lt;code&gt;&lt;br /&gt;@RunWith(MyRunner.class)&lt;br /&gt;public final class MyTest {&lt;br /&gt;  // ... some test-cases.&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;There are several ways @RunWith can make you testing filled days easier; for a real-world example you need to look no further than to JMock. I have implemented a &lt;span style="font-family:courier new;"&gt;Runner&lt;/span&gt; that makes it possible to do:&lt;br /&gt;&lt;code&gt;@RunWith(RuntimeIgnoreable.class)&lt;br /&gt;public final class MyTest {&lt;br /&gt;  @Test&lt;br /&gt;  public void perhapsIgnored() {&lt;br /&gt;    ignoreIf(perhapsTrue())&lt;br /&gt;    // ... the rest of the test-case.&lt;br /&gt;  }&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;Neat, ey? I think so at least. What's even neater is that the &lt;span style="font-family:courier new;"&gt;RuntimeIgnoreable&lt;/span&gt; class and the &lt;span style="font-family:courier new;"&gt;ignoreIf&lt;/span&gt; method was embaressingly straight-forward to implement. You can &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/DynamicIgnore"&gt;browse the code&lt;/a&gt;, or look at the individuals files:&lt;ul&gt;&lt;li&gt;&lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/DynamicIgnore/src/programmatically/speaking/dynamicignore/RuntimeIgnoreable.java"&gt;RuntimeIgnoreable&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/DynamicIgnore/src/programmatically/speaking/dynamicignore/Ignore.java"&gt;ignoreIf&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/DynamicIgnore/test/programmatically/speaking/dynamicignore/UsesDynamicIgnoreTest.java"&gt;example&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Oh, one final note: this was develop for JUnit 4.3. If you are using any other JUnit version, the you prbably need to make some minor changes to the code.&lt;/p&gt;&lt;p&gt;&lt;em&gt;Update: I just realize that &lt;a href="http://www.junitext.org/"&gt;JUnit Extensions&lt;/a&gt; does this (among aother things)...&lt;/em&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8297326768584664715?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8297326768584664715/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8297326768584664715' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8297326768584664715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8297326768584664715'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/10/run-time-equivalent-to-junits-ignore.html' title='A run-time equivalent to JUnit&apos;s @Ignore'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3918710134576993067</id><published>2008-09-18T09:22:00.000-07:00</published><updated>2008-09-18T10:34:27.485-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='code generation'/><title type='text'>Performance gain of memoization in Ruby</title><content type='html'>&lt;p&gt;I and a some friends at work are developing a code generator that simplifies how we use a third-party library. Basically, the problems with the library are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Hard to write code: we wish to express "get X.Y.Z", but the library force us to write "get Y from X by doing actions A and B, check that the result is a Y, cast it to a Y and then get Z by doing C actions and D". You get the point...&lt;/li&gt;&lt;li&gt;Hard to read code: given the code to "get X.Y.Z" is extremely hard to understand that it X.Y.Z that it returns. Self-documenting code is basically impossible to achieve here. We have tried, and fia&lt;/li&gt;&lt;li&gt;The library has no interfaces what-so-ever, making it very hard to our own code.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p&gt;We've developed the code generator without any effort to make it fast (making it correct is hard enought). However, there have been a few instances where we simply "had to" optimize, because our test-cases started to take minutes to complete. Not good at all.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The first optimization we did was to write the parse tree of a file (which rarely changes) to disk by  marshalling the Ruby objects. This made the parse phase go from 2-4 minutes to instantaneous. It was quite simple to implement, too. Very good! Actuallt, we didn't even use a profiler for this because it was to utterly obvious that it was the parse phase that was the bottleneck.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;For the second optimization, though, we used a profiler. We found that a single method was responsible for around 90% of the used CPU time and that is was called alot. What to do?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Well, lucky us, we had been very careful to not have any state in our classes. That is, the code generator is purely functional (well, almost) . This made it possible for us to cache the result of the method the first time it was called, and use the cached value for all consecutive calls. (This only took one additional line of code, by the way.) We ran our test-suit and it passed. Great!&lt;/p&gt;&lt;br /&gt;&lt;p&gt;We ran the profiler again. The performance gain was 25 fold. Good stuff.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;For me, this illustraits the age old truth that you should only optimize when you &lt;em&gt;know&lt;/em&gt; you need to optimze and what/where to optimize. But it also shows how easy it is to optimize functional code, especially if there is a good test-suit making sure you don't mess anything up.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3918710134576993067?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3918710134576993067/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3918710134576993067' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3918710134576993067'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3918710134576993067'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/09/performance-gain-of-memoization-in-ruby.html' title='Performance gain of memoization in Ruby'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-28136801969425123</id><published>2008-09-13T05:24:00.000-07:00</published><updated>2008-09-13T08:54:45.769-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='naming'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Itsy Bitsy Bag'/><title type='text'>Thoughts on naming methods and classes</title><content type='html'>&lt;p&gt;This may seem like a trivial thing to discuss, but I actually think is quite important because the name of a method or class can change how you think about a particualr piece of code. For me, there are at least two ways of naming a method:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;What it does, e.g., &lt;code&gt;donaldDuck = ducks.findByName("Donald")&lt;/code&gt;. This is probably the name you come up with when you realize that you need the particular method ("How do I ...? Ah, I need to find the duck named 'Donald'. Then I can ...").&lt;/li&gt;&lt;li&gt;what it returns or how it will be used, e.g., &lt;code&gt;donaldDuck = ducks.named("Donald")&lt;/code&gt;. I usually come up with this type of names when I think about the contexts the method will be called from. That is, how the code will &lt;em&gt;look &lt;/em&gt;when you &lt;em&gt;read&lt;/em&gt; it.&lt;/li&gt;&lt;/ol&gt;Let's take a concrete example to illustrait. Assume that we have a class &lt;code&gt;&lt;a href="http://programmaticallyspeaking.blogspot.com/2008/06/lisps-mapcar-for-java-onall-collect.html"&gt;Bag&lt;/a&gt;&lt;/code&gt; representing a set of objects. This class have a constructor taking a single &lt;code&gt;Class&lt;/code&gt; argument that is the type of the objects the &lt;code&gt;Bag&lt;/code&gt; contains. Thus, to get your self a new fancy bag you do:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;final Bag&amp;lt;Apple&amp;gt; appleBag = new Bag&amp;lt;Apple&amp;gt;(Apple.class);&lt;/code&gt;&lt;br /&gt;&lt;p&gt;"Yuck", you think to your self, "that's a lot of code just to get one lousy bag". To remedy this, you decide to write a static factory method in the &lt;code&gt;Bag&lt;/code&gt; class. How should should you name this method? &lt;/p&gt;&lt;ol&gt;&lt;li&gt;What it does: &lt;code&gt;static &amp;lt;T&amp;gt; Bag&amp;lt;T&amp;gt; newBag(final Class&amp;lt;T&amp;gt; contentType)&lt;/code&gt;&lt;/li&gt;&lt;li&gt;How/where it will be used: &lt;code&gt;static &amp;lt;T&amp;gt; Bag&amp;lt;T&amp;gt; of(final Class&amp;lt;T&amp;gt; contentType)&lt;/code&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Now, to put this into context:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;01 void Bag&amp;lt;Appple&amp;gt; chooseStylishBag() {&lt;br /&gt;02     if (isOutOfStule(oldBag))&lt;br /&gt;03         return Bag.of(Apples.class);&lt;br /&gt;04     return oldBag;&lt;br /&gt;05 }&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I think line 03 reads very nice. It's short and to the point, and does not expose implementation deatils, etc.&lt;br /&gt;&lt;br /&gt;All is good with that bag buissness -- or at least so you though. However, after a few days a fellow programmer says that (s)he does not like the name of the method because it is not clear that it create a new empty bag. You respond 'well, you could just read the documentation for the &lt;code&gt;of&lt;/code&gt; method'. You have't even completed that sentence before your colleague replies 'good code should not need documentation! It should be self-documented!'. (S)he is of course right. What to do?&lt;br /&gt;&lt;br /&gt;Let's go back and rewrite the &lt;code&gt;chooseStylishBag&lt;/code&gt; method such that it is clear that it returns an &lt;i&gt;empty &lt;/i&gt;&lt;code&gt;Bag&lt;/code&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;01 void Bag&amp;lt;Apple&amp;gt;&gt; chooseStylishBag() {&lt;br /&gt;02     if (isOutOfStule(oldBag))&lt;br /&gt;03         return EmptyBag.of(Apples.class);&lt;br /&gt;04     return oldBag;&lt;br /&gt;05 }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;That is, we've moved the &lt;code&gt;of&lt;/code&gt; factory method to a separate factory class that is called &lt;code&gt;EmptyBag&lt;/code&gt;. Your angry colleague does not complain any more. All is calm again.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-28136801969425123?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/28136801969425123/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=28136801969425123' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/28136801969425123'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/28136801969425123'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/09/thoughts-on-naming-methods-and-classes.html' title='Thoughts on naming methods and classes'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-2264165753634880292</id><published>2008-08-26T12:57:00.000-07:00</published><updated>2010-02-19T07:32:45.488-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='pair-programming'/><category scheme='http://www.blogger.com/atom/ns#' term='tools'/><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='links'/><title type='text'>Greatest pair-programming tool ever?</title><content type='html'>&lt;a href="http://www.vimeo.com/1195398"&gt;This&lt;/a&gt; has to be one of the greatest thing for pair-programmers since large wide-screen monitors. I haven't tried it yet though, so perhaps I'm wrong. :) Seems like a really really nice plug-in for Eclipse, though.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-2264165753634880292?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/2264165753634880292/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=2264165753634880292' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2264165753634880292'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/2264165753634880292'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/08/greatest-pair-programming-tool-ever.html' title='Greatest pair-programming tool ever?'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-1853720055149928325</id><published>2008-08-25T11:29:00.000-07:00</published><updated>2008-08-26T09:48:11.207-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='opinions'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='exception'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Checked exceptions exposes implementation?</title><content type='html'>I've read and heard the phrase &lt;i&gt;Checked exceptions are evil! They expose implementation details!&lt;/i&gt; a couple of time (&lt;a href="http://www.jelovic.com/articles/why_java_is_slow.htm"&gt;last time&lt;/a&gt; was an entartaining read for several other reasons...). I really don't understand this statement (hey, you, explain to me please).&lt;br /&gt;&lt;br /&gt;How can the &lt;code&gt;EncodingException&lt;/code&gt; part of the code below expose implementation details when &lt;code&gt;void encode()&lt;/code&gt; does not? The first say &lt;i&gt;I can fail to encode&lt;/i&gt;, the latter say &lt;i&gt;I can encode&lt;/i&gt;. What's the difference?&lt;code&gt;&lt;br /&gt; interface Encoder {&lt;br /&gt;     void encode(Object o) throws EncodingException;&lt;br /&gt; }&lt;/code&gt;&lt;br /&gt;For me, checked exceptions are vital to enforce proper error handling. But this can (probably) only be achieved if the exceptions fits the problem domain. For instance, throwing an &lt;code&gt;IOException&lt;/code&gt; in the interface above would be really really bad because it exposes details of the &lt;code&gt;Encoder&lt;/code&gt;, e.g., that is uses the network, or whatever. On the other hand, the only thing &lt;code&gt;EncodingException&lt;/code&gt; exposes is that an &lt;code&gt;Encoder&lt;/code&gt; can fail to encode the provided object. That's not an implementation detail, I think, that's the &lt;code&gt;Encoder&lt;/code&gt; being honest and not hiding it flaws.&lt;br /&gt;&lt;br /&gt;One of the most important lesson I've learnt from using exceptions is the importance of doing &lt;i&gt;try-catch-wrap-throw&lt;/i&gt;. For example, if an implementation of &lt;code&gt;Encoder&lt;/code&gt; uses a method that throws &lt;code&gt;IOException&lt;/code&gt; then the proper way of handling such exception is to catch it, wrap it in a &lt;code&gt;EncodingException&lt;/code&gt; and throw it to the client of the &lt;code&gt;Encoder&lt;/code&gt;. This cleans up ugly interfaces that throws many exceptions, resulting in clear description (with semantics, yeay!) of what can go wrong when a method is called. Exceptions that fits the problem domain is the key.&lt;br /&gt;&lt;br /&gt;A couple of times I've let domain exceptions inherit from each other to define that, for example, a &lt;code&gt;RangeException&lt;/code&gt; &lt;i&gt;is a&lt;/i&gt; &lt;code&gt;ConfigurationException&lt;/code&gt;. This seemed like a good idea to me at the time, however, it seldom helped the design (it didn't make it worse either, though). In fact, the only time I find it useful is when you need to distinguish between thrown exceptions in one place but handle them in the same way in a nother place. For example (where &lt;code&gt;LeftException&lt;/code&gt; and &lt;code&gt;RightException&lt;/code&gt; inherits from &lt;code&gt;BaseException&lt;/code&gt;):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;interface Something {&lt;br /&gt;   void doIt() throws LeftException, RightException;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class HandlesExceptionsSeparately {&lt;br /&gt;   void method(Something s) {&lt;br /&gt;       try {&lt;br /&gt;           s.doIt()&lt;br /&gt;       } catch(LeftException e) {&lt;br /&gt;           // handle it&lt;br /&gt;       } catch (RightException e) {&lt;br /&gt;           // handle in another way) {&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class HandlesExceptionsTogether {&lt;br /&gt;   void method(Something s) {&lt;br /&gt;       try {&lt;br /&gt;           s.doIt()&lt;br /&gt;       } catch(BaseException e) {&lt;br /&gt;           // handles both&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;But as I said, this is not very common for me. Although if you are developing a library and wish the user to have the ability to handle the different error-cases separetely, then could be useful.&lt;br /&gt;&lt;br /&gt;Well, these are some of my thoughts on checked exceptions. In summary: I think they're good stuff if done well. :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-1853720055149928325?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/1853720055149928325/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=1853720055149928325' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1853720055149928325'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/1853720055149928325'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/08/checked-exceptions-exposes.html' title='Checked exceptions exposes implementation?'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3447916128261530017</id><published>2008-08-03T03:14:00.001-07:00</published><updated>2008-08-05T13:16:14.883-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='parser'/><category scheme='http://www.blogger.com/atom/ns#' term='DSL'/><category scheme='http://www.blogger.com/atom/ns#' term='fear'/><category scheme='http://www.blogger.com/atom/ns#' term='regular expressions'/><title type='text'>Fear and Loathing in Parse Vegas</title><content type='html'>Martin Fowler writes about &lt;a href="http://martinfowler.com/bliki/ParserFear.html"&gt;&lt;i&gt;parser fear&lt;/i&gt;&lt;/a&gt; and I have to say "guilty as charged". I've done a few DSL but all have be so simple that I could hand-write a parser using &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/zen-of-regular-expressions.html"&gt;regular expressions&lt;/a&gt; and other string manipulations. To be honest, the resulting parser would probably be easier to understand, maintain, etc, if it was developed using a proper grammar and a parser generator. Despite (knowing) this, I kept writing those convoluted hand-written parsers.&lt;br /&gt;&lt;br /&gt;I did the compiler class at the university and I'm intressted in most things programming langage related, e.g., compilers and parsers. Despite this I never actually done a parser (with proper grammar) by my self. Why? I had parser fear.&lt;br /&gt;&lt;br /&gt;Fowler writes:&lt;br /&gt;&lt;blockquote&gt;So why is there an unreasonable fear of writing parsers for DSLs? I think it boils down to two main reasons.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You didn't do the compiler class at university and therefore think parsers are scary.&lt;/li&gt;&lt;li&gt;You did do the compiler class at university and are therefore convinced that parsers are scary.&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;br /&gt;I think the last bullet explains why I never did a proper-grammar-parser by my self.&lt;br /&gt;&lt;br /&gt;However, the last time I had to write a parser I (finally) realized that a proper-grammar-parser was a better idea than trying to hand-write something convoluted. The parser should be implemented in Ruby, so I googled (is that a verb now?) and found a &lt;a href="http://snippets.dzone.com/tag/rdparser"&gt;generic recursive decent parser&lt;/a&gt; -- all I had to do was to write the grammar, which was straight forward.&lt;br /&gt;&lt;p&gt;There were several resons that finally made me take the step to use a proper parser:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The language was complex enough to make my old approach unsuitable&lt;/li&gt;&lt;li&gt;The parser was really easy to integrate with my other Ruby code&lt;/li&gt;&lt;li&gt;No separate step for generating the parser (i.e. short turn-around time, and less complexity because there is no code generation)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;In essense: it was easy to use and test. That was the cure for my (irrational) anxiety towards parsers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3447916128261530017?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3447916128261530017/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3447916128261530017' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3447916128261530017'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3447916128261530017'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/08/fear-and-loathing-in-parse-vegas.html' title='Fear and Loathing in Parse Vegas'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8246681779350541077</id><published>2008-07-03T12:13:00.000-07:00</published><updated>2008-08-03T03:11:24.891-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='debugging'/><title type='text'>Lessons from a debugger</title><content type='html'>A few days ago I got &lt;tt&gt;undefined method `some_method' for nil:NilClass&lt;/tt&gt; when I executed a test-case I've just had written for a quite well-tested class. The test-case tested input that the class hadn't been designed to handled, but now I needed the class to handle it.&lt;br /&gt;&lt;br /&gt;Knowing that there was a suit of test-cases making sure I didn't break anything, I just added &lt;tt&gt;return if (!thing)&lt;/tt&gt; (where &lt;tt&gt;thing&lt;/tt&gt; is the object the &lt;tt&gt;some_method&lt;/tt&gt; was called on) and run the suit again. And gues what? All test-passed -- including the one I just written that didn't pass. I wrote another one testing a similar scenario and it also passed. I was satisfied.&lt;br /&gt;&lt;br /&gt;Why did I add that nil-check? Well, the error appeared deep in a recursive call-chain, and I simply guessed that the recursion should be stopped if &lt;span&gt;thing&lt;/span&gt; was nil. I didn't &lt;i&gt;know&lt;/i&gt; -- I just guessed.&lt;br /&gt;&lt;br /&gt;The point is that I didn't have to know, because if I was wrong any of the class test or multi-class tests would tell me I was. This is all good right? Well, it's not &lt;i&gt;all&lt;/i&gt; good. Why? I'll tell you in a moment.&lt;br /&gt;&lt;br /&gt;But first, think about a hard problem that you solved by putting in more effort than usual -- persuading someone to test more,  parsing a proprietary file format, writing C++ or reading Perl -- any hard problem will do. I'm pretty sure you learnt something really valueable from that experience (even though it didn't feel that way while solving it...). I'm sure we learn a lot from solving any problem by putting in more effort than ususal.&lt;br /&gt;&lt;br /&gt;Now, back to my story about the nil fix that made  the test-case pass. What did I learn from fixing it by adding a line that  I simply &lt;i&gt;guessed&lt;/i&gt; should be there? Zip, nothing,  ingenting. Of course, this shows that well-tested code is a Good Thing, but this isn't  news to anyone. (By the way, note that &lt;i&gt;a Good Thing&lt;/i&gt; isn't trademarked, registered, closed-sourced, or anything like that. Its free to use and I encurage you to do so as often as possible. :) Of course, any improvements you make to &lt;i&gt;a Good Thing&lt;/i&gt; have to be shared with the community.)&lt;br /&gt;&lt;br /&gt;On the other hand, what  would I have to do if the class was poorly tested? I would have to understand what the code did by reading, test/run it by hand, debug it, etc, before I added the nil-check. Then I would have to repeate the process to make sure everything worked as before. What would the outcome of all this work be? Probably the same nil-check as before, but I would also understand the code much better. Also, I might picked up some good design ideas, learnt to use the tools better, etc.&lt;br /&gt;&lt;br /&gt;Now, I'm not saying that poorly tested code is good. What I am saying is that working with poorly tested code that forces you to fire up the debugger and step through the program will make you debug programs better. I'm saying that working with deep inheritage hiearchies will make you realize &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/inheritance-is-overrated.html"&gt;that inheritance isn't always a good thing&lt;/a&gt;. I'm also saying that reading code with a lot a mutable instance variables and class variables will make you &lt;a href="http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/"&gt;appreciate&lt;/a&gt; (and use) 'final' or 'const' more.&lt;br /&gt;&lt;br /&gt;To take this a bit further, I think you actually get worse at debugging if you developing in an environment where the code is well-tested, because you never have to debug anything. This is true for me, at least.&lt;br /&gt;&lt;br /&gt;I've completely stopped using the debugger. I write test-cases that narrow down the problematic code instead. This combined with a few print-outs is all I need. I think this is easier, and more valuable in the long-run because my efforts are mirrored in a few test-cases that document that there was a bug that was fixed. Would I simply had fired up the debugger and found (and fixed) the problem, there would be no (exectubale) documentation of the bug-fix.&lt;br /&gt;&lt;br /&gt;So, one way of becoming a better developer is, I think, to improving quality of untested code because it'll forces you to reason about the program and it's control flow based on scarce information (e.g., logs and stack-traces) among other things. On the other hand, working with well-tested code is much easier:  write a test, make the change you have to make to the production code and run all tests. Do they still pass? Great! Code is ready to be checked-in. Good for the project's progress.  What have you learnt? Nothing!  Bad for you. In some sense.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8246681779350541077?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8246681779350541077/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8246681779350541077' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8246681779350541077'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8246681779350541077'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/07/lessons-from-debugger.html' title='Lessons from a debugger'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-4949461049328261083</id><published>2008-06-25T10:07:00.000-07:00</published><updated>2008-06-25T10:17:37.723-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='evaluate'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='paradox'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>The Blub paradox</title><content type='html'>Yesterday I read someting interesting concerning how programming languages are compared:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;«[T]o explain this point I'm going to use a hypothetical language called Blub. Blub falls right in the middle of the abtractness continuum. It is not the most powerful language, but it is more powerful than Cobol or machine language.&lt;br /&gt;&lt;br /&gt;And in fact, our hypothetical Blub programmer wouldn't use either of them. Of course he wouldn't program in machine language. That's what compilers are for. And as for Cobol, he doesn't know how anyone can get anything done with it. It doesn't even have x (Blub feature of your choice).&lt;br /&gt;&lt;br /&gt;As long as our hypothetical Blub programmer is looking down the power continuum, he knows he's looking down. Languages less powerful than Blub are obviously less powerful, because they're missing some feature he's used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn't realize he's looking up. What he sees are merely weird languages. He probably considers them about equivalent in power to Blub, but with all this other hairy stuff thrown in as well. Blub is good enough for him, because he thinks in Blub.&lt;br /&gt;&lt;br /&gt;When we switch to the point of view of a programmer using any of the languages higher up the power continuum, however, we find that he in turn looks down upon Blub. How can you get anything done in Blub? It doesn't even have y.&lt;br /&gt;&lt;br /&gt;By induction, the only programmers in a position to see all the differences in power between the various languages are those who understand the most powerful one. (This is probably what Eric Raymond meant about Lisp making you a better programmer.) You can't trust the opinions of the others, because of the Blub paradox: they're satisfied with whatever language they happen to use, because it dictates the way they think about programs.»&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Check &lt;a href="http://web.cs.mun.ca/~ulf/pld/princ.html#EvalPL"&gt;this&lt;/a&gt; out to read more about how to evaluate programming languages.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-4949461049328261083?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/4949461049328261083/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=4949461049328261083' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4949461049328261083'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4949461049328261083'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/06/blub-paradox.html' title='The Blub paradox'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-8985909919610946639</id><published>2008-06-04T08:39:00.000-07:00</published><updated>2008-06-04T13:18:29.691-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='LISP'/><category scheme='http://www.blogger.com/atom/ns#' term='Collections'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Itsy Bitsy Bag'/><title type='text'>LISP's mapcar for Java: onAll-collect</title><content type='html'>I've been working on a project were I need to iterate over a set of objects, get some property from all objects in the collection, and store that property in a new collection. This wouldn't be much if an issue if I did it in LISP or  Ruby or some think similar... but this is done in Java with th the &lt;tt&gt;java.util.Collection&lt;/tt&gt; framework. The Collection framework is nice and all, but when I have to write:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;span&gt;&lt;code&gt;List&amp;lt;SomeProperty&amp;gt; allProperties(List&amp;lt;SomeClass&amp;gt; objects) {&lt;br /&gt;  List&amp;lt;SomeProperty&amp;gt; properties = new ArrayList&amp;lt;SomeProperty&amp;gt;();&lt;br /&gt;  for (SomeClass c : objects)&lt;br /&gt;    properties.add(c.getProperty());&lt;br /&gt;  return properties;&lt;br /&gt;}&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;when all I really wish to say is:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;/code&gt;&lt;span&gt;&lt;code&gt;(defun all-propreties (objects)&lt;br /&gt;  (mapcar get-property objects))&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;I get a bit sad. Sure computer technology is progressing, but are computer languages? LISP appeared in the late 50-ies and Java in the mid 90-ties, but looking at the code above I can't really tell that there is almost 40 years between these two languages. Crazy...&lt;br /&gt;&lt;br /&gt;Anyway, to make me a bit happier I started to think about how the Java code above could be improved to make it a little bit more terse. What I came up with was this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;span&gt;&lt;code&gt;List&amp;lt;SomeProperty&amp;gt; allProperties(Bag&amp;lt;SomeClass&amp;gt; objects) {&lt;br /&gt;  return objects.collect(objects.onAll().getProperty());&lt;br /&gt;}&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;which  I honestly think is pretty neat. Then again, I'm just an ape-descended life form who are so amazingly primitive that I still think digital watches are a pretty neat idea too.&lt;br /&gt;&lt;br /&gt;How does this work? Well, first of all the &lt;tt&gt;objects&lt;/tt&gt; variable is not the same type in the two Java examples above. In the latter example, it is a class that I've implemented specially to deal with the scenario described. This class, which is called &lt;tt&gt;Bag&amp;lt;T&amp;gt;&lt;/tt&gt;, has a method &lt;tt&gt;onAll(T)&lt;/tt&gt; that returns an &lt;a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/Proxy.html"&gt;dynamic proxy&lt;/a&gt; implementing the &lt;tt&gt;T&lt;/tt&gt; interface. That is, in the example above the &lt;tt&gt;objects.onAll()&lt;/tt&gt; returns an instance of the &lt;tt&gt;SomeProperty&lt;/tt&gt; interface.&lt;br /&gt;&lt;br /&gt;This dynamic proxy handles every method call it receives by calling that method on each object in the &lt;tt&gt;Bag&lt;/tt&gt;. Also, if the called method is non-void, then it returns whatever the last object in the &lt;tt&gt;Bag&lt;/tt&gt; returned. This means that &lt;tt&gt;bag.onAll().someMethod()&lt;/tt&gt; behaves just as &lt;tt&gt;thing.someMethod()&lt;/tt&gt; if &lt;tt&gt;bag&lt;/tt&gt; contains just the &lt;tt&gt;thing&lt;/tt&gt; objects. This is a Good Thing in my book. :)&lt;br /&gt;&lt;br /&gt;&lt;i&gt;How about the &lt;tt&gt;Bag.collect&lt;/tt&gt; method?&lt;/i&gt;, you ask. &lt;i&gt;Funny you should ask that. I was just getting to that&lt;/i&gt;, I reply. &lt;i&gt;Cut to the chase already!&lt;/i&gt;, you say. &lt;i&gt;Cool it&lt;/i&gt;, I say, &lt;i&gt; or there won't be any desert!&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;(Ok, I'm getting a bit carried away.)&lt;br /&gt;&lt;br /&gt;The &lt;tt&gt;Bag.collect&lt;/tt&gt; simply returns the set of return values got when last calling a method on the &lt;tt&gt;onAll&lt;/tt&gt;-object. Ehm, it a bit hard explain with words... but I think you understand. If you don't, look at the &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/Sybil/src/programmatically/speaking/sybil/many/Bag.java?r=64"&gt;code&lt;/a&gt; and &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/Sybil/test/programmatically/speaking/sybil/many/BagTest.java?r=64"&gt;test-cases&lt;/a&gt;. :)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that the &lt;tt&gt;onAll&lt;/tt&gt; method returns an object that can be used for more than what's described above. Whenever you need to treat a set of objects as if they were one object, for instance the &lt;a href="http://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer pattern&lt;/a&gt;, the &lt;tt&gt;onAll&lt;/tt&gt;-object simplifies alot.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-8985909919610946639?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/8985909919610946639/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=8985909919610946639' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8985909919610946639'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/8985909919610946639'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/06/lisps-mapcar-for-java-onall-collect.html' title='LISP&apos;s mapcar for Java: onAll-collect'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7853701059297721686</id><published>2008-05-20T11:21:00.000-07:00</published><updated>2008-05-23T09:33:59.227-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='exception'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><title type='text'>What makes a class?</title><content type='html'>A few days ago, when I was "merrily" washing my dishes, I started to think about what makes a class necessary. That is, how we could identify code that should be placed in a class. I came up with a few obvious cases, and some less obivous. Obvious:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;a  new domain entity is needed,&lt;/li&gt;&lt;li&gt;to simplify testing, &lt;/li&gt;&lt;li&gt;remove conditional logic and special cases,&lt;br /&gt;&lt;/li&gt;&lt;li&gt;remove duplicated code,&lt;/li&gt;&lt;li&gt;move private private methods of a class to a new class; and&lt;/li&gt;&lt;li&gt;split a class that has  multiple responsibilities.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Yeah, I know, ob-vio-us... But as I said, when I joyfully made my forks and spoons clean and shiny, I found a couple of cases that are commonly ignored or missed (at least by me, until now):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;there are recurring patterns in variable names and variable type, e.g., &lt;tt&gt;java.nio.ByteBuffer payload = ...&lt;/tt&gt; indicates that there is a need for a &lt;tt&gt;Payload&lt;/tt&gt; class,&lt;/li&gt;&lt;li&gt;circular dependencies between  classes can be solved by introducing a new class, e.g., if &lt;tt&gt;Alice&lt;/tt&gt; and &lt;tt&gt;Bob&lt;/tt&gt; has references to each other for exchanging messages, then a  &lt;tt&gt;Channel&lt;/tt&gt; should be introduced that they both will use to send and receive messages;&lt;/li&gt;&lt;li&gt;an exception exposing implementation details should be replaced with a new exception mathing the abstraction level of the class/method throwing the exception,&lt;/ul&gt;&lt;br /&gt;A comment on classes that solves circular dependencies: it is possible to solve this kind of dependency by simply introducing an interface (e.g., &lt;tt&gt;Alice&lt;/tt&gt; and &lt;tt&gt;Bob&lt;/tt&gt; could implement a &lt;tt&gt;MessageReceiver&lt;/tt&gt; interface), but this misses the point a wish to make. By introducing a new &lt;i&gt;class&lt;/i&gt; instead of an interface, domain logic can be placed where it is more suitable (e.g., the &lt;tt&gt;Channel&lt;/tt&gt; can take care of serializing the messages passes between &lt;tt&gt;Alice&lt;/tt&gt; and &lt;tt&gt;Bob&lt;/tt&gt;).&lt;br /&gt;&lt;br /&gt;Anyway, these are a few ways we can use classes to simplify code by introducing additional classes. There are probably a lot more... :)&lt;br /&gt;&lt;br /&gt;Oh, by the way: some exceptions in the packages &lt;tt&gt;java....&lt;/tt&gt; miss a &lt;tt&gt;SomeException(String description, Throwable cause)&lt;/tt&gt; constructor. Why is this? It makes catch-wrap-rethrow really hard. This is very important for hiding imlementation details.&lt;br /&gt;&lt;br /&gt;Furthermore, why are there no proper hiearchy for (example for) the exceptions thrown by the reflection mechanism? I hate to catch four-five exceptions every time I do something with reflection... &lt;tt&gt;NoSuchMethodException&lt;/tt&gt;, &lt;tt&gt;SecurityException&lt;/tt&gt;, &lt;tt&gt;NoSuchFieldException&lt;/tt&gt;, &lt;tt&gt;InvocationTargetException&lt;/tt&gt;?! Come on! Why can't I just catch &lt;tt&gt;ReflectionException&lt;/tt&gt; and get it over with? Also, give me multi-catch so that I can catch more than one Exception in each catch-block. Please!&lt;br /&gt;&lt;br /&gt;Well, who ever said Java was perfect? Or even close to perfect...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7853701059297721686?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7853701059297721686/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7853701059297721686' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7853701059297721686'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7853701059297721686'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/05/what-makes-class.html' title='What makes a class?'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3630595356094992354</id><published>2008-05-07T07:34:00.000-07:00</published><updated>2008-05-07T08:41:11.894-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='testing'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Do not write tests for your code!</title><content type='html'>I am of the opinion that developers should not write test for their own code. The reason is that the tests just test that the production code does what it does -- not necessarily that it does the right thing. That is, the tests just "mirrors" the production code. In fact, the test can actually test the wrong thing entirely!&lt;br /&gt;&lt;br /&gt;On the other hand, if someone else writes the tests, then he/she does not know any details about the implementation, thus the tests do not mirror the production code. &lt;br /&gt;&lt;br /&gt;Of course, there are problems with this... how does the tester know what the code is supposed to do? Good documentation? The trouble is that the documentation will (probably) describe what the code does, not what it is supposed to do. Why? Because it (probably) written after the code. Also, to use an understatement: documentation is boring.&lt;br /&gt;&lt;br /&gt;The point I'm trying to make is that it is very hard to express what a piece of code is &lt;i&gt;supposed&lt;/i&gt; to do when that code is already written. Unless, of course, there are a few test-cases for that piece of code. To my experience, test-cases describe what code is supposed to do very accurately.&lt;br /&gt;&lt;br /&gt;Ok, I think you get where I'm going with this so I'm just going to cut to the chase. I hope you are of the opinion, like me, that developers should not write test for their own code. They should, however, write code passing their tests.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3630595356094992354?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3630595356094992354/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3630595356094992354' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3630595356094992354'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3630595356094992354'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/05/do-not-write-tests-for-your-code.html' title='Do not write tests for your code!'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-805550929052242825</id><published>2008-04-28T07:52:00.000-07:00</published><updated>2008-06-25T10:16:44.143-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='DSL'/><category scheme='http://www.blogger.com/atom/ns#' term='learning'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='zen'/><category scheme='http://www.blogger.com/atom/ns#' term='regular expressions'/><category scheme='http://www.blogger.com/atom/ns#' term='languages'/><title type='text'>The Zen of regular expressions</title><content type='html'>I'm a proud owner, and sometimes wearer, of &lt;a href="http://store.xkcd.com/"&gt;this (scroll down to &lt;i&gt;Regular Expressions Shirt&lt;/i&gt;)&lt;/a&gt;. On my way home from work today I started to think about whether I really know regular expressions. Sure, I can write expressions that match fairly complex patterns... but do I really know them? I came to the conclusion that I know regexp in the same sense as most seven-year-olds (i.e., first graders) can read and write: they know letter and short words, but not much more.&lt;br /&gt;&lt;br /&gt;The funny thing is that if I had been asked this question a few years ago I would have answered &lt;i&gt;of course I know regexps&lt;/i&gt; without much thought. Does that mean that I know less about regular expression now than I did then? No, I know more. I now know enough to know that I don't know them.&lt;br /&gt;&lt;br /&gt;The Zen of regular expressions:&lt;br /&gt;&lt;blockquote&gt;The first step towards knowing regular expressions is to realize you do not know them.&lt;/blockquote&gt;&lt;br /&gt;Since I'm just starting to reach this first step, I cannot tell what the next step will be... or how many steps there are. :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-805550929052242825?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/805550929052242825/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=805550929052242825' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/805550929052242825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/805550929052242825'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/04/zen-of-regular-expressions.html' title='The Zen of regular expressions'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-3290734630315151910</id><published>2008-04-21T08:23:00.000-07:00</published><updated>2008-04-21T10:08:12.734-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='project: Easy Configuration'/><category scheme='http://www.blogger.com/atom/ns#' term='boring'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Boring stuff you have to implement: Configuration, part 2</title><content type='html'>A while ago I wrote a  &lt;a href="http://programmaticallyspeaking.blogspot.com/2008/04/boring-stuff-you-have-to-implement.html"&gt;post&lt;/a&gt; where I proposed an easy way of specifying the configuration of an application. The idea is basically to define a configuration parameter by annotating a methods with information that describes the parameter. Of course, the value of the parameter is retrieved by calling the annotated method. My previous post contains an example.&lt;br /&gt;&lt;br /&gt;To implement this easy-configuration-thingie I use &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Proxy.html"&gt;dynamic proxies&lt;/a&gt;. If you haven't heard of dynamic proxies you have missed one of Javas powerful facilities for metaprogramming. Under the circumstances (e.g., static type-checking) I think it's pretty easy to use too.&lt;br /&gt;&lt;br /&gt;The basic idea behind dynamic proxies is quite simple: let all calls to methods of an interface be delegated to another method. This method is called &lt;tt&gt;invoke&lt;/tt&gt; and is declared in &lt;tt&gt;java.lang.reflect.InvocationHandler&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;As you may suspect, the &lt;tt&gt;invoke&lt;/tt&gt; method receives the all arguements given to the method defined in the interface (i.e., the method that delegated to &lt;tt&gt;invoke&lt;/tt&gt;). It also receives an arguments that describes which method that was called; this is an &lt;tt&gt;java.lang.reflect.Method&lt;/tt&gt; object, which among other things, contains the method's annotations.&lt;br /&gt;&lt;br /&gt;Back to the original topic: configuration. How can all this annotation stuff and proxy fluff be used to define and read configuration?&lt;br /&gt;&lt;br /&gt;Well, as the example in my earlier post shows, the interface that defines the configuration is annotated with the name and the type of the configuration parameter. Since the method's annotations are available to the &lt;tt&gt;invoke&lt;/tt&gt; method, &lt;tt&gt;invoke&lt;/tt&gt; can use the parameter name to look up its value (in a hashmap, or similarly) and return it. It's as simple as that!&lt;br /&gt;&lt;br /&gt;I've made a simple implementation of this availble &lt;a href="http://code.google.com/p/programaticallyspeaking/source/detail?r=26"&gt;here&lt;/a&gt; (follow the instructions on Google Code if you wish to check-out the entire Eclipse project).&lt;br /&gt;Note that some more development is needed before this code is useful, since it does not&lt;br /&gt;read any configuration from file (only default values can be read). &lt;br /&gt;&lt;br /&gt;In general, I tend to think that annotations simply are additional arguments to the annotated method (although a bit harder to use than ordinary arguments). This way of looking at annotations is even more suitable when used together with dynamic proxies, I think.&lt;br /&gt;&lt;br /&gt;You probably already have thought of this, but there are several other ways of using annotations + dynamic proxies: I've used it to parse binary messages and command line arguments (before I know about &lt;a href="http://jewelcli.sourceforge.net/"&gt;JewelCLI&lt;/a&gt;), and I guess you can come up with several other examples...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-3290734630315151910?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/3290734630315151910/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=3290734630315151910' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3290734630315151910'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/3290734630315151910'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/04/boring-stuff-you-have-to-implement_21.html' title='Boring stuff you have to implement: Configuration, part 2'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-4860085982015743810</id><published>2008-04-17T11:39:00.000-07:00</published><updated>2008-04-17T11:56:51.878-07:00</updated><title type='text'>Oups, sorry.</title><content type='html'>While updating my blog (I realized that I misspelled 'programmatically') I accidentily changed the address (on www.javablogs.com) to my other blog (in swedish), which does not discuss Java. My apologies to www.javablogs.com.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-4860085982015743810?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/4860085982015743810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=4860085982015743810' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4860085982015743810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/4860085982015743810'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/04/oups-sorry.html' title='Oups, sorry.'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-7243684960590822751</id><published>2008-04-16T11:46:00.000-07:00</published><updated>2008-04-17T11:12:00.012-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='opinions'/><category scheme='http://www.blogger.com/atom/ns#' term='inheritance'/><category scheme='http://www.blogger.com/atom/ns#' term='rambling'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Inheritance is overrated</title><content type='html'>I like the object-oriented way of developing software -- especially if there is some functional flawor in it. In most language it is fairly easy to at least emulate a functional programming language by simply changing the way you think about the problem and the solution.&lt;br /&gt;&lt;br /&gt;When I think in an functional way about a problem I have to solve using an object-oriented language, objects become collections of related &lt;i&gt;functions&lt;/i&gt; (with this I mean &lt;i&gt;pure function&lt;/i&gt;, i.e., they have no side effects). That is, I think about the program as lambdas that is passed around, rather than instances of classes. This may sound like a trivial and superficial difference but it is not.&lt;br /&gt;&lt;br /&gt;I have found that if I solve a problem in a functional way, the components of the solution (functions, classes, etc) are less coupled than if I solve it in an object-oriented way. Why is this?&lt;br /&gt;&lt;br /&gt;One reason it that an object &lt;i&gt;A&lt;/i&gt; is provided with objects &lt;i&gt;B..Z&lt;/i&gt; that A needs for doing whatever it needs to do. That is, &lt;i&gt;A&lt;/i&gt; only relies on that it gets something that it useful for its purposes, instead on relying on a particular implementation. Another reason is that classes' methods are often pure functions, which decreases coupling because classes does not depend on the state of another class or in which order methods are called.&lt;br /&gt;&lt;br /&gt;Enough rambling. Now to the point. The first reason basically says that a functional mind-set results in an structure of &lt;i&gt;has-a&lt;/i&gt; relations between objects, instead of the "object-oriented way" &lt;i&gt;is-a&lt;/i&gt;. With "is-a" I mean class-inheritance (the &lt;tt&gt;extends&lt;/tt&gt; keyword in Java), which is the strongest way of coupling two classes and the most difficult to reuse, refactor, and understand -- at least for me.&lt;br /&gt;&lt;br /&gt;On the other hand, I find interface-inheritance (the &lt;tt&gt;implements&lt;/tt&gt; keyword in Java) very useful and I rely on it dayly.&lt;br /&gt;&lt;br /&gt;If find it a bit funny that during the years I have used object-oriented languages, I have not once used inheritance... without regretting it. I'm getting better and better, of course, and during the last year I haven't used inheritance at all... and I'm not regretting it.&lt;br /&gt;&lt;br /&gt;Maybe it just me, but I find inheritance overrated.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-7243684960590822751?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/7243684960590822751/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=7243684960590822751' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7243684960590822751'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/7243684960590822751'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/04/inheritance-is-overrated.html' title='Inheritance is overrated'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-6991035643477147637</id><published>2008-04-12T08:28:00.000-07:00</published><updated>2008-04-13T05:30:53.611-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='optimizing'/><category scheme='http://www.blogger.com/atom/ns#' term='logging'/><category scheme='http://www.blogger.com/atom/ns#' term='lazy evaluation'/><title type='text'>Making deactivated logging 100 times faster</title><content type='html'>I think the &lt;tt&gt;java.util.logging&lt;/tt&gt; is a nice logging framework: it's easy to do simple things yet it is not limited. You can easily tweak it using custom filters, formatters, and handlers. One thing I do not like with it, however, is its performance.&lt;br /&gt; &lt;br /&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:130%;"&gt;&lt;i&gt;The problem with logging&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;I have no problem with the performance of &lt;tt&gt;java.lang.logging&lt;/tt&gt; when the logging is activated. It's the performance when logging is deactivated that is an issue for me. The problem, as I see it, is that when &lt;code&gt;&lt;br /&gt;  logger.info("someting: " + something.toString());&lt;br /&gt;&lt;/code&gt; is executed, the argument to &lt;tt&gt;info&lt;/tt&gt; is created (by concatinating two string, which is computationally heavy) despite logging being deactivated. This means that a string will be created and then directly thrown away without being used. To make things even worse, there is even a greater performance penalty if the &lt;tt&gt;toString&lt;/tt&gt; method of &lt;tt&gt;something&lt;/tt&gt; is computionally heavy.&lt;br /&gt;&lt;br /&gt;This is not problem with &lt;tt&gt;java.util.logging&lt;/tt&gt; per se, but rather a problem with the Java language. Don't get me wrong -- I like Java -- but in certain areas Java is simply too limited/limiting. I see at least three way of solving the problem described above:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;introducing some kind of macros to the language,&lt;/li&gt;&lt;li&gt;using aspect-oriented programming, or&lt;/li&gt;&lt;li&gt;performing  string concatination &lt;a href="http://en.wikipedia.org/wiki/Lazy_evaluation"&gt;lazily&lt;/a&gt;.&lt;/li&gt;&lt;/ol&gt;Personly, I think that the common variant of macros (the C/C++-kind) it a Bad Thing. On the other hand, the other variant of macros (the Lisp-kind) does not fit nicely in the Java languange because those kinds of macros operate on the &lt;a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree"&gt;AST&lt;/a&gt; (this is perfectly ok in Lisp because Lisp does not have any syntax -- you're actually creating the AST when you write the program).&lt;br /&gt;&lt;br /&gt;The second solution to the problem is aspect-oriented programming. To be honest, I don't know enough about that to be able to discuss it here. With the limited knowledge I do have, however, I think that it should be possible to instrument the piece of code above such that you get the following sematic:&lt;code&gt;&lt;br /&gt;  if (logger.logsAtLevel(Level.INFO) {&lt;br /&gt;    logger.info("someting: " + something.toString());&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The third solution -- performing string concatination lazily -- is the solution I will discuss for the rest of this post. I'm assuming that the methods used to create the log message, e.g., the &lt;tt&gt;toString&lt;/tt&gt; method, are are pure functions, i.e., has no side-effect. This is a perfectly legitimate assumption because deactivated logging should have no side effect as it is.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;i&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Lazy string concatination&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Ok, so how can we make string concatination in Java lazy? In C++ we could have overloaded the operator +, but this is not possible in Java.  One hacker-ish solution would be to implement new &lt;tt&gt;String&lt;/tt&gt; and &lt;tt&gt;StringBuilder&lt;/tt&gt; classes (which the compiler uses to implement string concatination) which performs concatination lazily, but this not trivial... (I have actually tried... (and failed)). Instead, we can implement a thin wrapper around &lt;tt&gt;java.util.logging.Logger&lt;/tt&gt; with the following methods:&lt;code&gt;&lt;br /&gt;  MyLogger log(Object msg);&lt;br /&gt;  MyLogger log(Object msg1, Object msg2);&lt;br /&gt;  MyLogger log(Object msg1, Object msg2, Object msg3);&lt;br /&gt;  // ... and so on.&lt;br /&gt;  void info(Object msg);&lt;br /&gt;  // ... and all the the other levels.&lt;br /&gt;&lt;/code&gt;which is used like this:&lt;code&gt;&lt;br /&gt;  myLogger.log("Received message: ", msg, " from ").info(msgProvider);&lt;br /&gt;&lt;/code&gt;which is the equivalent of&lt;code&gt;&lt;br /&gt;  logger.info("Received message: " + msg + " from " + msgProvider);&lt;br /&gt;&lt;/code&gt; when using a &lt;tt&gt;java.util.logging.Logger&lt;/tt&gt;. The &lt;tt&gt;log&lt;/tt&gt; methods is simply implemented by storing the references to the objects given as arguments. The &lt;tt&gt;info&lt;/tt&gt; method is implemented by calling &lt;tt&gt;toString&lt;/tt&gt; on its argument and the argument given to &lt;tt&gt;log&lt;/tt&gt; &lt;i&gt;if&lt;/i&gt; the logging is actived, otherwise it does nothing.&lt;br /&gt;&lt;br /&gt;I (kind of) have implemented such class; the difference is that instead of wrapping a &lt;tt&gt;java.util.logging.Logger&lt;/tt&gt; my class uses a &lt;tt&gt;java.util.logging.Handler&lt;/tt&gt; directly. The interface of this class, which I named &lt;tt&gt;Ln4j&lt;/tt&gt; (pun definitely intended), is the same as &lt;tt&gt;MyLogger&lt;/tt&gt; above, however.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;span&gt;&lt;i&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;Performance measuments&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;So, what kinds of performance numbers can we expect? &amp;lt;disclamer&amp;gt;I'm definitely no expert in measuring performance, but I have tried my best to create fair benchmarks.&amp;lt;/disclamer&amp;gt; These are the benchmarks:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;logging single constant string,&lt;br /&gt;&lt;/li&gt;&lt;li&gt;concatinating two constant string and log the result,&lt;/li&gt;&lt;li&gt;concatinating a constant string and a variable string and log the result,&lt;/li&gt;&lt;li&gt;concatinating  six short (4 characters) variable strings,&lt;/li&gt;&lt;li&gt;concatinating  six long (40 characters) variable strings,&lt;li&gt;concatinating a constant string and an &lt;tt&gt;int&lt;/tt&gt; and log the result,&lt;/li&gt;&lt;li&gt;concatinating a constant string and a &lt;tt&gt;List&amp;lt;double&amp;gt;&lt;/tt&gt; (of length 8) and log the result.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;I ran these benchmars with and without the &lt;tt&gt;-server&lt;/tt&gt; switch to the JVM and with logging activated and with logging deactivated. &lt;a href="http://programaticallyspeaking.googlecode.com/svn-history/r24/ln4j/result/ln4j-vs-java-util-logging.pdf"&gt;This&lt;/a&gt; is the result.&lt;br /&gt;&lt;br /&gt;In summary: with logging activated &lt;tt&gt;ln4j&lt;/tt&gt; performs a bit faster than &lt;tt&gt;java.util.logging&lt;/tt&gt;. However, since &lt;tt&gt;ln4j&lt;/tt&gt; is quite simple (e.g., it has no log levels) this small performance advantage would probably disapper if &lt;tt&gt;ln4j&lt;/tt&gt; implemented all functionality provided by &lt;tt&gt;java.util.logging.Logger&lt;/tt&gt;.&lt;br /&gt;When running the benchmarks with logging deactivated, there is usually considerable performance gains (no, the post title is no exaggeration). Of course, exact numbers depend on what is logged. When logging a single constant &lt;tt&gt;ln4j&lt;/tt&gt; is actually somewhat slower. However, in the benchmark that logs a list, &lt;tt&gt;ln4j&lt;/tt&gt; is 600-700 times faster than &lt;tt&gt;java.util.logging&lt;/tt&gt;. That optimzation for ya!&lt;br /&gt;&lt;br /&gt;I hope this post was informative and that you have learned something from reading it. I learned a lot when experimenting with lazy string concatination; let's hope it will native in Java 8. :)&lt;br /&gt;&lt;br /&gt;Oh, I almost forgot, &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/ln4j/src/programmatically/speaking/ln4j/LnLogger.java?r=20"&gt;here&lt;/a&gt; and &lt;a href="http://code.google.com/p/programaticallyspeaking/source/browse/ln4j/test/programmatically/speaking/ln4j/performance/Standard.java?r=22"&gt;here&lt;/a&gt; are the source used in the benchmarks.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8875558920754897232-6991035643477147637?l=programmaticallyspeaking.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programmaticallyspeaking.blogspot.com/feeds/6991035643477147637/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8875558920754897232&amp;postID=6991035643477147637' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6991035643477147637'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8875558920754897232/posts/default/6991035643477147637'/><link rel='alternate' type='text/html' href='http://programmaticallyspeaking.blogspot.com/2008/04/making-deactivated-logging-100-times.html' title='Making deactivated logging 100 times faster'/><author><name>Torgny</name><uri>http://www.blogger.com/profile/12146418455302215748</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><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8875558920754897232.post-5458943402372200421</id><published>2008-04-09T11:59:00.000-07:00</published><updated>2008-04-09T13:35:54.953-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='project: First-class MBean names'/><category scheme='http://www.blogger.com/atom/ns#' term='java beans'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='annoy'/><category scheme='http://www.blogger.com/atom/ns#' term='annotation'/><title type='text'>Making MBean names first-class</title><content type='html'>Time for yet 
