Better Software - June 2008 - (Page 14) Code Craft A “D” in Programming, Part 2 by Chuck Allison As I write this, my last official pitch for the D programming language, I notice that D is at position twelve and climbing in Tiobe’s ranking of the twenty most popular languages for February 2008 [1]. Earlier in March, the first book on D, Learn to Tango with D, hit the shelves [2]. In its preface, D’s designer, Walter Bright, said: “Amazingly, there is no language that enables precise control over execution while offering modern and proven constructs that improve productivity and reduce bugs … Often programming teams will resort to a hybrid approach, where they will mix Python and C++, trying to get the productivity of Python and the performance of C++. The frequency of this approach indicates that there is a large unmet need in the programming language department. D intends to fill that need.” Like C++, D supports down-to-the-metal programming when you need it, and it compiles to fast-and-lean, native executables. It also supports generic programming and templates in all their glory. The standard C library is available directly from D code. Like Python, D supports modules and packages, garbage collection, functions that behave as first-class entities, a clean (though C-like) syntax, and flexible, built-in data structures. Like Java, D has inner classes. Like C#, D gives you delegates, but in a more flexible way. D is a multi-paradigm language that may be just what you need most of the time. For many of you, I suppose the software engineering features of D would be of most interest, but in this article I’d like to bring to “closure” (pun intended) a running example from previous Code Craft articles as I explore some powerful features of the D language. // MyClass.java class MyClass { private int theData; public int getData() { return theData;} } Listing 2 Nested Functions and Closures Much of what we enjoy today about objects was accomplished in earlier days through other means. To hide data in C, for example, you just declared a file-scope variable to be static. The file was the container, the “object,” if you will, that held data values not directly accessible to users. See listing 1. /* file1.c */ static int theData = 7; // Private data int getData() { return theData; } Listing 1 This approach breaks down when you need multiple instances. File I/O is too cumbersome and expensive to use as a model for objects that contain hidden data. Object-oriented languages have direct support for in-memory object creation at runtime, of course, as in listing 2. How did we ever survive without classes? There were many ways, but I’d like to mention one that is still important: nested functions. Nested functions are not 14 BETTER SOFTWARE JUNE 2008 permitted in many modern languages but were de riguer in languages like Lisp, Algol, PL/I, Pascal, and Ada. C dropped nested functions for simplicity, but Python and D have brought them back because sometimes they are superior to using objects. To illustrate, let me return to an example I’ve used in recent Code Craft articles: function composition. In the January 2008 Code Craft, I presented the generic C++ function composer shown in listing 3. A Composer is an object that takes a list of single-valued functions (or entities callable as such) and calls them in turn in nested fashion so you end up with f1(f2(…fn(x)…)). The private data here is the sequence of functions, indicated by the pair of iterators, beg and end. Since objects aren’t the only way to hide data, let’s look at the nested-function solution in D shown in Listing 4. This rendition of compose is a function template, evidenced by the (T) following its name, and it accepts a dynamic array of functions, each of which takes a single T argument and returns a T value. It returns a delegate that is callable as a singlevalued function of type T. The nested function doit iterates through the list of functions, funs, in reverse order, applying each function and accumulating the result as it goes. Let’s examine this more closely. Notice that compose has only two statements: a definition of the nested function doit and a statement that returns a www.StickyMinds.com ISTOCKPHOTO http://www.StickyMinds.com
Table of Contents Feed for the Digital Edition of Better Software - June 2008 Better Software - June 2008 Contents Mark Your Calendar Contributors Technically Speaking eLightenment Code Craft Test Connection Management Chronicles Agile Model-Driven Development The Myth of Risk Management Stop the Insanity! Product Announcements 10 Things You Might Not Know About … The Last Word Ad Index Better Software - June 2008 Better Software - June 2008 - (Page Intro) Better Software - June 2008 - Better Software - June 2008 (Page Cover1) Better Software - June 2008 - Better Software - June 2008 (Page Cover2) Better Software - June 2008 - Better Software - June 2008 (Page 1) Better Software - June 2008 - Better Software - June 2008 (Page 2) Better Software - June 2008 - Contents (Page 3) Better Software - June 2008 - Mark Your Calendar (Page 4) Better Software - June 2008 - Mark Your Calendar (Page 5) Better Software - June 2008 - Mark Your Calendar (Page 6) Better Software - June 2008 - Mark Your Calendar (Page 7) Better Software - June 2008 - Contributors (Page 8) Better Software - June 2008 - Contributors (Page Telelogic1) Better Software - June 2008 - Contributors (Page Telelogic2) Better Software - June 2008 - Contributors (Page 9) Better Software - June 2008 - Contributors (Page 10) Better Software - June 2008 - Technically Speaking (Page 11) Better Software - June 2008 - eLightenment (Page 12) Better Software - June 2008 - eLightenment (Page 13) Better Software - June 2008 - Code Craft (Page 14) Better Software - June 2008 - Code Craft (Page 15) Better Software - June 2008 - Code Craft (Page 16) Better Software - June 2008 - Code Craft (Page COD1) Better Software - June 2008 - Code Craft (Page COD2) Better Software - June 2008 - Code Craft (Page COD3) Better Software - June 2008 - Code Craft (Page COD4) Better Software - June 2008 - Code Craft (Page 17) Better Software - June 2008 - Test Connection (Page 18) Better Software - June 2008 - Test Connection (Page 19) Better Software - June 2008 - Management Chronicles (Page 20) Better Software - June 2008 - Management Chronicles (Page 21) Better Software - June 2008 - Agile Model-Driven Development (Page 22) Better Software - June 2008 - Agile Model-Driven Development (Page 23) Better Software - June 2008 - Agile Model-Driven Development (Page 24) Better Software - June 2008 - Agile Model-Driven Development (Page 25) Better Software - June 2008 - Agile Model-Driven Development (Page 26) Better Software - June 2008 - Agile Model-Driven Development (Page 27) Better Software - June 2008 - Agile Model-Driven Development (Page 28) Better Software - June 2008 - Agile Model-Driven Development (Page 29) Better Software - June 2008 - The Myth of Risk Management (Page 30) Better Software - June 2008 - The Myth of Risk Management (Page 31) Better Software - June 2008 - The Myth of Risk Management (Page 32) Better Software - June 2008 - The Myth of Risk Management (Page 33) Better Software - June 2008 - The Myth of Risk Management (Page 34) Better Software - June 2008 - The Myth of Risk Management (Page 35) Better Software - June 2008 - Stop the Insanity! (Page 36) Better Software - June 2008 - Stop the Insanity! (Page 37) Better Software - June 2008 - Stop the Insanity! (Page 38) Better Software - June 2008 - Stop the Insanity! (Page 39) Better Software - June 2008 - Stop the Insanity! (Page 40) Better Software - June 2008 - Stop the Insanity! (Page 41) Better Software - June 2008 - Stop the Insanity! (Page 42) Better Software - June 2008 - Stop the Insanity! (Page 43) Better Software - June 2008 - Product Announcements (Page 44) Better Software - June 2008 - Product Announcements (Page 45) Better Software - June 2008 - 10 Things You Might Not Know About … (Page 46) Better Software - June 2008 - The Last Word (Page 47) Better Software - June 2008 - Ad Index (Page 48) Better Software - June 2008 - Ad Index (Page Cover3) Better Software - June 2008 - Ad Index (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.