<?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-9503662</id><updated>2011-07-28T15:12:20.824-04:00</updated><title type='text'>Meta Programming</title><subtitle type='html'>Theory and pragmatics of advanced programming techniques, with a focus on meta programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9503662.post-115646031189531604</id><published>2006-08-24T18:55:00.000-04:00</published><updated>2006-08-24T18:58:41.746-04:00</updated><title type='text'>Off to blog.davber.com!</title><content type='html'>I have moved my thoughts and spirit to &lt;a href="http://blog.davber.com"&gt;davber does IT&lt;/a&gt;. So, change your links.&lt;br /&gt;&lt;br /&gt;Thanks for visiting me here. You will encounter more "soft" content over at blog.davber.com. Just click on the proper category or tag to get to the hard core stuff.&lt;br /&gt;&lt;br /&gt;Bye!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-115646031189531604?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/115646031189531604/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=115646031189531604&amp;isPopup=true' title='38 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/115646031189531604'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/115646031189531604'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2006/08/off-to-blogdavbercom.html' title='Off to blog.davber.com!'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>38</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-115207893343740523</id><published>2006-07-05T01:55:00.000-04:00</published><updated>2006-07-05T02:14:49.193-04:00</updated><title type='text'>Swig Swings Java</title><content type='html'>&lt;span style="font-family:Courier New;font-size:85%;"&gt;First I was afraid, I was petrified. Kept thinking I could never live without a proper JNI reference manual by my side. But I spent so many nights thinking how to implement those native methods right. I grew strong, I learned how to carry on without a proper wrapper generator. &lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;But now Swig is back, from a quite silent place. I just Googled in and found that it had reached Version 1.3. I should have closed that stupid JNI reference book. If I had known for just one second that Swig would be back to help me.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;After having looked at that version I have changed my mind. Swig &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;does &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;provide some value - or at least does not make the native bridging less dynamic than raw JNI. A simple example of Swig with Java follows.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;For Java, you can specify an interface file, which &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;could &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;be the regular C header for the native code, such as&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// File: mathus.h&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;extern int fac(int arg);&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;This is done via the following command&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;swig -java -module Mathus mathus.h&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;which will create a few new files:&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1. Mathus.java - a class 'Mathus' with wrapper methods for all the functions specified in 'fac.h' (i.e., only the 'fac' function...) These wrappers are very thin (and do not add much value, to be honest.)&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2. MathusJNI.java - the Java side of the native implementation, which is nothing but a signature of the function and the keyword 'native'. Note that Java client code should *not* use this class, but rather the aforementioned wrapper class.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3. mathus_wrap.c - ah, the JNI implementation in all its g(l)ory. Read it and weep. The reason for all the obfuscation is to handle ALL imaginable compilers.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Let us create an implementation of that advanced numerical function:&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// mathus.c&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int fac(int arg)&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return arg == 0 ? 1 : arg*fac(arg-1);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;"But that prohibits those neat last call optimization tricks of the compiler, you should use an accumulating parameter!"&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Sit down, you computer science freak! This is not class, this is real life, with real stacks! And do &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;not &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;mention negative numbers!&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Ok, so now when we have these five files, whereof Swig created three, we just have to create a DLL. Since we do not want to spend money on Microsoft tools, but rather choose to give our money to the Gates Foundation in a more direct manner, we use GCC. The MinGW port, to be exact. Just install it - because &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;all &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;serious developers need a GCC implementation on their machine. Ok, a version of Emacs along with a decent LaTeX installation might qualify as well.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;You have to type (or, better yet, put in a makefile...) the following, after setting the variable JAVA_INCLUDE to point to the include directory of your Java JDK installation (which is usually %JDK_HOME%\include):&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;gcc -Wall -Wl,--kill-at -shared -o mathus.dll -I%JAVA_INCLUDE% -I%JAVA_INCLUDE%\win32 Mathus_wrap.c fac.c&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;The "-Wall" is for our S&amp;M needs. Yes, Mr. Compiler, I have been a bad boy! Spank those unused variables out of me!&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Now you have a nice JNI DLL. In order to use it, you just have to make sure to load it explicitly before the class with the native method (MathusJNI) is first used.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;For instance:&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// File: MathusClient.java&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public MathusClient {&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public static void main(String[] args) {&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Let us do something really intricate here:&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int arg = Integer.parseInt(args[0]);&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int facRes = Mathus.fac(arg);&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.out.println("fac(" + arg + ") = " + facRes);&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;static {&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Yep, let's load that DLL and get it over with...&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.loadLibrary("mathus");&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Just compile it and run. And, more importantly, enjoy! Your first Java app with some cojones.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;DISCLAIMER: it is late and I am typing all this from memory, so there might be typos, although I doubt it. There probably was one in that doubtful sentence...&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-115207893343740523?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/115207893343740523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=115207893343740523&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/115207893343740523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/115207893343740523'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2006/07/swig-swings-java.html' title='Swig Swings Java'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-112968764983647775</id><published>2005-10-18T22:07:00.000-04:00</published><updated>2005-10-18T22:07:29.863-04:00</updated><title type='text'>Casting Types In C++</title><content type='html'>These types do not match? What to do? We force one of the expressions to be of the same type as the other! Right?&lt;br/&gt;&lt;br/&gt;Wrong.&lt;br/&gt;&lt;br/&gt;One should avoid converting values between types, even if one knows – or assumes – that the conversion does not change the value per se.&lt;br/&gt;&lt;br/&gt;More importantly, one should avoid C-style conversions, such as&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Foo foo = (Foo)bar;&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;since they do whatever it takes, including totally unsafe, and platform-specific transformations.&lt;br/&gt;&lt;br/&gt;Using C++ there are three different standard type casting operators to use:&lt;br/&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-family:Courier New;"&gt;Foo foo = &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;"&gt;static_cast&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;"&gt;&amp;lt;Foo&amp;gt;(bar);&lt;/span&gt;&lt;/li&gt;&lt;br/&gt;&lt;li&gt;&lt;span style="font-family:Courier New;"&gt;Foo foo = &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;"&gt;dynamic_cast&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;"&gt;&amp;lt;Foo&amp;gt;(bar);&lt;/span&gt;&lt;/li&gt;&lt;br/&gt;&lt;li&gt;&lt;span style="font-family:Courier New;"&gt;Foo foo = &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;"&gt;reinterpret_cast&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;"&gt;&amp;lt;Foo&amp;gt;(bar);&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;br/&gt;The first operator will behave similarly on all platforms and with all compilers, and is the preferable one. That operator only works if the types are compatible, and will issue a warning if information might be lost, such as in&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned int val = 20U;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int n = static_cast&amp;lt;int&amp;gt;(val);&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int m = static_cast&amp;lt;int&amp;gt;(&amp;val); // will not compile!&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;The second operator downcasts a generic type to a more specific type. This is often used to downcast a pointer:&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Employee *employee = GetSomeEmployee();&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Boss *bigBoss = dynamic_cast&amp;lt;Boss&amp;gt;(employee);&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int who = dynamic_cast&amp;lt;int&amp;gt;(bigBoss); // will not compile!&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;br/&gt;It will only compile if the destination type is a sub class of the source type (as deduced statically from the expression.) Needless to say, one should &lt;strong&gt;not &lt;/strong&gt;subclass, but rather use polymorphic operations, such as the regular virtual functions of C++.&lt;br/&gt;&lt;br/&gt;The third operator is the ugly one, just trying to reinterpret the bits of the source as a value of the target type. Note especially that this kind of casting from a base class object to a subclass instance is &lt;strong&gt;not &lt;/strong&gt;guaranteed to work, since such a downcast often requires some manipulation of the pointer. But&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int who = reinterpret_cast&amp;lt;int&amp;gt;(bigBoss); // will compile, but watch out!&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;Q: &lt;/strong&gt;Hey, why are these casting operators so ugly? And why not just use the C-style conversion?&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Boss *bigBoss = (Boss*)employee;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;A: &lt;/strong&gt;Because the cast is potentially unsafe and unportable. It will actually try the C++-style casting operators in order, and potentially ending up using &lt;span style="font-family:Courier New;"&gt;reinterpret_cast &lt;/span&gt;which is highly unsafe. The ugliness is there to remind you that you should not cast at all…&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Never ever use C-style conversions&lt;/strong&gt;&lt;span style="font-family:Courier New;"&gt;.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;Try to avoid casting types – use polymorphism instead.&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Only use &lt;span style="font-family:Courier New;"&gt;reinterpret_cast &lt;/span&gt;when you writing compilers or abstract machines.&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-112968764983647775?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/112968764983647775/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=112968764983647775&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112968764983647775'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112968764983647775'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2005/10/casting-types-in-c.html' title='Casting Types In C++'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-112874586439928043</id><published>2005-10-08T00:31:00.000-04:00</published><updated>2005-10-08T00:31:04.403-04:00</updated><title type='text'>The Virtues of Laziness</title><content type='html'>One of the greatest virtues of programmers is that we are lazy. Regularly this means that we do not want to spend a lot of time and/or energy in doing stuff that bore us.&lt;br/&gt;&lt;br/&gt;We should be lazy in that common-sensical way, but we should also make our code lazy in two ways, whereof only the second one is analogous to the aforementioned human laziness, whereas the first one is usually considered its dual, “planning ahead”:&lt;br/&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;Do not repeat labor.&lt;/li&gt;&lt;br/&gt;&lt;li&gt;Do not do anything before you need to.&lt;/li&gt;&lt;/ol&gt;&lt;br/&gt;Even though these two statements seem simple and obvious, it is one thing to just read them and imagine oneself intrinsically following them by the nature of being a “programmer.” Actually, there are quite few “programmers” who follow these statements to their fullest, and reach that delicate balance of duals. A balance of expression.&lt;br/&gt;&lt;br/&gt;Let us look further at these two seemingly simple statements.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;Do not repeat labor&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;There are two aspects to this: logic and data.&lt;br/&gt;&lt;br/&gt;As to not repeating logic, this means identifying a recurring pattern of labor, describing that pattern formally and, finally, using these magnificent machines we call computers to carry out the corresponding laborious tasks over and over again. This goes for all levels, from the simplistic level of actually &lt;strong&gt;automating manual work &lt;/strong&gt;– this is what conventionally is called “programming” - to writing generic code, such as employing the notions and ideas of &lt;strong&gt;Generic Programming&lt;/strong&gt;.&lt;br/&gt;&lt;br/&gt;There is also a data aspect to not repeating labor and that is to not calculate the same data over and over again. This is called &lt;strong&gt;caching&lt;/strong&gt;.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;Do not do anything before you need to&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;This ranges from not trying to optimize code in advance to actually not calculating data we might not need.&lt;br/&gt;&lt;br/&gt;Unfortunately, only non-common programming languages have intrinsic support for this kind of laziness. Those languages are almost always in the category of functional languages, such as Haskell. Those languages are called &lt;strong&gt;lazy &lt;/strong&gt;– or &lt;strong&gt;non-strict &lt;/strong&gt;if you are into language semantics.&lt;br/&gt;&lt;br/&gt;Luckily, there are both libraries for more common languages that provide lazy evaluation, such as the eminent &lt;strong&gt;FC++ &lt;/strong&gt;for C++, and frequently used notions that are intrinsically lazy. Consider TCP/IP streams for this latter category. The revival of asynchronous queuing is also a variant of that example. The problem is that these realizations of the “lazy pattern” are purely inter-modular and quite often even inter-process. &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;What I am saying?&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Be lazy, but never ever sloppy ;-)&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;I will provide examples of how one can use laziness in C++ later.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-112874586439928043?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/112874586439928043/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=112874586439928043&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112874586439928043'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112874586439928043'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2005/10/virtues-of-laziness.html' title='The Virtues of Laziness'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-112874389519931504</id><published>2005-10-07T23:58:00.000-04:00</published><updated>2005-10-08T01:21:21.336-04:00</updated><title type='text'>Always Deal With Compiler Warnings</title><content type='html'>&lt;span style="font-family:Courier New;font-size:85%;"&gt;There are many rules of proper C++ programming, ranging from style to use of constructs. Some of these rules are so “obvious” that masters in the field no longer bother to mention them.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;One of these “obvious” and old rules is one of the most important ones, and, luckily, easiest to follow: &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;to always deal with compiler warnings&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;. Always. No exception.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;What this means is to simply &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;not have any warnings&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;. There are two ways to get rid of warnings:&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Comply with the compiler, i.e., write better code.&lt;/span&gt;&lt;/li&gt;&lt;br/&gt;&lt;li&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Hide the warnings – via compiler flags or pragmas.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;I hope it is obvious that the first one is the best option, and one should strongly work towards that goal. There are very few exceptions, where one might consider the second option. One of them is where the compiler does not comply with the language used, such as Microsoft’s C++ compiler’s constant complaining about not caring about “throw declarations.” Another one is when you are doing intrinsically tricky stuff with bits and nibbles, such as writing a compiler.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;In other words, you should almost always get rid of warnings by not provoking the compiler. And, you should &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;always &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;get rid of them.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Always.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;If only life outside our software confines was this easy.&lt;/span&gt;&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-112874389519931504?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/112874389519931504/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=112874389519931504&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112874389519931504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112874389519931504'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2005/10/always-deal-with-compiler-warnings.html' title='Always Deal With Compiler Warnings'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-112441635711236607</id><published>2005-08-18T21:52:00.000-04:00</published><updated>2005-08-18T22:04:57.693-04:00</updated><title type='text'>Exceptions are part of the interaction</title><content type='html'>&lt;em&gt;&lt;span style="font-size:85%;"&gt;This blog entry discusses exceptions and their relationship to other interaction points. I here argue that the throwing and catching of exceptions should be regarded more or less as any other interaction between modules and thus conform to the chosen abstractions of those modules.&lt;/span&gt;&lt;/em&gt;&lt;br/&gt;&lt;br/&gt;There is a constant debate about the virtues of checked exceptions. Checked exceptions mean that the potential thrower, or propagator, of the exception have to declare that potential act of throwing in advance, and that any users of the code have to either catch such exceptions or in turn declare itself as a propagator.&lt;br/&gt;&lt;br/&gt;In the naïve use of this model, a ripple effect is seen, where either:&lt;br/&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;A chain of client modules all have to declare the potential throwing of that same exception. Pass thru.&lt;/li&gt;&lt;br/&gt;&lt;li&gt;One of the client modules have to handle that exception, potentially thrown from deep down the call chain.&lt;/li&gt;&lt;/ol&gt;&lt;br/&gt;The first alternative seems redundant and tedious. The second one is scary, since the upper-level client module might have little knowledge about the lower-level exception thrown. The typical example of the second scenario is a mapping abstraction throwing a &lt;strong&gt;NoSuchKeyException &lt;/strong&gt;due to a non-existing key. That exception does not make much sense higher-up the call chain, where the module &lt;strong&gt;IncreaseEmployeeSalary &lt;/strong&gt;is trying to deal with the fortunate event of an employee getting a raise.&lt;br/&gt;&lt;br/&gt;It is quite understandable that developers, faced with these sad scenarios, tend to either silently ignore telling clients about that potential exception, where the language allows for such laziness, or use unchecked exceptions – a class of exceptions being so unpredictable that not even the implementer himself knows that it will be thrown. Unchecked exceptions often deal with resource availability issues, such as &lt;strong&gt;OutOfMemoryException&lt;/strong&gt;.&lt;br/&gt;&lt;br/&gt;In the face of having to deal with checked exceptions – after all, the serving code could have been implemented by someone else, not as lazy as the client developer – those developers would simply catch those, often lower-level, exceptions and do nothing, but perhaps log it. There is not much knowledge about the exception at that more abstract client level, so what else can one do?&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;&lt;em&gt;Answer: One should treat exceptions with the same respect as any other interaction between modules, i.e., make it part of the API, at the same abstraction level as function calls.&lt;/em&gt;&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;This implies that the &lt;strong&gt;IncreaseEmployeeSalary &lt;/strong&gt;module &lt;em&gt;should not &lt;/em&gt;throw a &lt;strong&gt;NoSuchKeyException&lt;/strong&gt;. Whether it informs its surrounding of that potential event – by declaring the throwing of that exception - or not is irrelevant to this discussion. That exception is simply at a completely wrong level of abstraction.&lt;br/&gt;&lt;br/&gt;What should happen is that &lt;strong&gt;IncreaseEmployeeSalary &lt;/strong&gt;is informed of an underlying problem, perhaps directly from that mapping abstraction, if the former happens to be a direct client of the latter. That event should in turn result in another event, possibly throwing an exception &lt;em&gt;at the proper abstraction level &lt;/em&gt;of the former module, e.g., &lt;strong&gt;EmployeeNotFoundException&lt;/strong&gt;. Note that there should not be an automatic “translation” of the lower-level to the upper-level exception, since the contract of the higher-level module might stipulate some other, non-exceptional, handling of that event.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;Potential Problems&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;&lt;em&gt;Frameworks have no idea what the domain-specific exceptions would be&lt;/em&gt;.Yes, that is true, although one could in most cases wrap such framework components in more domain-specific code, which could turn the exceptions to more adequate ones. This is harder with template-based generic constructs, which are even mixed in via inheritance in some cases. This is why most hard-core C++ developers have given up on declaring exception throwing, but simply either wait for that unhandled exception handler, which often terminates the thread, or catch anything at a high level.&lt;/li&gt;&lt;br/&gt;&lt;li&gt;&lt;em&gt;How can you know what exceptions are potentially thrown from deep down&lt;/em&gt;?In environments forcing the declaration of such acts, there are ways. The important point here is that the implementer &lt;em&gt;should &lt;/em&gt;know, either way, following the principle of treating exceptions as any other API element.Exceptions from deep down should be considered bugs.&lt;/li&gt;&lt;br/&gt;&lt;li&gt;&lt;em&gt;During debugging, at least, one needs to know what INITIALLY triggered this exception&lt;/em&gt;.Most language environments allow for nested exceptions, where a higher-level exception can carry the initiator as a sub object.&lt;/li&gt;&lt;/ol&gt;&lt;br/&gt;I am not stating that one should &lt;em&gt;always &lt;/em&gt;convert each exception at each level. It &lt;em&gt;might &lt;/em&gt;be the case that the low-level exception is fully adequate at the higher-level interface.&lt;br/&gt;&lt;br/&gt;The main point is that the client should always understand the meaning of the exception thrown from the serving code, talking the “language” appropriate between the two modules. The same modularity aspects as with class methods, in other words.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-112441635711236607?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/112441635711236607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=112441635711236607&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112441635711236607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112441635711236607'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2005/08/exceptions-are-part-of-interaction.html' title='Exceptions are part of the interaction'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-112431850211336404</id><published>2005-08-17T18:41:00.000-04:00</published><updated>2005-08-17T18:55:23.630-04:00</updated><title type='text'>10 Steps to Software Solution Nirvana</title><content type='html'>&lt;span style="font-family:Courier New;font-size:85%;"&gt;These steps are applicable to any kind of programming task, and in most languages. Admittedly, Steps #6 and #7 are hard to achieve in most programming languages, but we can at least try…&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;I know that most of us have heard of these steps in various contexts, but the point is to actually follow them, &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;strictly&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;. It is possible and it is my firm conviction that they will make you a better problem solver. Steps #1 to #5 are pretty easily digested for most developers, whereas Steps #6 to #10 might no be.&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Furthermore, the single most important step is Step #10. So, if you cannot follow the other steps, for any, political or not, reason, that is the step you should really contemplate and then take.&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;1. Never Jump &lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Do not use &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;goto&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;’s - not even disguised in the form of &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;break&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;’s of &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;continue&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;’s. The only exceptions to this rule are in &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;switch &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;statements and if you happen to be lucky enough to implement a threaded interpretation of an abstract machine.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Instead, if you want to affect the flow of control, please use those nifty built-in flow control constructs for conditional and iterative execution. If you are lost and really want to change the flow radically, raise some kind of error or exception signal. Refrain from extra-linguistic facilities such as &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;longjmp&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;2. Only One Exit Point&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Do not use multiple (normal, i.e., non-erroneous) exit points from a modularized piece of code (function, methods whatever...) This obviously incorporates never having various &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;return &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;statements.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;3. Avoid Massive If-Else&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Do not use massive &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;if&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;/&lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;else&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;’s to emulate a mapping. Represent that mapping instead, hashing or not. In some cases, the built-in core facilities such as &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;switch &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;are warranted, given the performance and small footprint, but those are exceptional cases.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;4. Use Recursion&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Yes, use recursion, rather than explicit iterations with managed state variables, i.e., watch out for excessive use of &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;while &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;and &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;for&lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;. Recursion did not cease to be useful just because you left school. Do not be afraid of that stack biting you, unless you work in a very limited environment. After all, &lt;/span&gt;&lt;em&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Conquer &amp; Divide &lt;/span&gt;&lt;/em&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;is not only a viable approach for heterogeneous (sub) problems…&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;5. Use Generic Operations&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Abstract not only “things” but also “general operations.” These operations often represent crosscutting concerns in the domain and, in lack of proper AOP tools, should be implemented via higher-order programming. Those 50 years of Structural Programming should not be completely thrown away, in other words.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;6. Abstract Functions&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Use higher-order functions and functions as first-order “objects.” Could be viewed as a corollary to Step #5, but is really not. This is more focused on Functional Programming thinking. Functional Programming is definitely achievable in most popular languages, such as Python, Java and C++.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;7. Avoid Variables&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;“What, is he crazy?” Perhaps, but it is my view that any explicit state-holding object is bad.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;So, what should we use instead? Use higher-order composers. These composers are often manifested as function adapters.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Combine, or compose, constructs instead of passing information between constructs via variables.&lt;/span&gt;&lt;br/&gt;&lt;em&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;For pure manifestations of no-variable languages, look at IFP (Illinois Functional Programming) or Super Combinators.&lt;/span&gt;&lt;/em&gt;&lt;br/&gt;&lt;em&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;/em&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;8. Avoid Index Variables&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Do not increase index variables just for the fun of it. I.e., avoid simple &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;for &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;loops. Instead, use generic traversing constructs (&lt;/span&gt;&lt;strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;foreach &lt;/span&gt;&lt;/strong&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;and such.) This is definitely a corollary of Step #7.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;9. Avoid Inheritance&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Let things cooperate instead of absorbing things by improper inheritance. The cooperation should preferably be setup in compile-time, for efficiency and proper type checking. This avoidance of inheritance is the latest fad in the object-oriented community. Nevertheless, it is a valid step to take…&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;strong&gt;&lt;span style="font-family:Arial;font-size:130%;"&gt;10. Code As You Think&lt;/span&gt;&lt;/strong&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;Use the same words when you code as when you describe, or contemplate, the problem and its solution. If those words do not exist in your programming environment, introduce them by extending the vocabulary (often &lt;/span&gt;&lt;em&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;definining&lt;/span&gt;&lt;/em&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;…) This might sound simple, but most of us give in to an urge of steering the computer, instead of – declaratively – describing the solution. This requires the use of top-down definitions at times, where words (such as functions or types) that are not yet defined are used. Scary, initially, but powerful.&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;/span&gt;&lt;br/&gt;&lt;span style="font-family:Courier New;font-size:85%;"&gt;I will later supply examples of code complying and breaking these steps.&lt;/span&gt;&lt;br/&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-112431850211336404?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/112431850211336404/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=112431850211336404&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112431850211336404'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/112431850211336404'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2005/08/10-steps-to-software-solution-nirvana.html' title='10 Steps to Software Solution Nirvana'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9503662.post-110243096864795099</id><published>2004-12-07T09:16:00.000-05:00</published><updated>2005-08-17T17:42:30.603-04:00</updated><title type='text'>meta != compile-time &amp;&amp; meta != type-manipulation</title><content type='html'>&lt;span style="font-size:130%;"&gt;The intent of this blog is to bring similarities between various advanced programming techniques to the surface, and to expose my weird view on things. If I rant, please stop me!&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;We start off by attacking a very common misconception. No, actually two intertwined misconceptions.&lt;br/&gt;&lt;br/&gt;Some programmers have come to be believed that meta programming is the art of using the compile-time features outside of the (macro expanding) preprocessor. Some believe that meta programming is "dealing with types in a fancy way."&lt;br/&gt;&lt;br/&gt;Both groups are wrong, in my opinion.&lt;br/&gt;&lt;br/&gt;Whereas meta programs &lt;em&gt;can &lt;/em&gt;definitely be executed during compile-time and they quite often deal with types, the basic definition - which seems to have been lost in the translative adaptation from Academia to the IT field - is&lt;br/&gt;&lt;br/&gt;&lt;ul&gt;&lt;li&gt;A &lt;strong&gt;meta program &lt;/strong&gt;operates on or yields one or more &lt;strong&gt;programs&lt;/strong&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;br/&gt;That simple. The meta program &lt;em&gt;can &lt;/em&gt;be implemented in the same language as the target programs. It can also operate on itself, introspectively.&lt;br/&gt;&lt;br/&gt;&lt;span style="font-size:130%;"&gt;Is it not typical?&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Certain languages, such as C++, provide a quite generic type system, where one can use the same "templated" classes or &lt;strong&gt;parametric types &lt;/strong&gt;to produce a set of customized types. This is &lt;strong&gt;not &lt;/strong&gt;meta programming. Generic, yes, but meta level? Not really. Not if one consider the genericity to be part of the object language.&lt;br/&gt;&lt;br/&gt;It is probably the higher order of those parametric types that confuse most people in the C++ community, coming from a flat C world without differing levels of abstraction. And, indeed, meta is a higher order notion and sounds abstract...&amp;nbsp;&amp;nbsp;Just as some developers confuse higher-order functions and meta programming.&lt;br/&gt;&lt;br/&gt;The parametric types &lt;em&gt;can &lt;/em&gt;be viewed as &lt;strong&gt;meta types&lt;/strong&gt;, though, since they denote a class of types, or, to use a more computer-scientific notion: &lt;strong&gt;a type of types&lt;/strong&gt;. So, the term &lt;strong&gt;meta type programming &lt;/strong&gt;is more adequate for what templates bring to the table, at least in the most common use. One &lt;em&gt;can &lt;/em&gt;use templates to create a proper meta level framework, such as the Boost.MPL library (&lt;a href="http://www.boost.org/"&gt;http://www.boost.org&lt;/a&gt;), or some of Alexandrescu's type generation ideas (&lt;a href="http://sourceforge.net/projects/loki-lib"&gt;http://sourceforge.net/projects/loki-lib&lt;/a&gt;) but such use is quite distinct from the "everyday" use of templates in C++ teams.&lt;br/&gt;&lt;br/&gt;&lt;span style="font-size:130%;"&gt;Is it not compile-time?&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Some languages focus on runtime performance, in terms of speed and resource allocation, and keeping introspective elements in the runtime module is simply not very feasible. C++ stretched it keep some rudimentary type information - basically being a thin wrapper around the necessary virtual function table for polymorphic types.&lt;br/&gt;&lt;br/&gt;Thus, in those runtime-constrained languages, if one wants to manipulate code, including types, it has to be done before runtime, i.e., during compilation and linkage.&lt;br/&gt;&lt;br/&gt;In other languages, reflective means enable meta level programming in runtime. These languages include Java and C#.&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9503662-110243096864795099?l=metaprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://metaprogramming.blogspot.com/feeds/110243096864795099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9503662&amp;postID=110243096864795099&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/110243096864795099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9503662/posts/default/110243096864795099'/><link rel='alternate' type='text/html' href='http://metaprogramming.blogspot.com/2004/12/meta-compile-time-meta-type.html' title='meta != compile-time &amp;&amp; meta != type-manipulation'/><author><name>David Bergman</name><uri>http://www.blogger.com/profile/11306360599828264592</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://www.iqsociety.org/hell/davidbergman.jpg'/></author><thr:total>1</thr:total></entry></feed>
