<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Category Dean's Deprecations</title>
    <link>http://blog.objectmentor.com/articles/category/deans-deprecations</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>&amp;quot;Programming Scala&amp;quot; Is Now Available</title>
      <description>&lt;p&gt;&lt;a href="http://oreilly.com/catalog/9780596155957/"&gt;Programming Scala&lt;/a&gt; is finally available online at &lt;a href="http://www.amazon.com/Programming-Scala-Animal-Guide-Wampler/dp/0596155956/ref=sr_1_3?ie=UTF8&amp;#38;s=books&amp;#38;qid=1253575163&amp;#38;sr=8-3"&gt;Amazon&lt;/a&gt;, &lt;a href="http://oreilly.com/catalog/9780596155957/"&gt;O&amp;#8217;Reilly&lt;/a&gt;,  &lt;a href="http://my.safaribooksonline.com/9780596801908"&gt;Safari&lt;/a&gt;, and hopefully at other online and physical bookstores, too. If you have trouble finding it, just memorize the &lt;span class="caps"&gt;ISBN&lt;/span&gt;, 9780596155957, to aid your search. ;)&lt;/p&gt;


&lt;center&gt;&lt;a href="http://oreilly.com/catalog/9780596155957/"&gt;&lt;img src="http://blog.objectmentor.com/files/prog_scala_mech_cover_front_503x660.png" style="border: 1px solid #7E205F;" /&gt;&lt;/a&gt;&lt;/center&gt;

	&lt;p&gt;&lt;br/&gt;You can download the complete code examples &lt;a href="http://examples.oreilly.com/9780596155964/"&gt;here&lt;/a&gt;. If you want to &amp;#8220;try before you buy&amp;#8221;, see our &lt;a href="http://programming-scala.labs.oreilly.com/"&gt;labs&lt;/a&gt; site.&lt;/p&gt;


	&lt;p&gt;I pitched the book idea just over one year ago. &lt;a href="http://twitter.com/al3x"&gt;Alex Payne&lt;/a&gt;, my co-author, was also talking to O&amp;#8217;Reilly about a book, so we joined forces. It&amp;#8217;s been a fast ride.&lt;/p&gt;


	&lt;p&gt;This book is for you if you are an experienced developer who wants a comprehensive, but fast introduction to Scala. We try to convince you why we like Scala so much, but we also tell you about those dark corners and gotchas that you&amp;#8217;ll find in any language. We even preview the forthcoming 2.8 version of Scala. I hope you&amp;#8217;ll give &lt;a href="http://oreilly.com/catalog/9780596155957/"&gt;Programming Scala&lt;/a&gt; a look.&lt;/p&gt;</description>
      <pubDate>Mon, 21 Sep 2009 17:48:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5d439b0f-ba3b-4e25-a0d5-b8023a94dbd4</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/09/21/programming-scala-is-now-available</link>
      <category>Dean's Deprecations</category>
      <category>Scala</category>
      <category>programmingscala</category>
      <category>books</category>
      <enclosure type="image/png" url="http://blog.objectmentor.com/files/prog_scala_mech_cover_front_503x6601.png" length="172253"/>
    </item>
    <item>
      <title>QCon SF 2008: Radical Simplification through Polyglot and Poly-paradigm Programming </title>
      <description>&lt;p&gt;InfoQ has posted the video of my talk at last year&amp;#8217;s QCon San Francisco on &lt;a href="http://www.infoq.com/presentations/polyglot-polyparadigm-programming"&gt;Radical Simplification through Polyglot and Poly-paradigm Programming&lt;/a&gt;. I make the case that relying on just one programming language or one modularity paradigm (&lt;em&gt;i.e.&lt;/em&gt;, object-oriented programming, functional programming, &lt;em&gt;etc.&lt;/em&gt;) is insufficient for most applications that we&amp;#8217;re building today. That includes embedded systems, games, up to complex Internet and enterprise applications.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m giving an &lt;a href="http://thestrangeloop.com/sessions/polyglot-and-polyparadigm-programming-better-agility"&gt; updated version of this talk&lt;/a&gt; at the &lt;a href="http://thestrangeloop.com/"&gt;Strange Loop Conference&lt;/a&gt;, October 22-23, in St. Louis. I hope to see you there.&lt;/p&gt;</description>
      <pubDate>Tue, 15 Sep 2009 10:35:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:58621ae8-c1b7-4110-8376-1bc0cc248122</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/09/15/qcon-sf-2008-radical-simplification-through-polyglot-and-poly-paradigm-programming</link>
      <category>Dean's Deprecations</category>
      <category>Videos</category>
      <category>Public Speaking Engagements</category>
      <category>Dynamic Languages</category>
      <category>Agile Methods</category>
      <category>Design Principles</category>
      <category>polyglot</category>
      <category>poly</category>
      <category>paradigm</category>
      <category>QCon</category>
      <category>strange</category>
      <category>loop</category>
    </item>
    <item>
      <title>A Milestone for &amp;quot;Programming Scala&amp;quot;</title>
      <description>&lt;p&gt;My co-author, &lt;a href="http://al3x.net/about.html"&gt;Alex Payne&lt;/a&gt;, and I hit a major milestone today for &lt;a href="http://programmingscala.com"&gt;Programming Scala&lt;/a&gt;. After a feverish holiday weekend of writing, we finished all the remaining sections of the book. This morning, we released it to O&amp;#8217;Reilly&amp;#8217;s crack production team. &amp;#8220;If you love something, set it free&amp;#8221; or something sappy like that. Of course, the corollary is, &amp;#8220;if it doesn&amp;#8217;t come back, hunt it down and kill it&amp;#8230;&amp;#8221;&lt;/p&gt;


	&lt;p&gt;But I digress. There will be more reviews and final edits. You can still add your comments online using the link above. However, the text is essentially done. It  has started the final journey that will turn our words into a book, something of a life-long dream of mine that I waited too long to achieve. Kudo&amp;#8217;s to Alex for pursuing this dream early in his career. If it&amp;#8217;s a dream you have entertained, know that it&amp;#8217;s never too late.&lt;/p&gt;


	&lt;p&gt;On the other hand, whatever modest qualities the book possesses reflect the combined years of experience that Alex and I have acquired, sometimes painfully. In a way, as I reflect on what I wrote, &lt;a href="http://programmingscala.com"&gt;Programming Scala&lt;/a&gt; is a software design book masquerading as a language book. The truly seductive quality of Scala is that it makes elegant design concepts possible, which is why I&amp;#8217;ve placed so much faith in the language.&lt;/p&gt;


	&lt;p&gt;Alex posted his &lt;a href="http://al3x.net/2009/07/07/the-tapir-book.html"&gt; own thoughts&lt;/a&gt; on the project, which I hope you&amp;#8217;ll take the time to read. I&amp;#8217;m grateful for his insights and experience with Scala, the elegance of his prose in the book, and the great work he&amp;#8217;s done at Twitter, which caused me to get so addicted to Twitter that I spent too much time tweeting over the last year, which meant I had to work my proverbial butt off this past weekend to get the book done. Thanks a lot!!&lt;/p&gt;</description>
      <pubDate>Tue, 07 Jul 2009 21:02:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b68bf7ee-86b3-4c22-9765-7e4825258747</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/07/07/a-milestone-for-programming-scala</link>
      <category>Dean's Deprecations</category>
      <category>Scala</category>
      <category>programmingscala</category>
      <category>books</category>
    </item>
    <item>
      <title>Rich Hickey on Testing</title>
      <description>&lt;p&gt;It was an interesting week at JavaOne, with lots of talks and hallway discussions about new languages on the &lt;span class="caps"&gt;JVM&lt;/span&gt;. One of those languages is Clojure.&lt;/p&gt;


	&lt;p&gt;Rich Hickey, the creator of Clojure, gave a talk at the Bay Area Clojure User Group Wednesday evening. During the Q&amp;#38;A part, he said that he&amp;#8217;s not big on writing tests, although he always runs the tests that other people have written before he commits changes.&lt;/p&gt;


	&lt;p&gt;Of course, there are many people, including us Object Mentors, who consider &lt;span class="caps"&gt;TDD&lt;/span&gt; to be an essential part of professional software development. Obviously not everyone agrees. James Coplien has been another critic of this view.&lt;/p&gt;


	&lt;p&gt;We should never accept any dogma. Why is &lt;span class="caps"&gt;TDD&lt;/span&gt; considered important? What does it purport to do? &lt;span class="caps"&gt;TDD&lt;/span&gt; provides two important benefits.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Driving the design.&lt;/li&gt;
		&lt;li&gt;Building a suite of automated regression tests.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So, if you can satisfy both requirements without &lt;span class="caps"&gt;TDD&lt;/span&gt;, then technically you don&amp;#8217;t need it. In Rich&amp;#8217;s case, he said he spends a lot of time thinking about what he&amp;#8217;s going to do before he does it. In this way, he satisfies the first requirement, driving the design. I had a spirited discussion with some Thoughtworkers afterwards and Ola Bini said what a lot of us think, &amp;#8220;I do that thinking by writing tests.&amp;#8221; I&amp;#8217;ll freely admit that when I am really experimenting with ideas, I might just write code, but once I know how to proceed, I return to the beginning and test drive the &amp;#8220;production&amp;#8221; code.&lt;/p&gt;


	&lt;p&gt;Rich also made an off-hand comment that if he screws something up, he&amp;#8217;s got thousands of users who will let him know! That &lt;em&gt;ex post facto&lt;/em&gt; testing, along with the Rich&amp;#8217;s own devotion to doing high-quality work, does a good job of handling regressions.&lt;/p&gt;


	&lt;p&gt;But Rich mentioned something else that is also very important. In a functional language, where values are immutability and mutable state is handled in specific, principled ways, regressions don&amp;#8217;t happen nearly as often. Clojure has one of the most deeply thought out approaches for handling state, which is the genius of Clojure.&lt;/p&gt;


	&lt;p&gt;I asked Rich how long he worked on Clojure before releasing it to the world. He spent about 2 1/2 years, much of that time working exclusively on Clojure (and eating through his savings). When he finally &amp;#8220;announced&amp;#8221; it, his &amp;#8220;marketing&amp;#8221; consisted of one email to some friends in the Common Lisp community. The rest was viral, a testament to the justified excitement Clojure has generated.&lt;/p&gt;


	&lt;p&gt;For me, I&amp;#8217;ll probably always do my design thinking through tests, especially when I&amp;#8217;m writing code in imperative languages, like Java and Ruby. I&amp;#8217;ll continue to encourage my clients to use &lt;span class="caps"&gt;TDD&lt;/span&gt;, because I find that &lt;span class="caps"&gt;TDD&lt;/span&gt; is the most productive way to achieve high quality. I want the safety net of a good test suite. I&amp;#8217;m also writing more and more of my code in a functional style, with minimal side effects and mutable data. You should, too.&lt;/p&gt;</description>
      <pubDate>Fri, 05 Jun 2009 12:40:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1af0ef13-e2af-4bbe-aa1a-158f3813eeaa</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/06/05/rich-hickey-on-testing</link>
      <category>Dean's Deprecations</category>
      <category>Software Craftsmanship</category>
      <category>Design Principles</category>
      <category>clojure</category>
      <category>TDD</category>
      <category>testing</category>
    </item>
    <item>
      <title>Bay-Area Scala Enthusiasts (BASE) Meeting: What's New In Scala 2.8</title>
      <description>&lt;p&gt;This week is JavaOne in San Francisco. The Bay-Area Scala Enthusiasts (BASE) held their monthly meeting. Martin Odersky, the creator of Scala, was the special guest. He discussed what&amp;#8217;s new In Scala 2.8, followed by Q&amp;#38;A. We met at Twitter HQ.&lt;/p&gt;


	&lt;p&gt;These are my notes, focusing primarily on Martin&amp;#8217;s presentation, and filled in afterwards with additional details. Any transcription errors or erroneous extrapolations are my own fault. It&amp;#8217;s also late in the day&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Some of the features are not yet in the &lt;span class="caps"&gt;SVN&lt;/span&gt; trunk, so don&amp;#8217;t assume my examples actually work! See the &lt;a href="http://scala-lang.org"&gt;scala-lang.org&lt;/a&gt; for more details on Scala 2.8 features.&lt;/p&gt;


	&lt;p&gt;There are a few more months before it is released. A preview is planned for July, followed by the final release in September or October.&lt;/p&gt;


	&lt;h2&gt;New Features&lt;/h2&gt;


	&lt;p&gt;Here are the new features for this release.&lt;/p&gt;


	&lt;h3&gt;Named and Default Arguments&lt;/h3&gt;


	&lt;p&gt;Scala method parameters can be declared to with default values, so callers don&amp;#8217;t have to specify a value and the &lt;code&gt;implicit&lt;/code&gt; convention doesn&amp;#8217;t have to be used. The default &amp;#8220;values&amp;#8221; aren&amp;#8217;t limited to constants. Any valid expression can be used. Here is an example that I made up (not in Martin&amp;#8217;s slides) that illustrates both specifying and using one default argument and using named arguments.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def joiner(strings: List[String], separator: String = " ") = strings.mkString(separator)

val strs = List("Now", "is", "the", "time", "for", "all", "good", "men", "...")
println(joiner(strs))
println(joiner(strs, "|"))
println(joiner(strings = strs, separator = "-"))
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Named and default arguments enable an elegant enhancement to case classes. It&amp;#8217;s great that I can declare a succinct value class like this.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
case class Person(firstName: String, lastName: String, age: Int)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;What if I want to make a copy that modifies one or more fields. There&amp;#8217;s no elegant way to add such a method in 2.7 without implementing every permutation, that is every possible combination of fields I might want to change. The new &lt;code&gt;copy&lt;/code&gt; method will make this easy.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
case class Person(firstName: String, lastName: String, age: Int)

val youngPerson = Person("Dean", "Wampler", 29)
val oldPerson = youngPerson copy (age = 30)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;I&amp;#8217;m using the infix notation for method invocation on the last line (&lt;em&gt;i.e.,&lt;/em&gt; it&amp;#8217;s equivalent to &lt;code&gt;... youngPerson.copy(...)&lt;/code&gt;). I can specify any combination of the fields I want to change in the list passed to &lt;code&gt;copy&lt;/code&gt;. The generated implementation of &lt;code&gt;copy&lt;/code&gt; will use the current values of any other fields as the default values.&lt;/p&gt;


	&lt;p&gt;The implementation looks something like this.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
case class Person(firstName: String, lastName: String, age: Int) {
  def copy (fName: String = this.firstName, 
            lName: String = this.lastName, 
            aje: Int = this.age) = new Person(fName, lName, aje)
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Quite elegant, once you have default and named arguments!!&lt;/p&gt;


	&lt;p&gt;Defaults for parameters can&amp;#8217;t refer to previous parameters in the list, unless the function is curried. (I&amp;#8217;m not sure I got this right, nor do I understand the reasons why this is true &amp;#8211; if it&amp;#8217;s true!)&lt;/p&gt;


	&lt;p&gt;By the way, Martin reminded us that method parameters are always evaluated left to right at the call site. Do you remember the rules for Java, C++, C#,...?&lt;/p&gt;


	&lt;h3&gt;Nested Annotations&lt;/h3&gt;


	&lt;p&gt;Annotations can now be nested, which is important for using some of the standard annotation definitions in the &lt;span class="caps"&gt;JDK&lt;/span&gt; and &lt;span class="caps"&gt;JEE&lt;/span&gt;. This feature also exploits named and default arguments.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
@Annotation1(foo = @Annotation2)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h3&gt;Package Objects&lt;/h3&gt;


	&lt;p&gt;People have complained that they want to define top-level definitions for a package, but they have to put those definitions, like types and methods, in an &lt;code&gt;object&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt;, which doesn&amp;#8217;t quite fit and it&amp;#8217;s awkward for referencing through package and type qualification. The problem was especially obvious when the team started working on the major reorganization of the collections (discussed below). So, Scala 2.8 will support &amp;#8220;package objects&amp;#8221;.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package object scala {
  type List[+T] = scala.collection.immutable.List
  val List = scala.collection.immutable.List
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Our friend &lt;code&gt;List&lt;/code&gt; is now moved to &lt;code&gt;scala.collection.immutable.List&lt;/code&gt;, but we would still like to reference it as if it were in the &lt;code&gt;scala&lt;/code&gt; package. The definition defines a package-level &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;val&lt;/code&gt; the effectively make List accessible in the &lt;code&gt;scala&lt;/code&gt; scope. In Scala 2.7 you would have to do something like the following (ignoring &lt;code&gt;Predef&lt;/code&gt; for a moment).&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package scala {
  object toplevel {
    type List[+T] = scala.collection.immutable.List
    val List = scala.collection.immutable.List
  }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;But then you would have to reference &lt;code&gt;List&lt;/code&gt; using &lt;code&gt;scala.toplevel.List&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Now, they got around this problem previously by putting a bunch of stuff like this in &lt;code&gt;Predef&lt;/code&gt; and importing it automatically, but that has several disadvantages.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;code&gt;Predef&lt;/code&gt; is a big, amorphous collection of stuff.&lt;/li&gt;
		&lt;li&gt;You can&amp;#8217;t define your own &lt;code&gt;Predef&lt;/code&gt; with the same convenient usage semantics, &lt;em&gt;i.e.,&lt;/em&gt; no special import required and no way to reference definitions like &lt;code&gt;package.type&lt;/code&gt;. You would have to use the alternative I just showed with &lt;code&gt;toplevel&lt;/code&gt; in the middle.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Package objects give you a place for definitions that you want to appear at the package scope without having to define them in a singleton object or class.&lt;/p&gt;


	&lt;p&gt;Finally, besides types and fields as shown, package objects can also define methods. They can also inherit from traits and classes.&lt;/p&gt;


	&lt;h3&gt;@specialized&lt;/h3&gt;


	&lt;p&gt;Scala generics are fully specified at declaration time using a uniform representation, not when they are used, like C++ templates. This supports the way Java works, where there isn&amp;#8217;t a giant link step to resolve all references, &lt;em&gt;etc.&lt;/em&gt; However, this has a major performance disadvantage for generic types when they are actually used with &lt;code&gt;AnyVal&lt;/code&gt; types that Scala optimizes to primitives.&lt;/p&gt;


	&lt;p&gt;For example, any closures require the use of FunctionN[T1, T2, ...], &lt;em&gt;e.g.,&lt;/em&gt;&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def m[T](x: T, f: T =&amp;gt; T) = f(x)

m(2, (x:Int) =&amp;gt; x * 2)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;f&lt;/code&gt; closure in the definition of &lt;code&gt;m&lt;/code&gt; will require instantiation of &lt;code&gt;Function2[T,T]&lt;/code&gt;. However, when use &lt;code&gt;AnyVal&lt;/code&gt; classes, as in the last line , this has the effect of causing primitive boxing and unboxing several times, hurting performance when this is completely unnecessary in the special case of primitives being used. This is also bad for arrays and some other data structures.&lt;/p&gt;


	&lt;p&gt;The new &lt;code&gt;@specialized&lt;/code&gt; annotation fixes this problem by causing scala to generate different versions of the user-specified generic type or method for each of the primitive types.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def m[@specialized T](x: T, f: T =&amp;gt; T) = f(x)

m(2, (x:Int) =&amp;gt; x * 2)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;There is a real risk of an explosion of code. Consider what would have to be generated to support every type permutation for &lt;code&gt;Function22&lt;/code&gt;! For this reason they only do cases with up to two type parameters in the library. You can also choose to annotate only some of the type parameters, as appropriate, and the annotation will support parameters that let you limit the primitive types that will be supported, &lt;em&gt;e.g.,&lt;/em&gt; only &lt;code&gt;Ints&lt;/code&gt; and &lt;code&gt;Longs&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;This feature is not yet in the 2.8 trunk, but it will be soon.&lt;/p&gt;


	&lt;h2&gt;Improved Collections&lt;/h2&gt;


	&lt;p&gt;Collections are getting a major revamp. First they want to eliminate gratuitous differences in package structure and implementations. In many cases, the &lt;code&gt;map&lt;/code&gt; method and others have to be redefined for each basic collection type, rather than shared between them.&lt;/p&gt;


	&lt;h3&gt;New Collections Design&lt;/h3&gt;


	&lt;p&gt;The new version of the library will support the following.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Uniform structure.&lt;/li&gt;
		&lt;li&gt;Every operation is implemented only once.&lt;/li&gt;
		&lt;li&gt;Selection of building blocks in a separate package called &lt;code&gt;scala.collection.generic&lt;/code&gt;. These are normally only used by implementers of immutable and mutable collections.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Because of the reorganization, some Scala 2.7 source code won&amp;#8217;t be compatible with 2.8 without modifications.&lt;/p&gt;


	&lt;h2&gt;Better Tools&lt;/h2&gt;


	&lt;ul&gt;
	&lt;li&gt;The &lt;span class="caps"&gt;REPL&lt;/span&gt; will have command completion, in addition to other enhancements.&lt;/li&gt;
		&lt;li&gt;They have greatly improved the &lt;span class="caps"&gt;IDE&lt;/span&gt; and compiler interface. Miles Sabin and Iulian Dragos worked on this with Martin. There is limited and somewhat unstable support in Eclipse now.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;New Control Abstractions&lt;/h2&gt;


	&lt;p&gt;Several new control abstractions are being introduced.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Continuations will be supported with a compiler plugin.&lt;/li&gt;
		&lt;li&gt;Scala has not had the &lt;code&gt;break&lt;/code&gt; keyword. It will now exist, but as a library method.&lt;/li&gt;
		&lt;li&gt;Scala will optimize trampolining tail calls (&lt;em&gt;e.g.&lt;/em&gt;, &lt;code&gt;foo1&lt;/code&gt; tail calls &lt;code&gt;foo2&lt;/code&gt;, which tail calls &lt;code&gt;foo1&lt;/code&gt;, and back and forth).&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h2&gt;More features&lt;/h2&gt;


	&lt;ul&gt;
	&lt;li&gt;The Swing wrapper library has been enhanced.&lt;/li&gt;
		&lt;li&gt;The performance has been improved in several ways.
	&lt;ul&gt;
	&lt;li&gt;Structural type dispatch&lt;/li&gt;
		&lt;li&gt;Actors&lt;/li&gt;
		&lt;li&gt;Vectors, sets, and maps. Their long-term goal is to implement the fastest ones available for the &lt;span class="caps"&gt;JVM&lt;/span&gt;.&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;These changes are not yet in the trunk.&lt;/p&gt;


	&lt;h2&gt;Beyond 2.8&lt;/h2&gt;


Longer term, they plan significant improvements in support for parallelism and concurrency, including new concurrency models besides actors, such as:
	&lt;ul&gt;
	&lt;li&gt;Transactions (STM)&lt;/li&gt;
		&lt;li&gt;Data parallelism&lt;/li&gt;
		&lt;li&gt;stream processing&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Clojure is influencing this. Martin praised the competition ;) Fortunately, the original designer of the data structures and algorithms used heavily by Clojure is working on Scala versions. (Name?)&lt;/p&gt;


	&lt;p&gt;Doug Lea wants to work with the team on concurrency data structures. The lack of closures in Java makes this effort difficult in Java.&lt;/p&gt;


	&lt;p&gt;There is some exciting work in advanced type system support for guaranteeing actor isolation and effect tracking. For example, this technology wouuld allow actors to exchange references to big objects without copying them while ensuring that they aren&amp;#8217;t modified concurrently.&lt;/p&gt;


	&lt;p&gt;On a final note, Bill Wake described a conversation he had with Joshua Bloch today who admitted that the time has arrived for him to look seriously at Scala. A possible endorsement from Joshua Bloch would be a major step for Scala.&lt;/p&gt;</description>
      <pubDate>Fri, 05 Jun 2009 02:13:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b489a56a-baea-484e-b0d6-686ecc39d34c</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/06/05/bay-area-scala-enthusiasts-base-meeting-whats-new-in-scala-2-8</link>
      <category>Dean's Deprecations</category>
      <category>Scala</category>
      <category>JavaOne</category>
    </item>
    <item>
      <title>Is the Supremacy of Object-Oriented Programming Over?</title>
      <description>&lt;p&gt;I never expected to see this. When I started my career, Object-Oriented Programming (OOP) was going mainstream. For many problems, it was and still is a natural way to &lt;em&gt;modularize&lt;/em&gt; an application. It grew to (mostly) rule the world. Now it seems that the supremacy of objects may be coming to an end, of sorts.&lt;/p&gt;


	&lt;p&gt;I say this because of recent trends in our industry and my hands-on experience with many enterprise and Internet applications, mostly at client sites. You might be thinking that I&amp;#8217;m referring to the mainstream breakout of Functional Programming (FP), which is happening right now. The &lt;em&gt;killer app&lt;/em&gt; for FP is concurrency. We&amp;#8217;ve all heard that more and more applications must be concurrent these days (which doesn&amp;#8217;t necessarily mean multithreaded). When we remove side effects from functions and disallow mutable variables, our concurrency issues largely go away. The success of the Actor model of concurrency, as used to great effect in &lt;a href="http://erlang.org/"&gt;Erlang&lt;/a&gt;, is one example of a functional-style approach. The rise of &lt;a href="http://labs.google.com/papers/mapreduce.html"&gt;map-reduce&lt;/a&gt; computations is another example of a functional technique going mainstream. A related phenomenon is the emergence of key-value store databases, like &lt;a href="http://labs.google.com/papers/bigtable.html"&gt;BigTable&lt;/a&gt; and &lt;a href="http://couchdb.apache.org/"&gt;CouchDB&lt;/a&gt;, is a reaction to the overhead of &lt;span class="caps"&gt;SQL&lt;/span&gt; databases, when the performance cost of the Relational Model isn&amp;#8217;t justified. These databases are typically managed with functional techniques, like map-reduce.&lt;/p&gt;


	&lt;p&gt;But actually, I&amp;#8217;m thinking of something else. Hybrid languages like &lt;a href="http://scala-lang.org"&gt;Scala&lt;/a&gt;, &lt;a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/"&gt;F#&lt;/a&gt;, and &lt;a href="http://caml.inria.fr/ocaml/index.en.html"&gt;OCaml&lt;/a&gt; have demonstrated that &lt;span class="caps"&gt;OOP&lt;/span&gt; and FP can complement each other. In a given context, they let you use the idioms that make the most sense for your particular needs. For example, immutable &amp;#8220;objects&amp;#8221; and functional-style pattern matching is a killer combination.&lt;/p&gt;


	&lt;p&gt;What&amp;#8217;s really got me thinking that objects are losing their supremacy is a very mundane problem. It&amp;#8217;s a problem that isn&amp;#8217;t new, but like concurrency, it just seems to grow worse and worse.&lt;/p&gt;


	&lt;p&gt;The problem is that there is never a stable, clear object model in applications any more. What constitutes a &lt;code&gt;BankAccount&lt;/code&gt; or &lt;code&gt;Customer&lt;/code&gt; or whatever is fluid. It changes with each iteration. It&amp;#8217;s different from one subsystem to another even within the &lt;em&gt;same&lt;/em&gt; iteration! I see a lot of misfit object models that try to be all things to all people, so they are bloated and the teams that own them can&amp;#8217;t be agile. The other extreme is &amp;#8220;balkanization&amp;#8221;, where each subsystem has its own model. We tend to think the latter case is bad. However, is lean and mean, but non-standard, worse than bloated, yet standardized?&lt;/p&gt;


	&lt;p&gt;The fact is, for a lot of these applications, it&amp;#8217;s just data. The ceremony of object wrappers doesn&amp;#8217;t carry its weight. Just put the data in a hash map (or a list if you don&amp;#8217;t need the bits &amp;#8220;labeled&amp;#8221;) and then process the collection with your iterate, map, and reduce functions. This may sound heretical, but how much Java code could you delete today if you replaced it with a stored procedure?&lt;/p&gt;


	&lt;p&gt;These alternatives won&amp;#8217;t work for all situations, of course. Sometimes polymorphism carries its weight. Unfortunately, it&amp;#8217;s too tempting to use objects as if more is always better, like &lt;a href="http://www.google.com/search?client=safari&amp;#38;rls=en-us&amp;#38;q=more+cowbell&amp;#38;ie=UTF-8&amp;#38;oe=UTF-8"&gt;cow bell&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;So what would replace objects for supremacy? Well, my point is really that there is no one true way. We&amp;#8217;ve led ourselves down the wrong path. Or, to be more precise, we followed a single, very good path, but we didn&amp;#8217;t know when to take a different path.&lt;/p&gt;


	&lt;p&gt;Increasingly, the best, most nimble designs I see use objects with a light touch; shallow hierarchies, small objects that try to obey the Single Responsibility Principle, composition rather than inheritance, &lt;em&gt;etc.&lt;/em&gt; Coupled with a liberal use of functional idioms (like iterate, map, and reduce), these designs strike the right balance between the protection of data hiding &lt;em&gt;vs.&lt;/em&gt; openness for easy processing. By the way, you can build these designs in almost any of our popular languages. Some languages make this easier than others, of course.&lt;/p&gt;


	&lt;p&gt;Despite the hype, I think &lt;a href="http://www.martinfowler.com/bliki/DomainSpecificLanguage.html"&gt;Domain-Specific Languages&lt;/a&gt; (DSLs) are also very important and worth mentioning in this context. (&lt;a href="http://en.wikipedia.org/wiki/Language-oriented_programming"&gt;Language-Oriented Programming&lt;/a&gt; &amp;#8211; &lt;span class="caps"&gt;LOP&lt;/span&gt; &amp;#8211; generalizes these ideas). It&amp;#8217;s true that people drink the &lt;span class="caps"&gt;DSL&lt;/span&gt; Kool-Aid and create a mess. However, when used appropriately, DSLs reduce a program to its &lt;a href="http://en.wikipedia.org/wiki/Essential_complexity"&gt;essential complexity&lt;/a&gt;, while hiding and modularizing the &lt;a href="http://en.wikipedia.org/wiki/Accidental_complexity"&gt;accidental complexity&lt;/a&gt; of the implementation. When it becomes easy to write a user story in code, we won&amp;#8217;t obsess as much over the details of a &lt;code&gt;BankAccount&lt;/code&gt; as they change from one story to another. We will embrace more flexible data persistence models, too.&lt;/p&gt;


	&lt;p&gt;Back to &lt;span class="caps"&gt;OOP&lt;/span&gt; and FP, I see the potential for their combination to lead to a rebirth of the old vision of &lt;em&gt;software components&lt;/em&gt;, but that&amp;#8217;s a topic for another blog post.&lt;/p&gt;</description>
      <pubDate>Mon, 20 Apr 2009 21:45:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:69ac398f-694c-4e0b-bc13-a8765608f6cf</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/04/20/is-the-supremacy-of-object-oriented-programming-over</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>design</category>
      <category>OOP</category>
      <category>FP</category>
      <category>polyglot</category>
      <category>Polyparadigm</category>
      <category>Scala</category>
      <category>F</category>
      <category>OCaml</category>
      <category>MapReduce</category>
      <category>BigTable</category>
      <category>CouchDB</category>
      <category>SQL</category>
      <category>dsl</category>
      <category>LOP</category>
    </item>
    <item>
      <title>Pat Eyler Interviews Dean Wampler and Alex Payne on &amp;quot;Programming Scala&amp;quot;.</title>
      <description>&lt;p&gt;&lt;a href="http://twitter.com/gnupate"&gt;Pat Eyler&lt;/a&gt; posted an &lt;a href="http://on-ruby.blogspot.com/2009/03/dean-wampler-and-alex-payne-author.html"&gt;interview&lt;/a&gt; with &lt;a href="http://twitter.com/al3x"&gt;Alex Payne&lt;/a&gt; and me (Dean Wampler), which we conducted over email. We dish on Scala, Functional Programming, and our forthcoming book &lt;a href="http://oreilly.com/catalog/9780596157746/index.html"&gt;Programming Scala&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 17 Mar 2009 12:48:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a53602a4-52af-4c4f-8bb5-105224343067</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/03/17/pat-eyler-interviews-dean-wampler-and-alex-payne-on-programming-scala</link>
      <category>Dean's Deprecations</category>
      <category>Scala</category>
    </item>
    <item>
      <title>Tighter Ruby Methods with Functional-style Pattern Matching, Using the Case Gem</title>
      <description>&lt;p&gt;Ruby doesn&amp;#8217;t have overloaded methods, which are methods with the same name, but different &lt;em&gt;signatures&lt;/em&gt; when you consider the argument lists and return values. This would be somewhat challenging to support in a dynamic language with very flexible options for method argument handling.&lt;/p&gt;


	&lt;p&gt;You can &amp;#8220;simulate&amp;#8221; overloading by parsing the argument list and taking different paths of execution based on the structure you find. This post discusses how &lt;em&gt;pattern matching&lt;/em&gt;, a hallmark of &lt;em&gt;functional programming&lt;/em&gt;, gives you powerful options.&lt;/p&gt;


	&lt;p&gt;First, let&amp;#8217;s look at a typical example that handles the arguments in an &lt;em&gt;ad hoc&lt;/em&gt; fashion. Consider the following &lt;code&gt;Person&lt;/code&gt; class. You can pass three arguments to the initializer, the &lt;code&gt;first_name&lt;/code&gt;, the &lt;code&gt;last_name&lt;/code&gt;, and the &lt;code&gt;age&lt;/code&gt;. Or, you can pass a hash using the keys &lt;code&gt;:first_name&lt;/code&gt;, &lt;code&gt;:last_name&lt;/code&gt;, and &lt;code&gt;:age&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
require "rubygems" 
require "spec" 

class Person
  attr_reader :first_name, :last_name, :age
  def initialize *args
    arg = args[0]
    if arg.kind_of? Hash       # 1
      @first_name = arg[:first_name]
      @last_name  = arg[:last_name]
      @age        = arg[:age]
    else
      @first_name = args[0]
      @last_name  = args[1]
      @age        = args[2]
    end
  end
end

describe "Person#initialize" do 
  it "should accept a hash with key-value pairs for the attributes" do
    person = Person.new :first_name =&amp;gt; "Dean", :last_name =&amp;gt; "Wampler", :age =&amp;gt; 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
  it "should accept a first name, last name, and age arguments" do
    person = Person.new "Dean", "Wampler", 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The condition on the &lt;code&gt;# 1&lt;/code&gt; comment line checks to see if the first argument is a &lt;code&gt;Hash&lt;/code&gt;. If so, the attribute&amp;#8217;s values are extracted from it. Otherwise, it is assumed that three arguments were specified in a particular order. They are passed to &lt;code&gt;#initialize&lt;/code&gt; in a three-element array. The two &lt;a href="http://rspec.info"&gt;rspec&lt;/a&gt; &lt;em&gt;examples&lt;/em&gt; exercise these behaviors. For simplicity, we ignore some more general cases, as well as error handling.&lt;/p&gt;


	&lt;p&gt;Another approach that is more flexible is to use duck typing, instead. For example, we could replace the line with the &lt;code&gt;# 1&lt;/code&gt; comment with this line:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
if arg.respond_to? :has_key?
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;There aren&amp;#8217;t many objects that respond to &lt;code&gt;#has_key?&lt;/code&gt;, so we&amp;#8217;re highly confident that we can use &lt;code&gt;[symbol]&lt;/code&gt; to extract the values from the hash.&lt;/p&gt;


	&lt;p&gt;This implementation is fairly straightforward. You&amp;#8217;ve probably written code like this yourself. However, it could get complicated for more involved cases.&lt;/p&gt;


	&lt;h2&gt;Pattern Matching, a Functional Programming Approach&lt;/h2&gt;


	&lt;p&gt;Most programming languages today have &lt;code&gt;switch&lt;/code&gt; or &lt;code&gt;case&lt;/code&gt; statements of some sort and most have support for regular expression matching. However, in &lt;em&gt;functional programming&lt;/em&gt; languages, pattern matching is so important and pervasive that these languages offer very powerful and convenient support for pattern matching.&lt;/p&gt;


	&lt;p&gt;Fortunately, we can get powerful pattern matching, typical of functional languages, in Ruby using the &lt;a href="http://rubyforge.org/frs/?group_id=3690"&gt;Case&lt;/a&gt; gem that is part of the MenTaLguY&amp;#8217;s &lt;a href="http://rubyforge.org/frs/?group_id=3690"&gt;Omnibus Concurrency library&lt;/a&gt;. &lt;code&gt;Omnibus&lt;/code&gt; provides support for the hot &lt;em&gt;Actor model of concurrency&lt;/em&gt;, which Erlang has made famous. However, it would be a shame to restrict the use of the &lt;code&gt;Case&lt;/code&gt; gem to parsing Actor messages. It&amp;#8217;s much more general purpose than that.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s rework our example using the &lt;a href="http://rubyforge.org/frs/?group_id=3690"&gt;Case&lt;/a&gt; gem.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
require "rubygems" 
require "spec" 
require "case" 

class Person
  attr_reader :first_name, :last_name, :age
  def initialize *args
    case args
    when Case[Hash]       # 1
      arg = args[0]
      @first_name = arg[:first_name]
      @last_name  = arg[:last_name]
      @age        = arg[:age]
    else
      @first_name = args[0]
      @last_name  = args[1]
      @age        = args[2]
    end
  end
end

describe "Person#initialize" do 
  it "should accept a first name, last name, and age arguments" do
    person = Person.new "Dean", "Wampler", 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
  it "should accept a has with :first_name =&amp;gt; fn, :last_name =&amp;gt; ln, and :age =&amp;gt; age" do
    person = Person.new :first_name =&amp;gt; "Dean", :last_name =&amp;gt; "Wampler", :age =&amp;gt; 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;We require the &lt;code&gt;case&lt;/code&gt; gem, which puts the &lt;code&gt;#===&lt;/code&gt; method on steroids. In the &lt;code&gt;when&lt;/code&gt; statement in &lt;code&gt;#initialize&lt;/code&gt;, the expression &lt;code&gt;when Case[Hash]&lt;/code&gt; matches on a one-element array where the element is a &lt;code&gt;Hash&lt;/code&gt;. We extract the key-value pairs as before. The &lt;code&gt;else&lt;/code&gt; clause assumes we have an array for the arguments.&lt;/p&gt;


	&lt;p&gt;So far, this is isn&amp;#8217;t very impressive, but all we did was to reproduce the original behavior. Let&amp;#8217;s extend the example to really exploit some of the neat features of the &lt;code&gt;Case&lt;/code&gt; gem&amp;#8217;s pattern matching. First, let&amp;#8217;s narrow the allowed array values.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
require "rubygems" 
require "spec" 
require "case" 

class Person
  attr_reader :first_name, :last_name, :age
  def initialize *args
    case args
    when Case[Hash]       # 1
      arg = args[0]
      @first_name = arg[:first_name]
      @last_name  = arg[:last_name]
      @age        = arg[:age]
    when Case[String, String, Integer]
      @first_name = args[0]
      @last_name  = args[1]
      @age        = args[2]
    else
      raise "Invalid arguments: #{args}" 
    end
  end
end

describe "Person#initialize" do 
  it "should accept a first name, last name, and age arguments" do
    person = Person.new "Dean", "Wampler", 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
  it "should accept a has with :first_name =&amp;gt; fn, :last_name =&amp;gt; ln, and :age =&amp;gt; age" do
    person = Person.new :first_name =&amp;gt; "Dean", :last_name =&amp;gt; "Wampler", :age =&amp;gt; 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
  it "should not accept an array unless it is a [String, String, Integer]" do
    lambda { person = Person.new "Dean", "Wampler", "39" }.should raise_error(Exception)
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The new expression &lt;code&gt;when Case[String, String, Integer]&lt;/code&gt; only matches a three-element array where the first two arguments are strings and the third argument is an integer, which are the types we want. If you use an array with a different number of arguments or the arguments have different types, this &lt;code&gt;when&lt;/code&gt; clause won&amp;#8217;t match. Instead, you&amp;#8217;ll get the default &lt;code&gt;else&lt;/code&gt; clause, which raises an exception. We added another rspec example to test this condition, where the user&amp;#8217;s age was specified as a string instead of as an integer. Of course, you could decide to attempt a conversion of this argument, to make your code more &amp;#8220;forgiving&amp;#8221; of user mistakes.&lt;/p&gt;


	&lt;p&gt;Similarly, what happens if the method supports default values some of the parameters. As written, we can&amp;#8217;t support that option, but let&amp;#8217;s look at a slight variation of &lt;code&gt;Person#initialize&lt;/code&gt;, where a hash of values is not supported, to see what would happen.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
require "rubygems" 
require "spec" 
require "case" 

class Person
  attr_reader :first_name, :last_name, :age
  def initialize first_name = "Bob", last_name = "Martin", age = 29
    case [first_name, last_name, age]
    when Case[String, String, Integer]
      @first_name = first_name
      @last_name  = last_name
      @age        = age
    else
      raise "Invalid arguments: #{first_name}, #{last_name}, #{age}" 
    end
  end
end

def check person, expected_fn, expected_ln, expected_age
  person.first_name.should == expected_fn
  person.last_name.should  == expected_ln
  person.age.should        == expected_age
end

describe "Person#initialize" do 
  it "should require a first name (string), last name (string), and age (integer) arguments" do
    person = Person.new "Dean", "Wampler", 39
    check person, "Dean", "Wampler", 39
  end
  it "should accept the defaults for all parameters" do
    person = Person.new
    check person, "Bob", "Martin", 29
  end
  it "should accept the defaults for the last name and age parameters" do
    person = Person.new "Dean" 
    check person, "Dean", "Martin", 29
  end
  it "should accept the defaults for the age parameter" do
    person = Person.new "Dean", "Wampler" 
    check person, "Dean", "Wampler", 29
  end
  it "should not accept the first name as a symbol" do
    lambda { person = Person.new :Dean, "Wampler", "39" }.should raise_error(Exception)
  end
  it "should not accept the last name as a symbol" do
  end
  it "should not accept the age as a string" do
    lambda { person = Person.new "Dean", "Wampler", "39" }.should raise_error(Exception)
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;We match on all three arguments as an array, asserting they are of the correct type. As you might expect, &lt;code&gt;#initialize&lt;/code&gt; always gets three parameters passed to it, including when default values are used.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s return to our original example, where the object can be constructed with a hash or a list of arguments. There are two more things (at least &amp;#8230;) that we can do. First, we&amp;#8217;re not yet validating the types of the values in the hash. Second, we can use the &lt;code&gt;Case&lt;/code&gt; gem to impose constraints on the values, such as requiring non-empty name strings and a positive age.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
require "rubygems" 
require "spec" 
require "case" 

class Person
  attr_reader :first_name, :last_name, :age
  def initialize *args
    case args
    when Case[Hash]
      arg = args[0]
      @first_name = arg[:first_name]
      @last_name  = arg[:last_name]
      @age        = arg[:age]
    when Case[String, String, Integer]
      @first_name = args[0]
      @last_name  = args[1]
      @age        = args[2]
    else
      raise "Invalid arguments: #{args}" 
    end
    validate_name @first_name, "first_name" 
    validate_name @last_name, "last_name" 
    validate_age
  end

  protected

  def validate_name name, field_name
    case name
    when Case::All[String, Case.guard {|s| s.length &amp;gt; 0 }]
    else
      raise "Invalid #{field_name}: #{first_name}" 
    end
  end

  def validate_age
    case @age
    when Case::All[Integer, Case.guard {|n| n &amp;gt; 0 }]
    else
      raise "Invalid age: #{@age}" 
    end
  end
end

describe "Person#initialize" do 
  it "should accept a first name, last name, and age arguments" do
    person = Person.new "Dean", "Wampler", 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
  it "should accept a has with :first_name =&amp;gt; fn, :last_name =&amp;gt; ln, and :age =&amp;gt; age" do
    person = Person.new :first_name =&amp;gt; "Dean", :last_name =&amp;gt; "Wampler", :age =&amp;gt; 39
    person.first_name.should == "Dean" 
    person.last_name.should  == "Wampler" 
    person.age.should        == 39
  end
  it "should not accept an array unless it is a [String, String, Integer]" do
    lambda { person = Person.new "Dean", "Wampler", "39" }.should raise_error(Exception)
  end
  it "should not accept a first name that is a zero-length string" do
    lambda { person = Person.new "", "Wampler", 39 }.should raise_error(Exception)
  end    
  it "should not accept a first name that is not a string" do
    lambda { person = Person.new :Dean, "Wampler", 39 }.should raise_error(Exception)
  end    
  it "should not accept a last name that is a zero-length string" do
    lambda { person = Person.new "Dean", "", 39 }.should raise_error(Exception)
  end    
  it "should not accept a last name that is not a string" do
    lambda { person = Person.new :Dean, :Wampler, 39 }.should raise_error(Exception)
  end    
  it "should not accept an age that is less than or equal to zero" do
    lambda { person = Person.new "Dean", "Wampler", -1 }.should raise_error(Exception)
    lambda { person = Person.new "Dean", "Wampler", 0 }.should raise_error(Exception)
  end    
  it "should not accept an age that is not an integer" do
    lambda { person = Person.new :Dean, :Wampler, "39" }.should raise_error(Exception)
  end    
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;We have added &lt;code&gt;validate_name&lt;/code&gt; and &lt;code&gt;validate_age&lt;/code&gt; methods that are invoked at the end of &lt;code&gt;#initialize&lt;/code&gt;. In &lt;code&gt;validate_name&lt;/code&gt;, the one &lt;code&gt;when&lt;/code&gt; clause requires &amp;#8220;all&amp;#8221; the conditions to be true, that the name is a string and that it has a non-zero length. Similarly, &lt;code&gt;validate_age&lt;/code&gt; has a &lt;code&gt;when&lt;/code&gt; clause that requires &lt;code&gt;age&lt;/code&gt; to be a positive integer.&lt;/p&gt;


	&lt;h2&gt;Final Thoughts&lt;/h2&gt;


	&lt;p&gt;So, how valuable is this? The code is certainly longer, but it specifies and enforces expected behavior more precisely. The rspec examples verify the enforcement. It smells a little of static typing, which is good or bad, depending on your point of view. ;)&lt;/p&gt;


	&lt;p&gt;Personally, I think the conditional checks are a good way to add robustness in small ways to libraries that will grow and evolve for a long time. The checks document the required behavior for code readers, like new team members, but of course, they should really get that information from the tests. ;) (However, it would be nice to extract the information into the &lt;code&gt;rdocs&lt;/code&gt;.)&lt;/p&gt;


	&lt;p&gt;For small, short-lived projects, I might not worry about the conditional checks as much (but how many times have those &amp;#8220;short-lived projects&amp;#8221; refused to die?).&lt;/p&gt;


	&lt;p&gt;You can read more about &lt;code&gt;Omnibus&lt;/code&gt; and &lt;code&gt;Case&lt;/code&gt; in this &lt;a href="http://www.infoq.com/articles/actors-rubinius-interview"&gt;InfoQ interview&lt;/a&gt; with MenTaLguY. I didn&amp;#8217;t discuss using the Actor model of concurrency, for which these gems were designed. For an example of Actors using Omnibus, see my &lt;a href="http://aspectprogramming.com/papers/BetterRubyThroughFP_v5.pdf"&gt;Better Ruby through Functional Programming&lt;/a&gt; presentation or the &lt;a href="http://confreaks.com/"&gt;Confreak&amp;#8217;s&lt;/a&gt; video of an earlier version of the presentation I gave at last year&amp;#8217;s &lt;a href="http://rubyconf2008.confreaks.com/better-ruby-through-functional-programming-2.html"&gt;RubyConf&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Mon, 16 Mar 2009 19:59:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4e6b0c8e-7b8b-4c8d-b20c-6e30fe39c98c</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/03/16/tighter-ruby-methods-with-functional-style-pattern-matching-using-the-case-gem</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Ruby</category>
      <category>methods</category>
      <category>functional</category>
      <category>programming</category>
      <category>pattern</category>
      <category>matching</category>
    </item>
    <item>
      <title>1st Ever Chicago Area Scala Enthusiasts (CASE) Meeting Tonight</title>
      <description>&lt;p&gt;Tonight is our first meeting, at the ThoughtWorks offices in the Aon building downtown. If you&amp;#8217;re going and you haven&amp;#8217;t &lt;span class="caps"&gt;RSVP&lt;/span&gt;&amp;#8217;ed, either send a tweet to @chicagoscala or reply here &lt;span class="caps"&gt;ASAP&lt;/span&gt;!&lt;/p&gt;


	&lt;p&gt;Hope to see you there. Our meetings will be the 3rd Thursday of each month.&lt;/p&gt;</description>
      <pubDate>Thu, 19 Feb 2009 18:17:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:2f572e0d-c976-46a2-a1ec-790ea17072b6</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/02/19/1st-ever-chicago-area-scala-enthusiasts-case-meeting-tonight</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>Scala</category>
      <category>CASE</category>
      <category>chicagoscala</category>
    </item>
    <item>
      <title>Organizing a Chicago Area Scala Enthusiasts (CASE) Group</title>
      <description>&lt;p&gt;I&amp;#8217;m organizing a group in Chicago for people interested in Scala, called the Chicago Area Scala Enthusiasts (CASE). If you&amp;#8217;re interested, join the &lt;a href="http://groups.google.com/group/chicagoscala"&gt;google group&lt;/a&gt; for more information.&lt;/p&gt;</description>
      <pubDate>Sat, 17 Jan 2009 17:02:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:1d72bceb-aa03-475c-aa1f-38b7b78be11f</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/01/17/organizing-a-chicago-area-scala-enthusiasts-case-group</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>Scala</category>
      <category>chicago</category>
    </item>
    <item>
      <title>Adopting New JVM Languages in the Enterprise (Update)</title>
      <description>&lt;p&gt;(Updated to add Groovy, which I should have mentioned the first time. Also mentioned Django under Python.)&lt;/p&gt;


	&lt;p&gt;This is an exciting time to be a Java programmer. The pace of innovation for the Java language is slowing down, in part due to concerns that the language is growing too big and in part due to economic difficulties at Sun, which means there are fewer developers assigned to Java. However, the real &lt;em&gt;crown jewel&lt;/em&gt; of the Java ecosystem, the &lt;span class="caps"&gt;JVM&lt;/span&gt;, has become an attractive platform for new languages. These languages give us exciting new opportunities for growth, while preserving our prior investment in code and deployment infrastructure.&lt;/p&gt;


	&lt;p&gt;This post emphasizes practical issues of evaluating and picking new &lt;span class="caps"&gt;JVM&lt;/span&gt; languages for an established Java-based enterprise.&lt;/p&gt;


	&lt;p&gt;The Interwebs are full of technical comparisons between Java and the different languages, &lt;em&gt;e.g.,&lt;/em&gt; why language X fixes Java&amp;#8217;s perceived issue Y. I won&amp;#8217;t rehash those arguments here, but I will describe some language features, as needed.&lt;/p&gt;


	&lt;p&gt;A similar &amp;#8220;polyglot&amp;#8221; trend is happening on the .NET platform.&lt;/p&gt;


	&lt;h2&gt;The New &lt;span class="caps"&gt;JVM&lt;/span&gt; Languages&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;ll limit my discussion to these representative (and best known) alternative languages for the &lt;span class="caps"&gt;JVM&lt;/span&gt;.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;a href="http://jruby.codehaus.org/"&gt;JRuby&lt;/a&gt; &amp;#8211; Ruby running on the &lt;span class="caps"&gt;JVM&lt;/span&gt;.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://scala-lang.org"&gt;Scala&lt;/a&gt; &amp;#8211; A hybrid object-oriented and functional language that runs on .NET as well as the &lt;span class="caps"&gt;JVM&lt;/span&gt;. (Disclaimer: I&amp;#8217;m co-writing a book on Scala for O&amp;#8217;Reilly.)&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://clojure.org"&gt;Clojure&lt;/a&gt; &amp;#8211; A Lisp dialect.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;I picked these languages because they seem to be the most likely candidates for most enterprises considering a new &lt;span class="caps"&gt;JVM&lt;/span&gt; language, although some of the languages listed below could make that claim.&lt;/p&gt;


	&lt;p&gt;There are other deserving languages besides these three, but I don&amp;#8217;t have the time to do them justice. Hopefully, you can generalize the subsequent discussion for these other languages.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;a href="http://groovy.codehaus.org/"&gt;Groovy&lt;/a&gt; &amp;#8211; A dynamically-typed language designed specifically for interoperability with Java. It will appeal to teams that want a dynamically-typed language that is closer to Java than Ruby. With &lt;a href="http://grails.org/"&gt;Grails&lt;/a&gt;, you have a combination that&amp;#8217;s comparable to Ruby on Rails.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://jython.org/"&gt;Jython&lt;/a&gt; &amp;#8211; The first non-Java language ported to the &lt;span class="caps"&gt;JVM&lt;/span&gt;, started by Jim Hugunin in 1997. Most of my remarks about JRuby are applicable to Jython. &lt;a href="http://www.djangoproject.com/"&gt;Django&lt;/a&gt; is the Python analog of Rails. If your Java shop already has a lot of Python, consider Jython.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.fandev.org/"&gt;Fan&lt;/a&gt; &amp;#8211; A hybrid object-oriented and functional language that runs on .NET, too. It has a lot of similarities to Scala, like a scripting-language feel.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://kenai.com/projects/ioke/"&gt;Ioke&lt;/a&gt; &amp;#8211; (pronounced &amp;#8220;eye-oh-key&amp;#8221;) An innovative language developed by Ola Bini and inspired by Io and Lisp. This is the newest language discussed here. Hence, it has a small following, but a lot of potential. The Io/Lisp-flavored syntax will be more challenging to average Java developers than Scala, JRuby, Jython, Fan, and JavaScript.&lt;/li&gt;
		&lt;li&gt;JavaScript, &lt;em&gt;e.g.,&lt;/em&gt; &lt;a href="http://www.mozilla.org/rhino/"&gt;Rhino&lt;/a&gt; &amp;#8211; Much maligned and misunderstood (&lt;em&gt;e.g.,&lt;/em&gt; due to buggy and inconsistent browser implementations), JavaScript continues to gain converts as an alternative scripting language for Java applications. It is the default scripting language supported by the &lt;span class="caps"&gt;JDK 6&lt;/span&gt; scripting interface.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://projectfortress.sun.com/Projects/Community/"&gt;Fortress&lt;/a&gt; &amp;#8211; A language designed as a replacement for high-performance &lt;span class="caps"&gt;FORTRAN&lt;/span&gt; for industrial and academic &amp;#8220;number crunching&amp;#8221;. This one will interest scientists and engineers&amp;#8230;&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Note: Like a lot of people, I use the term &lt;em&gt;scripting language&lt;/em&gt; to refer to languages with a lightweight syntax, usually dynamically typed. The name reflects their convenience for &amp;#8220;scripting&amp;#8221;, but that quality is sometimes seen as pejorative; they aren&amp;#8217;t seen as &amp;#8220;serious&amp;#8221; languages. I reject this view.&lt;/p&gt;


	&lt;p&gt;To learn more about what people are doing on the &lt;span class="caps"&gt;JVM&lt;/span&gt; today (with some guest .NET presentations), a good place to start is the recent &lt;a href="http://openjdk.java.net/projects/mlvm/jvmlangsummit/"&gt;&lt;span class="caps"&gt;JVM&lt;/span&gt; Language Summit&lt;/a&gt;.&lt;/p&gt;


	&lt;h2&gt;Criteria For Evaluating New &lt;span class="caps"&gt;JVM&lt;/span&gt; Languages&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;ll frame the discussion around a few criteria you should consider when evaluating language choices. I&amp;#8217;ll then discuss how each of the languages address those criteria. Since we&amp;#8217;re restricting ourselves to &lt;span class="caps"&gt;JVM&lt;/span&gt; languages, I assume that each language compiles to valid byte code, so code in the new language and code written in Java can call each other, at least at some level. The &amp;#8220;some level&amp;#8221; part will be one criterion. Substitute X for the language you are considering.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;strong&gt;Interoperability&lt;/strong&gt;: How easily can X code invoke Java code and &lt;em&gt;vice versa&lt;/em&gt;? Specifically:
	&lt;ol&gt;
	&lt;li&gt;Create objects (&lt;em&gt;i.e.,&lt;/em&gt; call &lt;code&gt;new Foo(...)&lt;/code&gt;).&lt;/li&gt;
		&lt;li&gt;Call methods on an object.&lt;/li&gt;
		&lt;li&gt;Call static methods on a class.&lt;/li&gt;
		&lt;li&gt;Extend a class.&lt;/li&gt;
		&lt;li&gt;Implement an interface.&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Object Model&lt;/strong&gt;: How different is the object model of X compared to Java&amp;#8217;s object model? (This is somewhat tied to the previous point.)&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;New &amp;#8220;Ideas&amp;#8221;&lt;/strong&gt;: Does X support newer programming trends:
	&lt;ol&gt;
	&lt;li&gt;Functional Programming.&lt;/li&gt;
		&lt;li&gt;Metaprogramming.&lt;/li&gt;
		&lt;li&gt;Easier approaches to writing robust concurrent applications.&lt;/li&gt;
		&lt;li&gt;Easier support for processing &lt;span class="caps"&gt;XML&lt;/span&gt;, SQL queries, &lt;em&gt;etc.&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;Support &lt;a href="http://www.martinfowler.com/bliki/DomainSpecificLanguage.html"&gt;internal &lt;span class="caps"&gt;DSL&lt;/span&gt;&lt;/a&gt; creation.&lt;/li&gt;
		&lt;li&gt;Easier presentation-tier development of web and thick-client UI&amp;#8217;s.&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Stability&lt;/strong&gt;: How stable is the language, in terms of:
	&lt;ol&gt;
	&lt;li&gt;Lack of Bugs.&lt;/li&gt;
		&lt;li&gt;Stability of the language&amp;#8217;s syntax, semantics, and library &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8217;s. (All the languages can call Java &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8217;s.)&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Performance&lt;/strong&gt;: How does code written in X perform?&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Adoption&lt;/strong&gt;: Is X easy to learn and use?&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Tool Support&lt;/strong&gt;: What about editors, &lt;span class="caps"&gt;IDE&lt;/span&gt;&amp;#8217;s, code coverage, &lt;em&gt;etc.&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Deployment&lt;/strong&gt;: How are apps and libraries written in X deployed? 
	&lt;ol&gt;
	&lt;li&gt;Do I have to modify my existing infrastructure, management, &lt;em&gt;etc.&lt;/em&gt;?&lt;/li&gt;
	&lt;/ol&gt;&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The &lt;strong&gt;Interoperability&lt;/strong&gt; point affects ease of adoption and use with a legacy Java code base. The &lt;strong&gt;Object Model&lt;/strong&gt; and &lt;strong&gt;Adoption&lt;/strong&gt; points address the barrier to adoption from the learning point of view. The &lt;strong&gt;New &amp;#8220;Ideas&amp;#8221;&lt;/strong&gt; point asks what each language brings to development that is not available in Java (or poorly supported) and is seen as valuable to the developer. Finally, &lt;strong&gt;Stability&lt;/strong&gt;, &lt;strong&gt;Performance&lt;/strong&gt;, and &lt;strong&gt;Deployment&lt;/strong&gt; address very practical issues that a candidate production language must address.&lt;/p&gt;


	&lt;h2&gt;Comparing the Languages&lt;/h2&gt;


	&lt;h3&gt;JRuby&lt;/h3&gt;


	&lt;p&gt;JRuby is the most popular alternative &lt;span class="caps"&gt;JVM&lt;/span&gt; langauge, driven largely by interest in &lt;a href="http://ruby-lang.org"&gt;Ruby&lt;/a&gt; and &lt;a href="http://rubyonrails.org/"&gt;Ruby on Rails&lt;/a&gt;.&lt;/p&gt;


	&lt;h4&gt;Interoperability&lt;/h4&gt;


	&lt;p&gt;Ruby&amp;#8217;s object model is a little different than Java&amp;#8217;s, but JRuby provides straightforward coding idioms that make it easy to call Java from JRuby. Calling JRuby from Java requires the &lt;span class="caps"&gt;JSR 223&lt;/span&gt; scripting interface or a similar approach, unless JRuby is used to compile the Ruby code to byte code first. In that case, shortcuts are possible, which are well documented.&lt;/p&gt;


	&lt;h4&gt;Object Model&lt;/h4&gt;


	&lt;p&gt;Ruby&amp;#8217;s object model is a little different than Java&amp;#8217;s. Ruby support &lt;em&gt;mixin&lt;/em&gt;-style modules, which behave like interfaces &lt;em&gt;with&lt;/em&gt; implementations. So, the Ruby object model needs to be learned, but it is straightforward or the Java developer.&lt;/p&gt;


	&lt;h4&gt;New Ideas&lt;/h4&gt;


	&lt;p&gt;JRuby brings closures to the &lt;span class="caps"&gt;JVM&lt;/span&gt;, a much desired feature that probably won&amp;#8217;t be added in the forthcoming Java 7. Using closures, Ruby supports a number of functional-style iterative operations, like mapping, filtering, and reducing/folding. However, Ruby does not fully support functional programming.&lt;/p&gt;


	&lt;p&gt;Ruby uses dynamic-typing instead of static-typing, which it exploits to provide extensive and powerful metaprogramming facilities.&lt;/p&gt;


	&lt;p&gt;Ruby doesn&amp;#8217;t offer any specific enhancements over Java for safe, robust concurrent programming.&lt;/p&gt;


	&lt;p&gt;Ruby &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8217;s make &lt;span class="caps"&gt;XML&lt;/span&gt; processing and database access relatively easy. Ruby on Rails is legendary for improving the productivity of web developers and similar benefits are available for thick-client developers using other libraries.&lt;/p&gt;


	&lt;p&gt;Ruby is also one of the best languages for defining &amp;#8220;internal&amp;#8221; &lt;span class="caps"&gt;DSL&lt;/span&gt;&amp;#8217;s, which are used to great affect in Rails (&lt;em&gt;e.g.,&lt;/em&gt; &lt;em&gt;ActiveRecord&lt;/em&gt;).&lt;/p&gt;


	&lt;h4&gt;Stability&lt;/h4&gt;


	&lt;p&gt;JRuby and Ruby are very stable and are widely used in production. JRuby is believed to be the best performing Ruby platform.&lt;/p&gt;


	&lt;p&gt;The Ruby syntax and &lt;span class="caps"&gt;API&lt;/span&gt; are undergoing some significant changes in the current 1.9.X release, but migration is not a major challenge.&lt;/p&gt;


	&lt;h4&gt;Performance&lt;/h4&gt;


	&lt;p&gt;JRuby is believed to be the best performing Ruby platform. While it is a topic of hot debate, Ruby and most dynamically-typed languages have higher runtime overhead compared to statically-typed languages. Also, the &lt;span class="caps"&gt;JVM&lt;/span&gt; has some known performance issues for dynamically-typed languages, some of which will be fixed in &lt;span class="caps"&gt;JDK 7&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;As always, enterprises should profile code written in their languages of choice to pick the best one for each particular task.&lt;/p&gt;


	&lt;h4&gt;Adoption&lt;/h4&gt;


	&lt;p&gt;Ruby is very easy to learn, although effective use of advanced techniques like metaprogramming require some time to master. JRuby-specific idioms are also easy to master and are well documented.&lt;/p&gt;


	&lt;h4&gt;Tool Support&lt;/h4&gt;


	&lt;p&gt;Ruby is experiencing tremendous growth in tool support. &lt;span class="caps"&gt;IDE&lt;/span&gt; support still lags support for Java, but IntelliJ, NetBeans, and Eclipse are working on Ruby support. JRuby users can exploit many Java tools.&lt;/p&gt;


	&lt;p&gt;Code analysis tools and testing tools (TDD and &lt;span class="caps"&gt;BDD&lt;/span&gt; styles) are now better than Java&amp;#8217;s.&lt;/p&gt;


	&lt;h4&gt;Deployment&lt;/h4&gt;


	&lt;p&gt;JRuby applications, even Ruby on Rails applications, can be deployed as jars or wars, requiring no modifications to an existing java-based infrastructure. Teams use this approach to minimize the &amp;#8220;friction&amp;#8221; of adopting Ruby, while also getting the performance benefits of the &lt;span class="caps"&gt;JVM&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Because JRuby code is byte code at runtime, it can be managed with &lt;span class="caps"&gt;JMX&lt;/span&gt;, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;


	&lt;h3&gt;Scala&lt;/h3&gt;


	&lt;p&gt;Scala is a statically-typed language that supports an improved object model (with a full &lt;em&gt;mixin&lt;/em&gt; mechanism called &lt;em&gt;traits&lt;/em&gt;; similar to Ruby &lt;em&gt;modules&lt;/em&gt;) and full support for functional programming, following a design goal of the inventor of Scala, Martin Odersky, that these two &lt;em&gt;paradigms&lt;/em&gt; can be integrated, despite some surface incompatibilities.  Odersky was involved in the design of Java generics (through earlier research languages) and he wrote the original version of the current &lt;code&gt;javac&lt;/code&gt;. The name is a contraction of &amp;#8220;scalable language&amp;#8221;, but the first &amp;#8220;a&amp;#8221; is pronounced like &amp;#8220;ah&amp;#8221;, not long as in the word &amp;#8220;hay&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;The syntax looks like a cross between Ruby (method definitions start with the &lt;code&gt;def&lt;/code&gt; keyword) and Java (&lt;em&gt;e.g.,&lt;/em&gt; curly braces). Type inferencing and other syntactic conventions significantly reduce the &amp;#8220;cluuter&amp;#8221;, such as the number of explicit type declarations (&amp;#8220;annotations&amp;#8221;) compared to Java. Scala syntax is very succinct, sometimes even more so than Ruby!  For more on Scala, see also my previous blog postings, &lt;a href="http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i"&gt;part 1&lt;/a&gt;, &lt;a href="http://blog.objectmentor.com/articles/2008/08/05/the-seductions-of-scala-part-ii-functional-programming"&gt;part 2&lt;/a&gt;, &lt;a href="http://blog.objectmentor.com/articles/2008/08/14/the-seductions-of-scala-part-iii-concurrent-programming"&gt;part 3&lt;/a&gt;, and this related post on &lt;a href="http://blog.objectmentor.com/articles/2008/09/27/traits-vs-aspects-in-scala"&gt;traits vs. aspects&lt;/a&gt;.&lt;/p&gt;


	&lt;h4&gt;Interoperability&lt;/h4&gt;


	&lt;p&gt;Scala&amp;#8217;s has the most seamless interoperability with Java of any of the languages discussed here. This is due in part to Scala&amp;#8217;s static typing and &amp;#8220;closed&amp;#8221; classes (as opposed to Ruby&amp;#8217;s &amp;#8220;open&amp;#8221; classes). It is &lt;em&gt;trivial&lt;/em&gt; to import and use Java classes, implement interfaces, &lt;em&gt;etc&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Direct &lt;span class="caps"&gt;API&lt;/span&gt; calls from Java to Scala are also supported. The developer needs to know how the names of Scala methods are encoding in byte code. For example, Scala methods can have &amp;#8220;operator&amp;#8221; names, like &amp;#8221;+&amp;#8221;. In the byte code, that name will be &amp;#8221;$plus&amp;#8221;.&lt;/p&gt;


	&lt;h4&gt;Object Model&lt;/h4&gt;


	&lt;p&gt;Scala&amp;#8217;s object model extends Java&amp;#8217;s model with &lt;em&gt;traits&lt;/em&gt;, which support flexble &lt;em&gt;mixin&lt;/em&gt; composition. Traits behave like interfaces &lt;em&gt;with&lt;/em&gt; implementations. The Scala object model provides other sophisticated features for building &amp;#8220;scalable applications&amp;#8221;.&lt;/p&gt;


	&lt;h4&gt;New Ideas&lt;/h4&gt;


	&lt;p&gt;Scala brings full support for functional programming to the &lt;span class="caps"&gt;JVM&lt;/span&gt;, including first-class function and closures. Other aspects of functional programming, like immutable variables and side-effect free functions, are encouraged by the language, but not mandated, as Scala is not a pure functional language. (Functional programming is very effective strategy for writing tread-safe programs, &lt;em&gt;etc.&lt;/em&gt;) Scala&amp;#8217;s &lt;em&gt;Actor&lt;/em&gt; library is a port of Erlang&amp;#8217;s &lt;em&gt;Actor&lt;/em&gt; library, a message-based concurrency approach.&lt;/p&gt;


	&lt;p&gt;In my view, the &lt;em&gt;Actor model is the best general-purpose approach to concurrency.&lt;/em&gt; There are times when multi-threaded code is needed for performance, but not for most concurrent applications. (Note: there are Actor libraries for Java, &lt;em&gt;e.g.,&lt;/em&gt; &lt;a href="http://www.malhar.net/sriram/kilim/"&gt;Kilim&lt;/a&gt;.)&lt;/p&gt;


	&lt;p&gt;Scala has very good support for building internal &lt;span class="caps"&gt;DSL&lt;/span&gt;&amp;#8217;s, although it is not quite as good as Ruby&amp;#8217;s features for this purpose. It has a combinator parser library that makes external &lt;span class="caps"&gt;DSL&lt;/span&gt; creation comparatively easy. Scala also offers some innovative &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8217;s for &lt;span class="caps"&gt;XML&lt;/span&gt; processing and Swing development.&lt;/p&gt;


	&lt;h4&gt;Stability&lt;/h4&gt;


	&lt;p&gt;Scala is over 5 years old and it is very stable. The &lt;span class="caps"&gt;API&lt;/span&gt; and syntax continue to evolve, but no major, disruptive changes are expected. In fact, the structure of the language is such that almost all changes occur in libraries, not the language grammar.&lt;/p&gt;


	&lt;p&gt;There are some well-known production deployments, such as back-end services at &lt;a href="http://twitter.com"&gt;twitter&lt;/a&gt;.&lt;/p&gt;


	&lt;h4&gt;Performance&lt;/h4&gt;


	&lt;p&gt;Scala provides comparable performance to Java, since it is very close &amp;#8220;structurally&amp;#8221; to Java code at the byte-code level, given the static typing and &amp;#8220;closed&amp;#8221; classes. Hence, Scala can exploit &lt;span class="caps"&gt;JVM&lt;/span&gt; optimizations that aren&amp;#8217;t available to dynamically-typed languages.&lt;/p&gt;


	&lt;p&gt;However, Scala will also benefit from planned improvements to support dynamically-typed languages, such as tail-call optimizations (which Scala current does in the compiler.) Hence, Scala &lt;em&gt;probably&lt;/em&gt; has marginally better performance than JRuby, in general. If true, Scala may be more appealing than JRuby as a general-purpose, &lt;em&gt;systems&lt;/em&gt; language, where performance is &lt;em&gt;critical&lt;/em&gt;.&lt;/p&gt;


	&lt;h4&gt;Adoption&lt;/h4&gt;


	&lt;p&gt;Scala is harder to learn and master than JRuby, because it is a more comprehensive language. It not only supports a sophisticated object model, but it also supports functional programming, type inferencing, &lt;em&gt;etc.&lt;/em&gt; In my view, the extra effort will be rewarded with higher productivity. Also, because it is closer to Java than JRuby and Clojure, new users will be able to start using it quickly as a &amp;#8220;better object-oriented Java&amp;#8221;, while they continue to learn the more advanced features, like functional programming, that will accelerate their productivity over the long term.&lt;/p&gt;


	&lt;h4&gt;Tool Support&lt;/h4&gt;


	&lt;p&gt;Scala support in &lt;span class="caps"&gt;IDE&lt;/span&gt;&amp;#8217;s still lags support for Java, but it is improving. IntelliJ, NetBeans, and Eclipse now support Scala with plugins. Maven and ant are widely used as the build tool for Scala applications. Several excellent &lt;span class="caps"&gt;TDD&lt;/span&gt; and &lt;span class="caps"&gt;BDD&lt;/span&gt; libraries are available.&lt;/p&gt;


	&lt;h4&gt;Deployment&lt;/h4&gt;


	&lt;p&gt;Scala applications are packaged and deployed just like Java applications, since Scala files are compiled to class files. A Scala runtime jar is also required.&lt;/p&gt;


	&lt;h3&gt;Clojure&lt;/h3&gt;


	&lt;p&gt;Of the three new &lt;span class="caps"&gt;JVM&lt;/span&gt; languages discussed here, Clojure is the least like Java, due to its Lisp syntax and innovative &amp;#8220;programming model&amp;#8221;. Yet it is also the most innovative and exciting new &lt;span class="caps"&gt;JVM&lt;/span&gt; language for many people. Clojure interoperates with Java code, but it emphasizes functional programming. Unlike the other languages, Clojure does not support object-oriented programming. Instead, it relies on mechanisms like multi-methods and macros to address design problems for which &lt;span class="caps"&gt;OOP&lt;/span&gt; is often used.&lt;/p&gt;


	&lt;p&gt;One exciting innovation in Clojure is support for &lt;em&gt;software transactional memory&lt;/em&gt;, which uses a database-style transactional approach to concurrent modifications of in-memory, mutable state. &lt;span class="caps"&gt;STM&lt;/span&gt; is somewhat controversial. You can google for arguments about its practicality, &lt;em&gt;etc.&lt;/em&gt; However, Clojure&amp;#8217;s implementation appears to be successful.&lt;/p&gt;


	&lt;p&gt;Clojure also has other innovative ways of supporting &amp;#8220;principled&amp;#8221; modification of mutable data, while encouraging the use of immutable data. These features with &lt;span class="caps"&gt;STM&lt;/span&gt; are the basis of Clojure&amp;#8217;s approach to robust concurrency.&lt;/p&gt;


	&lt;p&gt;Finally, Clojure implements several optimizations in the compiler that are important for functional programming, such as optimizing tail call recursion.&lt;/p&gt;


	&lt;p&gt;Disclaimer: I know less about Clojure than JRuby and Scala. While I have endeavored to get the facts right, there may be errors in the following analysis. Feedback is welcome.&lt;/p&gt;


	&lt;h4&gt;Interoperability&lt;/h4&gt;


	&lt;p&gt;Despite the Lisp syntax and functional-programming emphasis, Clojure interoperates with Java. Calling java from Clojure uses direct &lt;span class="caps"&gt;API&lt;/span&gt; calls, as for JRuby and Scala. Calling Clojure from Java is a more involved. You have to create Java proxies on the Clojure side to generate the byte code needed on the Java side. The idioms for doing this are straightforward, however.&lt;/p&gt;


	&lt;h4&gt;Object Model&lt;/h4&gt;


	&lt;p&gt;Clojure is not an object-oriented language. However, in order to interoperate with Java code, Clojure supports implementing interfaces and instantiating Java objects. Otherwise, Clojure offers a significant departure for develops well versed in object-oriented programming, but with little functional programming experience.&lt;/p&gt;


	&lt;h4&gt;New Ideas&lt;/h4&gt;


	&lt;p&gt;Clojure brings to the &lt;span class="caps"&gt;JVM&lt;/span&gt; full support for functional programming and popular Lisp concepts like macros, multi-methods, and powerful metaprogramming. It has innovative approaches to safe concurrency, including &amp;#8220;principled&amp;#8221; mechanisms for supporting mutable state, as discussed previously.&lt;/p&gt;


	&lt;p&gt;Clojure&amp;#8217;s succinct syntax and built-in libraries make processing &lt;span class="caps"&gt;XML&lt;/span&gt; succinct and efficient. &lt;span class="caps"&gt;DSL&lt;/span&gt; creation is also supported using Lisp mechanisms, like macros.&lt;/p&gt;


	&lt;h4&gt;Stability&lt;/h4&gt;


	&lt;p&gt;Clojure is the newest of the three languages profiled here. Hence, it may be the most subject to change. However, given the nature of Lisps, it is more likely that changes will occur in libraries than the language itself. Stability in terms of bugs does not appear to be an issue.&lt;/p&gt;


	&lt;p&gt;Clojure also has the fewest known production deployments of the three languages. However, industry adoption is expected to happen rapidly.&lt;/p&gt;


	&lt;h4&gt;Performance&lt;/h4&gt;


	&lt;p&gt;Clojure supports type &amp;#8220;hints&amp;#8221; to assist in optimizing performance. The preliminary discussions I have seen suggest that Clojure offers very good performance.&lt;/p&gt;


	&lt;h4&gt;Adoption&lt;/h4&gt;


	&lt;p&gt;Clojure is more of a departure from Java than is Scala. It will require a motivated team that likes Lisp ;) However, such a team may learn Clojure faster than Scala, since Clojure is a simpler language, &lt;em&gt;e.g.,&lt;/em&gt; because it doesn&amp;#8217;t have its own object model. Also, Lisps are well known for being simple languages, where the real learning comes in understanding how to use it effectively!&lt;/p&gt;


	&lt;p&gt;However, in my view, as for Scala, the extra learning effort will be rewarded with higher productivity.&lt;/p&gt;


	&lt;h4&gt;Tool Support&lt;/h4&gt;


	&lt;p&gt;As a new language, tool support is limited. Most Clojure developers use Emacs with its excellent Lisp support. Many Java tools can be used with Clojure.&lt;/p&gt;


	&lt;h4&gt;Deployment&lt;/h4&gt;


	&lt;p&gt;Clojure deployment appears to be as straightforward as for the other languages. A Clojure runtime jar is required.&lt;/p&gt;


	&lt;h3&gt;Comparisons&lt;/h3&gt;


	&lt;p&gt;Briefly, let&amp;#8217;s review the points and compare the three languages.&lt;/p&gt;


	&lt;h4&gt;Interoperability&lt;/h4&gt;


	&lt;p&gt;All three languages make calling Java code straightforward. Scala interoperates most seamlessly. Scala code is easiest to invoke from Java code, using direct &lt;span class="caps"&gt;API&lt;/span&gt; calls, as long as you know how Scala encodes method names that have &amp;#8220;illegal&amp;#8221; characters (according to the &lt;span class="caps"&gt;JVM&lt;/span&gt; spec.). Calling JRuby and Clojure code from Java is more involved.&lt;/p&gt;


	&lt;p&gt;Therefore, if you expect to continue writing Java code that needs to make frequent &lt;span class="caps"&gt;API&lt;/span&gt; calls to the  code in the new language, Scala will be a better choice.&lt;/p&gt;


	&lt;h4&gt;Object Model&lt;/h4&gt;


	&lt;p&gt;Scala is closest to Java&amp;#8217;s object model. Ruby&amp;#8217;s object model is superficially similar to Scala&amp;#8217;s, but the dynamic nature of Ruby brings significant differences. Both extend Java&amp;#8217;s object model with &lt;em&gt;mixin&lt;/em&gt; composition through &lt;em&gt;traits&lt;/em&gt; (Scala) or &lt;em&gt;modules&lt;/em&gt; (Ruby), that act like &lt;em&gt;interfaces with implementations&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Clojure is quite different, with an emphasis on functional programming and no direct support for object-oriented programming.&lt;/p&gt;


	&lt;h4&gt;New Ideas&lt;/h4&gt;


	&lt;p&gt;JRuby brings the productivity and power of a dynamically-typed language to the &lt;span class="caps"&gt;JVM&lt;/span&gt;, along with the drawbacks. It also brings some functional idioms.&lt;/p&gt;


	&lt;p&gt;Scala and Clojure bring full support for functional programming. Scala provides a complete Actor model of concurrency (as a library). Clojure brings software transactional memory and other innovations for writing robust concurrent applications. JRuby and Ruby don&amp;#8217;t add anything specific for concurrency.&lt;/p&gt;


	&lt;p&gt;JRuby, like Ruby, is exceptionally good for writing internal &lt;span class="caps"&gt;DSL&lt;/span&gt;&amp;#8217;s. Scala is also very good and Clojure benefits from Lisp&amp;#8217;s support for &lt;span class="caps"&gt;DSL&lt;/span&gt; creation.&lt;/p&gt;


	&lt;h4&gt;Stability&lt;/h4&gt;


	&lt;p&gt;All the language implementations are of high quality. Scala is the most mature, but JRuby has the widest adoption in production.&lt;/p&gt;


	&lt;h4&gt;Performance&lt;/h4&gt;


	&lt;p&gt;Performance should be comparable for all, but JRuby and Clojure have to deal with some inefficiencies inherent to running dynamic languages on the &lt;span class="caps"&gt;JVM&lt;/span&gt;. Your mileage may vary, so &lt;em&gt;please&lt;/em&gt; run realistic profiling experiments on sample implementations that are representative of your needs. Avoid &amp;#8220;prematurely optimization&amp;#8221; when choosing a new language. Often, team productivity and &amp;#8220;time to market&amp;#8221; are more important than raw performance.&lt;/p&gt;


	&lt;h4&gt;Adoption&lt;/h4&gt;


	&lt;p&gt;JRuby is the the easiest of the three languages to learn and adopt if you already have some Ruby or Ruby on Rails code in your environment.&lt;/p&gt;


	&lt;p&gt;Scala has the lowest barrier to adoption because it is the language that most resembles Java &amp;#8220;philosophically&amp;#8221; (static typing, emphasis on object-oriented programming, &lt;em&gt;etc.&lt;/em&gt;). Adopters can start with Scala as a &amp;#8220;better Java&amp;#8221; and gradually learn the advanced features (&lt;em&gt;mixin&lt;/em&gt; composition with traits and functional programming). Scala will appeal the most to teams that prefer statically-typed languages, yet want some of the benefits of dynamically-typed languages, like a succinct syntax.&lt;/p&gt;


	&lt;p&gt;However, Scala is the most complex of the three languages, while Clojure requires the biggest conceptual leap from Java.&lt;/p&gt;


	&lt;p&gt;Clojure will appeal to teams willing to explore more radical departures from what they are doing now, with potentially great payoffs!&lt;/p&gt;


	&lt;h4&gt;Deployment&lt;/h4&gt;


	&lt;p&gt;Deployment is easy with all three languages. Scala is most like Java, since you normally compile to class files (there is a limited interpreter mode). JRuby and Clojure code can be interpreted at runtime or compiled.&lt;/p&gt;


	&lt;h2&gt;Summary and Conclusions&lt;/h2&gt;


	&lt;p&gt;All three choices (or comparable substitutions from the list of other languages), will provide a Java team with a more modern language, yet fully leverage the existing investment in Java. Scala is the easiest incremental change. JRuby brings the vibrant Ruby world to the &lt;span class="caps"&gt;JVM&lt;/span&gt;. Clojure offers the most innovative departures from Java.&lt;/p&gt;</description>
      <pubDate>Thu, 15 Jan 2009 01:40:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:391065da-b6e2-4f4e-b3a1-cc911d3955df</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2009/01/15/adopting-new-jvm-languages-in-the-enterprise</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Agile Methods</category>
      <category>Scala</category>
      <category>JRuby</category>
      <category>clojure</category>
      <category>jvm</category>
      <category>enterprise</category>
      <category>Java</category>
    </item>
    <item>
      <title>Video of my RubyConf talk, &amp;quot;Better Ruby through Functional Programming&amp;quot;</title>
      <description>&lt;p&gt;Confreaks has started posting the videos from &lt;a href="http://rubyconf2008.confreaks.com/index.html"&gt;RubyConf&lt;/a&gt;.  Here&amp;#8217;s mine on &lt;a href="http://rubyconf2008.confreaks.com/better-ruby-through-functional-programming-2.html"&gt;Better Ruby through Functional Programming&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Please ignore the occasional Ruby (and Scala) bugs&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Thu, 27 Nov 2008 16:09:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:7adf0d3c-9f55-4fb4-b16f-35c22db87465</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/11/27/video-of-my-rubyconf-talk-better-ruby-through-functional-programming</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Ruby</category>
      <category>RubyConf</category>
      <category>funtional_programming</category>
    </item>
    <item>
      <title>Upcoming Speaking Engagements</title>
      <description>&lt;p&gt;I&amp;#8217;m speaking this Friday at &lt;a href="http://rubyconf.org/"&gt;RubyConf&lt;/a&gt; on &lt;a href="http://rubyconf.org/talks/46"&gt;Better Ruby Through Functional Programming&lt;/a&gt;. I&amp;#8217;ll introduce long-overlooked ideas from FP, why they are important for Ruby programmers, and how to use them in Ruby.&lt;/p&gt;


	&lt;p&gt;In two weeks, I&amp;#8217;m speaking on Wednesday, 11/19 at &lt;a href="http://qconsf.com/sf2008/"&gt;QCon San Francisco&lt;/a&gt; on &lt;a href="http://qconsf.com/sf2008/speaker/Dean+Wampler"&gt;Radical Simplification Through Polyglot and Poly-paradigm Programming&lt;/a&gt;. The idea of this talk is that combining the right languages and modularity paradigms (&lt;em&gt;i.e.,&lt;/em&gt; objects, functions, aspects) can simplify your code base and reduce the amount of code you have to write and manage, providing numerous benefits.&lt;/p&gt;


	&lt;p&gt;Back in Chicago, I&amp;#8217;m speaking at the &lt;a href="http://polyglotprogrammers.com/"&gt;Polyglot Programmer&amp;#8217;s&lt;/a&gt; meeting on &lt;a href="http://polyglotprogrammers.com/usa/illinois/chicago/"&gt;The Seductions of Scala&lt;/a&gt;, 11/13. It&amp;#8217;s an intro to the Scala language, which could become the language of choice for the &lt;span class="caps"&gt;JVM&lt;/span&gt;. I&amp;#8217;m repeating this talk at the &lt;a href="http://www.cjug.org/Wiki.jsp?page=2008.12.16.downtown"&gt;Chicago Java User&amp;#8217;s Group&lt;/a&gt; on 12/16. I&amp;#8217;m co-writing a book on Scala with &lt;a href="http://twitter.com/al3x"&gt;Alex Payne&lt;/a&gt;. O&amp;#8217;Reilly will be the publisher.&lt;/p&gt;


	&lt;p&gt;Incidently, Bob Martin is also speaking in Chicago on 11/13 at the &lt;a href="http://www.aplnchicago.org/"&gt;&lt;span class="caps"&gt;APLN&lt;/span&gt; Chicago&lt;/a&gt; meeting on &lt;a href="http://www.aplnchicago.org/Calendar.php"&gt;Software Professionalism&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Mon, 03 Nov 2008 18:13:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:4d0b05fe-743b-46a8-b236-625089da6095</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/11/03/upcoming-speaking-engagements</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>Agile Methods</category>
      <category>Clean Code</category>
      <category>talks</category>
      <category>RubyConf</category>
      <category>APLN</category>
      <category>Polyglot_Programmers</category>
      <category>Scala</category>
      <category>QCon</category>
    </item>
    <item>
      <title>A Scala-style &amp;quot;with&amp;quot; Construct for Ruby</title>
      <description>&lt;p&gt;Scala has a &amp;#8220;mixin&amp;#8221; construct called &lt;em&gt;traits&lt;/em&gt;, which are roughly analogous to Ruby &lt;em&gt;modules&lt;/em&gt;. They allow you to create reusable, modular bits of state and behavior and use them to compose classes and other traits or modules.&lt;/p&gt;


	&lt;p&gt;The syntax for using Scala traits is quite elegant. It&amp;#8217;s straightforward to implement the same syntax in Ruby and doing so has a few useful advantages.&lt;/p&gt;


	&lt;p&gt;For example, here is a Scala example that uses a trait to trace calls to a &lt;code&gt;Worker.work&lt;/code&gt; method.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
// run with "scala example.scala" 

class Worker {
    def work() = "work" 
}

trait WorkerTracer extends Worker {
    override def work() = "Before, " + super.work() + ", After" 
}

val worker = new Worker with WorkerTracer

println(worker.work())        // =&amp;gt; Before, work, After
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Note that &lt;code&gt;WorkerTracer&lt;/code&gt; extends &lt;code&gt;Worker&lt;/code&gt; so it can override the &lt;code&gt;work&lt;/code&gt; method. Since Scala is statically typed, you can&amp;#8217;t just define an &lt;code&gt;override&lt;/code&gt; method and call &lt;code&gt;super&lt;/code&gt; unless the compiler knows there really is a &amp;#8220;super&amp;#8221; method!&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s a Ruby equivalent.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# run with "ruby example.rb" 

module WorkerTracer
    def work; "Before, #{super}, After"; end
end

class Worker 
    def work; "work"; end
end

class TracedWorker &amp;lt; Worker 
  include WorkerTracer
end

worker = TracedWorker.new

puts worker.work          # =&amp;gt; Before, work, After
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Note that we have to create a subclass, which isn&amp;#8217;t required for the Scala case (but can be done when desired).&lt;/p&gt;


	&lt;p&gt;If you know that you will always want to trace calls to &lt;code&gt;work&lt;/code&gt; in the Ruby case, you might be tempted to dispense with the subclass and just add &lt;code&gt;include WorkerTracer&lt;/code&gt; in &lt;code&gt;Worker&lt;/code&gt;. Unfortunately, this won&amp;#8217;t work. Due to the way that Ruby resolves methods, the version of &lt;code&gt;work&lt;/code&gt; in the module will not be found before the version defined in &lt;code&gt;Worker&lt;/code&gt; itself. Hence the subclass seems to be the only option.&lt;/p&gt;


	&lt;p&gt;However, we can work around this using metaprogramming. We can use &lt;code&gt;WorkerTracer#append_features(...)&lt;/code&gt;. What goes in the argument list? If we pass &lt;code&gt;Worker&lt;/code&gt;, then all instances of &lt;code&gt;Worker&lt;/code&gt; will be effected, but actually we&amp;#8217;ll still have the problem with the method resolution rules.&lt;/p&gt;


	&lt;p&gt;If we just want to affect one object &lt;em&gt;and&lt;/em&gt; work around the method resolution roles, then we need to pass the &lt;em&gt;singleton class&lt;/em&gt; (or &lt;em&gt;eigenclass&lt;/em&gt; or &lt;em&gt;metaclass&lt;/em&gt; ...) for the object, which you can get with the following expression.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
metaclass = class &amp;lt;&amp;lt; worker; self; end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;So, to encapsulate all this and to get back to the original goal of implementing &lt;code&gt;with&lt;/code&gt;-style semantics, here is an implementation that adds a &lt;code&gt;with&lt;/code&gt; method to &lt;code&gt;Object&lt;/code&gt;, wrapped in an &lt;a href="http://rspec.info"&gt;rspec&lt;/a&gt; example.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# run with "spec ruby_with_spec.rb" 

require 'rubygems'
require 'spec'

# Warning, monkeypatching Object, especially with a name
# that might be commonly used is fraught with peril!!

class Object
  def with *modules
    metaclass = class &amp;lt;&amp;lt; self; self; end
    modules.flatten.each do |m|
      m.send :append_features, metaclass
    end
    self
  end
end

module WorkerTracer
    def work; "Before, #{super}, After"; end
end

module WorkerTracer1
    def work; "Before1, #{super}, After1"; end
end

class Worker 
    def work; "work"; end
end

describe "Object#with" do
  it "should make no changes to an object if no modules are specified" do
    worker = Worker.new.with
    worker.work.should == "work" 
  end

  it "should override any methods with a module's methods of the same name" do
    worker = Worker.new.with WorkerTracer
    worker.work.should == "Before, work, After" 
  end

  it "should stack overrides for multiple modules" do
    worker = Worker.new.with(WorkerTracer).with(WorkerTracer1)
    worker.work.should == "Before1, Before, work, After, After1" 
  end

  it "should stack overrides for a list of modules" do
    worker = Worker.new.with WorkerTracer, WorkerTracer1
    worker.work.should == "Before1, Before, work, After, After1" 
  end

  it "should stack overrides for an array of modules" do
    worker = Worker.new.with [WorkerTracer, WorkerTracer1]
    worker.work.should == "Before1, Before, work, After, After1" 
  end
end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;You should carefully consider the warning about monkeypatching &lt;code&gt;Object&lt;/code&gt;!  Also, note that &lt;code&gt;Module.append_features&lt;/code&gt; is actually private, so I had to use &lt;code&gt;m.send :append_features, ...&lt;/code&gt; instead.&lt;/p&gt;


	&lt;p&gt;The syntax is reasonably intuitive and it eliminates the need for an explicit subclass. You can pass a single module, or a list or array of them. Because &lt;code&gt;with&lt;/code&gt; returns the object, you can also chain &lt;code&gt;with&lt;/code&gt; calls.&lt;/p&gt;


	&lt;p&gt;A final note; many developers steer clear of metaprogramming and reflection features in their languages, out of fear. While prudence is &lt;em&gt;definitely&lt;/em&gt; wise, the power of these tools can dramatically accelerate your productivity. &lt;em&gt;Metaprogramming is just programming.&lt;/em&gt; Every developer should master it.&lt;/p&gt;</description>
      <pubDate>Mon, 29 Sep 2008 22:41:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b54295c4-67e0-4acb-a9f6-ea098a3a29e2</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/29/a-scala-style-_with_-construct-for-ruby</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>Ruby</category>
      <category>Scala</category>
      <category>with</category>
      <category>mixins</category>
      <category>design</category>
      <category>Metaprogramming</category>
    </item>
    <item>
      <title>Traits vs. Aspects in Scala</title>
      <description>&lt;p&gt;Scala &lt;em&gt;traits&lt;/em&gt; provide a mixin composition mechanism that has been missing in Java. Roughly speaking, you can think of &lt;em&gt;traits&lt;/em&gt; as analogous to Java interfaces, but with implementations.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Aspects&lt;/em&gt;, &lt;em&gt;e.g.,&lt;/em&gt; those written in &lt;a href="http://aspectj.org"&gt;AspectJ&lt;/a&gt;, are another mechanism for mixin composition in Java. How do &lt;em&gt;aspects&lt;/em&gt; and &lt;em&gt;traits&lt;/em&gt; compare?&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s look at an example trait first, then re-implement the same behavior using an AspectJ aspect, and finally compare the two approaches.&lt;/p&gt;


	&lt;h3&gt;Observing with Traits&lt;/h3&gt;


	&lt;p&gt;In a previous &lt;a href="http://blog.objectmentor.com/articles/2008/08/14/the-seductions-of-scala-part-iii-concurrent-programming"&gt;post on Scala&lt;/a&gt;, I gave an example of the &lt;em&gt;Observer Pattern&lt;/em&gt; implemented using a trait. Chris Shorrock and &lt;a href="http://james-iry.blogspot.com/"&gt;James Iry&lt;/a&gt; provided improved versions in the comments. I&amp;#8217;ll use James&amp;#8217; example here.&lt;/p&gt;


	&lt;p&gt;To keep things as simple as possible, let&amp;#8217;s observe a simple &lt;code&gt;Counter&lt;/code&gt;, which increments an internal count variable by the number input to an &lt;code&gt;add&lt;/code&gt; method.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

class Counter {
    var count = 0
    def add(i: Int) = count += i
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;count&lt;/code&gt; field is actually public, but I will only write to it through &lt;code&gt;add&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Here is James&amp;#8217; &lt;em&gt;Subject&lt;/em&gt; trait that implements the &lt;em&gt;Observer Pattern&lt;/em&gt;.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

trait Subject {
  type Observer = { def receiveUpdate(subject:Any) }

  private var observers = List[Observer]()
  def addObserver(observer:Observer) = observers ::= observer
  def notifyObservers = observers foreach (_.receiveUpdate(this))
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Effectively, this says that we can use any object as an &lt;code&gt;Observer&lt;/code&gt; as long as it matches the &lt;em&gt;structural type&lt;/em&gt; &lt;code&gt;{ def receiveUpdate(subject:Any) }&lt;/code&gt;. Think of structural types as anonymous interfaces. Here, a valid observer is one that has a &lt;code&gt;receiveUpdate&lt;/code&gt; method taking an argument of &lt;code&gt;Any&lt;/code&gt; type.&lt;/p&gt;


	&lt;p&gt;The rest of the trait manages a list of observers and defines a &lt;code&gt;notifyObservers&lt;/code&gt; method. The expression &lt;code&gt;observers ::= observer&lt;/code&gt; uses the &lt;code&gt;List&lt;/code&gt; &lt;code&gt;::&lt;/code&gt; (&amp;#8220;cons&amp;#8221;) operator to &lt;em&gt;prepend&lt;/em&gt; an item to the list. (Note, I am using the default immutable &lt;code&gt;List&lt;/code&gt;, so a new copy is created everytime.)&lt;/p&gt;


	&lt;p&gt;The &lt;code&gt;notifyObservers&lt;/code&gt; method iterates through the observers, calling &lt;code&gt;receiveUpdate&lt;/code&gt; on each one. The &lt;code&gt;_&lt;/code&gt; that gets replaced with each observer during the iteration.&lt;/p&gt;


	&lt;p&gt;Finally, here is a &lt;a href="http://code.google.com/p/specs"&gt;specs&lt;/a&gt; file that exercises the code.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

import org.specs._

object CounterObserverSpec extends Specification {
    "A Counter Observer" should {
        "observe counter increments" in {
            class CounterObserver {
                var updates = 0
                def receiveUpdate(subject:Any) = updates += 1
            }
            class WatchedCounter extends Counter with Subject {
                override def add(i: Int) = { 
                    super.add(i)
                    notifyObservers
                }
            }
            var watchedCounter = new WatchedCounter
            var counterObserver = new CounterObserver
            watchedCounter.addObserver(counterObserver)
            for (i &amp;lt;- 1 to 3) watchedCounter.add(i)
            counterObserver.updates must_== 3
            watchedCounter.count must_== 6
    }
  }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;em&gt;specs&lt;/em&gt; library is a &lt;span class="caps"&gt;BDD&lt;/span&gt; tool inspired by &lt;a href="http://rspec.info"&gt;rspec&lt;/a&gt; in Rubyland.&lt;/p&gt;


	&lt;p&gt;I won&amp;#8217;t discuss it all the specs-specific details here, but hopefully you&amp;#8217;ll get the general idea of what it&amp;#8217;s doing.&lt;/p&gt;


	&lt;p&gt;Inside the &lt;code&gt;"observe counter increments" in {...}&lt;/code&gt;, I start by declaring two classes, &lt;code&gt;CounterObserver&lt;/code&gt; and &lt;code&gt;WatchedCounter&lt;/code&gt;. &lt;code&gt;CounterObserver&lt;/code&gt; satisfies our required &lt;em&gt;structural type&lt;/em&gt;, &lt;em&gt;i.e.,&lt;/em&gt; it provides a &lt;code&gt;receiveUpdate&lt;/code&gt; method.&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;WatchedCounter&lt;/code&gt; subclasses &lt;code&gt;Counter&lt;/code&gt; and mixes in the &lt;code&gt;Subject&lt;/code&gt; trait. It overrides the &lt;code&gt;add&lt;/code&gt; method, where it calls &lt;code&gt;Counter&lt;/code&gt;&amp;#8217;s &lt;code&gt;add&lt;/code&gt; first, then notifies the observers. No parentheses are used in the invocation of &lt;code&gt;notifyObservers&lt;/code&gt; because the method was not defined to take any!&lt;/p&gt;


	&lt;p&gt;Next, I create an instance of each class, add the observer to the &lt;code&gt;WatchedCounter&lt;/code&gt;, and make 3 calls to &lt;code&gt;watchedCounter.add&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Finally, I use the &amp;#8220;&lt;code&gt;actual must_== expected&lt;/code&gt;&amp;#8221; idiom to test the results. The observer should have seen 3 updates, while the counter should have a total of 6.&lt;/p&gt;


	&lt;p&gt;The following simple bash shell script will build and run the code.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
SCALA_HOME=...
SCALA_SPECS_HOME=...
CP=$SCALA_HOME/lib/scala-library.jar:$SCALA_SPECS_HOME/specs-1.3.1.jar:bin
rm -rf bin
mkdir -p bin
scalac -d bin -cp $CP src/example/*.scala
scala -cp $CP example/CounterObserverSpec
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Note that I put all the sources in a &lt;code&gt;src/example&lt;/code&gt; directory. Also, I&amp;#8217;m using v1.3.1 of &lt;em&gt;specs&lt;/em&gt;, as well as v2.7.1 of Scala. You should get the following output.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
Specification "CounterObserverSpec" 
  A Counter Observer should
  + observe counter increments

Total for specification "CounterObserverSpec":
Finished in 0 second, 60 ms
1 example, 2 assertions, 0 failure, 0 error
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h3&gt;Observing with Aspects&lt;/h3&gt;


	&lt;p&gt;Because Scala compiles to Java byte code, I can use AspectJ to advice Scala code! For this to work, you have to be aware of how Scala represents its concepts in byte code. For example, object declarations, &lt;em&gt;e.g.,&lt;/em&gt; &lt;code&gt;object Foo {...}&lt;/code&gt; become static final classes. Also, method names like &lt;code&gt;+&lt;/code&gt; become &lt;code&gt;$plus&lt;/code&gt; in byte code.&lt;/p&gt;


	&lt;p&gt;However, most Scala type, method, and variable names can be used as is in AspectJ. This is true for my example.&lt;/p&gt;


	&lt;p&gt;Here is an aspect that observes calls to &lt;code&gt;Counter.add&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

public aspect CounterObserver {
    after(Object counter, int value): 
        call(void *.add(int)) &amp;#38;&amp;#38; target(counter) &amp;#38;&amp;#38; args(value) {

        RecordedObservations.record("adding "+value);
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;You can read this aspect as follows, &lt;em&gt;after calling &lt;code&gt;Counter.add&lt;/code&gt; (and keeping track of the Counter object that was called, and the value passed to the method), call the static method &lt;code&gt;record&lt;/code&gt; on the &lt;code&gt;RecordedObservations&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m using a separate Scala object &lt;code&gt;RecordedObservations&lt;/code&gt;&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

object RecordedObservations {
    private var messages = List[String]()
    def record(message: String):Unit = messages ::= message
    def count() = messages.length
    def reset():Unit = messages = Nil
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Recall that this is effectively a static final Java class. I need this separate object, rather than keeping information in the aspect itself, because of the simple-minded way I&amp;#8217;m building the code. ;) However, it&amp;#8217;s generally a good idea with aspects to delegate most of the work to Java or Scala code anyway.&lt;/p&gt;


	&lt;p&gt;Now, the &amp;#8220;spec&amp;#8221; file is:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

import org.specs._

object CounterObserverSpec extends Specification {
    "A Counter Observer" should {
        "observe counter increments" in {
            RecordedObservations.reset()
            var counter = new Counter
            for (i &amp;lt;- 1 to 3) counter.add(i)
            RecordedObservations.count() must_== 3
            counter.count must_== 6
    }
  }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This time, I don&amp;#8217;t need two more classes for the adding a mixin trait or defining an observer. Also, I call &lt;code&gt;RecordedObservations.count&lt;/code&gt; to ensure it was called 3 times.&lt;/p&gt;


	&lt;p&gt;The build script is also slightly different to add the AspectJ compilation.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
SCALA_HOME=...
SCALA_SPECS_HOME=...
ASPECTJ_HOME=...
CP=$SCALA_HOME/lib/scala-library.jar:$SCALA_SPECS_HOME/specs-1.3.1.jar:$ASPECTJ_HOME/lib/aspectjrt.jar:bin
rm -rf bin app.jar
mkdir -p bin
scalac -d bin -cp $CP src/example/*.scala 
ajc -1.5 -outjar app.jar -cp $CP -inpath bin src/example/CounterObserver.aj
aj -cp $ASPECTJ_HOME/lib/aspectjweaver.jar:app.jar:$CP example.CounterObserverSpec
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;ajc&lt;/code&gt; command not only compiles the aspect, but it &amp;#8220;weaves&amp;#8221; into the compiled Scala classes in the &lt;code&gt;bin&lt;/code&gt; directory. Actually, it only affects the &lt;code&gt;Counter&lt;/code&gt; class. Then it writes all the woven and unmodified class files to &lt;code&gt;app.jar&lt;/code&gt;, which is used to execute the test. Note that for production use, you might prefer &lt;a href="http://www.eclipse.org/aspectj/doc/released/devguide/ltw.html"&gt;load-time weaving&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;The output is the same as before (except for the milliseconds), so I won&amp;#8217;t show it here.&lt;/p&gt;


	&lt;h3&gt;Comparing Traits with Aspects&lt;/h3&gt;


	&lt;p&gt;So far, both approaches are equally viable. The traits approach obviously doesn&amp;#8217;t require a separate language and corresponding tool set.&lt;/p&gt;


	&lt;p&gt;However, traits have one important limitation with respect to aspects. Aspects let you define &lt;em&gt;pointcuts&lt;/em&gt; that are queries over all possible points where new behavior or modifications might be desired. These points are called &lt;em&gt;join points&lt;/em&gt; in aspect terminology. The aspect I showed above has a simple pointcut that selects one join point, calls to the &lt;code&gt;Counter.add&lt;/code&gt; method.&lt;/p&gt;


	&lt;p&gt;However, what if I wanted to observe all state changes in all classes in a package? Defining traits for each case would be tedious and error prone, since it would be easy to overlook some cases. With an aspect framework like AspectJ, I can implement observation at all the points I care about in a &lt;em&gt;modular&lt;/em&gt; way.&lt;/p&gt;


	&lt;p&gt;Aspect frameworks support this by providing wildcard mechanisms. I won&amp;#8217;t go into the details here, but the &lt;code&gt;*&lt;/code&gt; in the previous aspect is an example, matching any type. Also, one of the most powerful techniques for writing robust aspects is to use pointcuts that reference only annotations, a form of abstraction. As a final example, if I add an annotation &lt;code&gt;Adder&lt;/code&gt; to &lt;code&gt;Counter.add&lt;/code&gt;,&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

class Counter {
    var count = 0
    @Adder def add(i: Int) = count += i
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Then I can rewrite the aspect as follows.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
package example

public aspect CounterObserver {
    after(Object counter, int value): 
        call(@Adder void *.*(int)) &amp;#38;&amp;#38; target(counter) &amp;#38;&amp;#38; args(value) {

        RecordedObservations.record("adding "+value);
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now, there are no type and method names in the pointcut. Any instance method on any visible type that takes one &lt;code&gt;int&lt;/code&gt; (or Scala &lt;code&gt;Int&lt;/code&gt;) argument and is annotated with &lt;code&gt;Adder&lt;/code&gt; will get matched.&lt;/p&gt;


	&lt;p&gt;Note: Scala requires that you create any custom annotations as normal Java annotations. Also, if you intend to use them with Aspects, use runtime retention policy, which will be necessary if you use &lt;a href="http://www.eclipse.org/aspectj/doc/released/devguide/ltw.html"&gt;load-time weaving&lt;/a&gt;.&lt;/p&gt;


	&lt;h3&gt;Conclusion&lt;/h3&gt;


	&lt;p&gt;If you need to mix in behavior in a specific, relatively-localized set of classes, Scala traits are probably all you need and you don&amp;#8217;t need another language. If you need more &amp;#8220;pervasive&amp;#8221; modifications (&lt;em&gt;e.g.,&lt;/em&gt; tracing, policy enforcement, security), consider using aspects.&lt;/p&gt;


	&lt;h4&gt;Acknowledgements&lt;/h4&gt;


	&lt;p&gt;Thanks to Ramnivas Laddad, whose forthcoming 2&lt;sup&gt;nd&lt;/sup&gt; Edition of &lt;cite&gt;AspectJ in Action&lt;/cite&gt; got me thinking about this topic.&lt;/p&gt;</description>
      <pubDate>Sat, 27 Sep 2008 22:33:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8628eb5f-f195-4ebe-a10a-f3ee9d66d591</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/27/traits-vs-aspects-in-scala</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>Scala</category>
      <category>aspects</category>
      <category>AspectJ</category>
      <category>mixins</category>
    </item>
    <item>
      <title>User Stories for Cross-Component Teams</title>
      <description>&lt;p&gt;I&amp;#8217;m working on an Agile Transition for a large organization. They are organized into component teams. They implement features by forming temporary feature teams with representatives from each of the relevant components, usually one developer per component.&lt;/p&gt;


	&lt;p&gt;Doing User Stories for such cross-component features can be challenging.&lt;/p&gt;


	&lt;p&gt;Now, it would be nice if the developers just pair-programmed with each other, ignoring their assigned component boundaries, but we&amp;#8217;re not quite there yet. Also, there are other issues we are addressing, such as the granularity of feature definitions, &lt;em&gt;etc., etc.&lt;/em&gt; Becoming truly agile will take time.&lt;/p&gt;


	&lt;p&gt;Given where we are, it&amp;#8217;s just not feasible to estimate a single story point value for each cross-component user story, because the work for each component varies considerably. A particular story might be the equivalent of 1 point for the UI part, 8 points for the middle-tier part, 2 points for the database part, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;So, what we&amp;#8217;re doing is treating the user story as an &amp;#8220;umbrella&amp;#8221;, with individual component stories underneath. We&amp;#8217;re estimating and tracking the points for each component story. The total points for the user story is the sum of the component story points, plus any extra we decide is needed for the integration and final acceptance testing work.&lt;/p&gt;


	&lt;p&gt;This model allows us to track the work more closely, as long as we remember that &lt;strong&gt;&lt;em&gt;component points mean nothing from the point of view of delivering customer value!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;I prefer this approach to documenting tasks, because it keeps the focus on delivering value to the &lt;em&gt;client&lt;/em&gt; of each story. For the component stories, the client will be another component.&lt;/p&gt;


	&lt;h3&gt;Automated Acceptance Tests for Component Stories&lt;/h3&gt;


	&lt;p&gt;Just as for user stories, we are defining &lt;em&gt;automated acceptance tests&lt;/em&gt; for each component. We&amp;#8217;re using JUnit for them, since we don&amp;#8217;t need a customer-friendly specification format, like &lt;a href="http://fitnesse.org"&gt;FitNesse&lt;/a&gt; or &lt;a href="http://rspec.info"&gt;RSpec&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;This is also a (sneaky&amp;#8230;) way to get the developers from different components to pair together. Say for example that we have a component story for the midtier and the UI is the &lt;em&gt;client&lt;/em&gt;. The UI developer and the midtier developer pair to produce the the acceptance criteria for the story.&lt;/p&gt;


	&lt;p&gt;For each component story, the pair of programmers produce the following:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;JUnit tests that define the acceptance criteria for the component story.&lt;/li&gt;
		&lt;li&gt;One or more interfaces that will be used by the client of the component. They will also be implemented by concrete classes in the component.&lt;/li&gt;
		&lt;li&gt;A &lt;a href="http://www.martinfowler.com/bliki/TestDouble.html"&gt;test double&lt;/a&gt; that passes the JUnit tests and allows the client to move forward while the component feature is being implemented.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;In a sense, the &amp;#8220;contract&amp;#8221; of the component story is the interfaces, which specify the static structure, and the JUnit tests, which specify the dynamic behavior of the feature.&lt;/p&gt;


	&lt;p&gt;This model of pair-programming the component interface should solve the common, inefficient communication problems when component interactions need to be changed. You know the scenario;  a &lt;em&gt;client&lt;/em&gt; component developer or manager tells a &lt;em&gt;server&lt;/em&gt; component developer or manager that a change is needed. A developer (probably a different one&amp;#8230;) on the &lt;em&gt;server&lt;/em&gt; component team makes up an interface, checks it into version control, and waits for feedback from the &lt;em&gt;client&lt;/em&gt; team. Meanwhile, the &lt;em&gt;server&lt;/em&gt; component developer starts implementing the changes.&lt;/p&gt;


	&lt;p&gt;A few days before the big drop to QA for final integration testing, the &lt;em&gt;server&lt;/em&gt; component developer realizes that the interface is missing some essential features. At the same time, the &lt;em&gt;client&lt;/em&gt; component developer finally gets around to using the new interface and discovers a different set of missing essential features. Hilarity ensues&amp;#8230;&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;re just getting started with this approach, but so far it is proving to be an effective way to organize our work and to be more efficient.&lt;/p&gt;</description>
      <pubDate>Fri, 19 Sep 2008 19:53:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:73b5a7a1-a1ae-49f5-9743-2368c0006606</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/19/user-stories-for-cross-component-teams</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>agile</category>
      <category>stories</category>
      <category>components</category>
      <category>acceptance</category>
      <category>Tests</category>
    </item>
    <item>
      <title>Configuration Management Systems, Automated Tests, CI, and Complexity</title>
      <description>&lt;p&gt;I&amp;#8217;m working with a client that has a very complex branching structure in their commercial CM system, which will remain nameless. Why is it so complex? Because everyone is afraid to merge to the integration branches.&lt;/p&gt;


	&lt;p&gt;This is a common symptom in teams that don&amp;#8217;t have have good automated test coverage and don&amp;#8217;t use continuous integration (CI). &lt;strong&gt;Fear&lt;/strong&gt; is their lot in life. They&amp;#8217;ll keep lots of little branches and only merge to integration when they&amp;#8217;re ready for the &amp;#8220;big bang&amp;#8221; integration.&lt;/p&gt;


	&lt;p&gt;I spoke with a manager at the client site today who expressed frustration that no one really knows which branch they should be committing to and when they should merge to the integration branches.&lt;/p&gt;


	&lt;p&gt;In contrast, projects with good test coverage and CI do almost all their work on the integration branch. They have little fear, because any problems will get caught and fixed quickly.  So, the first point of this post is:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Automated test coverage and CI drastically simplify your use of configuration management.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Here&amp;#8217;s something else I&amp;#8217;ve noticed, open source CM systems seem to focus on different priorities than commercial systems. &lt;strong&gt;Disclaimer:&lt;/strong&gt; I know I&amp;#8217;m generalizing a bit here.&lt;/p&gt;


	&lt;p&gt;The commercial systems tend to be really good at managing complex branching, with fancy &lt;span class="caps"&gt;GUI&lt;/span&gt; tools to help manage the big trees of branches, facilitate merges, &lt;em&gt;etc.&lt;/em&gt; That seems to be their biggest selling feature, &lt;span class="caps"&gt;GUI&lt;/span&gt;&amp;#8217;s to manage the complexity.&lt;/p&gt;


	&lt;p&gt;In contrast, most of the open-source CM systems have lower-tech &lt;span class="caps"&gt;GUI&lt;/span&gt;&amp;#8217;s, if any, but the teams using them don&amp;#8217;t seem to care that much. Usually, this is because these teams are also practicing &lt;span class="caps"&gt;TDD&lt;/span&gt; and CI, so they just don&amp;#8217;t need the wizardry as much.&lt;/p&gt;


	&lt;p&gt;The open-source CM systems seem to be better at scalability and performance. Some are pioneering  distributed CM, &lt;em&gt;e.g.,&lt;/em&gt; Git and Mercurial. Git, for example, was designed to manage a massive project called Linux. Maybe you&amp;#8217;ve heard of it.&lt;/p&gt;


	&lt;p&gt;Distributed CM is not easy, but it&amp;#8217;s a lot easier to do if you don&amp;#8217;t need to worry as much about complex branch hierarchies.&lt;/p&gt;


	&lt;p&gt;Most of the commercial tools I&amp;#8217;ve seen don&amp;#8217;t scale well and some require way too much administration. My client is apparently the biggest user of their particular tool and the developers complain all the time about performance. This tool is not designed to scale horizontally. The only hope is use faster hardware. In this case, the vendor has focused on managing complexity. To be frank, even their &lt;span class="caps"&gt;GUI&lt;/span&gt; tool is an uninspired and slow Java fat client.&lt;/p&gt;


	&lt;p&gt;So the second point of this post is:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Avoid CM tools that encourage complexity. Pick the ones that scale.&lt;/p&gt;
	&lt;/blockquote&gt;</description>
      <pubDate>Mon, 08 Sep 2008 16:53:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:cdb615bb-896e-41b4-8b99-804dd3511432</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/08/configuration-management-systems-automated-tests-ci-and-complexity</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>TDD</category>
      <category>ci</category>
      <category>cm</category>
      <category>Tests</category>
    </item>
    <item>
      <title>The Liskov Substitution Principle for &amp;quot;Duck-Typed&amp;quot; Languages</title>
      <description>&lt;p&gt;&lt;span class="caps"&gt;OCP&lt;/span&gt; and &lt;span class="caps"&gt;LSP&lt;/span&gt; together tell us how to organize similar &lt;em&gt;vs.&lt;/em&gt; variant behaviors. I blogged the other day about &lt;a href="http://blog.objectmentor.com/articles/2008/09/04/the-open-closed-principle-for-languages-with-open-classes"&gt;&lt;span class="caps"&gt;OCP&lt;/span&gt; in the context of languages with open classes&lt;/a&gt;
 (&lt;em&gt;i.e.,&lt;/em&gt; dynamically-typed languages). Let&amp;#8217;s look at the &lt;a href="http://www.objectmentor.com/resources/articles/lsp.pdf"&gt;Liskov Substitution Principle&lt;/a&gt; (LSP).&lt;/p&gt;


	&lt;p&gt;The Liskov Substitution Principle was coined by Barbara Liskov in &lt;a href="http://portal.acm.org/citation.cfm?id=62141"&gt;Data Abstraction and Hierarchy&lt;/a&gt; (1987).&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I&amp;#8217;ve always liked the elegant simplicity, yet power, of &lt;span class="caps"&gt;LSP&lt;/span&gt;. In less formal terms, it says that if a client (program) expects objects of one type to behave in a certain way, then it&amp;#8217;s only okay to substitute objects of another type if the same expectations are satisfied.&lt;/p&gt;


	&lt;p&gt;This is our best definition of inheritance. The well-known &lt;strong&gt;is-a&lt;/strong&gt; relationship between types is not precise enough. Rather, the relationship has to be &lt;strong&gt;behaves-as-a&lt;/strong&gt;, which unfortunately is more of a mouthful. Note that &lt;strong&gt;is-a&lt;/strong&gt; focuses on the &lt;em&gt;structural&lt;/em&gt; relationship, while &lt;strong&gt;behaves-as-a&lt;/strong&gt; focuses on the &lt;em&gt;behavioral&lt;/em&gt; relationship. A very useful, pre-TDD design technique called &lt;a href="http://en.wikipedia.org/wiki/Design_by_contract"&gt;Design by Contract&lt;/a&gt; emerges out of &lt;span class="caps"&gt;LSP&lt;/span&gt;, but that&amp;#8217;s another topic.&lt;/p&gt;


	&lt;p&gt;Note that there is a slight assumption that I made in the previous paragraph. I said that &lt;span class="caps"&gt;LSP&lt;/span&gt; defines &lt;em&gt;inheritance&lt;/em&gt;. Why inheritance specifically and not &lt;em&gt;substitutability&lt;/em&gt;, in general? Well, inheritance has been the main vehicle for substitutability for most OO languages, especially the statically-typed ones.&lt;/p&gt;


	&lt;p&gt;For example, a Java application might use a simple tracing abstraction like this.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public interface Tracing {
    void trace(String message);
}
    &lt;/code&gt;
&lt;/pre&gt;  

	&lt;p&gt;Clients might use this to trace methods calls to a log. Only classes that implement the &lt;code&gt;Tracer&lt;/code&gt; interface can be given to these clients. For example,&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public class TracerClient {
    private Tracer tracer;

    public TracerClient(Tracer tracer) {
        this.tracer = tracer;
    }

    public void doWork() {
        tracer.trace("in doWork():");
        // ...
    }
}
    &lt;/code&gt;
&lt;/pre&gt;  

	&lt;p&gt;However, &lt;a href="http://en.wikipedia.org/wiki/Duck_typing"&gt;Duck Typing&lt;/a&gt; is another form of substitutability that is commonly seen in dynamically-typed languages, like Ruby and Python.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;If it walks like a duck and quacks like a duck, it must be a duck.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Informally, duck typing says that a client can use any object you give it as long as the object implements the methods the client wants to invoke on it. Put another way, the object must respond to the messages the client wants to send to it.&lt;/p&gt;


	&lt;p&gt;The object appears to be a &amp;#8220;duck&amp;#8221; as far as the client is concerned.&lt;/p&gt;


	&lt;p&gt;In or example, clients only care about the &lt;code&gt;trace(message)&lt;/code&gt; method being supported. So, we might do the following in Ruby.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class TracerClient 
  def initialize tracer 
    @tracer = tracer
  end

  def do_work
    @tracer.trace "in do_work:" 
    # ... 
  end
end

class MyTracer
  def trace message
    p message
  end
end

client = TracerClient.new(MyTracer.new)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;No &amp;#8220;interface&amp;#8221; is necessary. I just need to pass an object to &lt;code&gt;TracerClient.initialize&lt;/code&gt; that responds to the &lt;code&gt;trace&lt;/code&gt; message. Here, I defined a class for the purpose. You could also add the &lt;code&gt;trace&lt;/code&gt; method to another type or object.&lt;/p&gt;


	&lt;p&gt;So, &lt;span class="caps"&gt;LSP&lt;/span&gt; is still essential, in the generic sense of valid substitutability, but it doesn&amp;#8217;t have to be inheritance based.&lt;/p&gt;


	&lt;p&gt;Is Duck Typing good or bad? It largely comes down to your view about dynamically-typed &lt;em&gt;vs.&lt;/em&gt; statically-typed languages. I don&amp;#8217;t want to get into that debate here! However, I&amp;#8217;ll make a few remarks.&lt;/p&gt;


	&lt;p&gt;On the negative side, without a &lt;code&gt;Tracer&lt;/code&gt; abstraction, you have to rely on appropriate naming of objects to convey what they do (but you should be doing that anyway). Also, it&amp;#8217;s harder to find all the &amp;#8220;tracing-behaving&amp;#8221; objects in the system.&lt;/p&gt;


	&lt;p&gt;On the other hand, the client really doesn&amp;#8217;t care about a &amp;#8220;Tracer&amp;#8221; type, only a single method. So, we&amp;#8217;ve decoupled &amp;#8220;client&amp;#8221; and &amp;#8220;server&amp;#8221; just a bit more. This decoupling is more evident when using closures to express behavior, &lt;em&gt;e.g.,&lt;/em&gt; for &lt;code&gt;Enumerable&lt;/code&gt; methods. In our case, we could write the following.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class TracerClient2 
  def initialize &amp;#38;tracer 
    @tracer = tracer
  end

  def do_work 
    @tracer.call "in do_work:" 
    # ... 
  end
end

client = TracerClient2.new {|message| p "block tracer: #{message}"}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;For comparison, consider how we might approach substitutability in &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt;. As a statically-typed language, Scala doesn&amp;#8217;t support duck typing &lt;em&gt;per se&lt;/em&gt;, but it does support a very similar mechanism called &lt;a href="http://neilbartlett.name/blog/2007/09/13/statically-checked-duck-typing-in-scala/"&gt;structural types&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Essentially, structural types let us declare that a method parameter must support one or more methods, without having to say it supports a full interface. Loosely speaking, it&amp;#8217;s like using an anonymous interface.&lt;/p&gt;


	&lt;p&gt;In our Java example, when we declare a tracer object in our client, we would be able to declare that is supports &lt;code&gt;trace&lt;/code&gt;, without having to specify that it implements a full interface.&lt;/p&gt;


	&lt;p&gt;To be explicit, recall our Java constructor for &lt;code&gt;TestClient&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public class TracerClient {
    public TracerClient(Tracer tracer) { ... }
    // ...
    }
}
    &lt;/code&gt;
&lt;/pre&gt;  

In Scala, a complete example would be the following.
&lt;pre&gt;
    &lt;code&gt;
class ScalaTracerClient(val tracer: { def trace(message:String) }) {
    def doWork() = { tracer.trace("doWork") }
}

class ScalaTracer() {
    def trace(message: String) = { println("Scala: "+message) }
}

object TestScalaTracerClient {
    def main() {
        val client = new ScalaTracerClient(new ScalaTracer())
        client.doWork();
    }
}
TestScalaTracerClient.main()
    &lt;/code&gt;
&lt;/pre&gt;  

	&lt;p&gt;Recall from &lt;a href="http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i"&gt;my previous blogs on Scala&lt;/a&gt;, the argument list to the class name is the constructor arguments. The constructor takes a &lt;code&gt;tracer&lt;/code&gt; argument whose &amp;#8220;type&amp;#8221; (after the &amp;#8217;:&amp;#8217;) is &lt;code&gt;{ def trace(message:String) }&lt;/code&gt;. That is, all we require of &lt;code&gt;tracer&lt;/code&gt; is that it support the &lt;code&gt;trace&lt;/code&gt; method.&lt;/p&gt;


	&lt;p&gt;So, we get duck type-like behavior, but statically type checked. We&amp;#8217;ll get a compile error, rather than a run-time error, if someone passes an object to the client that doesn&amp;#8217;t respond to &lt;code&gt;tracer&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;To conclude, &lt;span class="caps"&gt;LSP&lt;/span&gt; can be reworded &lt;em&gt;very&lt;/em&gt; slightly.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is &lt;strong&gt;substitutable for&lt;/strong&gt; T.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I replaced &lt;strong&gt;a subtype of&lt;/strong&gt; with &lt;strong&gt;substitutable for&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;An important point is that the idea of a &amp;#8220;contract&amp;#8221; between the types and their clients is still important, even in a  language with duck-typing or structural typing. However, languages with these features give us more ways to extend our system, while still supporting &lt;span class="caps"&gt;LSP&lt;/span&gt;.&lt;/p&gt;</description>
      <pubDate>Sat, 06 Sep 2008 23:48:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:48d425c0-b536-44de-bf6a-b83755c064b9</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/06/the-liskov-substitution-principle-for-duck-typed-languages</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Ruby</category>
      <category>ducktyping</category>
      <category>Scala</category>
      <category>Java</category>
      <category>design</category>
      <category>LSP</category>
    </item>
    <item>
      <title>The Open-Closed Principle for Languages with Open Classes</title>
      <description>&lt;p&gt;We&amp;#8217;ve been having a discussion inside Object Mentor World Design Headquarters about the meaning of the &lt;span class="caps"&gt;OCP&lt;/span&gt; for dynamic languages, like Ruby, with open classes.&lt;/p&gt;


	&lt;p&gt;For example, in Ruby it&amp;#8217;s normal to define a class or module, &lt;em&gt;e.g.,&lt;/em&gt;&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo.rb
class Foo
    def method1 *args
        ...
    end
end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;and later re-open the class and add (or redefine) methods,&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo2.rb
class Foo
    def method2 *args
        ...
    end
end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Users of &lt;code&gt;Foo&lt;/code&gt; see all the methods, as if &lt;code&gt;Foo&lt;/code&gt; had one definition.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
foo = Foo.new
foo.method1 :arg1, :arg2
foo.method2 :arg1, :arg2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Do open classes violate the Open-Closed Principle? Bertrand Meyer articulated &lt;span class="caps"&gt;OCP&lt;/span&gt;. Here is his definition&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Software entities (classes, modules, functions, &lt;em&gt;etc.&lt;/em&gt;) should be open for extension, but closed for modification.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;He elaborated on it &lt;a href="http://se.ethz.ch/~meyer/publications/computer/implicitness.pdf"&gt;here&lt;/a&gt;.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;... This is the open-closed principle, which in my opinion is one of the central innovations of object technology: the ability to use a software component as it is, while retaining the possibility of adding to it later through inheritance. Unlike the records or structures of other approaches, a class of object technology is both closed and open: closed because we can start using it for other components (its clients); open because we can at any time add new properties without invalidating its existing clients.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;a href="http://se.ethz.ch/~meyer/publications/computer/implicitness.pdf"&gt;Tell Less, Say More: The Power of Implicitness&lt;/a&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;So, if one client &lt;code&gt;require&lt;/code&gt;&amp;#8217;s only &lt;code&gt;foo.rb&lt;/code&gt; and only uses &lt;code&gt;method1&lt;/code&gt;, that client doesn&amp;#8217;t care what &lt;code&gt;foo2.rb&lt;/code&gt; does. However, if the client also &lt;code&gt;require&lt;/code&gt;&amp;#8217;s &lt;code&gt;foo2.rb&lt;/code&gt;, perhaps indirectly through another &lt;code&gt;require&lt;/code&gt;, problems will ensue unless the client is unaffected by what &lt;code&gt;foo2.rb&lt;/code&gt; does.  This looks a lot like the way &amp;#8220;good&amp;#8221; inheritance should behave.&lt;/p&gt;


	&lt;p&gt;So, the answer is &lt;em&gt;no&lt;/em&gt;, we aren&amp;#8217;t violating &lt;span class="caps"&gt;OCP&lt;/span&gt;, as long as we extend a re-opened class following the same rules we would use when inheriting from it.&lt;/p&gt;


	&lt;p&gt;If we use inheritance instead:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo.rb
class Foo
    def method1 *args
        ...
    end
end
...
class DerivedFoo &amp;lt; Foo
    def method2 *args
        ...
    end
end
...
foo = SubFoo.new    # Instantiate different class...
foo.method1 :arg1, :arg2
foo.method2 :arg1, :arg2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;One notable difference is that we have to instantiate a different class. This is an important difference. While you can often just use inheritance, and maybe you should prefer it, inheritance only works if you have full control over what types get instantiated and &lt;em&gt;it&amp;#8217;s easy to change which types you use&lt;/em&gt;. Of course, inheritance is also the best approach when you need all behavioral variants &lt;em&gt;simulateneously&lt;/em&gt;, &lt;em&gt;i.e.,&lt;/em&gt; each variant in one or more objects.&lt;/p&gt;


	&lt;p&gt;Sometimes you want to affect the behavior of all instances transparently, without changing the types that are instantiated. A slightly better example, logging method calls, illustrates the point. Here we use the &amp;#8220;famous&amp;#8221; &lt;code&gt;alias_method&lt;/code&gt; in Ruby.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo.rb
class Foo
    def method1 *args
        ...
    end
end
# logging_foo.rb
class Foo
    alias_method :old_method1, :method1
    def method1 *args
        p "Inside method1(#{args.inspect})" 
        old_method1 *args
    end
end
...
foo = Foo.new
foo.method1 :arg1, :arg2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;&lt;code&gt;Foo.method1&lt;/code&gt; behaves like a subclass override, with extended behavior that still obeys the &lt;a href="http://www.objectmentor.com/resources/articles/lsp.pdf"&gt;Liskov-Substitution Principle&lt;/a&gt; (LSP).&lt;/p&gt;


	&lt;p&gt;So, I think the &lt;span class="caps"&gt;OCP&lt;/span&gt; can be reworded slightly.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Software entities (classes, modules, functions, &lt;em&gt;etc.&lt;/em&gt;) should be open for extension, but closed for &lt;strong&gt;source&lt;/strong&gt; modification.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;We should not re-open the original source, but adding functionality through a separate source file is okay.&lt;/p&gt;


	&lt;p&gt;Actually, I prefer a slightly different wording.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Software entities (classes, modules, functions, &lt;em&gt;etc.&lt;/em&gt;) should be open for extension, but closed for &lt;strong&gt;source and contract&lt;/strong&gt; modification.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;The extra &lt;strong&gt;and contract&lt;/strong&gt; is redundant with &lt;span class="caps"&gt;LSP&lt;/span&gt;. I don&amp;#8217;t think this kind of redundancy is necessarily bad. ;) The &lt;em&gt;contract&lt;/em&gt; is the set of &lt;em&gt;behavioral expectations&lt;/em&gt; between the &amp;#8220;entity&amp;#8221; and its client(s). Just as it is &lt;a href="http://blog.objectmentor.com/articles/2007/02/17/liskov-substitution-principle-and-the-ruby-core-libraries"&gt;bad to break the contract with inheritance&lt;/a&gt;, it is also bad to break it through open classes.&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;OCP&lt;/span&gt; and &lt;span class="caps"&gt;LSP&lt;/span&gt; together are our most important design principles for effective organization of similar &lt;em&gt;vs.&lt;/em&gt; variant behaviors. Inheritance is one way we do this. Open classes provide another way. Aspects provide a third way and are subject to the same &lt;a href="http://www.aosd.net/2007/program/industry/I6-AspectDesignPrinciples.pdf"&gt;design issues&lt;/a&gt;.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. &lt;span class="caps"&gt;ISBN 0136290493&lt;/span&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 04 Sep 2008 21:42:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8045ef44-c256-4c10-9edf-7f9fde4a26bd</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/04/the-open-closed-principle-for-languages-with-open-classes</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>OCP</category>
      <category>languages</category>
      <category>Ruby</category>
    </item>
    <item>
      <title>Tag: How Did I Get Started in Software Development</title>
      <description>&lt;p&gt;&lt;a href="http://www.8thlight.com/main/bios/micah"&gt;Micah Martin&lt;/a&gt; tagged me a while ago:&lt;/p&gt;


	&lt;h3&gt;How old were you when you started programming.&lt;/h3&gt;


	&lt;p&gt;I was around 15.&lt;/p&gt;


	&lt;h3&gt;How did you get started programming.&lt;/h3&gt;


	&lt;p&gt;This was in the mid-70&amp;#8217;s. In school, we had access to a time-shared computer running Basic. My first &amp;#8220;real&amp;#8221; programs were in college when I wrote Fortran code for a &lt;span class="caps"&gt;PDP&lt;/span&gt;-11 in a Physics professor&amp;#8217;s lab.&lt;/p&gt;


	&lt;h3&gt;What was your first language?&lt;/h3&gt;


	&lt;p&gt;Basic&lt;/p&gt;


	&lt;h3&gt;What languages have you used since you started programming?&lt;/h3&gt;


	&lt;p&gt;Roughly in order of adoption:&lt;/p&gt;


	&lt;p&gt;Fortran, C, Assembly Language, C++, PL/1, Perl, &lt;span class="caps"&gt;TCL&lt;/span&gt;/TK, Python, Java, &lt;span class="caps"&gt;HTML&lt;/span&gt;, JavaScript, &lt;span class="caps"&gt;CSS&lt;/span&gt;, SQL, Ruby, Scala&lt;/p&gt;


	&lt;h3&gt;What was the first real program you wrote?&lt;/h3&gt;


	&lt;p&gt;Data analysis programs in Fortran  for that Physics professor. Later, in graduate school, I started applying OO principles to the massive Fortran simulations I wrote. I needed OO to manage the complex objects!&lt;/p&gt;


	&lt;h3&gt;What was your first professional programming gig?&lt;/h3&gt;


	&lt;p&gt;Writing PL/1 and C code for a 3-dimensional scanning system running on the &lt;span class="caps"&gt;RMX OS&lt;/span&gt; on proprietary Intel mini-computers. The tools were atrocious and we forced our customers to use a green screen terminal interface, because nothing else was available!&lt;/p&gt;


	&lt;h3&gt;If there is one thing you learned along the way that you would tell new developers, what would it be?&lt;/h3&gt;


	&lt;p&gt;Take the initiative to learn from potential mentors. They are too hard to find in our industry, so grab the opportunities when you can. The other recommendation I would make is to pay attention to the business side. Do you really want to do all that hard work for a project that will face-plant once it reaches the marketplace??&lt;/p&gt;


	&lt;h3&gt;What&#8217;s the most fun you&#8217;ve ever had programming?&lt;/h3&gt;


	&lt;p&gt;Leading a team of C++ developers writing a new user interface for a debugger that worked with in-circuit emulators (ICE&amp;#8217;s). I always enjoyed UI development and the technical challenges were good. Unfortunately, it was one of those face-plants&amp;#8230;&lt;/p&gt;


	&lt;h3&gt;Tag, you&amp;#8217;re it: Bob Koss, Brett Schuchert&lt;/h3&gt;</description>
      <pubDate>Fri, 29 Aug 2008 22:36:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:781947e0-b458-41ba-abf4-b6dd5771ccc1</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/08/29/tag-how-did-i-get-started-in-software-development</link>
      <category>Dean's Deprecations</category>
    </item>
    <item>
      <title>The Seductions of Scala, Part III - Concurrent Programming</title>
      <description>&lt;p&gt;This is my third and last blog entry on &lt;em&gt;The Seductions of Scala&lt;/em&gt;, where we&amp;#8217;ll look at concurrency using &lt;code&gt;Actors&lt;/code&gt; and draw some final conclusions.&lt;/p&gt;


	&lt;h2&gt;Writing Robust, Concurrent Programs with Scala&lt;/h2&gt;


	&lt;p&gt;The most commonly used model of concurrency in imperative languages (and databases) uses shared, mutable state with access synchronization. (Recall that synchronization isn&amp;#8217;t necessary for reading immutable objects.)&lt;/p&gt;


	&lt;p&gt;However, it&amp;#8217;s widely known that this kind of concurrency programming is very difficult to do properly and few programmers are skilled enough to write such programs.&lt;/p&gt;


	&lt;p&gt;Because pure functional languages have no side effects and no shared, mutable state, there is nothing to synchronize. This is the main reason for the resurgent interest in function programming recently, as a potential solution to the so-called &lt;a href="http://www.technologyreview.com/Infotech/17682/page1/"&gt;multicore problem&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Instead, most functional languages, in particular, &lt;a href="http://www.erlang.org"&gt;Erlang&lt;/a&gt; and Scala, use the &lt;a href="http://en.wikipedia.org/wiki/Actor_model"&gt;Actor model of concurrency&lt;/a&gt;, where autonomous &amp;#8220;objects&amp;#8221; run in separate processes or threads and they pass messages back and forth to communicate. The simplicity of the Actor model makes it far easier to create robust programs. Erlang processes are so lightweight that it is common for server-side applications to have thousands of communicating processes.&lt;/p&gt;


	&lt;h3&gt;Actors in Scala&lt;/h3&gt;


	&lt;p&gt;Let&amp;#8217;s finish our survey of Scala with an example using Scala&amp;#8217;s Actors library.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s a simple Actor that just counts to 10, printing each number, one per second.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
import scala.actors._
object CountingActor extends Actor { 
    def act() { 
        for (i &amp;lt;- 1 to 10) { 
            println("Number: "+i)
            Thread.sleep(1000) 
        } 
    } 
} 

CountingActor.start()
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The last line starts the actor, which implicitly invokes the &lt;code&gt;act&lt;/code&gt; method. This actor does not respond to any messages from other actors.&lt;/p&gt;


	&lt;p&gt;Here is an actor that responds to messages, echoing the message it receives.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
import scala.actors.Actor._ 
val echoActor = actor {
    while (true) {
        receive {
            case msg =&amp;gt; println("received: "+msg)
        }
    }
}
echoActor ! "hello" 
echoActor ! "world!" 
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;In this case, we do the equivalent of a Java &amp;#8220;static import&amp;#8221; of the methods on &lt;code&gt;Actor&lt;/code&gt;, &lt;em&gt;e.g.,&lt;/em&gt; &lt;code&gt;actor&lt;/code&gt;. Also, we don&amp;#8217;t actually need a special class, we can just create an object with the desired behavior. This object has an infinite loop that effectively blocks while waiting for an incoming message. The &lt;code&gt;receive&lt;/code&gt; method gets a block that is a match statement, which matches on &lt;em&gt;anything&lt;/em&gt; received and prints it out.&lt;/p&gt;


	&lt;p&gt;Messages are sent using the &lt;code&gt;target_actor ! message&lt;/code&gt; syntax.&lt;/p&gt;


	&lt;p&gt;As a final example, let&amp;#8217;s do something non-trivial; a contrived network node monitor.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
import scala.actors._
import scala.actors.Actor._
import java.net.InetAddress 
import java.io.IOException

case class NodeStatusRequest(address: InetAddress, respondTo: Actor) 

sealed abstract class NodeStatus
case class Available(address: InetAddress) extends NodeStatus
case class Unresponsive(address: InetAddress, reason: Option[String]) extends NodeStatus

object NetworkMonitor extends Actor {
    def act() {
        loop {
            react {  // Like receive, but uses thread polling for efficiency.
                case NodeStatusRequest(address, actor) =&amp;gt; 
                    actor ! checkNodeStatus(address)
                case "EXIT" =&amp;gt; exit()
            }
        }
    }
    val timeoutInMillis = 1000;
    def checkNodeStatus(address: InetAddress) = {
        try {
            if (address.isReachable(timeoutInMillis)) 
                Available(address)
            else
                Unresponsive(address, None)
        } catch {
            case ex: IOException =&amp;gt; 
                Unresponsive(address, Some("IOException thrown: "+ex.getMessage()))
        }
    }
}

// Try it out:

val allOnes = Array(1, 1, 1, 1).map(_.toByte)
NetworkMonitor.start()
NetworkMonitor ! NodeStatusRequest(InetAddress.getByName("www.scala-lang.org"), self)
NetworkMonitor ! NodeStatusRequest(InetAddress.getByAddress("localhost", allOnes), self)
NetworkMonitor ! NodeStatusRequest(InetAddress.getByName("www.objectmentor.com"), self)
NetworkMonitor ! "EXIT" 
self ! "No one expects the Spanish Inquisition!!" 

def handleNodeStatusResponse(response: NodeStatus) = response match {
    // Sealed classes help here
    case Available(address) =&amp;gt; 
        println("Node "+address+" is alive.")
    case Unresponsive(address, None) =&amp;gt; 
        println("Node "+address+" is unavailable. Reason: &amp;lt;unknown&amp;gt;")
    case Unresponsive(address, Some(reason)) =&amp;gt; 
        println("Node "+address+" is unavailable. Reason: "+reason)
}

for (i &amp;lt;- 1 to 4) self.receive {   // Sealed classes don't help here
    case (response: NodeStatus) =&amp;gt; handleNodeStatusResponse(response)
    case unexpected =&amp;gt; println("Unexpected response: "+unexpected)
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;We begin by importing the &lt;code&gt;Actor&lt;/code&gt; classes, the methods on &lt;code&gt;Actor&lt;/code&gt;, like &lt;code&gt;actor&lt;/code&gt;, and a few Java classes we need.&lt;/p&gt;


	&lt;p&gt;Next we define a &lt;em&gt;sealed&lt;/em&gt; abstract base class. The &lt;code&gt;sealed&lt;/code&gt; keyword tells the compiler that the only subclasses will be defined in this file. This is useful for the case statements that use them. The compiler will know that it doesn&amp;#8217;t have to worry about potential cases that aren&amp;#8217;t covered, if new &lt;code&gt;NodeStatus&lt;/code&gt; subclasses are created. Otherwise, we would have to add a default case clause (&lt;em&gt;e.g.,&lt;/em&gt; &lt;code&gt;case _ =&amp;gt; ...&lt;/code&gt;) to prevent warnings (and possible errors!) about not matching an input. Sealed class hierarchies are a useful feature for robustness (but watch for potential Open/Closed Principle violations!).&lt;/p&gt;


	&lt;p&gt;The sealed class hierarchy encapsulates all the possible node status values (somewhat contrived for the example). The node is either &lt;code&gt;Available&lt;/code&gt; or &lt;code&gt;Unresponsive&lt;/code&gt;. If &lt;code&gt;Unresponsive&lt;/code&gt;, an optional &lt;code&gt;reason&lt;/code&gt; message is returned.&lt;/p&gt;


	&lt;p&gt;Note that we only get the benefit of sealed classes here because we match on them in the &lt;code&gt;handleNodeStatusResponse&lt;/code&gt; message, which requires a &lt;code&gt;response&lt;/code&gt; argument of type &lt;code&gt;NodeStatus&lt;/code&gt;. In contrast, the &lt;code&gt;receive&lt;/code&gt; method effectively takes an &lt;code&gt;Any&lt;/code&gt; argument, so sealed classes don&amp;#8217;t help on the line with the comment &amp;#8220;Sealed classes don&amp;#8217;t help here&amp;#8221;. In that case, we really need a default, the &lt;code&gt;case unexpected =&amp;gt; ...&lt;/code&gt; clause. (I added the message &lt;code&gt;self ! "No one expects the Spanish Inquisition!!"&lt;/code&gt; to test this default handler.)&lt;/p&gt;


	&lt;p&gt;In the first draft of this blog post, I didn&amp;#8217;t know these details about sealed classes. I used a simpler implementation that couldn&amp;#8217;t benefit from sealed classes. Thanks to the first commenter, LaLit Pant, who corrected my mistake!&lt;/p&gt;


	&lt;p&gt;The &lt;code&gt;NetworkMonitor&lt;/code&gt; loops, waiting for a &lt;code&gt;NodeStatusRequest&lt;/code&gt; or the special string &amp;#8220;EXIT&amp;#8221;, which tells it to quit. Note that the actor sending the request passes itself, so the monitor can reply to it.&lt;/p&gt;


	&lt;p&gt;The &lt;code&gt;checkNodeStatus&lt;/code&gt; attempts to contact the node, with a 1 second timeout. It returns an appropriate &lt;code&gt;NodeStatus&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Then we try it out with three addresses. Note that we pass &lt;code&gt;self&lt;/code&gt; as the requesting actor. This is an &lt;code&gt;Actor&lt;/code&gt; wrapping the current thread, imported from &lt;code&gt;Actor&lt;/code&gt;. It is analogous to Java&amp;#8217;s &lt;code&gt;Thread.currentThread()&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Curiously enough, when I run this code, I get the following results.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
Unexpected response: No one expects the Spanish Inquisition!!
Node www.scala-lang.org/128.178.154.102 is unavailable. Reason: &amp;lt;unknown&amp;gt;
Node localhost/1.1.1.1 is unavailable. Reason: &amp;lt;unknown&amp;gt;
Node www.objectmentor.com/206.191.6.12 is alive.
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The message about the Spanish Inquisition was sent last, but processed first, probably because &lt;code&gt;self&lt;/code&gt; sent it to itself.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m not sure why www.scala-lang.org couldn&amp;#8217;t be reached. A longer timeout didn&amp;#8217;t help. According to the Javadocs for &lt;a href="http://java.sun.com/javase/6/docs/api/java/net/InetAddress.html#isReachable(int"&gt;InetAddress.isReachable&lt;/a&gt;), it uses &lt;span class="caps"&gt;ICMP ECHO&lt;/span&gt; REQUESTs if the privilege can be obtained, otherwise it tries to establish a &lt;span class="caps"&gt;TCP&lt;/span&gt; connection on port 7 (Echo) of the destination host. Perhaps neither is supported on the scala-lang.org site.&lt;/p&gt;


	&lt;h2&gt;Conclusions&lt;/h2&gt;


	&lt;p&gt;Here are some concluding observations about Scala &lt;em&gt;vis-&#224;-vis&lt;/em&gt; Java and other options.&lt;/p&gt;


	&lt;h3&gt;A Better Java&lt;/h3&gt;


	&lt;p&gt;Ignoring the functional programming aspects for a moment, I think Scala improves on Java in a number of very useful ways, including:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;strong&gt;A more succinct syntax.&lt;/strong&gt; There&amp;#8217;s far less boilerplate, like for fields and their accessors. Type inference and optional semicolons, curly braces, &lt;em&gt;etc.&lt;/em&gt; also reduce &amp;#8220;noise&amp;#8221;.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;A true &lt;em&gt;mixin&lt;/em&gt; model.&lt;/strong&gt; The addition of &lt;em&gt;traits&lt;/em&gt; solves the problem of not having a good &lt;a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;&lt;span class="caps"&gt;DRY&lt;/span&gt;&lt;/a&gt; way to mix in additional functionality declared by Java interfaces.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;More flexible method names and invocation syntax.&lt;/strong&gt; Java took away operator overloading; Scala gives it back, as well as other benefits of using non-alphanumeric characters in method names. (Ruby programmers enjoy writing &lt;code&gt;list.empty?&lt;/code&gt;, for example.) &lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Tuples.&lt;/strong&gt; A personal favorite, I&amp;#8217;ve always wanted the ability to return multiple values from a method, without having to create an &lt;em&gt;ad hoc&lt;/em&gt; class to hold the values.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Better separation of mutable &lt;em&gt;vs.&lt;/em&gt; immutable objects.&lt;/strong&gt; While Java provides some ability to make objects &lt;code&gt;final&lt;/code&gt;, Scala makes the distinction between mutability and immutability more explicit and encourages the latter as a more robust programming style.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;First-class functions and closures.&lt;/strong&gt; Okay, these last two points are really about FP, but they sure help in OO code, too!&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Better mechanisms for avoiding &lt;code&gt;null&lt;/code&gt;&amp;#8217;s.&lt;/strong&gt; The &lt;code&gt;Option&lt;/code&gt; type makes code more robust than allowing &lt;code&gt;null&lt;/code&gt; values.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Interoperability with Java libraries.&lt;/strong&gt; Scala compiles to byte code so adding Scala code to existing
Java applications is about as seamless as possible.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;So, even if you don&amp;#8217;t believe in FP, you will gain a lot just by using Scala as a better Java.&lt;/p&gt;


	&lt;h3&gt;Functional Programming&lt;/h3&gt;


	&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt;, you shouldn&amp;#8217;t ignore the benefits of FP!&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;strong&gt;Better robustness.&lt;/strong&gt; Not only for concurrent programs, but using immutable objects (a.k.a. &lt;em&gt;value objects&lt;/em&gt;) reduces the potential for bugs.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;A workable concurrency model.&lt;/strong&gt; I use the term &lt;em&gt;workable&lt;/em&gt; because so few developers can write robust concurrent code using the synchronization on shared state model. Even for those of you who can, why bother when Actors are so much easier??&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Reduced code complexity.&lt;/strong&gt; Functional code tends to be very succinct. I can&amp;#8217;t overestimate the importance of rooting out &lt;strong&gt;all&lt;/strong&gt; &lt;em&gt;accidental&lt;/em&gt; complexity in your code base. Excess complexity is one of the most pervasive detriments to productivity and morale that I see in my clients&amp;#8217; code bases!&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;First-class functions and closures.&lt;/strong&gt; Composition and succinct code are much easier with first-class functions.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Pattern matching.&lt;/strong&gt; FP-style pattern matching makes &amp;#8220;routing&amp;#8221; of messages and delegation much easier.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;Of course, you can mimic some of these features in pure Java and I encourage you to do so if you aren&amp;#8217;t using Scala.&lt;/p&gt;


	&lt;h3&gt;Static &lt;em&gt;vs.&lt;/em&gt; Dynamic Typing&lt;/h3&gt;


	&lt;p&gt;The debate on the relative merits of static &lt;em&gt;vs.&lt;/em&gt; dynamic typing is outside our scope, but I will make a few personal observations.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve been a dedicated &lt;a href="http://www.rubyist.net/~slagell/ruby/"&gt;Rubyist&lt;/a&gt; for a while. It is hard to deny the way that dynamic typing simplifies code and as I said in the previous section, I take code complexity &lt;em&gt;very&lt;/em&gt; seriously.&lt;/p&gt;


	&lt;p&gt;Scala&amp;#8217;s type system and type inference go a long way towards providing the benefits of static typing with the cleaner syntax of dynamic typing, but Scala doesn&amp;#8217;t eliminate the extra complexity of static typing.&lt;/p&gt;


	&lt;p&gt;Recall my Observer example from the &lt;a href="http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i"&gt;first blog post&lt;/a&gt;, where I used traits to implement it.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
trait Observer[S] {
    def receiveUpdate(subject: S);
}

trait Subject[S] { 
    this: S =&amp;gt;
    private var observers: List[Observer[S]] = Nil
    def addObserver(observer: Observer[S]) = observers = observer :: observers

    def notifyObservers() = observers.foreach(_.receiveUpdate(this))
}
    &lt;/code&gt;
&lt;/pre&gt;

In Ruby, we might implement it this way.
&lt;pre&gt;
    &lt;code&gt;
module Subject 
    def add_observer(observer) 
      @observers ||= []
      @observers &amp;lt;&amp;lt; observer  # append, rather than replace with new array
  end

    def notify_observers
      @observers.each {|o| o.receive_update(self)} if @observers
  end
end
    &lt;/code&gt;
&lt;/pre&gt;        

	&lt;p&gt;There is no need for an &lt;code&gt;Observer&lt;/code&gt; module. As long as every observer responds to the &lt;code&gt;receive_update&lt;/code&gt; &amp;#8220;message&amp;#8221;, we&amp;#8217;re fine.&lt;/p&gt;


	&lt;p&gt;I commented the line where I append to the existing &lt;code&gt;@observers&lt;/code&gt; array, rather than build a new one, which would be the FP and Scala way. Appending to the existing array would be more typical of Ruby code, but this implementation is not as thread safe as an FP-style approach.&lt;/p&gt;


	&lt;p&gt;The trailing &lt;code&gt;if&lt;/code&gt; expression in &lt;code&gt;notify_observers&lt;/code&gt; means that nothing is done if &lt;code&gt;@observers&lt;/code&gt; is still &lt;code&gt;nil&lt;/code&gt;, &lt;em&gt;i.e.,&lt;/em&gt; it was never initialized in &lt;code&gt;add_observer&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;So, which is better? The amount of code is not that different, but it took me significantly longer to write the Scala version. In part, this was due to my novice chops, but the reason it took me so long was because I had to solve a design issue resulting from the static typing. I had to learn about the &lt;em&gt;typed self&lt;/em&gt; construct used in the first line of the &lt;code&gt;Subject&lt;/code&gt; trait. This was the only way to allow the &lt;code&gt;Observer.receiveUpdate&lt;/code&gt; method accept to an argument of type &lt;code&gt;S&lt;/code&gt;, rather than of type &lt;code&gt;Subject[S]&lt;/code&gt;. It was worth it to me to achieve the &amp;#8220;cleaner&amp;#8221; &lt;span class="caps"&gt;API&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Okay, perhaps I&amp;#8217;ll know this next time and spend about the same amount of time implementing a Ruby &lt;em&gt;vs.&lt;/em&gt; Scala version of something. However, I think it&amp;#8217;s notable that sometimes static typing can get in the way of your intentions and goal of achieving clarity. (At other times, the types add useful documentation.) I know this isn&amp;#8217;t the only valid argument you can make, one way or the other, but it&amp;#8217;s one reason that dynamic languages are so appealing.&lt;/p&gt;


	&lt;h3&gt;&lt;em&gt;Poly-paradigm&lt;/em&gt; Languages &lt;em&gt;vs.&lt;/em&gt; Mixing Several Languages&lt;/h3&gt;


	&lt;p&gt;So, you&amp;#8217;re convinced that you should use FP sometimes and &lt;span class="caps"&gt;OOP&lt;/span&gt; sometimes. Should you pick a &lt;a href="http://aspectprogramming.com/papers/PolyglotPolyParadigm_v1.pdf"&gt;poly-paradigm&lt;/a&gt; language, like Scala? Or, should you combine several languages, each of which implements one paradigm?&lt;/p&gt;


	&lt;p&gt;A potential downside of Scala is that supporting different &lt;em&gt;modularity paradigms&lt;/em&gt;, like &lt;span class="caps"&gt;OOP&lt;/span&gt; and FP, increases the complexity in the language. I think Odersky and company have done a superb job combining FP and &lt;span class="caps"&gt;OOP&lt;/span&gt; in Scala, but if you compare Scala FP code to Haskell or Erlang FP code, the latter tend to be more succinct and often easier to understand (once you learn the syntax).&lt;/p&gt;


	&lt;p&gt;Indeed, Scala will not be easy for developers to master. It will be a powerful tool for professionals. As a consultant, I work with developers with a range of skills. I would not expect some of them to prosper with Scala. Should that rule out the language? &lt;b&gt;NO&lt;/b&gt;. Rather it would be better to &amp;#8220;liberate&amp;#8221; the better developers with a more powerful tool.&lt;/p&gt;


	&lt;p&gt;So, if your application needs &lt;span class="caps"&gt;OOP&lt;/span&gt; and FP concepts interspersed, consider Scala. If your application needs discrete services, some of which are predominantly &lt;span class="caps"&gt;OOP&lt;/span&gt; and others of which are predominantly FP, then consider Scala or Java for the &lt;span class="caps"&gt;OOP&lt;/span&gt; parts and Erlang or another FP language for the FP parts.&lt;/p&gt;


	&lt;p&gt;Also, Erlang&amp;#8217;s Actor model is more mature than Scala&amp;#8217;s, so Erlang might have an edge for a highly-concurrent server application.&lt;/p&gt;


	&lt;p&gt;Of course, you should do your own analysis&amp;#8230;&lt;/p&gt;


	&lt;h3&gt;Final Thoughts&lt;/h3&gt;


	&lt;p&gt;Java the language has had a great ride. It was a godsend to us beleaguered C++ programmers in the mid &amp;#8216;90&amp;#8217;s. However, compared to Scala, Java now feels obsolete. The &lt;span class="caps"&gt;JVM&lt;/span&gt; is another story. It is arguably the best VM available.&lt;/p&gt;


	&lt;p&gt;I hope Scala replaces Java as the main &lt;span class="caps"&gt;JVM&lt;/span&gt; language for projects that prefer statically-typed languages. Fans of dynamically-typed languages might prefer JRuby, Groovy, or Jython. It&amp;#8217;s hard to argue with all the &lt;span class="caps"&gt;OOP&lt;/span&gt; and FP goodness that Scala provides. You will learn a lot about good language and application design by learning Scala. It will certainly be a prominent tool in my toolkit from now on.&lt;/p&gt;</description>
      <pubDate>Thu, 14 Aug 2008 16:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d9c6812f-bb73-4568-bb96-17c9470ec0a6</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/08/14/the-seductions-of-scala-part-iii-concurrent-programming</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>Scala</category>
      <category>Java</category>
      <category>Ruby</category>
      <category>static</category>
      <category>dynamic</category>
      <category>FP</category>
      <category>OOP</category>
    </item>
    <item>
      <title>The Seductions of Scala, Part II - Functional Programming</title>
      <description>&lt;p&gt;&lt;em&gt;A Functional Programming Language for the &lt;span class="caps"&gt;JVM&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;In my &lt;a href="http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i"&gt;last blog post&lt;/a&gt;, I discussed Scala&amp;#8217;s support for &lt;span class="caps"&gt;OOP&lt;/span&gt; and general improvements compared to Java. In this post, which I&amp;#8217;m posting from &lt;a href="http://www.agile2008.org/"&gt;Agile 2008&lt;/a&gt;, I discuss Scala&amp;#8217;s support for &lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt; (FP) and why it should be of interest to OO developers.&lt;/p&gt;


	&lt;h3&gt;A Brief Overview of Functional Programming&lt;/h3&gt;


	&lt;p&gt;You might ask, don&amp;#8217;t most programming languages have functions? FP uses the term in the mathematical sense of the word. I hate to bring up bad memories, but you might recall from your school days that when you solved a function like&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
y = sin(x)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;for &lt;code&gt;y&lt;/code&gt;, given a value of &lt;code&gt;x&lt;/code&gt;, you could input the same value of &lt;code&gt;x&lt;/code&gt; an arbitrary number of times and you would get the same value of &lt;code&gt;y&lt;/code&gt;. This means that &lt;code&gt;sin(x)&lt;/code&gt; has no &lt;em&gt;side effects&lt;/em&gt;. In other words, unlike our &lt;em&gt;imperative&lt;/em&gt; OO or &lt;em&gt;procedural&lt;/em&gt; code, no global or object state gets changed. All the work that a mathematical function does has to be returned in the result.&lt;/p&gt;


	&lt;p&gt;Similarly, the idea of a &lt;em&gt;variable&lt;/em&gt; is a little different than what we&amp;#8217;re used to in &lt;em&gt;imperative&lt;/em&gt; code. While the value of &lt;code&gt;y&lt;/code&gt; will vary with the value of &lt;code&gt;x&lt;/code&gt;, once you have fixed &lt;code&gt;x&lt;/code&gt;, you have also fixed &lt;code&gt;y&lt;/code&gt;. The implication for FP is that &amp;#8220;variables&amp;#8221; are immutable; once assigned, they cannot be changed. I&amp;#8217;ll call such immutable variables &lt;em&gt;value objects&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Now, it would actually be hard for a &amp;#8220;pure&amp;#8221; FP language to have no side effects, ever. I/O would be rather difficult, for example, since the state of the input or output stream changes with each operation. So, in practice, all &amp;#8220;pure&amp;#8221; FP languages provide some mechanisms for breaking the rules in a controlled way.&lt;/p&gt;


	&lt;p&gt;Functions are first-class objects in FP. You can create named or anonymous functions (&lt;em&gt;e.g.&lt;/em&gt;, &lt;em&gt;closures&lt;/em&gt; or &lt;em&gt;blocks&lt;/em&gt;), assign them to variables, pass them as arguments to other functions, &lt;em&gt;etc.&lt;/em&gt; Java doesn&amp;#8217;t support this. You have to create objects that wrap the methods you want to invoke.&lt;/p&gt;


	&lt;p&gt;Functional programs tend to be much more declarative in nature than imperative programs. This is perhaps more obvious in pure FP languages, like Erlang and Haskell, than it is in Scala.&lt;/p&gt;


	&lt;p&gt;For example, the definition of Fibonacci numbers is the following.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
F(n) = F(n-1) + F(n-2) where F(1)=1 and F(2)=1
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;An here is a complete implementation in Haskell.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
module Main where 
-- Function f returns the n'th Fibonacci number. 
-- It uses binary recursion. 
f n | n &amp;lt;= 2 = 1 
    | n &amp;gt;  2 = f (n-1) + f (n-2) 
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Without understanding the intricacies of Haskell syntax, you can see that the code closely matches the &amp;#8220;specification&amp;#8221; above it. The &lt;code&gt;f n | ...&lt;/code&gt; syntax defines the function &lt;code&gt;f&lt;/code&gt; taking an argument &lt;code&gt;n&lt;/code&gt; and the two cases of &lt;code&gt;n&lt;/code&gt; values are shown on separate lines, where one case is for &lt;code&gt;n &amp;lt;= 2&lt;/code&gt; and the other case if for &lt;code&gt;n &amp;gt; 2&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;The code uses the recursive relationship between different values of the function and the special-case values when &lt;code&gt;n = 1&lt;/code&gt; and &lt;code&gt;n = 2&lt;/code&gt;. The Haskell runtime does the rest of the work.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s interesting that most &lt;a href="http://www.martinfowler.com/bliki/DomainSpecificLanguage.html"&gt;domain-specific languages&lt;/a&gt; are also declarative in nature. Think of how JMock, EasyMock or Rails&amp;#8217; ActiveRecord code look. The code is more succinct and it lets the &amp;#8220;system&amp;#8221; do most of the heavy lifting.&lt;/p&gt;


	&lt;h3&gt;Functional Programming&amp;#8217;s Benefits for You&lt;/h3&gt;


	&lt;h4&gt;Value Objects and Side-Effect Free Functions&lt;/h4&gt;


	&lt;p&gt;It&amp;#8217;s the immutable variables and side-effect free functions that help solve the &lt;a href="http://www.technologyreview.com/Infotech/17682/page1/"&gt;multicore problem&lt;/a&gt;. Synchronized access to shared state is not required if there is no state to manage. This makes robust concurrent programs far easier to write.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ll discuss concurrency in Scala in my third post. For now, let&amp;#8217;s discuss other ways that FP in Scala helps to improve code, concurrent or not.&lt;/p&gt;


	&lt;p&gt;Value objects are beneficial because you can pass one around without worrying that someone will change it in a way that breaks other users of the object. Value objects aren&amp;#8217;t unique to FP, of course. They have been promoted in &lt;a href="http://domaindrivendesign.org/"&gt;Domain Driven Design&lt;/a&gt; (DDD), for example.&lt;/p&gt;


	&lt;p&gt;Similarly, side-effect free functions are safer to use. There is less risk that a caller will change some state inappropriately. The caller doesn&amp;#8217;t have to worry as much about calling a function. There are fewer surprises and everything of &amp;#8220;consequence&amp;#8221; that the function does is returned to the caller. It&amp;#8217;s easier to keep to the &lt;a href="http://www.objectmentor.com/resources/articles/srp.pdf"&gt;Single Responsibility Principle&lt;/a&gt; when writing side-effect free functions.&lt;/p&gt;


	&lt;p&gt;Of course, you can write side-effect free methods and immutable variables in Java code, but it&amp;#8217;s mostly a matter of discipline; the language doesn&amp;#8217;t give you any enforcement mechanisms.&lt;/p&gt;


	&lt;p&gt;Scala gives you a helpful enforcement mechanism; the ability to declare variables as &lt;code&gt;val&lt;/code&gt;&amp;#8217;s (&lt;em&gt;i.e.,&lt;/em&gt; &amp;#8220;values&amp;#8221;) &lt;em&gt;vs.&lt;/em&gt; &lt;code&gt;var&lt;/code&gt;&amp;#8217;s (&lt;em&gt;i.e.,&lt;/em&gt; &amp;#8220;variables&amp;#8221;, um&amp;#8230; back to the imperative programming sense of the word&amp;#8230;). In fact, &lt;code&gt;val&lt;/code&gt; is the default, where neither is required by the language. Also, the Scala library contains both immutable and mutable collections and it &amp;#8220;encourages&amp;#8221; you to use the immutable collections.&lt;/p&gt;


	&lt;p&gt;However, because Scala combines both &lt;span class="caps"&gt;OOP&lt;/span&gt; and FP, it doesn&amp;#8217;t force FP purity. The upside is that you get to use the approach that best fits the problem you&amp;#8217;re trying to solve. It&amp;#8217;s interesting that some of the Scala library classes expose FP-style interfaces, immutability and side-effect free functions, while using more traditional imperative code to implement them!&lt;/p&gt;


	&lt;h4&gt;Closures and First-Class Functions&lt;/h4&gt;


	&lt;p&gt;True to its functional side, Scala gives you true closures and first-class functions. If you&amp;#8217;re a Groovy or Ruby programmer, you&amp;#8217;re used to the following kind of code.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class ExpensiveResource {
    def open(worker: () =&amp;gt; Unit) = {
        try {
            println("Doing expensive initialization")
            worker()
        } finally {
            close()
        }
    }
    def close() = {
        println("Doing expensive cleanup")
    }
}
// Example use:
try {
    (new ExpensiveResource()) open { () =&amp;gt;        // 1
        println("Using Resource")                 // 2
        throw new Exception("Thrown exception")   // 3
    }                                             // 4
} catch {
    case ex: Throwable =&amp;gt; println("Exception caught: "+ex)
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Running this code will yield:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
Doing expensive initialization
Using Resource
Doing expensive cleanup
Exception caught: java.lang.Exception: Thrown exception
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;ExpensiveResource.open&lt;/code&gt; method invokes the user-specified &lt;code&gt;worker&lt;/code&gt; function. The syntax &lt;code&gt;worker: () =&amp;gt; Unit&lt;/code&gt; defines the &lt;code&gt;worker&lt;/code&gt; parameter as a function that takes no arguments and returns nothing (recall that &lt;code&gt;Unit&lt;/code&gt; is the equivalent of &lt;code&gt;void&lt;/code&gt;).&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;ExpensiveResource.open&lt;/code&gt; handles the details of initializing the resource, invoking the worker, and doing the necessary cleanup.&lt;/p&gt;


	&lt;p&gt;The example marked with the comment &lt;code&gt;// 1&lt;/code&gt; creates a new &lt;code&gt;ExpensiveResource&lt;/code&gt;, then calls &lt;code&gt;open&lt;/code&gt;, passing it an anonymous function, called a &lt;em&gt;function literal&lt;/em&gt; in Scala terminology. The function literal is of the form &lt;code&gt;(arg_list_) =&amp;gt; function body&lt;/code&gt; or &lt;code&gt;() =&amp;gt; println(...) ...&lt;/code&gt;, in our case.&lt;/p&gt;


	&lt;p&gt;A special syntax trick is used on this line; if a method takes one argument, you can change expressions of the form &lt;code&gt;object.method(arg)&lt;/code&gt; to &lt;code&gt;object method {arg}&lt;/code&gt;. This syntax is supported to allow user-defined methods to read like control structures (think &lt;code&gt;for&lt;/code&gt; statements &amp;#8211; see the next section). If you&amp;#8217;re familiar with Ruby, the four commented lines read a lot like Ruby syntax for passing blocks to methods.&lt;/p&gt;


	&lt;p&gt;Idioms like this are very important. A library writer can encapsulate all complex, error-prone logic and allow the user to specify only the unique work required in a given situation. For example, How many times have you written code that opened an I/O stream or a database connection, used it, then cleaned up. How many times did you get the idiom &lt;a href="http://blog.objectmentor.com/articles/2008/07/31/always-close-in-a-finally-block"&gt;wrong&lt;/a&gt;, especially the proper cleanup when an exception is thrown? First-class functions allow writers of I/O, database and other resource libraries to do the correct implementation &lt;em&gt;once&lt;/em&gt;, eliminating user error and duplication. Here&amp;#8217;s a rhetorical question I always ask myself:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;How can I make it &lt;strong&gt;impossible&lt;/strong&gt; for the user of this &lt;span class="caps"&gt;API&lt;/span&gt; to fail?&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;h4&gt;Iterations&lt;/h4&gt;


	&lt;p&gt;Iteration through collections, &lt;code&gt;Lists&lt;/code&gt; in particular, is even more common in FP than in imperative languages. Hence, iteration is highly evolved. Consider this example:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
object RequireWordsStartingWithPrefix {
    def main(args: Array[String]) = {
        val prefix = args(0)
        for {
            i &amp;lt;- 1 to (args.length - 1)   // no semicolon
            if args(i).startsWith(prefix)
        } println("args("+i+"): "+args(i))
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Compiling this code with &lt;code&gt;scalac&lt;/code&gt; and then running it on the command line with the command&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
scala RequireWordsStartingWithPrefix xx xy1 xx1 yy1 xx2 xy2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;produces the result&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
args(2): xx1
args(5): xx2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The for loop assigns a loop variable &lt;code&gt;i&lt;/code&gt; with each argument, but only if the &lt;code&gt;if&lt;/code&gt; statement is true. Instead of curly braces, the for loop argument list could also be parenthesized, but then each line as shown would have to be separated by a semi-colon, like we&amp;#8217;re used to seeing with Java for loops.&lt;/p&gt;


	&lt;p&gt;We can have an arbitrary number of assignments and conditionals. In fact, it&amp;#8217;s quite common to filter lists:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
object RequireWordsStartingWithPrefix2 {
    def main(args: Array[String]) = {
        val prefix = args(0)
        args.slice(1, args.length)
            .filter((arg: String) =&amp;gt; arg.startsWith(prefix))
            .foreach((arg: String) =&amp;gt; println("arg: "+arg))
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This version yields the same result. In this case, the args array is sliced (loping off the search prefix), the resulting array is filtered using a function literal and the filtered array is iterated over to print out the matching arguments, again using a function literal.  This version of the algorithm should look familiar to Ruby programmers.&lt;/p&gt;


	&lt;h4&gt;Rolling Your Own &lt;em&gt;Function Objects&lt;/em&gt;&lt;/h4&gt;


	&lt;p&gt;Scala still has to support the constraints of the &lt;span class="caps"&gt;JVM&lt;/span&gt;. As a comment to the first blog post said, the Scala compiler wraps closures and &amp;#8220;bare&amp;#8221; functions in &lt;code&gt;Function&lt;/code&gt; objects. You can also make other objects behave like functions. If your object implements the &lt;code&gt;apply&lt;/code&gt; method, that method will be invoked if you put parentheses with an matching argument list on the object, as in the following example.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class HelloFunction {
    def apply() = "hello" 
    def apply(name: String) = "hello "+name
}
val hello = new HelloFunction
println(hello())        // =&amp;gt; "hello" 
println(hello("Dean"))  // =&amp;gt; "hello Dean" 
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h4&gt;Option, None, Some&amp;#8230;&lt;/h4&gt;


	&lt;p&gt;Null pointer exceptions suck. You can still get them in Scala code, because Scala runs on the &lt;span class="caps"&gt;JVM&lt;/span&gt; and interoperates with Java libraries, but Scala offers a better way.&lt;/p&gt;


	&lt;p&gt;Typically, a reference might be null when there is nothing appropriate to assign to it. Following the conventions in some FP languages, Scala has an &lt;code&gt;Option&lt;/code&gt; type with two subtypes, &lt;code&gt;Some&lt;/code&gt;, which wraps a value, and &lt;code&gt;None&lt;/code&gt;, which is used instead of &lt;code&gt;null&lt;/code&gt;. The following example, which also demonstrates Scala&amp;#8217;s &lt;code&gt;Map&lt;/code&gt; support, shows these types in action.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
val hotLangs = Map(
    "Scala" -&amp;gt; "Rocks", 
    "Haskell" -&amp;gt; "Ethereal", 
    "Java" -&amp;gt; null)
println(hotLangs.get("Scala"))          // =&amp;gt; Some(Rocks)
println(hotLangs.get("Java"))           // =&amp;gt; Some(null)
println(hotLangs.get("C++"))            // =&amp;gt; None
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Note that &lt;code&gt;Map&lt;/code&gt; stores values in &lt;code&gt;Options&lt;/code&gt; objects, as shown by the &lt;code&gt;println&lt;/code&gt; statements.&lt;/p&gt;


	&lt;p&gt;By the way, those &lt;code&gt;-&amp;gt;&lt;/code&gt; aren&amp;#8217;t special operators; they&amp;#8217;re methods. Like &lt;code&gt;::&lt;/code&gt;, valid method names aren&amp;#8217;t limited to alphanumerics, &lt;code&gt;_&lt;/code&gt;, and &lt;code&gt;$&lt;/code&gt;.&lt;/p&gt;


	&lt;h4&gt;Pattern Matching&lt;/h4&gt;


	&lt;p&gt;The last FP feature I&amp;#8217;ll discuss in this post is pattern matching, which is exploited more fully in FP languages than in imperative languages.&lt;/p&gt;


	&lt;p&gt;Using our previous definition of &lt;code&gt;hotLangs&lt;/code&gt;, here&amp;#8217;s how you might use matching.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def show(key: String) = {
    val value: Option[String] = hotLangs.get(key)
    value match {
        case Some(x) =&amp;gt; x
        case None =&amp;gt; "No hotness found" 
    }
}
println(show("Scala"))  // =&amp;gt; "Rocks" 
println(show("Java"))   // =&amp;gt; "null" 
println(show("C++"))    // =&amp;gt; "No hotness found" 
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The first &lt;code&gt;case&lt;/code&gt; statement, &lt;code&gt;case Some(x) =&amp;gt; x&lt;/code&gt;, says &amp;#8220;if the &lt;code&gt;value&lt;/code&gt; I&amp;#8217;m matching against is a &lt;code&gt;Some&lt;/code&gt; that could be constructed with the &lt;code&gt;Some[+String](x: A)&lt;/code&gt; constructor, then return the &lt;code&gt;x&lt;/code&gt;, the thing the &lt;code&gt;Some&lt;/code&gt; contains.&amp;#8221; Okay, there&amp;#8217;s a lot going on here, so more background information is in order.&lt;/p&gt;


	&lt;p&gt;In Scala, like Ruby and other languages, the last value computed in a function is returned by it. Also, almost everything returns a value, including &lt;code&gt;match&lt;/code&gt; statements, so when the &lt;code&gt;Some(x) =&amp;gt; x&lt;/code&gt; case is chosen, &lt;code&gt;x&lt;/code&gt; is returned by the &lt;code&gt;match&lt;/code&gt; and hence by the function.&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;Some&lt;/code&gt; is a generic class and the &lt;code&gt;show&lt;/code&gt; function returns a &lt;code&gt;String&lt;/code&gt;, so the match is to &lt;code&gt;Some[+String]&lt;/code&gt;. The &lt;code&gt;+&lt;/code&gt; in the &lt;code&gt;+String&lt;/code&gt; expression is analogous to Java&amp;#8217;s &lt;code&gt;extends&lt;/code&gt;, &lt;em&gt;i.e.,&lt;/em&gt; &lt;code&gt;&amp;lt;? extends String&amp;gt;&lt;/code&gt;. Capiche?&lt;/p&gt;


	&lt;p&gt;Idioms like &lt;code&gt;case Some(x) =&amp;gt; x&lt;/code&gt; are called &lt;em&gt;extractors&lt;/em&gt; in Scala and are used a lot in Scala, as well as in FP, in general. Here&amp;#8217;s another example using Lists and our friend &lt;code&gt;::&lt;/code&gt;, the &amp;#8220;cons&amp;#8221; operator.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def countScalas(list: List[String]): Int = {
    list match {
        case "Scala" :: tail =&amp;gt; countScalas(tail) + 1
        case _ :: tail       =&amp;gt; countScalas(tail)
        case Nil             =&amp;gt; 0
    }
}
val langs = List("Scala", "Java", "C++", "Scala", "Python", "Ruby")
val count = countScalas(langs)
println(count)    // =&amp;gt; 2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;We&amp;#8217;re counting the number of occurrences of &amp;#8220;Scala&amp;#8221; in a list of strings, using matching and recursion and no explicit iteration.  An expression of the form &lt;code&gt;head :: tail&lt;/code&gt; applied to a list returns the first element set as the &lt;code&gt;head&lt;/code&gt; variable and the rest of the list set as the &lt;code&gt;tail&lt;/code&gt; variable. In our case, the first &lt;code&gt;case&lt;/code&gt; statement looks for the particular case where the head equals &lt;code&gt;Scala&lt;/code&gt;. The second &lt;code&gt;case&lt;/code&gt; matches all lists, except for the empty list (&lt;code&gt;Nil&lt;/code&gt;). Since matches are eager, the first &lt;code&gt;case&lt;/code&gt; will always pick out the &lt;code&gt;List("Scala", ...)&lt;/code&gt; case first. Note that in the second &lt;code&gt;case&lt;/code&gt;, we don&amp;#8217;t actually care about the value, so we use the placeholder &lt;code&gt;_&lt;/code&gt;. Both the first and second &lt;code&gt;case&lt;/code&gt;&amp;#8217;s call &lt;code&gt;countScalas&lt;/code&gt; recursively.&lt;/p&gt;


	&lt;p&gt;Pattern matching like this is powerful, yet succinct and elegant. We&amp;#8217;ll see more examples of matching in the next blog post on concurrency using message passing.&lt;/p&gt;


	&lt;h2&gt;Recap of Scala&amp;#8217;s Functional Programming&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;ve just touched the tip of the iceberg concerning functional programming (and I hope I got all the details right!). Hopefully, you can begin to see why we&amp;#8217;ve overlooked FP for too long!&lt;/p&gt;


	&lt;p&gt;In my last post, I&amp;#8217;ll wrap up with a look at Scala&amp;#8217;s approach to concurrency, the Actor model of message passing.&lt;/p&gt;</description>
      <pubDate>Tue, 05 Aug 2008 20:32:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e80aea1f-2ab7-41fa-b431-8d7a5a29c6ed</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/08/05/the-seductions-of-scala-part-ii-functional-programming</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>functional</category>
      <category>objects</category>
      <category>FP</category>
      <category>OOP</category>
      <category>Scala</category>
      <category>Java</category>
    </item>
    <item>
      <title>The Seductions of Scala, Part I</title>
      <description>&lt;p&gt;(Update 12/23/2008: Thanks to Apostolos Syropoulos for pointing out an earlier reference for the concept of &amp;#8220;traits&amp;#8221;).&lt;/p&gt;


	&lt;p&gt;Because of all the recent hoo-ha about &lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt; (&lt;em&gt;e.g.,&lt;/em&gt; as a &amp;#8220;cure&amp;#8221; for the &lt;a href="http://www.technologyreview.com/Infotech/17682/page1/"&gt;multicore problem&lt;/a&gt;), I decided to cast aside my dysfunctional ways and learn one of the FP languages. The question was, which one?&lt;/p&gt;


	&lt;p&gt;My distinguished colleague, &lt;a href="http://www.objectmentor.com/omTeam/feathers_m.html"&gt;Michael Feathers&lt;/a&gt;, has been on a &lt;a href="http://www.haskell.org/"&gt;Haskell&lt;/a&gt; &lt;a href="http://twitter.com/mfeathers"&gt;binge&lt;/a&gt; of late. Haskell is a pure functional language and is probably most interesting as the &amp;#8220;flagship language&amp;#8221; for academic exploration, rather than production use. (That was not meant as flame bait&amp;#8230;) It&amp;#8217;s hard to underestimate the influence Haskell has had on language design, including Java generics, .NET &lt;span class="caps"&gt;LINQ&lt;/span&gt; and F#, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;However, I decided to learn &lt;a href="http://www.scala-lang.org"&gt;Scala&lt;/a&gt; first, because it is a &lt;span class="caps"&gt;JVM&lt;/span&gt; language that combines object-oriented and functional programming in one language. At ~13 years of age, Java is a bit dated. Scala has the potential of &lt;a href="http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming"&gt;replacing Java&lt;/a&gt; as the principle language of the &lt;span class="caps"&gt;JVM&lt;/span&gt;, an extraordinary piece of engineering that is arguably now more valuable than the language itself. (Note: there is also a .NET version of Scala under development.)&lt;/p&gt;


	&lt;p&gt;Here are some of my observations, divided over three blog posts.&lt;/p&gt;


	&lt;p&gt;First, a few disclaimers. I am a Scala novice, so any flaws in my analysis reflect on me, not Scala! Also, this is by no means an exhaustive analysis of the pros and cons of Scala &lt;em&gt;vs.&lt;/em&gt; other options. Start with the &lt;a href="http://www.scala-lang.org"&gt;Scala website&lt;/a&gt; for more complete information.&lt;/p&gt;


	&lt;h2&gt;A Better &lt;span class="caps"&gt;OOP&lt;/span&gt; Language&lt;/h2&gt;


	&lt;p&gt;Scala works seamlessly with Java. You can invoke Java APIs, extend Java classes and implement Java interfaces. You can even invoke Scala code from Java, once you understand how certain &amp;#8220;Scala-isms&amp;#8221; are translated to Java constructs (&lt;code&gt;javap&lt;/code&gt; is your friend). Scala syntax is more succinct and removes a lot of tedious boilerplate from Java code.&lt;/p&gt;


	&lt;p&gt;For example, the following &lt;code&gt;Person&lt;/code&gt; class in Java:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class Person {
    private String firstName;
    private String lastName;
    private int    age;

    public Person(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName  = lastName;
        this.age       = age;
    }

    public void setFirstName(String firstName) { this.firstName = firstName; }
    public void String getFirstName() { return this.firstName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
    public void String getLastName() { return this.lastName; }
    public void setAge(int age) { this.age = age; }
    public void int getAge() { return this.age; }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;can be written in Scala thusly:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class Person(var firstName: String, var lastName: String, var age: Int)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Yes, that&amp;#8217;s it. The constructor is the argument list to the class, where each parameter is declared as a variable (&lt;code&gt;var&lt;/code&gt; keyword). It automatically generates the &lt;em&gt;equivalent&lt;/em&gt; of getter and setter methods, meaning they look like Ruby-style attribute accessors; the getter is &lt;code&gt;foo&lt;/code&gt; instead of &lt;code&gt;getFoo&lt;/code&gt; and the setter is &lt;code&gt;foo = &lt;/code&gt; instead of &lt;code&gt;setFoo&lt;/code&gt;. Actually, the setter function is really &lt;code&gt;foo_=&lt;/code&gt;, but Scala lets you use the &lt;code&gt;foo = &lt;/code&gt; &lt;em&gt;sugar&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Lots of other well designed conventions allow the language to define almost everything as a method, yet support forms of syntactic sugar like the illusion of operator overloading, Ruby-like &lt;span class="caps"&gt;DSL&lt;/span&gt;&amp;#8217;s, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;You also get fewer semicolons, no requirements tying package and class definitions to the file system structure, type inference, multi-valued returns (tuples), and a better type and generics model.&lt;/p&gt;


	&lt;p&gt;One of the biggest deficiencies of Java is the lack of a complete &lt;em&gt;mixin&lt;/em&gt; model. Mixins are small, focused (think &lt;a href="http://www.objectmentor.com/resources/articles/srp.pdf"&gt;Single Responsibility Principle&lt;/a&gt; ...) bits of state and behavior that can be added to classes (or objects) to extend them as needed. In a language like C++, you can use multiple inheritance for mixins. Because Java only supports single inheritance and interfaces, which can&amp;#8217;t have any state and behavior, implementing a mixin-based design has always required various hacks. &lt;a href="http://www.aosd.net"&gt;Aspect-Oriented Programming&lt;/a&gt; is also one &lt;em&gt;partial&lt;/em&gt; solution to this problem.&lt;/p&gt;


	&lt;p&gt;The most exciting &lt;span class="caps"&gt;OOP&lt;/span&gt; enhancement Scala brings is its support for  &lt;a href="http://www.scala-lang.org/intro/traits.html"&gt;Traits&lt;/a&gt;, a concept first described &lt;a href="http://portal.acm.org/citation.cfm?doid=800210.806468"&gt;here&lt;/a&gt; and more recently discussed &lt;a href="http://portal.acm.org/citation.cfm?id=1119479.1119483"&gt;here&lt;/a&gt;. Traits support Mixins (and other design techniques) through composition rather than inheritance. You could think of traits as interfaces with implementations. They work a lot like Ruby modules.&lt;/p&gt;


	&lt;p&gt;Here is an example of the &lt;a href="http://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer Pattern&lt;/a&gt; written as traits, where they are used to monitor changes to a bank account balance. First, here are reusable &lt;code&gt;Subject&lt;/code&gt; and &lt;code&gt;Observer&lt;/code&gt; traits.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
trait Observer[S] {
    def receiveUpdate(subject: S);
}

trait Subject[S] { 
    this: S =&amp;gt;
    private var observers: List[Observer[S]] = Nil
    def addObserver(observer: Observer[S]) = observers = observer :: observers

    def notifyObservers() = observers.foreach(_.receiveUpdate(this))
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;In Scala, generics are declared with square brackets, &lt;code&gt;[...]&lt;/code&gt;, rather than angled brackets, &lt;code&gt;&amp;lt;...&amp;gt;&lt;/code&gt;. Method definitions begin with the &lt;code&gt;def&lt;/code&gt; keyword. The &lt;code&gt;Observer&lt;/code&gt; trait defines one abstract method, which is called by the &lt;code&gt;Subject&lt;/code&gt; to notify the observer of changes. The &lt;code&gt;Subject&lt;/code&gt; is passed to the &lt;code&gt;Observer&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;This trait looks exactly like a Java interface. In fact, that&amp;#8217;s how traits are represented in Java byte code. If the trait has state and behavior, like &lt;code&gt;Subject&lt;/code&gt;, the byte code representation involves additional elements.&lt;/p&gt;


	&lt;p&gt;The &lt;code&gt;Subject&lt;/code&gt; trait is more complex. The strange line, &lt;code&gt;this: S =&amp;gt; &lt;/code&gt;, is called a &lt;em&gt;self type&lt;/em&gt; declaration. It tells the compiler that whenever &lt;code&gt;this&lt;/code&gt; is referenced in the trait, treat its type as &lt;code&gt;S&lt;/code&gt;, rather than &lt;code&gt;Subject[S]&lt;/code&gt;. Without this declaration, the call to &lt;code&gt;receiveUpdate&lt;/code&gt; in the &lt;code&gt;notifyObservers&lt;/code&gt; method would not compile, because it would attempt to pass a &lt;code&gt;Subject[S]&lt;/code&gt; object, rather than a &lt;code&gt;S&lt;/code&gt; object. The self type declaration solves this problem.&lt;/p&gt;


	&lt;p&gt;The next line creates a private list of observers, initialized to &lt;code&gt;Nil&lt;/code&gt;, which is an empty list. Variable declarations are &lt;code&gt;name: type&lt;/code&gt;. Why didn&amp;#8217;t they follow Java conventions, &lt;em&gt;i.e.,&lt;/em&gt; &lt;code&gt;type name&lt;/code&gt;? Because this syntax makes the code easier to parse when &lt;em&gt;type inference&lt;/em&gt; is used, meaning where the explicit &lt;code&gt;:type&lt;/code&gt; is omitted and inferred.&lt;/p&gt;


	&lt;p&gt;In fact, I&amp;#8217;m using type inference for all the method declarations, because the compiler can figure out what each method returns, in my examples. In this case, they all return type &lt;code&gt;Unit&lt;/code&gt;, the equivalent of Java&amp;#8217;s &lt;code&gt;void&lt;/code&gt;. (The name &lt;code&gt;Unit&lt;/code&gt; is a common term in functional languages.)&lt;/p&gt;


	&lt;p&gt;The third line defines a method for adding a new observer to the list. Notice that concrete method definitions are of the form&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def methodName(parameter: type, ...) = {
    method body
}  
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;In this case, because there is only one line, I dispensed with the &lt;code&gt;{...}&lt;/code&gt;. The equals sign before the body emphasizes the functional nature of scala, that all methods are objects, too. We&amp;#8217;ll revisit this in a moment and in the next post.&lt;/p&gt;


	&lt;p&gt;The method body prepends the new observer object to the existing list. Actually, a new list is created. The &lt;code&gt;::&lt;/code&gt; operator, called &amp;#8220;cons&amp;#8221;, &lt;em&gt;binds to the right&lt;/em&gt;. This &amp;#8220;operator&amp;#8221; is really a method call, which could actually be written like this, &lt;code&gt;observers.::(observer)&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Our final method in &lt;code&gt;Subject&lt;/code&gt; is &lt;code&gt;notifyObservers&lt;/code&gt;. It iterates through observers and invokes the block &lt;code&gt;observer.receiveUpdate(this)&lt;/code&gt; on each observer. The &lt;code&gt;_&lt;/code&gt; evaluates to the current observer reference. For comparison, in Ruby, you would define this method like so:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def notifyObservers() 
    @observers.each { |o| o.receiveUpdate(self) }
end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Okay, let&amp;#8217;s look at how you would actually use these traits. First, our &amp;#8220;plain-old Scala object&amp;#8221; (POSO) &lt;code&gt;Account&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class Account(initialBalance: Double) {
    private var currentBalance = initialBalance
    def balance = currentBalance
    def deposit(amount: Double)  = currentBalance += amount
    def withdraw(amount: Double) = currentBalance -= amount
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Hopefully, this is self explanatory, except for two things. First, recall that the whole class declaration is actually the constructor, which is why we have an &lt;code&gt;initialBalance: Double&lt;/code&gt; parameter on &lt;code&gt;Account&lt;/code&gt;. This looks strange to the Java-trained eye, but it actually works well and is another example of Scala&amp;#8217;s economy. (You can define multiple constructors, but I won&amp;#8217;t go into that here&amp;#8230;).&lt;/p&gt;


	&lt;p&gt;Second, note that I omitted the parentheses when I defined the &lt;code&gt;balance&lt;/code&gt; &amp;#8220;getter&amp;#8221; method. This supports the &lt;em&gt;uniform access principle&lt;/em&gt;. Clients will simply call &lt;code&gt;myAccount.balance&lt;/code&gt;, without parentheses and I could redefine &lt;code&gt;balance&lt;/code&gt; to be a &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;val&lt;/code&gt; and the client code would not have to change!&lt;/p&gt;


	&lt;p&gt;Next, a subclass that supports observation.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class ObservedAccount(initialBalance: Double) extends Account(initialBalance) with Subject[Account] {
    override def deposit(amount: Double) = {
        super.deposit(amount)
        notifyObservers()
    }
    override def withdraw(amount: Double) = {
        super.withdraw(amount)
        notifyObservers()
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;with&lt;/code&gt; keyword is how a trait is used, much the way that you &lt;code&gt;implement&lt;/code&gt; an interface in Java, but now you don&amp;#8217;t have to implement the interface&amp;#8217;s methods. We&amp;#8217;ve already done that.&lt;/p&gt;


	&lt;p&gt;Note that the expression, &lt;code&gt;ObservedAccount(initialBalance: Double) extends Account(initialBalance)&lt;/code&gt;, not only defines the (single) inheritance relationship, it also functions as the constructor&amp;#8217;s call to &lt;code&gt;super(initialBalance)&lt;/code&gt;, so that &lt;code&gt;Account&lt;/code&gt; is properly initialized.&lt;/p&gt;


	&lt;p&gt;Next, we have to override the &lt;code&gt;deposit&lt;/code&gt; and &lt;code&gt;withdraw&lt;/code&gt; methods, calling the parent methods and then invoking &lt;code&gt;notifyObservers&lt;/code&gt;. Anytime you override a concrete method, scala requires the &lt;code&gt;override&lt;/code&gt; keyword. This tells you unambiguously that you are overriding a method and the Scala compiler throws an error if you aren&amp;#8217;t actually overriding a method, &lt;em&gt;e.g.,&lt;/em&gt; because of a typo. Hence, the keyword is much more reliable (and hence useful&amp;#8230;) than Java&amp;#8217;s &lt;code&gt;@Override&lt;/code&gt; annotation.&lt;/p&gt;


	&lt;p&gt;Finally, here is an &lt;code&gt;Observer&lt;/code&gt; that prints to stdout when the balance changes.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
class AccountReporter extends Observer[Account] {
    def receiveUpdate(account: Account) =
        println("Observed balance change: "+account.balance)
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Rather than use &lt;code&gt;with&lt;/code&gt;, I just extend the &lt;code&gt;Observer&lt;/code&gt; trait, because I don&amp;#8217;t have another parent class.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s some code to test what we&amp;#8217;ve done.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
def changingBalance(account: Account) = {
    println("==== Starting balance: " + account.balance)
    println("Depositing $10.0")
    account.deposit(10.0)
    println("new balance: " + account.balance)
    println("Withdrawing $5.60")
    account.withdraw(5.6)
    println("new balance: " + account.balance)
}

var a = new Account(0.0)
changingBalance(a)

var oa = new ObservedAccount(0.0)
changingBalance(oa)
oa.addObserver(new AccountReporter)
changingBalance(oa)
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Which prints out:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
==== Starting balance: 0.0
Depositing $10.0
new balance: 10.0
Withdrawing $5.60
new balance: 4.4
==== Starting balance: 0.0
Depositing $10.0
new balance: 10.0
Withdrawing $5.60
new balance: 4.4
==== Starting balance: 4.4
Depositing $10.0
Observed balance change: 14.4
new balance: 14.4
Withdrawing $5.60
Observed balance change: 8.8
new balance: 8.8
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Note that we only observe the last transaction.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.scala-lang.org/downloads/index.html"&gt;Download Scala&lt;/a&gt; and try it out. Put all this code in one &lt;code&gt;observer.scala&lt;/code&gt; file, for example, and run the command:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
scala observer.scala
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h2&gt;But Wait, There&amp;#8217;s More!&lt;/h2&gt;


	&lt;p&gt;In the next post, I&amp;#8217;ll look at Scala&amp;#8217;s support for Functional Programming and why OO programmers should find it interesting. In the third post, I&amp;#8217;ll look at the specific case of concurrent programming in Scala and make some concluding observations of the pros and cons of Scala.&lt;/p&gt;


	&lt;p&gt;For now, here are some references for more information.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;The &lt;a href="http://www.scala-lang.org"&gt;Scala website&lt;/a&gt;, for downloads, documentation, mailing lists, &lt;em&gt;etc.&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;Ted Neward&amp;#8217;s excellent &lt;a href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=scala+neward"&gt;multipart introduction&lt;/a&gt; to Scala at &lt;a href="http://www.ibm.com/developerworks"&gt;developerWorks&lt;/a&gt;.&lt;/li&gt;
		&lt;li&gt;The forthcoming &lt;a href="http://www.artima.com/shop/programming_in_scala"&gt;Programming in Scala&lt;/a&gt; book.&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Sun, 03 Aug 2008 15:30:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d4a4acbf-e300-4146-83c7-0536785997e1</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>Java</category>
      <category>Scala</category>
      <category>statically</category>
      <category>typed</category>
      <category>dynamically</category>
      <category>OOP</category>
      <category>FP</category>
      <category>functional</category>
      <category>object</category>
      <category>oriented</category>
    </item>
    <item>
      <title>Always &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt; in a &amp;lt;tt&amp;gt;finally&amp;lt;/tt&amp;gt; block</title>
      <description>&lt;p&gt;Here&amp;#8217;s one for my fellow Java programmers, but it&amp;#8217;s really generally applicable.&lt;/p&gt;


	&lt;p&gt;When you call &lt;tt&gt;close()&lt;/tt&gt; on I/O streams, readers, writers, network sockets, database connections, &lt;em&gt;etc.&lt;/em&gt;, it&amp;#8217;s easy to forgot the most appropriate idiom. I just spent a few hours fixing some examples of misuse in otherwise very good Java code.&lt;/p&gt;


	&lt;p&gt;What&amp;#8217;s wrong the following code?&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public void writeContentToFile(String content, String fileName) throws Exception {
    File output = new File(fileName);
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(output), "UTF-8");
    writer.write(content);
    writer.close();
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;It doesn&amp;#8217;t look all that bad. It tells it&amp;#8217;s story. It&amp;#8217;s easy to understand.&lt;/p&gt;


	&lt;p&gt;However, &lt;em&gt;it&amp;#8217;s quite likely&lt;/em&gt; that you won&amp;#8217;t get to the last line, which closes the &lt;tt&gt;writer&lt;/tt&gt;, from time to time. File and network I/O errors are common. For example, what if you can&amp;#8217;t actually write to the location specified by &lt;tt&gt;fileName&lt;/tt&gt;? So, we have to be more defensive. We want to be sure we always clean up.&lt;/p&gt;


	&lt;p&gt;The correct idiom is to use a &lt;tt&gt;try &amp;#8230; finally &amp;#8230;&lt;/tt&gt; block.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
public void writeContentToFile(String content, String fileName) throws Exception {
    File output = new File(getFileSystemPath() + contentFilename);
    OutputStreamWriter writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(output), "UTF-8");
        writer.write(content);
    } finally {
        if (writer != null)
            writer.close();
    }
}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Now, no matter what happens, the writer will be closed, if it&amp;#8217;s not null, even if writing the output was unsuccessful.&lt;/p&gt;


	&lt;p&gt;Note that we don&amp;#8217;t necessarily need a &lt;tt&gt;catch&lt;/tt&gt; block, because in this case we&amp;#8217;re willing to let any &lt;tt&gt;Exception&lt;/tt&gt;s propagate up the stack (notice the throws clause). A lot of developers don&amp;#8217;t realize that there are times when you need a &lt;tt&gt;try&lt;/tt&gt; block, but not necessarily a &lt;tt&gt;catch&lt;/tt&gt; block. This is one of those times.&lt;/p&gt;


	&lt;p&gt;So, anytime you need to clean up or otherwise release resources, use a &lt;tt&gt;finally&lt;/tt&gt; block to ensure that the clean up happens, no matter what.&lt;/p&gt;</description>
      <pubDate>Thu, 31 Jul 2008 00:12:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2eda3563-e87a-4c15-a93d-f842f8de8f68</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/07/31/always-close-in-a-finally-block</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>close</category>
      <category>finally</category>
      <category>Java</category>
    </item>
    <item>
      <title>The Ascendency of Dynamic X vs. Static X, where X = ...</title>
      <description>&lt;p&gt;I noticed a curious symmetry the other day. For several values of &lt;em&gt;X&lt;/em&gt;, a dynamic approach has been gaining traction over a static approach, in some cases for several years.&lt;/p&gt;


	&lt;h2&gt;X = Languages&lt;/h2&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;The Ascendency of Dynamic Languages vs. Static Languages&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;This one is pretty obvious. It&amp;#8217;s hard not to notice the resurgent interest in dynamically-typed languages, like Ruby, Python, Erlang, and even stalwarts like Lisp and Smalltalk.&lt;/p&gt;


	&lt;p&gt;There is a healthy debate about the relative merits of dynamic &lt;em&gt;vs.&lt;/em&gt; static typing, but the &amp;#8220;hotness&amp;#8221; factor is undeniable.&lt;/p&gt;


	&lt;h2&gt;X = Correctness Analysis&lt;/h2&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;The Ascendency of Dynamic Correctness Analysis vs. Static Correctness Analysis&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Analysis of code to prove correctness has been a research topic for years and the tools have become pretty good. If you&amp;#8217;re in the Java world, tools like &lt;a href="http://pmd.sourceforge.net/"&gt;&lt;span class="caps"&gt;PMD&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://findbugs.sourceforge.net/"&gt;FindBugs&lt;/a&gt; find a lot of real and potential issues.&lt;/p&gt;


	&lt;p&gt;One thing none of these tools have ever been able to do is to analyze conformance of your code to your project&amp;#8217;s &lt;em&gt;requirements&lt;/em&gt;. I suppose you could probably build such tools using the same analysis techniques, but the cost would be too prohibitive for individual projects.&lt;/p&gt;


	&lt;p&gt;However, while analyzing the code statically is very hard, watching what the code actually does at runtime is more tractable and cost-effective, using automated tests.&lt;/p&gt;


	&lt;p&gt;Test-driving code results in a suite of unit, feature, and acceptance tests that do a good enough job, for most applications, of finding logic &lt;em&gt;and&lt;/em&gt; requirements bugs. The way test-&lt;em&gt;first&lt;/em&gt; development improves the design helps ensure correctness in the first place.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s worth emphasizing that automated tests exercise the code using &lt;em&gt;representative&lt;/em&gt; data sets and scenarios, so they don&amp;#8217;t constitute a proof of correctness. However, they are good enough for most applications.&lt;/p&gt;


	&lt;h2&gt;X = Optimization&lt;/h2&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;The Ascendency of Dynamic Optimization vs. Static Optimization&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Perhaps the least well known of these &lt;em&gt;X&lt;/em&gt;&amp;#8217;s is optimization. Mature compilers like &lt;a href="http://gcc.gnu.org/"&gt;gcc&lt;/a&gt; have sophisticated optimizations based on static analysis of code (you can see where this is going&amp;#8230;).&lt;/p&gt;


	&lt;p&gt;On the other hand, the javac compiler does not do a lot of optimizations. Rather, the &lt;span class="caps"&gt;JVM&lt;/span&gt; does.&lt;/p&gt;


	&lt;p&gt;The &lt;span class="caps"&gt;JVM&lt;/span&gt; watches the code execute and it performs optimizations the compiler could never do, like speculatively inlining polymorphic method calls, based on which types are actually having their methods invoked. The &lt;span class="caps"&gt;JVM&lt;/span&gt; puts in low-overhead guards to confirm that its assumptions are valid for each invocation. If not, the &lt;span class="caps"&gt;JVM&lt;/span&gt; &lt;em&gt;de-optimizes&lt;/em&gt; the code.&lt;/p&gt;


	&lt;p&gt;The &lt;span class="caps"&gt;JVM&lt;/span&gt; can do this optimization because it sees how the code is really used at runtime, while the compiler has no idea when it looks at the code.&lt;/p&gt;


	&lt;p&gt;Just as for correctness analysis, static optimizations can only go so far. Dynamic optimizations simply bypass a lot of the difficulty and often yield better results.&lt;/p&gt;


	&lt;p&gt;Steve Yegge provided a nice &lt;a href="http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html"&gt;overview&lt;/a&gt; recently of &lt;span class="caps"&gt;JVM&lt;/span&gt; optimizations, as part of a larger discussion on dynamic languages.&lt;/p&gt;


	&lt;p&gt;&lt;br/&gt;
There are other dynamic &lt;em&gt;vs.&lt;/em&gt; static things I could cite (think networking), but I&amp;#8217;ll leave it at these three, for now.&lt;/p&gt;</description>
      <pubDate>Sat, 26 Jul 2008 21:48:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:80de9d7c-10cc-41a1-8d98-eb71a637b37a</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/07/26/the-ascendency-of-dynamic-x-_vs-_-static-x-where-x</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>dynamic</category>
      <category>static</category>
      <category>languages</category>
      <category>correctness</category>
      <category>optimization</category>
    </item>
    <item>
      <title>Contracts and Integration Tests for Component Interfaces</title>
      <description>&lt;p&gt;I am mentoring a team that is transitioning to XP, the first team in a planned, corporate-wide transition. Recently we ran into miscommunication problems about an interface we are providing to another team.&lt;/p&gt;


	&lt;p&gt;The problems didn&amp;#8217;t surface until a &amp;#8220;big-bang&amp;#8221; integration right before a major release, when it was too late to fix the problem. The feature was backed out of the release, as a result.&lt;/p&gt;


	&lt;p&gt;There are several lessons to take away from this experience and a few techniques for preventing these problems in the first place.&lt;/p&gt;


	&lt;p&gt;End-to-end &lt;em&gt;automated&lt;/em&gt; integration tests are a well-established way of catching these problems early on. The team I&amp;#8217;m mentoring has set up its own continuous-integration (CI) server and the team is getting pretty good at writing &lt;em&gt;acceptance&lt;/em&gt; tests using &lt;a href="http://fitnesse.org"&gt;FitNesse&lt;/a&gt;. However, these tests only cover the components provided by the team, not the true end-to-end &lt;em&gt;user stories&lt;/em&gt;. So, they are imperfect as both acceptance tests and integration tests. Our longer-term goal is to automate &lt;em&gt;true&lt;/em&gt; end-to-end acceptance and integration tests, across all components and services.&lt;/p&gt;


	&lt;p&gt;In this particular case, the other team is following a waterfall-style of development, with &lt;em&gt;big design up front&lt;/em&gt;. Therefore, my team needed to give them an interface to design against, before we were ready to actually implement the service.&lt;/p&gt;


	&lt;p&gt;There are a couple of problems with this approach. First, the two teams should really &amp;#8220;pair&amp;#8221; to work out the interface and behavior across their components. As I said, we&amp;#8217;re just starting to go Agile, but my goal is to have &lt;em&gt;virtual&lt;/em&gt; feature teams, where members of the required component teams come together as needed to implement features. This would help prevent the miscommunication of one team defining an interface and sharing it with another team through documentation, &lt;em&gt;etc.&lt;/em&gt;  Getting people to communicate face-to-face and to write code together would minimize miscommunication.&lt;/p&gt;


	&lt;p&gt;Second, defining a service interface without the implementation is risky, because it&amp;#8217;s very likely you will miss important details. The best way to work out the details of the interface is to test drive it in some way.&lt;/p&gt;


	&lt;p&gt;This suggests another technique I want to introduce to the team. When defining an interface for external consumption, don&amp;#8217;t just deliver the &amp;#8220;static&amp;#8221; interface (source files, documentation, &lt;em&gt;etc.&lt;/em&gt;), also deliver working &lt;a href="http://www.mockobjects.com/"&gt;Mock Objects&lt;/a&gt; that the other team can test against. You should develop these mocks as you test drive the interface, even if you aren&amp;#8217;t yet working on the full implementation (for schedule or other reasons).&lt;/p&gt;


	&lt;p&gt;The mocks encapsulate and enforce the behavioral &lt;strong&gt;contract&lt;/strong&gt; of the interface. &lt;a href="http://en.wikipedia.org/wiki/Design_by_contract"&gt;Design by Contract&lt;/a&gt; is a very effective way of thinking about interface design and implementing automated enforcement of it. Test-driven development mostly serves the same practical function, but thinking in &amp;#8220;contractual&amp;#8221; terms brings clarity to tests that is often missing in many of the tests I see.&lt;/p&gt;


	&lt;p&gt;Many developers already use mocks for components that don&amp;#8217;t exist yet and find that the mocks help them design the interfaces to those components, even while the mocks are being used to test clients of the components.&lt;/p&gt;


	&lt;p&gt;Of course, there is no guarantee that the mocks faithfully represent the actual behavior, but they will minimize surprises. Whether you have mocks or not, there is no substitute for running automated integration tests on real components as soon as possible.&lt;/p&gt;</description>
      <pubDate>Sun, 29 Jun 2008 21:54:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b57496fe-d160-42bd-97cc-73608c9333d1</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/06/29/contracts-and-integration-tests-for-component-interfaces</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>Design Principles</category>
      <category>design</category>
      <category>by</category>
      <category>contract</category>
      <category>continuous</category>
      <category>integration</category>
      <category>components</category>
      <category>interfaces</category>
      <category>XP</category>
      <category>agile</category>
    </item>
    <item>
      <title>Observations on Test-Driving User Interfaces</title>
      <description>&lt;p&gt;Test driving user interface development has always been a challenge. Recently, I&amp;#8217;ve worked with two projects where most of the work has been on the user-interface components.&lt;/p&gt;


	&lt;p&gt;The first project is using Adobe Flex to create a rich interface. The team decided to adopt &lt;a href="http://funfx.rubyforge.org/"&gt;FunFX&lt;/a&gt; for acceptance testing. You write your tests in Ruby, typically using &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html"&gt;Test::Unit&lt;/a&gt; or &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;FunFX places some constraints on your Flex application. You have to define the &lt;span class="caps"&gt;GUI&lt;/span&gt; objects in &lt;span class="caps"&gt;MXML&lt;/span&gt;, the &lt;span class="caps"&gt;XML&lt;/span&gt;-based file format for Flex applications, rather than ActionScript, and you need to add ids to all elements you want to reference.[1]&lt;/p&gt;


	&lt;p&gt;These are reasonable constraints and the first constraint promotes better quality, in fact. The &lt;span class="caps"&gt;MXML&lt;/span&gt; format is more succinct (despite the &lt;span class="caps"&gt;XML&lt;/span&gt; &amp;#8220;noise&amp;#8221;) and declarative than ActionScript code. This is almost always true of UI code in most languages (with notable exceptions&amp;#8230;). Declarative &lt;em&gt;vs.&lt;/em&gt; imperative code tends to improve quality because less code means fewer bugs, less to maintain, and it frees the implementor of the declarative &amp;#8220;language&amp;#8221; to pick the best implementation strategies, optimizations, &lt;em&gt;etc.&lt;/em&gt; This characteristic is typical of Functional Languages and well-designed Domain Specific Languages, as well.&lt;/p&gt;


	&lt;p&gt;I don&amp;#8217;t think you can underestimate the benefit of writing &lt;strong&gt;less&lt;/strong&gt; code. I see too many teams whose problems would diminish considerably if they just got rid of duplication and learned to be &lt;em&gt;concise&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;The second project is a wiki-based application written in Java. To make deployment as simple as possible, the implementors avoided the Servlet &lt;span class="caps"&gt;API&lt;/span&gt; (no need to install Tomcat, &lt;em&gt;etc.&lt;/em&gt;) and rolled their own web server and page rendering components. (I&amp;#8217;m not sure I would have made these decisions myself, but I don&amp;#8217;t think they are bad, either&amp;#8230;)&lt;/p&gt;


	&lt;p&gt;The rendering components are object-oriented and use a number of design patterns, such as page factories with builder objects that reflect the &amp;#8220;widgets&amp;#8221; in the UI, &lt;span class="caps"&gt;HTML&lt;/span&gt; tags, &lt;em&gt;etc.&lt;/em&gt; This approach makes the UI very testable with JUnit and &lt;a href="http://www.fitnesse.org"&gt;FitNesse&lt;/a&gt;. In fact, the development process was a model of test-driven development.&lt;/p&gt;


	&lt;p&gt;However, the final result is flawed!  It is much too difficult to change the &lt;em&gt;look and feel&lt;/em&gt; of the application, which is essential for most UI&amp;#8217;s, especially web UI&amp;#8217;s. The project made the wrong tradeoffs; the design choices met the requirements of &lt;span class="caps"&gt;TDD&lt;/span&gt; very well, but they made maintenance and enhancement expensive and tedious. The application is now several years old and it has become dated, because of the expense of &amp;#8220;refreshing&amp;#8221; the &lt;em&gt;look and feel&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;What should have been done? These days, most dynamic web UI&amp;#8217;s are built with templating engines, of which there are many in the most common programming languages. Pages defined in a templating engine are very declarative, except for the special tags where behavior is inserted. The pages are easy to change. It is mostly obvious where a particular visual element is generated, since most of the &amp;#8220;tags&amp;#8221; in the template look exactly like the tags in the rendered page. &amp;#8220;Declarative&amp;#8221; templates, like good &lt;span class="caps"&gt;DSL&lt;/span&gt;&amp;#8217;s, can be read, understood, and even edited by the &lt;em&gt;stakeholders&lt;/em&gt;, in this case the graphical designers.&lt;/p&gt;


	&lt;p&gt;But how do you test these page templates? When test-driving UI&amp;#8217;s it is important to decide what to test and what &lt;strong&gt;not&lt;/strong&gt; to test. The general rule for &lt;span class="caps"&gt;TDD&lt;/span&gt; is to &lt;em&gt;test anything that can break&lt;/em&gt;.  The corollary, especially relevant for UI&amp;#8217;s, is &lt;em&gt;don&amp;#8217;t test anything when you don&amp;#8217;t care if it changes&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;It is usually the &lt;em&gt;dynamic behavior&lt;/em&gt; of the UI that can break and should be tested. Templating engines provide special tags for inserting dynamic behavior in the underlying language (Java, Ruby, &lt;em&gt;etc.&lt;/em&gt;). &lt;em&gt;This&lt;/em&gt; is what you should test. It is usually best to keep the &lt;em&gt;scripts&lt;/em&gt; in these tags as small as possible; the scripts just delegate to code, which can be test-driven in the usual way.&lt;/p&gt;


	&lt;p&gt;I see too many UI tests that compare long strings of &lt;span class="caps"&gt;HTML&lt;/span&gt;. These tests break whenever someone makes a minor &lt;em&gt;look and feel&lt;/em&gt; or other inconsequential change. Part of the art of &lt;span class="caps"&gt;UI TDD&lt;/span&gt; is knowing how to test just what can break and nothing more. In the second project, incidental changes to the UI break tests that should be &lt;em&gt;agnostic&lt;/em&gt; to such changes.&lt;/p&gt;


	&lt;p&gt;To conclude, keep your UI&amp;#8217;s as &lt;em&gt;declarative&lt;/em&gt; as you can. Only test the &amp;#8220;declarations&amp;#8221; (&lt;i&gt;e.g.&lt;/i&gt;, templates) in areas where they might &lt;em&gt;break&lt;/em&gt;, meaning if it changes, it&amp;#8217;s a bug. You&amp;#8217;ll get the full benefits of &lt;span class="caps"&gt;TDD&lt;/span&gt; and the freedom to change the UI easily and frequently, as needed.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Disclaimer: my information on FunFX is second hand, so I might not have the details exactly correct; see the &lt;a href="http://funfx.rubyforge.org/"&gt;FunFX&lt;/a&gt; documentation for details.&lt;/p&gt;</description>
      <pubDate>Sun, 22 Jun 2008 16:52:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:107f8948-2e28-48d7-a495-85d56801b376</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/06/22/observations-on-test-driving-user-interfaces</link>
      <category>Dean's Deprecations</category>
      <category>Testing GUIs</category>
      <category>Design Principles</category>
      <category>TDD</category>
      <category>gui</category>
      <category>declarative</category>
      <category>functional</category>
      <category>design</category>
    </item>
    <item>
      <title> What We Can Learn from the Ojibwe Language</title>
      <description>&lt;p&gt;Ojibwe (sometimes spelled Ojibwa; the last syllable is pronounced &amp;#8220;way&amp;#8221;) is one of the few Native American languages that isn&amp;#8217;t immediately threatened with extinction. It is spoken by about 10,000 people around the Great Lakes region. Brothers David and Anton Treuer are helping to keep it alive, as they discussed in a recent &lt;a href="http://www.npr.org/templates/story/story.php?storyId=89851668"&gt;Fresh Air interview&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Ojibwe is a language that is optimized for an aboriginal people whose lives and livelihoods depend on an intimate awareness of their environment, especially the weather and water conditions. They have many nouns and verbs for fine gradations of rain, snow, ice conditions, the way water waves look and sound, &lt;em&gt;etc&lt;/em&gt;. You would want this clarity of detail if you ventured out on a lake every day to fish for dinner.&lt;/p&gt;


	&lt;p&gt;In the past, speaking languages like Ojibwe was actively suppressed by the government, in an attempt to assimilate Native Americans. Today, the threat of extinction is more from the sheer ubiquity of English. I think there is another force at play, too. People living a modern, so-called &amp;#8220;developed&amp;#8221; lifestyle just don&amp;#8217;t need to be so aware of their environment anymore. In fact, most of us are pretty &amp;#8220;tone deaf&amp;#8221; to the nuances of weather and water, which is sad in a way. We just don&amp;#8217;t perceive the need for the richness of an Ojibwe to communicate what&amp;#8217;s important to us, like sports trivia and fashion tips.&lt;/p&gt;


	&lt;p&gt;So, what does Ojibwe have to do with programming languages? Our language choices inform the way we frame problem solving and design. I was reminded of this recently while reading Ted Neward&amp;#8217;s &lt;a href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=scala+neward"&gt;series of articles on Scala&lt;/a&gt;. Scala is a &lt;span class="caps"&gt;JVM&lt;/span&gt; language that provides first-class support for &lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt; and object-oriented design refinements like &lt;a href="http://www.iam.unibe.ch/~scg/Research/Traits/"&gt;traits&lt;/a&gt;, which provide &lt;a href="http://c2.com/cgi/wiki?MixIn"&gt;mixin&lt;/a&gt; behavior.&lt;/p&gt;


	&lt;p&gt;While you can write Java-like code in Scala, Neward demonstrates how exploiting Scala features can result in very different code for many problems. The Scala examples are simpler, but sometimes that simplicity only becomes apparent after you grasp the underlying design principle in use, like closures or functional idioms.&lt;/p&gt;


	&lt;p&gt;One of the best pieces of advice in the &lt;a href="http://www.pragprog.com/the-pragmatic-programmer"&gt;Pragmatic Programmer&lt;/a&gt; is to learn a new language every year. You should pick a language that is very different from what you know already, not one that is fundamentally similar. Even if you won&amp;#8217;t use that language in your professional work, understanding its principles, patterns, and idioms will inform your work in whatever languages you actually use.&lt;/p&gt;


	&lt;p&gt;For example, there is a lot of &lt;a href="http://www.technologyreview.com/Infotech/17682/page1/"&gt;fretting&lt;/a&gt; these days about concurrent programming, given the rise of multi-core CPUs and multiprocessor computers.  We know &lt;a href="http://www.javaconcurrencyinpractice.com/"&gt;how to write concurrent programs&lt;/a&gt; in our most popular &lt;em&gt;imperative&lt;/em&gt; languages, like Java and C++, but that knowledge is somewhat specialized and not widely known in the community. This is the main reason that &lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt; is suddenly interesting again. It is inherently easier to write concurrent applications using side-effect-free code. I expect that we will fail to meet the concurrency challenge if we rely exclusively on the mechanisms in our imperative languages.&lt;/p&gt;


	&lt;p&gt;So, you could adopt a functional language for all or part of your concurrent application. Or, if you can&amp;#8217;t use Scala (or Haskell or Erlang or &amp;#8230;) you could at least apply functional idioms, like  side-effect-free functions, avoidance of mutable objects, &lt;em&gt;etc.&lt;/em&gt; in your current imperative language. However, even that won&amp;#8217;t be an option unless you understand those principles in the first place.&lt;/p&gt;


	&lt;p&gt;Learning a new language is more than learning a new vocabulary. It&amp;#8217;s even more than learning new design techniques. It&amp;#8217;s also learning to see common things from a fresh perspective, with greater clarity.&lt;/p&gt;</description>
      <pubDate>Sat, 03 May 2008 09:48:57 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d5cda248-84da-418d-81ae-5419241465a0</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/05/03/what-we-can-learn-from-the-ojibwe-language</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>languages</category>
      <category>polyglot</category>
      <category>programming</category>
      <category>multilingual</category>
      <category>design</category>
    </item>
    <item>
      <title>Unit Testing C and C++ ... with Ruby and RSpec!</title>
      <description>&lt;p&gt;If you&amp;#8217;re writing C/C&amp;#043;&amp;#043; code, it&amp;#8217;s natural to write your unit tests in the same language (or use C&amp;#043;&amp;#043; for your C test code). All the well-known unit testing tools take this approach.&lt;/p&gt;


	&lt;p&gt;I think we can agree that neither language offers the best developer productivity among all the language choices out there. Most of us use either language because of perceived performance requirements, institutional and industry tradition, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;There&amp;#8217;s growing interest, however, in mixing languages, tools, and &lt;em&gt;paradigms&lt;/em&gt; to get the best tool for a particular job. &amp;lt;shameless-plug&amp;gt;I&amp;#8217;m giving a talk March 7&lt;sup&gt;th&lt;/sup&gt; at &lt;a href="https://www.cmpevents.com/SDw8/a.asp?option=C&amp;#38;V=11&amp;#38;SessID=6102"&gt;SD West&lt;/a&gt; on this very topic, called &lt;a href="https://www.cmpevents.com/SDw8/a.asp?option=C&amp;#38;V=11&amp;#38;SessID=6102"&gt;Polyglot and Poly-Paradigm Programming&lt;/a&gt; &amp;lt;/shameless-plug&amp;gt;.&lt;/p&gt;


	&lt;p&gt;So, why not use a more productive language for your C or C&amp;#043;&amp;#043; unit tests? You have more freedom in your development chores than what&amp;#8217;s required for production. Why not use Ruby&amp;#8217;s &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt;, a &lt;a href="http://behaviour-driven.org/"&gt;Behavior-Driven Development&lt;/a&gt; tool for acceptance and unit testing? Or, you could use Ruby&amp;#8217;s version of &lt;a href="http://www.junit.org"&gt;JUnit&lt;/a&gt;, called &lt;a href="http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html"&gt;Test::Unit&lt;/a&gt;. The hard part is integrating Ruby and C/C&amp;#043;&amp;#043;. If you&amp;#8217;ve been looking for an excuse to bring Ruby (or Tcl or Python or Java or&amp;#8230;) into your C/C&amp;#043;&amp;#043; environment, starting with development tasks is usually the path of least resistance.&lt;/p&gt;


	&lt;p&gt;I did some experimenting over the last few days to integrate &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt;  using &lt;a href="http://www.swig.org"&gt;&lt;span class="caps"&gt;SWIG&lt;/span&gt;&lt;/a&gt; (Simplified Wrapper and Interface Generator), a tool for bridging libraries written in C and C&amp;#043;&amp;#043; to other languages, like Ruby. The &lt;a href="http://www.swig.org/Doc1.3/Ruby.html"&gt;Ruby section&lt;/a&gt; of the &lt;span class="caps"&gt;SWIG&lt;/span&gt; manual was very helpful.&lt;/p&gt;


	&lt;h2&gt;My Proof-of-Concept Code&lt;/h2&gt;


	&lt;p&gt;Here is a zip file of my experiment: &lt;a href="http://blog.objectmentor.com/files/rspec_for_cpp.zip" title="rspec_for_cpp.zip"&gt;rspec_for_cpp.zip&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;This is far from a complete and working solution, but I think it shows promise. See the &lt;strong&gt;Current Limitations&lt;/strong&gt; section below for details.&lt;/p&gt;


	&lt;p&gt;Unzip the file into a directory. I&amp;#8217;ll assume you named it &lt;code&gt;rspec_for_cpp&lt;/code&gt;. You need to have &lt;code&gt;gmake&lt;/code&gt;, &lt;code&gt;gcc&lt;/code&gt;, &lt;span class="caps"&gt;SWIG&lt;/span&gt; and Ruby installed, along with the RSpec &amp;#8220;gem&amp;#8221;. Right now, it only builds on &lt;span class="caps"&gt;OS X&lt;/span&gt; and Linux (at least the configurations on my machines running those OS&amp;#8217;s &amp;#8211; see the discussion below). To run the build, use the following commands:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        $ cd rspec_for_cpp/cpp
        $ make 
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;You should see it finish with the lines&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        ( cd ../spec; spec *_spec.rb )
        .........

        Finished in 0.0***** seconds

        9 examples, 0 failures
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Congratulations, you&amp;#8217;ve just tested some C and C&amp;#043;&amp;#043; code with RSpec! (Or, if you didn&amp;#8217;t succeed, see the notes in the &lt;code&gt;Makefile&lt;/code&gt; and the discussion below.)&lt;/p&gt;


	&lt;h2&gt;The Details&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;ll briefly walk you through the files in the zip and the key steps required to make it all work.&lt;/p&gt;


	&lt;h3&gt;&lt;code&gt;cexample.h&lt;/code&gt;&lt;/h3&gt;


	&lt;p&gt;Here is a simple C header file.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        /* cexample.h */
        #ifndef CEXAMPLE_H
        #define CEXAMPLE_H
        #ifdef __cplusplus
         extern "C" {
        #endif
        char* returnString(char* input);
        double returnDouble(int input);
        void  doNothing();

        #ifdef __cplusplus
         }
        #endif
        #endif
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Of course, in a pure C shop, you won&amp;#8217;t need the &lt;code&gt;#ifdef __cplusplus&lt;/code&gt; stuff. I found this was essential in my experiment when I mixed C and C&amp;#043;&amp;#043;, as you might expect.&lt;/p&gt;


	&lt;h3&gt;&lt;code&gt;cpp/cexample.c&lt;/code&gt;&lt;/h3&gt;


	&lt;p&gt;Here is the corresponding C source file.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        /* cexample.h */

        char* returnString(char* input) {
            return input;
        }

        double returnDouble(int input) {
            return (double) input;
        }

        void  doNothing() {}
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h3&gt;&lt;code&gt;cpp/CppExample.h&lt;/code&gt;&lt;/h3&gt;


	&lt;p&gt;Here is a C&amp;#043;&amp;#043; header file.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        #ifndef CPPEXAMPLE_H
        #define CPPEXAMPLE_H

        #include &amp;lt;string&amp;gt;

        class CppExample 
        {
        public:
            CppExample();
            CppExample(const CppExample&amp;#38; foo);
            CppExample(const char* title, int flag);
            virtual ~CppExample();

            const char* title() const;
            void        title(const char* title);
            int         flag() const;
            void        flag(int value);

            static int countOfCppExamples();
        private:
            std::string _title;
            int         _flag;
        };

        #endif
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h3&gt;&lt;code&gt;cpp/CppExample.cpp&lt;/code&gt;&lt;/h3&gt;


	&lt;p&gt;Here is the corresponding C&amp;#043;&amp;#043; source file.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        #include "CppExample.h" 

        CppExample::CppExample() : _title("") {}
        CppExample::CppExample(const CppExample&amp;#38; foo): _title(foo._title) {}
        CppExample::CppExample(const char* title, int flag) : _title(title), _flag(flag) {}
        CppExample::~CppExample() {}

        const char* CppExample::title() const { return _title.c_str(); }
        void        CppExample::title(const char* name) { _title = name; }

        int  CppExample::flag() const { return _flag; }
        void CppExample::flag(int value) { _flag = value; }

        int CppExample::countOfCppExamples() { return 1; }
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;h3&gt;&lt;code&gt;cpp/example.i&lt;/code&gt;&lt;/h3&gt;


	&lt;p&gt;Typically in &lt;span class="caps"&gt;SWIG&lt;/span&gt;, you specify a &lt;code&gt;.i&lt;/code&gt; file to the &lt;code&gt;swig&lt;/code&gt; command to define the &lt;em&gt;module&lt;/em&gt; that wraps the classes and global functions, which classes and functions to expose to the target language (usually all in our case), and other assorted customization options, which are discussed in the &lt;a href="http://www.swig.org/Doc1.3"&gt;&lt;span class="caps"&gt;SWIG&lt;/span&gt; manual&lt;/a&gt;. I&amp;#8217;ll show the &lt;code&gt;swig&lt;/code&gt; command in a minute. For now, note that I&amp;#8217;m going to generate an &lt;code&gt;example_wrap.cpp&lt;/code&gt; file that will function as the bridge between the languages.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s my &lt;code&gt;example.i&lt;/code&gt;, where I named the module &lt;code&gt;example&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        %module example
        %{
            #include "cexample.h" 
            #include "CppExample.h"    
        %}
        %include "cexample.h" 
        %include "CppExample.h" 
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;It looks odd to have header files appear twice. The code inside the &lt;code&gt;%{...%}&lt;/code&gt; (with a &amp;#8217;#&amp;#8217; before each &lt;code&gt;include&lt;/code&gt;) are standard C and C&amp;#043;&amp;#043; statements, &lt;em&gt;etc.&lt;/em&gt; that will be inserted verbatim into the generated &amp;#8220;wrapper&amp;#8221; file, &lt;code&gt;example_wrap.cpp&lt;/code&gt;, so that file will compile when it references anything declared in the header files. The second case, with a &amp;#8217;%&amp;#8217; before the &lt;code&gt;include&lt;/code&gt; statements&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;, tells &lt;span class="caps"&gt;SWIG&lt;/span&gt; to make all the declarations in those header files available to the target language. (You can be more selective, if you prefer&amp;#8230;)&lt;/p&gt;


	&lt;p&gt;Following Ruby conventions, the Ruby plugin for &lt;span class="caps"&gt;SWIG&lt;/span&gt; automatically names the module with an upper case first letter (&lt;code&gt;Example&lt;/code&gt;), but you use &lt;code&gt;require 'example'&lt;/code&gt; to include it, as we&amp;#8217;ll see shortly.&lt;/p&gt;


	&lt;h3&gt;Building&lt;/h3&gt;


	&lt;p&gt;See the &lt;code&gt;cpp/Makefile&lt;/code&gt; for the gory details. In a nutshell, you run the &lt;code&gt;swig&lt;/code&gt; command like this.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        swig -c++ -ruby -Wall -o example_wrap.cpp example.i
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Next, you create a dynamically-linked library, as appropriate for your platform, so the Ruby interpreter can load the module dynamically when required. The &lt;code&gt;Makefile&lt;/code&gt; can do this for Linux and &lt;span class="caps"&gt;OS X&lt;/span&gt; platforms. See the &lt;a href="http://www.swig.org/Doc1.3/Ruby.html"&gt;Ruby section&lt;/a&gt; of the &lt;span class="caps"&gt;SWIG&lt;/span&gt; manual for Windows specifics.&lt;/p&gt;


	&lt;p&gt;If you test-drive your code, which tends to drive you towards minimally-coupled &amp;#8220;modules&amp;#8221;, then you can keep your libraries and build times small, which will make the build and test cycle very fast!&lt;/p&gt;


	&lt;h3&gt;&lt;code&gt;spec/cexample_spec.rb&lt;/code&gt; and &lt;code&gt;spec/cppexample_spec.rb&lt;/code&gt;&lt;/h3&gt;


	&lt;p&gt;Finally, here are the RSpec files that exercise the C and C&amp;#043;&amp;#043; code. (Disclaimer: these aren&amp;#8217;t the best spec files I&amp;#8217;ve ever written. For one thing, they don&amp;#8217;t exercise all the &lt;code&gt;CppExample&lt;/code&gt; methods! So sue me&amp;#8230; :)&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
        require File.dirname(__FILE__) + '/spec_helper'
        require 'example'

        describe "Example (C functions)" do
          it "should be a constant on Module" do
            Module.constants.should include('Example')
          end
          it "should have the methods defined in the C header file" do
            Example.methods.should include('returnString')
            Example.methods.should include('returnDouble')
            Example.methods.should include('doNothing')
          end
        end

        describe Example, ".returnString" do
          it "should return the input char * string as a Ruby string unchanged" do
            Example.returnString("bar!").should == "bar!" 
          end  
        end

        describe Example, ".returnDouble" do
          it "should return the input integer as a double" do
            Example.returnDouble(10).should == 10.0
          end
        end

        describe Example, ".doNothing" do
          it "should exist, but do nothing" do
            lambda { Example.doNothing }.should_not raise_error
          end
        end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;and&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
    require File.dirname(__FILE__) + '/spec_helper'
    require 'example'

    describe Example::CppExample do
      it "should be a constant on module Example" do
        Example.constants.should include('CppExample')
      end
    end

    describe Example::CppExample, ".new" do
      it "should create a new object of type CppExample" do
        example = Example::CppExample.new("example1", 1)
        example.title.should == "example1" 
        example.flag.should  == 1
      end
    end

    describe Example::CppExample, "#title(value)" do
      it "should set the title" do
        example = Example::CppExample.new("example1", 1)
        example.title("title2")
        example.title.should == "title2" 
      end
    end

    describe Example::CppExample, "#flag(value)" do
      it "should set the flag" do
        example = Example::CppExample.new("example1", 1)
        example.flag(2)
        example.flag.should == 2
      end
    end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;If you love RSpec like I do, this is a very compelling thing to see!&lt;/p&gt;


	&lt;p&gt;And now for the small print:&lt;/p&gt;


	&lt;h2&gt;Current Limitations&lt;/h2&gt;


	&lt;p&gt;As I said, this is just an experiment at this point. Volunteers to make this &lt;em&gt;battle-ready&lt;/em&gt; would be most welcome!&lt;/p&gt;


	&lt;h3&gt;General&lt;/h3&gt;


	&lt;h4&gt;The Example &lt;code&gt;Makefile&lt;/code&gt; File&lt;/h4&gt;


	&lt;p&gt;&lt;strong&gt;It Must Be Hand Edited for Each New or Renamed Source File&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;You&amp;#8217;ve probably already solved this problem for your own make files. Just merge in the example &lt;code&gt;Makefile&lt;/code&gt; to pick up the &lt;span class="caps"&gt;SWIG&lt;/span&gt;- and RSpec-related targets and rules.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;It Only Knows How to Build Shared Libraries for Mac &lt;span class="caps"&gt;OS X&lt;/span&gt; and Linux (and Not Very Well)&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Some definitions are probably unique to my &lt;span class="caps"&gt;OS X&lt;/span&gt; and Linux machines. Windows is not supported at all. However, this is also easy rectify. Start with the notes in the &lt;code&gt;Makefile&lt;/code&gt; itself.&lt;/p&gt;


	&lt;h4&gt;The &lt;code&gt;module.i&lt;/code&gt; File Must Be Hand Edited for Each File Change&lt;/h4&gt;


	&lt;p&gt;Since the format is simple, a make task could fill a template file with the changed list of source files during the build.&lt;/p&gt;


	&lt;h4&gt;Better Automation&lt;/h4&gt;


	&lt;p&gt;It should be straightforward to provide scripts, &lt;span class="caps"&gt;IDE&lt;/span&gt;/Editor shortcuts, &lt;em&gt;etc&lt;/em&gt;. that automate some of the tasks of adding new methods and classes to your C and C&amp;#043;&amp;#043; code when you introduce them  &lt;i&gt;first&lt;/i&gt; in your &amp;#8220;spec&amp;#8221; files. (The true &lt;span class="caps"&gt;TDD&lt;/span&gt; way, of course.)&lt;/p&gt;


	&lt;h3&gt;Specific Issues for C Code Testing&lt;/h3&gt;


	&lt;p&gt;I don&amp;#8217;t know of any other C-specific issues, so unit testing with Ruby is most viable today for C code. Although I haven&amp;#8217;t experimented extensively, C functions and variables are easily mapped by &lt;span class="caps"&gt;SWIG&lt;/span&gt; to a Ruby module. The &lt;a href="http://www.swig.org/Doc1.3/Ruby.html"&gt;Ruby section&lt;/a&gt; of the &lt;span class="caps"&gt;SWIG&lt;/span&gt; manual discusses this mapping in some detail.&lt;/p&gt;


	&lt;h3&gt;Specific Issues for C&amp;#043;&amp;#043; Code Testing&lt;/h3&gt;


	&lt;p&gt;More work will be required to make this viable. It&amp;#8217;s important to note that &lt;span class="caps"&gt;SWIG&lt;/span&gt; cannot handle all C&amp;#043;&amp;#043; constructs (although there are workarounds for most issues, if you&amp;#8217;re committed to this approach&amp;#8230;). For example, namespaces, nested classes, some template and some method overloading scenarios are not supported. The &lt;a href="http://www.swig.org/Doc1.3"&gt;&lt;span class="caps"&gt;SWIG&lt;/span&gt; manual&lt;/a&gt; has details.&lt;/p&gt;


	&lt;p&gt;Also, during my experiment, &lt;span class="caps"&gt;SWIG&lt;/span&gt; didn&amp;#8217;t seem to map &lt;code&gt;const std::string&amp;#38;&lt;/code&gt; objects in C&amp;#043;&amp;#043; method signatures to Ruby strings, as I would have expected (&lt;code&gt;char*&lt;/code&gt; worked fine).&lt;/p&gt;


	&lt;h2&gt;Is It a Viable Approach?&lt;/h2&gt;


	&lt;p&gt;Once the &lt;strong&gt;General&lt;/strong&gt; issues listed above are handled, I think this approach would work very well for C code. For C&amp;#043;&amp;#043; code, there are more issues that need to be addressed, and programmers who are committed to this strategy will need to tolerate some issues (or just use C&amp;#043;&amp;#043;-language tools for some scenarios).&lt;/p&gt;


	&lt;h2&gt;Conclusions: Making It Development-Team Ready&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;d like to see this approach pushed to its logical limit. I think it has the potential to really improve the productivity of C and C&amp;#043;&amp;#043; developers and the quality of their test coverage, by leveraging the productivity and power of dynamically-typed languages like Ruby. If you prefer, you could use Tcl, Python, even Java instead.&lt;/p&gt;


	&lt;h3&gt;License&lt;/h3&gt;


	&lt;p&gt;This code is complete open and free to use. Of course, use it at your own risk; I offer it without warranty, &lt;em&gt;etc.&lt;/em&gt;, &lt;em&gt;etc&lt;/em&gt;. When I polish it to the point of making it an &amp;#8220;official&amp;#8221; project, I will probably release under the Apache license.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; I spent a lot of time debugging problems because I had a &amp;#8217;#&amp;#8217; where I should have had a &amp;#8217;%&amp;#8217;! &lt;em&gt;Caveat emptor&lt;/em&gt;!&lt;/p&gt;</description>
      <pubDate>Mon, 04 Feb 2008 22:08:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:cba4dc23-cbce-4173-be6f-14c7847dcfea</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/02/04/unit-testing-c-and-c-with-ruby-and-rspec</link>
      <category>Embedded</category>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Agile Methods</category>
      <category>c</category>
      <category>RSpec</category>
      <category>Ruby</category>
      <category>TDD</category>
      <category>testing</category>
    </item>
    <item>
      <title>TDD for AspectJ Aspects</title>
      <description>&lt;p&gt;There was a query on the &lt;span class="caps"&gt;TDD&lt;/span&gt; mailing list about how to test drive aspects. Here is an edited version of my reply to that list.&lt;/p&gt;


	&lt;p&gt;Just as for regular classes, &lt;span class="caps"&gt;TDD&lt;/span&gt; can drive aspects to a better design.&lt;/p&gt;


	&lt;p&gt;Assume that I&amp;#8217;m testing a logging aspect that logs when certain methods are called. Here&amp;#8217;s the JUnit 4 test:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;package logging;
import static org.junit.Assert.*;
import org.junit.Test;
import app.TestApp;

public class LoggerTest {
    @Test
    public void FakeLoggerShouldBeCalledForAllMethodsOnTestClasses() {
        String message = "hello!";
        new TestApp().doFirst(message);
        assertTrue(FakeLogger.messageReceived().contains(message));
        String message2 = "World!";
        new TestApp().doSecond(message, message2);
        assertTrue(FakeLogger.messageReceived().contains(message));
    }
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Already, you might guess that &lt;code&gt;FakeLogger&lt;/code&gt; will be a test-only version of something, in this case, my logging aspect. Similarly, &lt;code&gt;TestApp&lt;/code&gt; is a simple class that I&amp;#8217;m using only for testing. You might choose to use one or more production classes, though.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;package app;
@Watchable
public class TestApp {
    public void doFirst(String message) {}
    public void doSecond(String message1, String message2) {}
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;and &lt;code&gt;@Watchable&lt;/code&gt; is a marker annotation that allows me to define pointcuts in my logger aspect without fragile coupling to concrete names, etc. You could also use an interface.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;package app;
public @interface Watchable {}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I made up &lt;code&gt;@Watchable&lt;/code&gt; as a way of marking classes where the public methods might be of &amp;#8220;interest&amp;#8221; to particular observers of some kind. It&amp;#8217;s analogous to the &lt;span class="caps"&gt;EJB 3&lt;/span&gt; annotations that mark classes as &amp;#8220;persistable&amp;#8221; without implying too many details of what that might mean.&lt;/p&gt;


	&lt;p&gt;Now, the actual logging is divided into an abstract base aspect and a test-only concrete &lt;a href="&lt;/p"&gt;sub-aspect&lt;/a&gt;&amp;gt;


&lt;pre&gt;&lt;code&gt;package logging;

import org.aspectj.lang.JoinPoint;
import app.Watchable;

abstract public aspect AbstractLogger {
    // Limit the scope to the packages and types you care about.
    public abstract pointcut scope();

    // Define how messages are actually logged.
    public abstract void logMessage(String message);

    // Notice the coupling is to the @Watchable abstraction.
    pointcut watch(Object object):
        scope() &amp;#38;&amp;#38; call(* (@Watchable *).*(..)) &amp;#38;&amp;#38; target(object);

    before(Object watchable): watch(watchable) {
        logMessage(makeLogMessage(thisJoinPoint));
    }

    public static String makeLogMessage(JoinPoint joinPoint) {
        StringBuffer buff = new StringBuffer();
        buff.append(joinPoint.toString()).append(", args = ");
        for (Object arg: joinPoint.getArgs())
            buff.append(arg.toString()).append(", ");
        return buff.toString();
    }
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;and&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;package logging;

public aspect FakeLogger extends AbstractLogger {
    // Only match on calls from the unit tests.
    public pointcut scope(): within(logging.*Test);

    public void logMessage(String message) {
        lastMessage = message; 
    }

    static String lastMessage = null;
    public static String messageReceived() {
        return lastMessage;
    }
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Pointcut"&gt;Pointcuts&lt;/a&gt; in aspects are like most other dependencies, best avoided ;) ... or at least minimized and based on abstractions, just like associations and inheritance relationships.&lt;/p&gt;


	&lt;p&gt;So, my test &amp;#8220;pressure&amp;#8221; drove the design in terms of where I needed abstraction in the Logger aspect: (i) how a message is actually logged and (ii) what classes get &amp;#8220;advised&amp;#8221; with logging behavior.&lt;/p&gt;


	&lt;p&gt;Just as for &lt;span class="caps"&gt;TDD&lt;/span&gt; of regular classes, the design ends up with minimized dependencies and flexibility (abstraction) where it&amp;#8217;s most useful.&lt;/p&gt;


	&lt;p&gt;I can now implement the real, concrete logger, which will also be a sub-aspect of &lt;code&gt;AbstractLogger&lt;/code&gt;. It will define the &lt;code&gt;scope()&lt;/code&gt; pointcut to be a larger section of the system and it will send the message to the real logging subsystem.&lt;/p&gt;</description>
      <pubDate>Tue, 02 Oct 2007 11:34:24 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:dba855da-6d02-428b-a74b-3f90ff03cdd4</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/10/02/tdd-for-aspectj-aspects</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>Design Principles</category>
      <category>TDD</category>
      <category>aspects</category>
      <category>AspectJ</category>
    </item>
    <item>
      <title>Why you have time for TDD (but may not know it yet...)</title>
      <description>&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; Updated 9/30/2007 to improve the graphs and to clarify the content.&lt;/p&gt;


	&lt;p&gt;A common objection to &lt;span class="caps"&gt;TDD&lt;/span&gt; is this; &amp;#8220;We don&amp;#8217;t have time to write so many tests. We don&amp;#8217;t even have enough time to write features!&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s why people who say this probably already have enough time in the (real) schedule, they just don&amp;#8217;t know it yet.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s start with an idealized &lt;a href="http://www.controlchaos.com/about/burndown.php"&gt;Scrum-style &amp;#8220;burn-down chart&amp;#8221;&lt;/a&gt; for a fictional project run in a &amp;#8220;traditional&amp;#8221; way (even though traditional projects don&amp;#8217;t use burn-down charts&amp;#8230;).&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/ideal_timeline2.png" border="0" height="352" width="575" alt="ideal_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;We have time increasing on the x axis and the number of &amp;#8220;features&amp;#8221; &lt;em&gt;remaining to implement&lt;/em&gt; on the y axis (it could also be hours or &amp;#8220;story points&amp;#8221; remaining). During a project, a nice feature of burn-down charts is that you can extend the line to see where it intersects the x axis, which is a rough indicator of when you&amp;#8217;ll actually finish.&lt;/p&gt;


	&lt;p&gt;The optimistic planners for our fictional project plan to give the software to QA near the end of the project. They expect QA to find nothing serious, so the release will occur soon thereafter on date &lt;strong&gt;T0&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;Of course, it never works out that way:&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/actual_timeline2.png" border="0" height="352" width="575" alt="actual_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;The red line is the actual effort for our fictional project. It&amp;#8217;s quite natural for the planned list of features to change as the team reacts to market changes, &lt;em&gt;etc.&lt;/em&gt;. This is why the line goes up sometimes (in &amp;#8220;good&amp;#8221; projects, too!). Since this is a &amp;#8220;traditional&amp;#8221; project, I&amp;#8217;m assuming that there are no automated tests that actually &lt;em&gt;prove&lt;/em&gt; that a given feature is really done. We&amp;#8217;re effectively running &amp;#8220;open loop&amp;#8221;, without the feedback of tests.&lt;/p&gt;


	&lt;p&gt;Inevitably, the project goes over budget and th planned QA drop comes late. Then things get ugly. Without our automated &lt;strong&gt;unit&lt;/strong&gt; tests, there are lots of little bugs in the code. Without our automated &lt;strong&gt;integration&lt;/strong&gt; tests, there are problems when the subsystems are run together. Without our &lt;strong&gt;acceptance&lt;/strong&gt; tests, the implemented features don&amp;#8217;t quite match the actual requirements for them.&lt;/p&gt;


	&lt;p&gt;Hence, a chaotic, end-of-project &amp;#8220;birthing&amp;#8221; period ensues, where QA reports a list of big and small problems, followed by a frantic effort (usually involving weekends&amp;#8230;) by the developers to address the problems, followed by another QA drop, followed by&amp;#8230;, and so forth.&lt;/p&gt;


	&lt;p&gt;Finally, out of exhaustion and because everyone else is angry at the painful schedule slip, the team declares &amp;#8220;victory&amp;#8221; and ships it, at time &lt;strong&gt;T1&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;ve all lived through projects like this one.&lt;/p&gt;


	&lt;p&gt;Now, if you remember your calculus classes (sorry to bring up painful memories), you will recall that the area under the curve is the total quantity of whatever the curve represents. So, the actual &lt;em&gt;total&lt;/em&gt; feature work required for our project corresponds to the area under the red line, while the planned work corresponds to the area under the black line. So, we really &lt;em&gt;did&lt;/em&gt; have more time than we originally thought.&lt;/p&gt;


	&lt;p&gt;Now consider a Test-Driven Development (TDD) project &lt;a href="#note1"&gt;[1]&lt;/a&gt;:&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/tdd_timeline2.png" border="0" height="348" width="564" alt="tdd_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;Here, the blue line is similar to the red line, at least early in the project. Now we have frequent &amp;#8220;milestones&amp;#8221; where we &lt;em&gt;verify the state&lt;/em&gt; of the project with the three kinds of automated tests mentioned above. Each milestone is the end of an iteration (usually 1-4 weeks apart). Not shown are the 5-minute &lt;span class="caps"&gt;TDD&lt;/span&gt; cycles and the feedback from the &lt;a href="http://martinfowler.com/articles/continuousIntegration.html"&gt;continuous integration&lt;/a&gt; process that does our builds and runs all our tests after every block of commits to version control (many times a day).&lt;/p&gt;


	&lt;p&gt;The graph suggests that the total amount of effort will be higher than the &lt;em&gt;expected&lt;/em&gt; effort without tests, which may be true &lt;a href="#note2"&gt;[2]&lt;/a&gt;. However, because of the constant feedback during the whole life of the project, we &lt;em&gt;really&lt;/em&gt; know where we actually are at any time. By measuring our progress in this way, we will know early whether or not we can meet the target date with the planned feature set. With early warnings, we can adjust accordingly, either dropping features or moving the target date, with relatively little pain. Whereas, without this feedback, we really don&amp;#8217;t &lt;em&gt;know&lt;/em&gt; what&amp;#8217;s done until something, &lt;em&gt;e.g.,&lt;/em&gt; the QA process, gives us that feedback. Hence, at time &lt;strong&gt;T0&lt;/strong&gt;, just before the big QA drop, the traditional project has little certainty about what features are really completed.&lt;/p&gt;


	&lt;p&gt;So, we&amp;#8217;ll experience less of the traditional end-of-project chaos, because there will be fewer surprises. Without the feedback from automated tests, QA is find lots of problems, causing the chaotic and painful end-of-project experience. Finding and trying to fix major problems late in the game can even kill a project.&lt;/p&gt;


	&lt;p&gt;So, &lt;span class="caps"&gt;TDD&lt;/span&gt; converts that unknown schedule time at the end into known time early in the project. You really &lt;strong&gt;do&lt;/strong&gt; have time for automated tests and your tests will make your projects more predictable and less painful at the end.&lt;/p&gt;


	&lt;p&gt;Note: I appreciate the early comments and questions that helped me clarify this post.&lt;/p&gt;


	&lt;p&gt;&lt;a name="note1"&gt;&lt;/a&gt;
[1] As one commenter remarked, this post doesn&amp;#8217;t actually make the case for &lt;span class="caps"&gt;TDD&lt;/span&gt; itself &lt;em&gt;vs.&lt;/em&gt; alternative &amp;#8220;test-heavy&amp;#8221; strategies, but I think it&amp;#8217;s pretty clear that &lt;span class="caps"&gt;TDD&lt;/span&gt; is the best of the known test-heavy strategies, as argued elsewhere.&lt;/p&gt;


	&lt;p&gt;&lt;a name="note2"&gt;&lt;/a&gt;
[2] There is some evidence that &lt;span class="caps"&gt;TDD&lt;/span&gt; and pair programming lead to &lt;em&gt;smaller&lt;/em&gt; applications, because they help avoid unnecessary features. Also, they provide constant feedback to the team, including the stake holders, on what the feature set should really be and which features are most important to complete first.&lt;/p&gt;</description>
      <pubDate>Sun, 30 Sep 2007 20:01:02 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:98c607d8-ce1b-4e35-aabd-8717de80bde6</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/30/why-you-have-time-for-tdd-but-may-not-know-it-yet</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>projects</category>
      <category>schedules</category>
      <category>TDD</category>
    </item>
    <item>
      <title>Why you have time for TDD (but may not know it yet...)</title>
      <description>&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; Updated 9/30/2007 to improve the graphs and to clarify the content.&lt;/p&gt;


	&lt;p&gt;A common objection to &lt;span class="caps"&gt;TDD&lt;/span&gt; is this; &amp;#8220;We don&amp;#8217;t have time to write so many tests. We don&amp;#8217;t even have enough time to write features!&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s why people who say this probably already have enough time in the (real) schedule, they just don&amp;#8217;t know it yet.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s start with an idealized &lt;a href="http://www.controlchaos.com/about/burndown.php"&gt;Scrum-style &amp;#8220;burn-down chart&amp;#8221;&lt;/a&gt; for a fictional project run in a &amp;#8220;traditional&amp;#8221; way (even though traditional projects don&amp;#8217;t use burn-down charts&amp;#8230;).&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/ideal_timeline2.png" border="0" height="352" width="575" alt="ideal_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;We have time increasing on the x axis and the number of &amp;#8220;features&amp;#8221; &lt;em&gt;remaining to implement&lt;/em&gt; on the y axis (it could also be hours or &amp;#8220;story points&amp;#8221; remaining). During a project, a nice feature of burn-down charts is that you can extend the line to see where it intersects the x axis, which is a rough indicator of when you&amp;#8217;ll actually finish.&lt;/p&gt;


	&lt;p&gt;The optimistic planners for our fictional project plan to give the software to QA near the end of the project. They expect QA to find nothing serious, so the release will occur soon thereafter on date &lt;strong&gt;T0&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;Of course, it never works out that way:&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/actual_timeline2.png" border="0" height="352" width="575" alt="actual_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;The red line is the actual effort for our fictional project. It&amp;#8217;s quite natural for the planned list of features to change as the team reacts to market changes, &lt;em&gt;etc.&lt;/em&gt;. This is why the line goes up sometimes (in &amp;#8220;good&amp;#8221; projects, too!). Since this is a &amp;#8220;traditional&amp;#8221; project, I&amp;#8217;m assuming that there are no automated tests that actually &lt;em&gt;prove&lt;/em&gt; that a given feature is really done. We&amp;#8217;re effectively running &amp;#8220;open loop&amp;#8221;, without the feedback of tests.&lt;/p&gt;


	&lt;p&gt;Inevitably, the project goes over budget and th planned QA drop comes late. Then things get ugly. Without our automated &lt;strong&gt;unit&lt;/strong&gt; tests, there are lots of little bugs in the code. Without our automated &lt;strong&gt;integration&lt;/strong&gt; tests, there are problems when the subsystems are run together. Without our &lt;strong&gt;acceptance&lt;/strong&gt; tests, the implemented features don&amp;#8217;t quite match the actual requirements for them.&lt;/p&gt;


	&lt;p&gt;Hence, a chaotic, end-of-project &amp;#8220;birthing&amp;#8221; period ensues, where QA reports a list of big and small problems, followed by a frantic effort (usually involving weekends&amp;#8230;) by the developers to address the problems, followed by another QA drop, followed by&amp;#8230;, and so forth.&lt;/p&gt;


	&lt;p&gt;Finally, out of exhaustion and because everyone else is angry at the painful schedule slip, the team declares &amp;#8220;victory&amp;#8221; and ships it, at time &lt;strong&gt;T1&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;ve all lived through projects like this one.&lt;/p&gt;


	&lt;p&gt;Now, if you remember your calculus classes (sorry to bring up painful memories), you will recall that the area under the curve is the total quantity of whatever the curve represents. So, the actual &lt;em&gt;total&lt;/em&gt; feature work required for our project corresponds to the area under the red line, while the planned work corresponds to the area under the black line. So, we really &lt;em&gt;did&lt;/em&gt; have more time than we originally thought.&lt;/p&gt;


	&lt;p&gt;Now consider a Test-Driven Development (TDD) project &lt;a href="#note1"&gt;[1]&lt;/a&gt;:&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/tdd_timeline2.png" border="0" height="348" width="564" alt="tdd_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;Here, the blue line is similar to the red line, at least early in the project. Now we have frequent &amp;#8220;milestones&amp;#8221; where we &lt;em&gt;verify the state&lt;/em&gt; of the project with the three kinds of automated tests mentioned above. Each milestone is the end of an iteration (usually 1-4 weeks apart). Not shown are the 5-minute &lt;span class="caps"&gt;TDD&lt;/span&gt; cycles and the feedback from the &lt;a href="http://martinfowler.com/articles/continuousIntegration.html"&gt;continuous integration&lt;/a&gt; process that does our builds and runs all our tests after every block of commits to version control (many times a day).&lt;/p&gt;


	&lt;p&gt;The graph suggests that the total amount of effort will be higher than the &lt;em&gt;expected&lt;/em&gt; effort without tests, which may be true &lt;a href="#note2"&gt;[2]&lt;/a&gt;. However, because of the constant feedback during the whole life of the project, we &lt;em&gt;really&lt;/em&gt; know where we actually are at any time. By measuring our progress in this way, we will know early whether or not we can meet the target date with the planned feature set. With early warnings, we can adjust accordingly, either dropping features or moving the target date, with relatively little pain. Whereas, without this feedback, we really don&amp;#8217;t &lt;em&gt;know&lt;/em&gt; what&amp;#8217;s done until something, &lt;em&gt;e.g.,&lt;/em&gt; the QA process, gives us that feedback. Hence, at time &lt;strong&gt;T0&lt;/strong&gt;, just before the big QA drop, the traditional project has little certainty about what features are really completed.&lt;/p&gt;


	&lt;p&gt;So, we&amp;#8217;ll experience less of the traditional end-of-project chaos, because there will be fewer surprises. Without the feedback from automated tests, QA is find lots of problems, causing the chaotic and painful end-of-project experience. Finding and trying to fix major problems late in the game can even kill a project.&lt;/p&gt;


	&lt;p&gt;So, &lt;span class="caps"&gt;TDD&lt;/span&gt; converts that unknown schedule time at the end into known time early in the project. You really &lt;strong&gt;do&lt;/strong&gt; have time for automated tests and your tests will make your projects more predictable and less painful at the end.&lt;/p&gt;


	&lt;p&gt;Note: I appreciate the early comments and questions that helped me clarify this post.&lt;/p&gt;


	&lt;p&gt;&lt;a name="note1"&gt;&lt;/a&gt;
[1] As one commenter remarked, this post doesn&amp;#8217;t actually make the case for &lt;span class="caps"&gt;TDD&lt;/span&gt; itself &lt;em&gt;vs.&lt;/em&gt; alternative &amp;#8220;test-heavy&amp;#8221; strategies, but I think it&amp;#8217;s pretty clear that &lt;span class="caps"&gt;TDD&lt;/span&gt; is the best of the known test-heavy strategies, as argued elsewhere.&lt;/p&gt;


	&lt;p&gt;&lt;a name="note2"&gt;&lt;/a&gt;
[2] There is some evidence that &lt;span class="caps"&gt;TDD&lt;/span&gt; and pair programming lead to &lt;em&gt;smaller&lt;/em&gt; applications, because they help avoid unnecessary features. Also, they provide constant feedback to the team, including the stake holders, on what the feature set should really be and which features are most important to complete first.&lt;/p&gt;</description>
      <pubDate>Sun, 30 Sep 2007 20:01:02 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:98c607d8-ce1b-4e35-aabd-8717de80bde6</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/30/why-you-have-time-for-tdd-but-may-not-know-it-yet</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>projects</category>
      <category>schedules</category>
      <category>TDD</category>
    </item>
    <item>
      <title>Why you have time for TDD (but may not know it yet...)</title>
      <description>&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; Updated 9/30/2007 to improve the graphs and to clarify the content.&lt;/p&gt;


	&lt;p&gt;A common objection to &lt;span class="caps"&gt;TDD&lt;/span&gt; is this; &amp;#8220;We don&amp;#8217;t have time to write so many tests. We don&amp;#8217;t even have enough time to write features!&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s why people who say this probably already have enough time in the (real) schedule, they just don&amp;#8217;t know it yet.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s start with an idealized &lt;a href="http://www.controlchaos.com/about/burndown.php"&gt;Scrum-style &amp;#8220;burn-down chart&amp;#8221;&lt;/a&gt; for a fictional project run in a &amp;#8220;traditional&amp;#8221; way (even though traditional projects don&amp;#8217;t use burn-down charts&amp;#8230;).&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/ideal_timeline2.png" border="0" height="352" width="575" alt="ideal_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;We have time increasing on the x axis and the number of &amp;#8220;features&amp;#8221; &lt;em&gt;remaining to implement&lt;/em&gt; on the y axis (it could also be hours or &amp;#8220;story points&amp;#8221; remaining). During a project, a nice feature of burn-down charts is that you can extend the line to see where it intersects the x axis, which is a rough indicator of when you&amp;#8217;ll actually finish.&lt;/p&gt;


	&lt;p&gt;The optimistic planners for our fictional project plan to give the software to QA near the end of the project. They expect QA to find nothing serious, so the release will occur soon thereafter on date &lt;strong&gt;T0&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;Of course, it never works out that way:&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/actual_timeline2.png" border="0" height="352" width="575" alt="actual_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;The red line is the actual effort for our fictional project. It&amp;#8217;s quite natural for the planned list of features to change as the team reacts to market changes, &lt;em&gt;etc.&lt;/em&gt;. This is why the line goes up sometimes (in &amp;#8220;good&amp;#8221; projects, too!). Since this is a &amp;#8220;traditional&amp;#8221; project, I&amp;#8217;m assuming that there are no automated tests that actually &lt;em&gt;prove&lt;/em&gt; that a given feature is really done. We&amp;#8217;re effectively running &amp;#8220;open loop&amp;#8221;, without the feedback of tests.&lt;/p&gt;


	&lt;p&gt;Inevitably, the project goes over budget and th planned QA drop comes late. Then things get ugly. Without our automated &lt;strong&gt;unit&lt;/strong&gt; tests, there are lots of little bugs in the code. Without our automated &lt;strong&gt;integration&lt;/strong&gt; tests, there are problems when the subsystems are run together. Without our &lt;strong&gt;acceptance&lt;/strong&gt; tests, the implemented features don&amp;#8217;t quite match the actual requirements for them.&lt;/p&gt;


	&lt;p&gt;Hence, a chaotic, end-of-project &amp;#8220;birthing&amp;#8221; period ensues, where QA reports a list of big and small problems, followed by a frantic effort (usually involving weekends&amp;#8230;) by the developers to address the problems, followed by another QA drop, followed by&amp;#8230;, and so forth.&lt;/p&gt;


	&lt;p&gt;Finally, out of exhaustion and because everyone else is angry at the painful schedule slip, the team declares &amp;#8220;victory&amp;#8221; and ships it, at time &lt;strong&gt;T1&lt;/strong&gt;.&lt;/p&gt;


	&lt;p&gt;We&amp;#8217;ve all lived through projects like this one.&lt;/p&gt;


	&lt;p&gt;Now, if you remember your calculus classes (sorry to bring up painful memories), you will recall that the area under the curve is the total quantity of whatever the curve represents. So, the actual &lt;em&gt;total&lt;/em&gt; feature work required for our project corresponds to the area under the red line, while the planned work corresponds to the area under the black line. So, we really &lt;em&gt;did&lt;/em&gt; have more time than we originally thought.&lt;/p&gt;


	&lt;p&gt;Now consider a Test-Driven Development (TDD) project &lt;a href="#note1"&gt;[1]&lt;/a&gt;:&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://blog.objectmentor.com/files/tdd_timeline2.png" border="0" height="348" width="564" alt="tdd_timeline2.png" align="center" /&gt;
&lt;/p&gt;

	&lt;p&gt;Here, the blue line is similar to the red line, at least early in the project. Now we have frequent &amp;#8220;milestones&amp;#8221; where we &lt;em&gt;verify the state&lt;/em&gt; of the project with the three kinds of automated tests mentioned above. Each milestone is the end of an iteration (usually 1-4 weeks apart). Not shown are the 5-minute &lt;span class="caps"&gt;TDD&lt;/span&gt; cycles and the feedback from the &lt;a href="http://martinfowler.com/articles/continuousIntegration.html"&gt;continuous integration&lt;/a&gt; process that does our builds and runs all our tests after every block of commits to version control (many times a day).&lt;/p&gt;


	&lt;p&gt;The graph suggests that the total amount of effort will be higher than the &lt;em&gt;expected&lt;/em&gt; effort without tests, which may be true &lt;a href="#note2"&gt;[2]&lt;/a&gt;. However, because of the constant feedback during the whole life of the project, we &lt;em&gt;really&lt;/em&gt; know where we actually are at any time. By measuring our progress in this way, we will know early whether or not we can meet the target date with the planned feature set. With early warnings, we can adjust accordingly, either dropping features or moving the target date, with relatively little pain. Whereas, without this feedback, we really don&amp;#8217;t &lt;em&gt;know&lt;/em&gt; what&amp;#8217;s done until something, &lt;em&gt;e.g.,&lt;/em&gt; the QA process, gives us that feedback. Hence, at time &lt;strong&gt;T0&lt;/strong&gt;, just before the big QA drop, the traditional project has little certainty about what features are really completed.&lt;/p&gt;


	&lt;p&gt;So, we&amp;#8217;ll experience less of the traditional end-of-project chaos, because there will be fewer surprises. Without the feedback from automated tests, QA is find lots of problems, causing the chaotic and painful end-of-project experience. Finding and trying to fix major problems late in the game can even kill a project.&lt;/p&gt;


	&lt;p&gt;So, &lt;span class="caps"&gt;TDD&lt;/span&gt; converts that unknown schedule time at the end into known time early in the project. You really &lt;strong&gt;do&lt;/strong&gt; have time for automated tests and your tests will make your projects more predictable and less painful at the end.&lt;/p&gt;


	&lt;p&gt;Note: I appreciate the early comments and questions that helped me clarify this post.&lt;/p&gt;


	&lt;p&gt;&lt;a name="note1"&gt;&lt;/a&gt;
[1] As one commenter remarked, this post doesn&amp;#8217;t actually make the case for &lt;span class="caps"&gt;TDD&lt;/span&gt; itself &lt;em&gt;vs.&lt;/em&gt; alternative &amp;#8220;test-heavy&amp;#8221; strategies, but I think it&amp;#8217;s pretty clear that &lt;span class="caps"&gt;TDD&lt;/span&gt; is the best of the known test-heavy strategies, as argued elsewhere.&lt;/p&gt;


	&lt;p&gt;&lt;a name="note2"&gt;&lt;/a&gt;
[2] There is some evidence that &lt;span class="caps"&gt;TDD&lt;/span&gt; and pair programming lead to &lt;em&gt;smaller&lt;/em&gt; applications, because they help avoid unnecessary features. Also, they provide constant feedback to the team, including the stake holders, on what the feature set should really be and which features are most important to complete first.&lt;/p&gt;</description>
      <pubDate>Sun, 30 Sep 2007 20:01:02 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:98c607d8-ce1b-4e35-aabd-8717de80bde6</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/30/why-you-have-time-for-tdd-but-may-not-know-it-yet</link>
      <category>Dean's Deprecations</category>
      <category>Agile Methods</category>
      <category>projects</category>
      <category>schedules</category>
      <category>TDD</category>
    </item>
    <item>
      <title>ANN: Talks at the Chicago Ruby Users Group (Oct. 1, Dec. 3)</title>
      <description>&lt;p&gt;I&amp;#8217;m speaking on &lt;a href="http://aquarium.rubyforge.org"&gt;Aquarium&lt;/a&gt; this Monday night (Oct. 1st). Details are &lt;a href="http://chirb.org"&gt;here&lt;/a&gt;. David Chelimsky will also be talking about new developments in RSpec and RBehave.&lt;/p&gt;


	&lt;p&gt;At the Dec. 3 Chirb meeting, the two of us are reprising our Agile 2007 tutorial &lt;em&gt;Ruby&amp;#8217;s Secret Sauce: Metaprogramming&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Please join us!&lt;/p&gt;</description>
      <pubDate>Sat, 29 Sep 2007 09:52:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:430c0979-4e0c-4a0c-8afd-57b3579618d1</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/29/ann-talks-at-the-chicago-ruby-users-group-oct-1-dec-3</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>Dynamic Languages</category>
      <category>Ruby</category>
      <category>aquarium</category>
      <category>Metaprogramming</category>
      <category>AOP</category>
      <category>aspects</category>
      <category>Chirb</category>
    </item>
    <item>
      <title>ANN: OOPSLA Tutorial on &amp;quot;Principles of Aspect-Oriented Design in Java and AspectJ&amp;quot;</title>
      <description>&lt;p&gt;I&amp;#8217;m doing a tutorial on aspect-oriented design principles with examples in Java and AspectJ at &lt;span class="caps"&gt;OOPSLA&lt;/span&gt; this year (October 21st). You can find a description &lt;a href="http://www.oopsla.org/oopsla2007/index.php?page=sub/&amp;#38;id=90"&gt;here&lt;/a&gt;. I believe Friday, 9/14, is the last day for early, discounted registration, so sign up now!&lt;/p&gt;


	&lt;p&gt;A short presentation on the same subject can be found &lt;a href="http://aspectprogramming.com/papers/AOPIntroAndPrinciplesTalk.pdf"&gt;here&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 13 Sep 2007 11:34:29 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:bf38657f-7056-46cd-bd52-5135540e5e6a</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/13/ann-oopsla-tutorial-on-principles-of-aspect-oriented-design-in-java-and-aspectj</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>Design Principles</category>
      <category>AOP</category>
      <category>design</category>
      <category>oopsla</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Why we write code and don't just draw diagrams</title>
      <description>&lt;p&gt;Advocates of graphical notations have long hoped we would reach the point were we only draw diagrams and don&amp;#8217;t write textual code. There have even been a few visual programming environments that have come and gone over the years.&lt;/p&gt;


	&lt;p&gt;If &lt;i&gt;a picture is worth a thousand words&lt;/i&gt;, then why hasn&amp;#8217;t this happened?&lt;/p&gt;


	&lt;p&gt;What that phrase really means is that we get the &amp;#8220;gist&amp;#8221; or the &amp;#8220;gestalt&amp;#8221; of a situation when we look at a picture, but nothing expresses the intricate details like text, the 1000 words. Since computers are literal-minded and don&amp;#8217;t &amp;#8220;do gist&amp;#8221;, they require those details spelled out explicitly.&lt;/p&gt;


	&lt;p&gt;Well, couldn&amp;#8217;t we still do that with a sufficiently expressive graphical notation? Certainly, but then we run into the pragmatic issue that typing textual details will &lt;i&gt;always&lt;/i&gt; be faster than drawing them.&lt;/p&gt;


	&lt;p&gt;I came to this realization a few years ago when I worked for a Well Known Company developing &lt;span class="caps"&gt;UML&lt;/span&gt;-based tools for Java developers. The tool&amp;#8217;s UI could have been more efficient, but there was no way to beat the speed of typing text.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s also true that some languages are rather verbose. This is one of the ways in which Domain-Specific Languages (DSL&amp;#8217;s) are going to be increasingly important. A well-designed &lt;span class="caps"&gt;DSL&lt;/span&gt; will let you express those high-level concepts succinctly.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m not claiming that there is no place for graphical representations. &lt;span class="caps"&gt;UML&lt;/span&gt; is great for those quick design sessions, when you&amp;#8217;re strategizing at a high level. Also, the easiest way to find component dependency cycles is to see them graphically.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m also not discounting those scenarios where a diagram-driven approach actually works. I&amp;#8217;ve heard of some success developing control systems that are predominantly well-defined, complex state machines.&lt;/p&gt;


	&lt;p&gt;Still, for the general case, code written in succinct languages with well-designed &lt;span class="caps"&gt;API&lt;/span&gt;&amp;#8217;s and &lt;span class="caps"&gt;DSL&lt;/span&gt;&amp;#8217;s will trump a diagram-driven approach.&lt;/p&gt;</description>
      <pubDate>Thu, 06 Sep 2007 10:45:59 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:7b916f25-17c3-49e3-88dc-3e4ba9fcf8c6</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/06/why-we-write-code-and-dont-just-draw-diagrams</link>
      <category>Dean's Deprecations</category>
      <category>UML</category>
      <category>diagrams</category>
      <category>text</category>
      <category>code</category>
      <category>design</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/8804</trackback:ping>
    </item>
    <item>
      <title>CJUG West 9/6/07: Aspect-Oriented Programming and Software Design</title>
      <description>&lt;p&gt;I&amp;#8217;m giving a talk at the &lt;a href="http://www.cjug.org/Wiki.jsp?page=Welcome"&gt;Chicago Java User&amp;#8217;s Group West&lt;/a&gt; meeting this Thursday at 6:30 PM. The topic is &lt;a href="http://www.cjug.org/Wiki.jsp?page=2007.09.06.west"&gt;Aspect-Oriented Programming and Software Design in Java and AspectJ&lt;/a&gt;. I&amp;#8217;ll briefly describe the problems that &lt;span class="caps"&gt;AOP&lt;/span&gt; addresses and how the principles of object-oriented design influence &lt;span class="caps"&gt;AOP&lt;/span&gt; and &lt;i&gt;vice versa&lt;/i&gt;. If you&amp;#8217;re in the area, I hope to see you &lt;a href="http://www.cjug.org/Wiki.jsp?page=2007.09.06.west"&gt;there&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 04 Sep 2007 17:55:47 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:bd3e42fa-602d-4d02-9aef-7f5332628a19</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/09/04/cjug-west-9-6-07-aspect-oriented-programming-and-software-design</link>
      <category>Dean's Deprecations</category>
      <category>Public Speaking Engagements</category>
      <category>AOP</category>
      <category>AOD</category>
      <category>design</category>
      <category>Java</category>
      <category>AspectJ</category>
      <category>CJUG</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/8802</trackback:ping>
    </item>
    <item>
      <title>Announcement: Aquarium v0.1.0 - An Aspect-Oriented Programming Toolkit for Ruby</title>
      <description>I just released the first version of a new Aspect-Oriented Programming toolkit for Ruby called &lt;a href="http://aquarium.rubyforge.org/"&gt;Aquarium&lt;/a&gt;. 

I blogged about the goals of the project &lt;a href="http://blog.aspectprogramming.com/"&gt;here&lt;/a&gt;. Briefly, they are 
&lt;ul&gt;
&lt;li&gt;Create a powerful &lt;i&gt;pointcut language&lt;/i&gt;, the most important part of an AOP toolkit.
&lt;li&gt;Provide Robust support for concurrent &lt;i&gt;advice&lt;/i&gt; at the same &lt;i&gt;join point.&lt;/i&gt;.
&lt;li&gt;Provide runtime addition and removal of aspects.&lt;/li&gt;
&lt;li&gt;Provide a test bed for implementation ideas for &lt;a href="http://www.martinfowler.com/bliki/DomainSpecificLanguage.html"&gt;DSL's&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

There is extensive documentation at the &lt;a href="http://aquarium.rubyforge.org/"&gt;Aquarium&lt;/a&gt; site. Please give it a try and let me know what you think!


</description>
      <pubDate>Thu, 23 Aug 2007 17:26:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ef446f2a-179b-4d3e-8a33-f7ef1d49a9dc</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/08/23/announcement-aquarium-v0-1-0-an-aspect-oriented-programming-toolkit-for-ruby</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>aspect</category>
      <category>oriented</category>
      <category>programming</category>
      <category>Ruby</category>
      <category>aquarium</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/8799</trackback:ping>
    </item>
    <item>
      <title>Applications Should Use Several Languages</title>
      <description>&lt;p&gt;Yesterday, I &lt;a href="http://typo.objectmentor.com/articles/2007/07/03/observations-on-tdd-in-c-long"&gt;blogged&lt;/a&gt; about &lt;a href="http://typo.objectmentor.com/articles/2007/07/03/observations-on-tdd-in-c-long"&gt;&lt;span class="caps"&gt;TDD&lt;/span&gt; in C++&lt;/a&gt; and ended with a suggestion for the dilemma of needing optimal performance some of the time and optimal productivity the rest of the time. I suggested that you should use more than one language for your applications.&lt;/p&gt;


	&lt;p&gt;If you are developing web applications, you are already doing this, of course. Your web tier probably uses several &amp;#8220;languages&amp;#8221;, &lt;em&gt;e.g.,&lt;/em&gt; HTML, JavaScript, &lt;span class="caps"&gt;JSP&lt;/span&gt;/ASP, &lt;span class="caps"&gt;CSS&lt;/span&gt;, Java, &lt;em&gt;etc.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;However, most people use only one language for the business/mid tier. I think you should consider using several; a high-productivity language environment for most of your work, with the occasional critical functionality implemented in C or C++ to optimize performance, but &lt;em&gt;only after actually measuring&lt;/em&gt; where the bottlenecks are located.&lt;/p&gt;


	&lt;p&gt;This approach is much too rare, but it has historical precedents. One of the most successful and long-lived software projects of all time is Emacs. It consists of a core C-based runtime with most of the functionality implemented in Emacs lisp &amp;#8220;components&amp;#8221;. The relative ease of extending Emacs using lisp has resulted in a rich assortment of support tools for various operating systems, languages, build tools, &lt;em&gt;etc.&lt;/em&gt; Even modern IDEs and and other graphical editors have not completely displaced Emacs.&lt;/p&gt;


	&lt;p&gt;Java has embraced the mixed language philosophy somewhat reluctantly. &lt;span class="caps"&gt;JNI&lt;/span&gt; is the official and most commonly-used &lt;span class="caps"&gt;API&lt;/span&gt; for invoking &amp;#8220;native&amp;#8221; code, but it is somewhat hard to use and few people actually use it. In contrast, for example, the Ruby world has always embraced this approach. Ruby has an easy to use &lt;span class="caps"&gt;API&lt;/span&gt; for invoking native C code and good alternatives exist for invoking code in other languages. As a result, many of the 3rd-party Ruby libraries (or &lt;em&gt;gems&lt;/em&gt;) contain both Ruby and native C code. The latter is built on the fly when you install the gem. Hence, there are many high-performance Ruby applications. This is not a contradiction in terms, because the performance-critical sections run natively, even though interpreted Ruby is relatively slow.&lt;/p&gt;


	&lt;p&gt;Of course, you have to be judicious in how you use mixed-language programming. Crossing the language boundary is often somewhat heavyweight, so you should avoid doing such invocations inside tight loops, for example.&lt;/p&gt;


	&lt;p&gt;So, I think the solution to the dilemma of needing high performance sometimes and high productivity the rest of the time is to pick the right tools for each circumstance and make them interoperate. Even constrained embedded devices like cell phones would  be easier to implement if most of the code were written in a language like Ruby, Python, Smalltalk, or Java and performance-critical components were written in C or C++.&lt;/p&gt;


	&lt;p&gt;If I were starting such a greenfield project, I would assume that &lt;em&gt;time-to-money&lt;/em&gt; is the top priority and write most of my code in Ruby (my personal current favorite), using &lt;span class="caps"&gt;TDD&lt;/span&gt; of course. I would profile it constantly, as part of the nightly or &lt;a href="http://www.martinfowler.com/articles/continuousIntegration.html"&gt;continuous-integration&lt;/a&gt; build. When bottlenecks emerge, I would first determine if a refactoring is sufficient to fix them and if not, I would rewrite the critical sections in C. If the project were for an embedded device, I would also watch the resource usage carefully.&lt;/p&gt;


	&lt;p&gt;For my embedded device, I would test from the beginning whether or not the overhead of the interpreter/VM and the overall performance are acceptable. I would also be sure that I have adequate tool support for the inevitable remote debugging and diagnostics I&amp;#8217;ll have to do. If I made the wrong tool choices after all, I would know early on, when it&amp;#8217;s still relatively painless to retool.&lt;/p&gt;


	&lt;p&gt;If you&amp;#8217;re an IT or web-site developer, you have fewer performance limitations and more options. You might decide to make the cross-language boundary a &lt;em&gt;cross-process&lt;/em&gt; boundary, &lt;em&gt;e.g.,&lt;/em&gt; by communicating through some sort of &lt;em&gt;lightweight&lt;/em&gt; web services. This is one way to leverage legacy C/C++ code while developing new functionality in a more productive language.&lt;/p&gt;</description>
      <pubDate>Wed, 04 Jul 2007 11:38:31 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d93783af-ac4e-43c1-a405-e86ebb1d9efa</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/07/04/applications-should-use-several-languages</link>
      <category>Dean's Deprecations</category>
      <category>c</category>
      <category>Ruby</category>
      <category>Java</category>
      <category>Smalltalk</category>
      <category>design</category>
      <category>architecture</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/8786</trackback:ping>
    </item>
    <item>
      <title>Observations on TDD in C++ (long)</title>
      <description>&lt;p&gt;I spent all of June mentoring teams on &lt;span class="caps"&gt;TDD&lt;/span&gt; in C++ with some Java. While C++ was my language of choice through most of the 90&amp;#8217;s, I think far too many teams are using it today when there are better options for their particular needs.&lt;/p&gt;


	&lt;p&gt;During the month, I took notes on all the ways that C++ development is less productive than development in languages like Java, particular if you try to practice &lt;span class="caps"&gt;TDD&lt;/span&gt;. I&amp;#8217;m not trying to start a language flame war. There are times when C++ is the appropriate tool, as we&amp;#8217;ll see.&lt;/p&gt;


	&lt;p&gt;Most of the points below have been discussed before, but it is useful to list them in one place and to highlight a few particular observations.&lt;/p&gt;


	&lt;p&gt;Based on my observations last month, as well as previously experience, I&amp;#8217;ve come to the conclusion that &lt;span class="caps"&gt;TDD&lt;/span&gt; in C++ is about an &lt;em&gt;order of magnitude&lt;/em&gt; slower than &lt;span class="caps"&gt;TDD&lt;/span&gt; in Java. Mostly, this is due to poor or non-existent tool support for automated refactorings, no error detection as you type, and the requirement to compile and link an executable test.&lt;/p&gt;


	&lt;p&gt;So, here is my list of impediments that I encountered last month. I&amp;#8217;ll mostly use Java as the comparison language, but the arguments are more or less the same for C# and the popular dynamic languages, like Ruby, Python, and Smalltalk. Note that the dynamic languages tend to have less complete tool support, but they make up for it in other ways (off-topic for this blog).&lt;/p&gt;


	&lt;h3&gt;Getting Started&lt;/h3&gt;


	&lt;p&gt;There is more setup effort involved in configuring your build environment to use your chosen unit testing framework (&lt;em&gt;e.g.&lt;/em&gt;, CppUnit) and to create small executables, one each for a single or a few tests. Creating many small tests, rather than one big test (&lt;em&gt;e.g.&lt;/em&gt;, a variant of the actual application). This is important to minimize the &lt;span class="caps"&gt;TDD&lt;/span&gt; cycle.&lt;/p&gt;


	&lt;p&gt;Fortunately, this setup is a one-time &amp;#8220;charge&amp;#8221;. The harder part, if you have legacy code, is refactoring it to break hard dependencies so you can write unit tests. This is true for legacy code in any language, of course.&lt;/p&gt;


	&lt;h3&gt;Complex Syntax&lt;/h3&gt;


	&lt;p&gt;C++ has a very complex syntax. This makes it hard to parse, limiting the capabilities of automated tools and slowing build times (more below).&lt;/p&gt;


	&lt;p&gt;The syntax also makes it harder to program in the language and not just for novices. Even for experts, the &lt;em&gt;visual noise&lt;/em&gt; of pointer and reference syntax obscures the story the code is trying to tell. That is, C++ code is inherently less &lt;em&gt;clean&lt;/em&gt; than code in most other languages in widespread use.&lt;/p&gt;


	&lt;p&gt;Also, the need for the developer to remember whether each variable is a pointer, a reference, or a &amp;#8220;value&amp;#8221;, and how to manage its life-cycle, requires mental effort that could be applied to the logic of the code instead.&lt;/p&gt;


	&lt;h3&gt;Obsolete Tool Support&lt;/h3&gt;


	&lt;p&gt;No editor or &lt;span class="caps"&gt;IDE&lt;/span&gt; supports non-trivial, automated refactorings. (Some do simple refactorings like &amp;#8220;rename&amp;#8221;.) This means you have to resort to tedious, slow, and error-prone manual refactorings. &lt;strong&gt;Extract Method&lt;/strong&gt; is made worse by the fact that you usually have to edit two files, an implementation and a header file.&lt;/p&gt;


	&lt;p&gt;There are no widely-used tools that provide on-the-fly parsing and error indications. This alone increases the time between typing an error and learning about it by an order of magnitude. Since a build is usually required, you tend to type a lot between builds, thereby learning about many errors at once. Working through them takes time. (There may be some commercial tools with limited support for on-the-fly parsing, but they are not widely used.)&lt;/p&gt;


	&lt;p&gt;Similarly, none of the common development tools support incremental loading of object code that could be used for faster unit testing and hence a faster &lt;span class="caps"&gt;TDD&lt;/span&gt; cycle. Most teams just build executables. Even when they structure the build process to generate small, focused executables for unit tests, the &lt;span class="caps"&gt;TDD&lt;/span&gt; cycle times remain much longer than for Java.&lt;/p&gt;


	&lt;p&gt;Finally, while there is at least one mocking framework available for C++, it is much harder to use than comparable frameworks in newer languages.&lt;/p&gt;


	&lt;h3&gt;Manual Memory Management&lt;/h3&gt;


	&lt;p&gt;We all know that manual memory management leads to time spent finding and fixing memory errors and leaks. Avoiding these problems in the first place also consumes a lot of thought and design effort. In Java, you just spend far less time thinking about &amp;#8220;who owns this object and is therefore responsible for managing its life-cycle&amp;#8221;.&lt;/p&gt;


	&lt;h4&gt;Dependency Management&lt;/h4&gt;


	&lt;p&gt;Intelligent handling of include directives is entirely up to the developer. We have all used the following &amp;#8220;guard&amp;#8221; idiom:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    #ifndef MY_CLASS_H
    #define MY_CLASS_H
    ...
    #endif&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Unfortunately, this isn&amp;#8217;t good enough. The file will still get opened and read in its entirety every time it is included. You could also put the guard directives around the include statement:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    #ifndef MY_CLASS_H
    #include &amp;quot;myclass.h&amp;quot;
    #endif&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is tedious and few people do it, but it does avoid the wasted file I/O.&lt;/p&gt;


	&lt;p&gt;Finally, too few people simply declare a required class with no body:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    class MyClass;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is sufficient when one header references another class as a pointer or reference. In our experience with clients, we have often seen build times improve significantly when teams cleaned up their header file usage and dependencies, in general. Still, why is all this necessary in the 21st century?&lt;/p&gt;


	&lt;p&gt;This problem is made worse by the unfortunate inclusion of private and protected declarations in the same header file included by clients of the class. This creates &lt;em&gt;phantom dependencies&lt;/em&gt; from the clients to class details that they can&amp;#8217;t access directly.&lt;/p&gt;


	&lt;h4&gt;Other Debugging Issues&lt;/h4&gt;


	&lt;p&gt;Limited or non-existent context information when an exception is thrown makes the origin of the exception harder to find. To fill the gap, you tend to spend more time &lt;em&gt;adding&lt;/em&gt; this information manually through logging statements in catch blocks, &lt;em&gt;etc&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;The &lt;tt&gt;std::exception&lt;/tt&gt; class doesn&amp;#8217;t appear to have a &lt;tt&gt;std::string&lt;/tt&gt; or &lt;tt&gt;const char&amp;#42;&lt;/tt&gt; argument in a constructor for a message. You could just throw a string, but that precludes using an exception class with a meaningful name.&lt;/p&gt;


	&lt;p&gt;Compiler error messages are hard to read and often misleading. In part this is due to the complexity of the syntax and the parsing problem mentioned previously. Errors involving template usage are particular hard to debug.&lt;/p&gt;


	&lt;h4&gt;Reflection and Metaprogramming&lt;/h4&gt;


	&lt;p&gt;Many of the productivity gains from using dynamic languages and (to a lesser extent) Java and C# are due to their reflection and metaprogramming facilities. C++ relies more on &lt;em&gt;template metaprogramming&lt;/em&gt;, rather than APIs or other built-in language features that are easier to use and more full-featured. Preprocessor hacks are also used frequently. Better reflection and metaprogramming support would permit more robust &lt;em&gt;proxy&lt;/em&gt; or &lt;em&gt;aspect&lt;/em&gt; solutions to be used. (However, to be fair, sometimes a preprocessor hack has the virtue of being &amp;#8220;the simplest thing that could possibly work.&amp;#8221;)&lt;/p&gt;


	&lt;h4&gt;Library Issues&lt;/h4&gt;


	&lt;p&gt;Speaking of &lt;tt&gt;std::string&lt;/tt&gt; and &lt;tt&gt;char&amp;#42;&lt;/tt&gt;, it is hard to avoid writing two versions of methods, one which takes &lt;tt&gt;const std::string&amp;amp;&lt;/tt&gt; arguments and one which takes &lt;tt&gt;const char&amp;#42;&lt;/tt&gt; arguments. It doesn&amp;#8217;t matter that one method can usually delegate to the other one; this is wasted effort.&lt;/p&gt;


	&lt;h3&gt;Discussion&lt;/h3&gt;


	&lt;p&gt;So, C++ makes it hard for me to work the way that I want to work today, which is test-driven, creating clean code that works. That&amp;#8217;s why I rarely choose it for a project.&lt;/p&gt;


	&lt;p&gt;However, to be fair, there are legitimate reasons for almost all of the perceived &amp;#8220;deficiencies&amp;#8221; listed above. C++ emphasizes performance and backwards-compatibility with C over all other considerations. However, they come at the expense of other interests, like effective &lt;span class="caps"&gt;TDD&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;It &lt;em&gt;is&lt;/em&gt; a &lt;strong&gt;good&lt;/strong&gt; thing that we have languages that were designed with performance as the top design goal, because there are circumstances where performance is the number one requirement. However, most teams that use C++ as their primary language are making an optimal choice for, say, 10% of their code, but which is suboptimal the other 90%. Your numbers will vary; I picked 10% &lt;em&gt;vs.&lt;/em&gt; 90% based on the fact that performance bottlenecks are usually localized and they should be found by actual measurements, not guesses!&lt;/p&gt;


	&lt;h4&gt;Workarounds&lt;/h4&gt;


	&lt;p&gt;If it&amp;#8217;s true that &lt;span class="caps"&gt;TDD&lt;/span&gt; is an order of magnitude slower for C++ then what do we do? No doubt really good C++ developers have optimized their processes as best as they can, but in the end, you will just have to live with longer &lt;span class="caps"&gt;TDD&lt;/span&gt; cycles. Instead of &lt;em&gt;write just enough test to fail, make it pass, refactor&lt;/em&gt;, it will be more like &lt;em&gt;write a complete test, write the implementation, build it, fix the compilation errors, run it, fix the logic errors to make the test pass, and then refactor&lt;/em&gt;.&lt;/p&gt;


	&lt;h4&gt;A Real Resolution?&lt;/h4&gt;


	&lt;p&gt;You could consider switching to the &lt;a href="http://www.digitalmars.com/d/"&gt;D language&lt;/a&gt;, which is link compatible with C and appears to avoid many of the problems described above.&lt;/p&gt;


	&lt;p&gt;There is another way out of the dilemma of needing optimal performance some of the time and optimal productivity the rest of the time; use more than one language. I&amp;#8217;ll discuss this idea in my next blog.&lt;/p&gt;</description>
      <pubDate>Tue, 03 Jul 2007 23:15:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:399b620b-c563-4b96-9556-17e24c8f8f74</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/07/03/observations-on-tdd-in-c-long</link>
      <category>Dean's Deprecations</category>
      <category>c</category>
      <category>TDD</category>
      <category>D</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/8785</trackback:ping>
    </item>
    <item>
      <title>Observations on TDD in C++ (long)</title>
      <description>&lt;p&gt;I spent all of June mentoring teams on &lt;span class="caps"&gt;TDD&lt;/span&gt; in C++ with some Java. While C++ was my language of choice through most of the 90&amp;#8217;s, I think far too many teams are using it today when there are better options for their particular needs.&lt;/p&gt;


	&lt;p&gt;During the month, I took notes on all the ways that C++ development is less productive than development in languages like Java, particular if you try to practice &lt;span class="caps"&gt;TDD&lt;/span&gt;. I&amp;#8217;m not trying to start a language flame war. There are times when C++ is the appropriate tool, as we&amp;#8217;ll see.&lt;/p&gt;


	&lt;p&gt;Most of the points below have been discussed before, but it is useful to list them in one place and to highlight a few particular observations.&lt;/p&gt;


	&lt;p&gt;Based on my observations last month, as well as previously experience, I&amp;#8217;ve come to the conclusion that &lt;span class="caps"&gt;TDD&lt;/span&gt; in C++ is about an &lt;em&gt;order of magnitude&lt;/em&gt; slower than &lt;span class="caps"&gt;TDD&lt;/span&gt; in Java. Mostly, this is due to poor or non-existent tool support for automated refactorings, no error detection as you type, and the requirement to compile and link an executable test.&lt;/p&gt;


	&lt;p&gt;So, here is my list of impediments that I encountered last month. I&amp;#8217;ll mostly use Java as the comparison language, but the arguments are more or less the same for C# and the popular dynamic languages, like Ruby, Python, and Smalltalk. Note that the dynamic languages tend to have less complete tool support, but they make up for it in other ways (off-topic for this blog).&lt;/p&gt;


	&lt;h3&gt;Getting Started&lt;/h3&gt;


	&lt;p&gt;There is more setup effort involved in configuring your build environment to use your chosen unit testing framework (&lt;em&gt;e.g.&lt;/em&gt;, CppUnit) and to create small executables, one each for a single or a few tests. Creating many small tests, rather than one big test (&lt;em&gt;e.g.&lt;/em&gt;, a variant of the actual application). This is important to minimize the &lt;span class="caps"&gt;TDD&lt;/span&gt; cycle.&lt;/p&gt;


	&lt;p&gt;Fortunately, this setup is a one-time &amp;#8220;charge&amp;#8221;. The harder part, if you have legacy code, is refactoring it to break hard dependencies so you can write unit tests. This is true for legacy code in any language, of course.&lt;/p&gt;


	&lt;h3&gt;Complex Syntax&lt;/h3&gt;


	&lt;p&gt;C++ has a very complex syntax. This makes it hard to parse, limiting the capabilities of automated tools and slowing build times (more below).&lt;/p&gt;


	&lt;p&gt;The syntax also makes it harder to program in the language and not just for novices. Even for experts, the &lt;em&gt;visual noise&lt;/em&gt; of pointer and reference syntax obscures the story the code is trying to tell. That is, C++ code is inherently less &lt;em&gt;clean&lt;/em&gt; than code in most other languages in widespread use.&lt;/p&gt;


	&lt;p&gt;Also, the need for the developer to remember whether each variable is a pointer, a reference, or a &amp;#8220;value&amp;#8221;, and how to manage its life-cycle, requires mental effort that could be applied to the logic of the code instead.&lt;/p&gt;


	&lt;h3&gt;Obsolete Tool Support&lt;/h3&gt;


	&lt;p&gt;No editor or &lt;span class="caps"&gt;IDE&lt;/span&gt; supports non-trivial, automated refactorings. (Some do simple refactorings like &amp;#8220;rename&amp;#8221;.) This means you have to resort to tedious, slow, and error-prone manual refactorings. &lt;strong&gt;Extract Method&lt;/strong&gt; is made worse by the fact that you usually have to edit two files, an implementation and a header file.&lt;/p&gt;


	&lt;p&gt;There are no widely-used tools that provide on-the-fly parsing and error indications. This alone increases the time between typing an error and learning about it by an order of magnitude. Since a build is usually required, you tend to type a lot between builds, thereby learning about many errors at once. Working through them takes time. (There may be some commercial tools with limited support for on-the-fly parsing, but they are not widely used.)&lt;/p&gt;


	&lt;p&gt;Similarly, none of the common development tools support incremental loading of object code that could be used for faster unit testing and hence a faster &lt;span class="caps"&gt;TDD&lt;/span&gt; cycle. Most teams just build executables. Even when they structure the build process to generate small, focused executables for unit tests, the &lt;span class="caps"&gt;TDD&lt;/span&gt; cycle times remain much longer than for Java.&lt;/p&gt;


	&lt;p&gt;Finally, while there is at least one mocking framework available for C++, it is much harder to use than comparable frameworks in newer languages.&lt;/p&gt;


	&lt;h3&gt;Manual Memory Management&lt;/h3&gt;


	&lt;p&gt;We all know that manual memory management leads to time spent finding and fixing memory errors and leaks. Avoiding these problems in the first place also consumes a lot of thought and design effort. In Java, you just spend far less time thinking about &amp;#8220;who owns this object and is therefore responsible for managing its life-cycle&amp;#8221;.&lt;/p&gt;


	&lt;h4&gt;Dependency Management&lt;/h4&gt;


	&lt;p&gt;Intelligent handling of include directives is entirely up to the developer. We have all used the following &amp;#8220;guard&amp;#8221; idiom:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    #ifndef MY_CLASS_H
    #define MY_CLASS_H
    ...
    #endif&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Unfortunately, this isn&amp;#8217;t good enough. The file will still get opened and read in its entirety every time it is included. You could also put the guard directives around the include statement:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    #ifndef MY_CLASS_H
    #include &amp;quot;myclass.h&amp;quot;
    #endif&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is tedious and few people do it, but it does avoid the wasted file I/O.&lt;/p&gt;


	&lt;p&gt;Finally, too few people simply declare a required class with no body:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    class MyClass;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is sufficient when one header references another class as a pointer or reference. In our experience with clients, we have often seen build times improve significantly when teams cleaned up their header file usage and dependencies, in general. Still, why is all this necessary in the 21st century?&lt;/p&gt;


	&lt;p&gt;This problem is made worse by the unfortunate inclusion of private and protected declarations in the same header file included by clients of the class. This creates &lt;em&gt;phantom dependencies&lt;/em&gt; from the clients to class details that they can&amp;#8217;t access directly.&lt;/p&gt;


	&lt;h4&gt;Other Debugging Issues&lt;/h4&gt;


	&lt;p&gt;Limited or non-existent context information when an exception is thrown makes the origin of the exception harder to find. To fill the gap, you tend to spend more time &lt;em&gt;adding&lt;/em&gt; this information manually through logging statements in catch blocks, &lt;em&gt;etc&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;The &lt;tt&gt;std::exception&lt;/tt&gt; class doesn&amp;#8217;t appear to have a &lt;tt&gt;std::string&lt;/tt&gt; or &lt;tt&gt;const char&amp;#42;&lt;/tt&gt; argument in a constructor for a message. You could just throw a string, but that precludes using an exception class with a meaningful name.&lt;/p&gt;


	&lt;p&gt;Compiler error messages are hard to read and often misleading. In part this is due to the complexity of the syntax and the parsing problem mentioned previously. Errors involving template usage are particular hard to debug.&lt;/p&gt;


	&lt;h4&gt;Reflection and Metaprogramming&lt;/h4&gt;


	&lt;p&gt;Many of the productivity gains from using dynamic languages and (to a lesser extent) Java and C# are due to their reflection and metaprogramming facilities. C++ relies more on &lt;em&gt;template metaprogramming&lt;/em&gt;, rather than APIs or other built-in language features that are easier to use and more full-featured. Preprocessor hacks are also used frequently. Better reflection and metaprogramming support would permit more robust &lt;em&gt;proxy&lt;/em&gt; or &lt;em&gt;aspect&lt;/em&gt; solutions to be used. (However, to be fair, sometimes a preprocessor hack has the virtue of being &amp;#8220;the simplest thing that could possibly work.&amp;#8221;)&lt;/p&gt;


	&lt;h4&gt;Library Issues&lt;/h4&gt;


	&lt;p&gt;Speaking of &lt;tt&gt;std::string&lt;/tt&gt; and &lt;tt&gt;char&amp;#42;&lt;/tt&gt;, it is hard to avoid writing two versions of methods, one which takes &lt;tt&gt;const std::string&amp;amp;&lt;/tt&gt; arguments and one which takes &lt;tt&gt;const char&amp;#42;&lt;/tt&gt; arguments. It doesn&amp;#8217;t matter that one method can usually delegate to the other one; this is wasted effort.&lt;/p&gt;


	&lt;h3&gt;Discussion&lt;/h3&gt;


	&lt;p&gt;So, C++ makes it hard for me to work the way that I want to work today, which is test-driven, creating clean code that works. That&amp;#8217;s why I rarely choose it for a project.&lt;/p&gt;


	&lt;p&gt;However, to be fair, there are legitimate reasons for almost all of the perceived &amp;#8220;deficiencies&amp;#8221; listed above. C++ emphasizes performance and backwards-compatibility with C over all other considerations. However, they come at the expense of other interests, like effective &lt;span class="caps"&gt;TDD&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;It &lt;em&gt;is&lt;/em&gt; a &lt;strong&gt;good&lt;/strong&gt; thing that we have languages that were designed with performance as the top design goal, because there are circumstances where performance is the number one requirement. However, most teams that use C++ as their primary language are making an optimal choice for, say, 10% of their code, but which is suboptimal the other 90%. Your numbers will vary; I picked 10% &lt;em&gt;vs.&lt;/em&gt; 90% based on the fact that performance bottlenecks are usually localized and they should be found by actual measurements, not guesses!&lt;/p&gt;


	&lt;h4&gt;Workarounds&lt;/h4&gt;


	&lt;p&gt;If it&amp;#8217;s true that &lt;span class="caps"&gt;TDD&lt;/span&gt; is an order of magnitude slower for C++ then what do we do? No doubt really good C++ developers have optimized their processes as best as they can, but in the end, you will just have to live with longer &lt;span class="caps"&gt;TDD&lt;/span&gt; cycles. Instead of &lt;em&gt;write just enough test to fail, make it pass, refactor&lt;/em&gt;, it will be more like &lt;em&gt;write a complete test, write the implementation, build it, fix the compilation errors, run it, fix the logic errors to make the test pass, and then refactor&lt;/em&gt;.&lt;/p&gt;


	&lt;h4&gt;A Real Resolution?&lt;/h4&gt;


	&lt;p&gt;You could consider switching to the &lt;a href="http://www.digitalmars.com/d/"&gt;D language&lt;/a&gt;, which is link compatible with C and appears to avoid many of the problems described above.&lt;/p&gt;


	&lt;p&gt;There is another way out of the dilemma of needing optimal performance some of the time and optimal productivity the rest of the time; use more than one language. I&amp;#8217;ll discuss this idea in my next blog.&lt;/p&gt;</description>
      <pubDate>Tue, 03 Jul 2007 23:15:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:399b620b-c563-4b96-9556-17e24c8f8f74</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/07/03/observations-on-tdd-in-c-long</link>
      <category>Dean's Deprecations</category>
      <category>c</category>
      <category>TDD</category>
      <category>D</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/8785</trackback:ping>
    </item>
    <item>
      <title>Are &amp;quot;else&amp;quot; blocks the root of all evil?</title>
      <description>&lt;p&gt;So, I&amp;#8217;m pair programming C++ code with a client today and he makes an observation that makes me pause.&lt;/p&gt;


	&lt;p&gt;&lt;i&gt;The well-structured, open-source code I&amp;#8217;ve looked at typically has very few &lt;code&gt;else&lt;/code&gt; blocks. You might see a conditional test with a return statement if the conditional evaluates to true, but not many &lt;code&gt;if/else&lt;/code&gt; blocks. 
&lt;/i&gt;&lt;/p&gt;


	&lt;p&gt;(I&amp;#8217;m quoting from memory&amp;#8230;) Now, this may seem crazy at first, but one of the principles we teach at Object Mentor is the &lt;a href="http://www.objectmentor.com/resources/articles/srp.pdf"&gt;Single Responsibility Principle&lt;/a&gt;, which states that a class should have only one reason to change. This principle also applies to methods. More loosely defined, a class or method should do only one thing.&lt;/p&gt;


	&lt;p&gt;So, if a method has an if/else block, is it doing two (or more) things and therefore violating the &lt;span class="caps"&gt;SRP&lt;/span&gt;?&lt;/p&gt;


	&lt;p&gt;Okay, so this is a bit too restrictive (and the title was a bit of an attention grabber&amp;#8230; ;). We&amp;#8217;re not talking about something &lt;i&gt;really&lt;/i&gt; evil, like &lt;a href="http://www.cookcomputing.com/blog/archives/000084.html"&gt;premature optimization&lt;/a&gt;, after all!&lt;/p&gt;


	&lt;p&gt;However, look at your own &lt;code&gt;if/else&lt;/code&gt; blocks and ask yourself if maybe your code would express its intent better if you refactored it to eliminate some of those &lt;code&gt;else&lt;/code&gt; blocks.&lt;/p&gt;


	&lt;p&gt;So, is there something to this idea?&lt;/p&gt;</description>
      <pubDate>Tue, 05 Jun 2007 14:42:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ed9210a9-bfd3-4fec-b086-29ba4e1e9b77</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/06/05/are-else-blocks-the-root-of-all-evil</link>
      <category>Dean's Deprecations</category>
      <category>Design Principles</category>
      <category>design</category>
      <category>SRP</category>
      <category>else</category>
      <category>blocks</category>
    </item>
    <item>
      <title>What I've Learned from Master Chef Rino Baglio</title>
      <description>&lt;p&gt;If you want to learn &lt;i&gt;Craftsmanship&lt;/i&gt;, you would be hard pressed to find a better mentor than &lt;a href="http://www.pazzaluna.com/about/chef.htm"&gt;Rino Baglio&lt;/a&gt;, the Executive Master Chef at &lt;a href="http://www.pazzaluna.com/"&gt;Pazzaluna&lt;/a&gt;, in St. Paul, Minnesota. I had a chance to catch up with Rino this past weekend when I was in Minneapolis to teach a &lt;a href="http://web4.cs.ucl.ac.uk/icse07/index.php?id=90"&gt;tutorial&lt;/a&gt; on Aspect-Oriented Design at &lt;a href="http://web4.cs.ucl.ac.uk/icse07"&gt;&lt;span class="caps"&gt;ICSE 2007&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;I have known Rino for over a decade, starting when Ann and I were loyal patrons of &lt;i&gt;Il Bacio&lt;/i&gt; in Redmond, Washington, the restaurant he owned and operated with his wife Patsy until a few years ago. Through his cooking classes and many conversations about food and the restaurant business, I learned a lot about what it really means to be a chef and the long mentoring process that true chefs go through.&lt;/p&gt;


	&lt;p&gt;In the U.S., we think that passing a two-year culinary program qualifies you to be a chef. In Italy, an aspiring chef apprentices to a master at the age of 13 or so and spends the next 20-odd years mastering the craft before deserving the designation of &amp;#8220;chef&amp;#8221;. You can spend 7 years just working through the stations in a restaurant, cold dishes, salads, sauces, &lt;i&gt;etc.&lt;/i&gt;, just to become a &amp;#8220;cook&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Here are some of the characteristics of a true craftsman.&lt;/p&gt;


&lt;h3&gt;A craftsman is widely recognized by peers&lt;/h3&gt;
Rino recently won an &lt;a href="http://www.pazzaluna.com/about/chef_award.htm"&gt;international competition&lt;/a&gt; in Italy, one of many times he&amp;#8217;s been recognized nationally and internationally. 

&lt;h3&gt;A craftsman is passionate about the craft&lt;/h3&gt;
Rino says that if you are passionate about food, you will work on the presentation of even humble dishes. Pasta, as well as lobster, deserves an attractive presentation.

&lt;h3&gt;A craftsman delivers value to the customer while meeting business objectives&lt;/h3&gt;
Rino keeps the kitchen lean and efficient. He keeps costs low by relying on high-quality ingredients, keeping waste to a minimum, and constantly improving the skills of his staff, all without &lt;i&gt;ever&lt;/i&gt; compromising quality. In the year Rino has been at Pazzaluna, costs have dropped, while business and profits have increased.

&lt;h3&gt;A craftsman knows that quality is the number one priority&lt;/h3&gt;
Rino knows that cutting quality today means less business tomorrow. He keeps quality high by keeping morale high. Morale is high because his staff is constantly learning new and better recipes. Also, as you watch him interact with his staff, you can see that he treats all of them, from his &lt;i&gt;sous&lt;/i&gt; chefs to the dishwashers, with dignity and respect, while always holding them to high standards. 

&lt;h3&gt;A craftsman never stops learning&lt;/h3&gt;
You would think that he knows it all, by now. Yet, he has never forgotten a lesson his own mentor taught him, &amp;#8220;you can learn something from even the worst cook, because he always knows something you don&amp;#8217;t!&amp;#8221; How many &lt;i&gt;gurus&lt;/i&gt; do you know that think they have nothing left to learn?

	&lt;p&gt;&lt;br/&gt;
What does all this have to do with software? Pretty much everything. Like cuisine, &lt;a href="http://conferences.oreillynet.com/cs/rails2007/view/e_sess/11608"&gt;clean code&lt;/a&gt; is part art, part science. Clean code is created by passionate craftsman who are fanatical and fastidious about every detail. Clean code is the product of years of accumulated experience. The decisions a master makes moment-by-moment, whether test-driving the next feature or fighting a fire, reflect the wisdom and breadth of knowledge that produce high-quality results quickly and efficiently. Finally, a master leads by example, bringing the rest of the team up to his or her standards.&lt;/p&gt;


	&lt;p&gt;So, if you&amp;#8217;re young and ambitious, latch onto the mentors around you. If you can&amp;#8217;t find any, find another job. (Your organization is doomed anyway; so you might as well move on now.) If you&amp;#8217;re older and wiser, seek out the promising junior people, teach them what you know, and learn from them as well! Oh, and if you want to taste real Italian food, make a pilgrimage to St. Paul. Tell Rino I sent you.&lt;/p&gt;</description>
      <pubDate>Sun, 27 May 2007 21:39:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:879f813c-0ff9-450a-a9ef-2b3d4fd84848</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/05/27/what-ive-learned-from-master-chef-rino-baglio</link>
      <category>Dean's Deprecations</category>
    </item>
    <item>
      <title>100% Code Coverage?</title>
      <description>&lt;p&gt;Should you strive for 100% code coverage from your unit tests? It&amp;#8217;s probably not mandatory and if your goal is 100% coverage, you&amp;#8217;ll focus on that goal and not focus on writing the best tests for the &lt;i&gt;behavior&lt;/i&gt; of your code.&lt;/p&gt;


	&lt;p&gt;That said, here are some thoughts on why I still like to get close to 100%.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;I&amp;#8217;m anal retentive. I don&amp;#8217;t like those little red bits in my coverage report. (Okay, that&amp;#8217;s not a good reason&amp;#8230;)&lt;/li&gt;
		&lt;li&gt;Every time I run the coverage report, I have to inspect all the uninteresting cases to find the interesting cases I should cover.&lt;/li&gt;
		&lt;li&gt;The tests are the specification and documentation of the code, so if something nontrivial but unexpected happens, there should still be a test to &amp;#8220;document&amp;#8221; the behavior, even if the test is hard to write.&lt;/li&gt;
		&lt;li&gt;Maybe those places without coverage are telling me to fix the design.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I was thinking about this last point the other day when considering a bit of Java code that does a downcast (assume that&amp;#8217;s a good idea, for the sake of argument&amp;#8230;), wrapped in a try/catch block for the potential &lt;code&gt;ClassCastException&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;
public void handleEvent (Event event) throws ApplicationException {
  try {
    SpecialEvent specialEvent = (SpecialEvent) event;
    doSomethingSpecial (specialEvent);
  } catch (ClassCastException cce) { 
    throw new ApplicationException(cce);
  }
}
&lt;/pre&gt;

	&lt;p&gt;To get 100% coverage, you would have to write a test that inputs an object of a different subtype of &lt;code&gt;Event&lt;/code&gt; to trigger coverage of the catch block. As we all know, these sorts of error-handling code blocks are typically the hardest to cover and ones we&amp;#8217;re most likely to ignore. (When was the last time you saw a &lt;code&gt;ClassCastException&lt;/code&gt; anyway?)&lt;/p&gt;


	&lt;p&gt;So my thought was this, we want 100% of the production code to be developed with &lt;span class="caps"&gt;TDD&lt;/span&gt;, so what if we made 100% coverage a similar goal? How would that change our designs? We might decide that since we have to write a test to cover this error-handling scenario, maybe we should rethink the scenario itself. Is it necessary? Could we eliminate the catch block with a better overall design, in this case, making sure that we test all &lt;i&gt;callers&lt;/i&gt; and ensure that they obey the method&amp;#8217;s &amp;#8216;contract&amp;#8217;? Should we just let the &lt;code&gt;ClassCastException&lt;/code&gt; fly out of the function and let a higher-level catch block handle it? After all, catching and rethrowing a different exception is slightly smelly and the code would be cleaner without the try/catch block. (For completeness, a good use of exception wrapping is to avoid namespace pollution. We might not want application layer A to know anything about layer C&amp;#8217;s exception types, so we wrap a C exception in an A exception, which gets passed through layer B&amp;#8230;)&lt;/p&gt;


	&lt;p&gt;100% coverage is often impossible or impractical, because of language or tool oddities. Still, if you give in early, you&amp;#8217;re overlooking some potential benefits.&lt;/p&gt;</description>
      <pubDate>Wed, 16 May 2007 22:50:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1042dbcd-42aa-4dd1-a2de-3c371b910ba7</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/05/16/100-code-coverage</link>
      <category>Dean's Deprecations</category>
      <category>TDD</category>
      <category>code</category>
      <category>coverage</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/7523</trackback:ping>
    </item>
    <item>
      <title>AOP and Dynamic Languages: Contradiction in Terms or Match Made in Heaven?</title>
      <description>&lt;p&gt;Consider this &lt;a href="c2.com/cgi/wiki?AspectsAndDynamicLanguages"&gt;quote&lt;/a&gt; from Dave Thomas (of the Pragmatic Programmers) on AOP (aspect-oriented programming, a.k.a. aspect-oriented software development - AOSD) :&lt;/p&gt;
&lt;blockquote&gt;Once you have decent reflection and metaclasses, AOP is 
just part of the language.&lt;/blockquote&gt;
&lt;p&gt;People who work with dynamic languages don't see any need for AOP-specific facilities in their language. They don't necessarily dispute the value of AOP for Java, where metaprogramming facilities are weaker, but for them, AOP is just a constrained form of metaprogramming. Are they right?&lt;/p&gt;


&lt;p&gt;It's easy to see why people feel this way when you consider that most of the applications of AOP have been to solve obvious "cross-cutting concerns" (CCCs) like object-relational mapping, transactions, security, &lt;i&gt;etc.&lt;/i&gt; In other words, AOP looks like just one of many tools in your toolbox to solve a particular group of problems.&lt;/p&gt;
&lt;p&gt;I'll use Ruby as my example dynamic language, since Ruby is the example I know best. It's interesting to look at Ruby on Rails source code, where you find a lot of "AOP-like" code that addresses the CCCs I just mentioned (and more). This is easy enough to do using Ruby's metaprogramming tools, even though tooling that supports AOP &lt;i&gt;semantics&lt;/i&gt; would probably make this code easier to write and maintain.&lt;/p&gt;
&lt;p&gt;This is going to be a long blog entry already, so I won't cite detailed examples here, but consider how Rails uses &lt;code&gt;method_missing&lt;/code&gt; to effectively "introduce" new methods into classes and modules. For example, in &lt;a href="http://wiki.rubyonrails.com/rails/pages/ActiveRecord"&gt;ActiveRecord&lt;/a&gt;, the many possible &lt;code&gt;find&lt;/code&gt; methods and attribute read/write methods are "implemented" this way.&lt;/p&gt;
&lt;p&gt;By the way, another excellent Ruby framework, &lt;a href="http://rspec.rubyforge.org/"&gt;RSpec&lt;/a&gt; used &lt;code&gt;method_missing&lt;/code&gt; for similar purposes, but recently refactored its implementation and public API to avoid &lt;code&gt;method_missing&lt;/code&gt;, because having multiple frameworks attempt to use the same "hook" proved very fragile!&lt;/p&gt;
&lt;p&gt;Also in Rails, method "aliasing" is done approximately 175 times, often to wrap ("advise") methods with new behaviors.&lt;/p&gt;
&lt;p&gt;Still, is there &lt;i&gt;really&lt;/i&gt; a need for AOP tooling in dynamic languages? First, consider that in the early days of OOP, some of us "faked" OOP using whatever constructs our languages provided. I wrote plenty of C code that used &lt;code&gt;struct&lt;/code&gt;s as objects and method pointers to simulate method overloading and overriding. However, few people would argue today that such an approach is "good enough". If we're thinking in objects, it sure helps to have a language that matches those semantics.&lt;/p&gt;
&lt;p&gt;Similarly, it's true that you can implement AOP using sufficiently powerful metaprogramming facilities, but it's a lot harder than having native AOP semantics in your language (or at least a close approximation thereof in libraries and their &lt;a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language"&gt;DSLs&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Before proceeding, let me remind you what AOP is for in the first place. AOP is essentially a new approach to modularization that complements other approaches, like objects. It tries to solve a group of problems that other modularity approaches can't handle, namely the fine-grained interaction of multiple domain models that is required to implement required functionality.&lt;/p&gt;
&lt;p&gt;Take the classic example of security management. Presumably, you have one strategy and implementation for handling authentication and authorization. This is one domain and your application's "core business logic" is another domain.&lt;/p&gt;
&lt;p&gt;In a non-AOP system, it is necessary to insert duplicate or nearly duplicate code throughout the system that invokes the security subsystem. This duplication violates &lt;a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;DRY&lt;/a&gt;, it clutters the logic of the code where it is inserted, it is difficult to test, maintain, replace with a new implementation, &lt;i&gt;etc.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Now you may say that you handle this through a Spring XML configuration file or an EJB deployment configuration file, for example. Congratulations, you are using an AOP or AOP-like system!&lt;/p&gt;
&lt;p&gt;What AOP seeks to do is to allow you to specify that repeated behavior in one place, in a modular way.&lt;/p&gt;
&lt;p&gt;There are four pieces required for an AOP system:&lt;/p&gt;
&lt;h4&gt;1. Interception&lt;/h4&gt;
&lt;p&gt;You need to be able to "intercept" execution points in the program. We call these &lt;a href="http://www.eclipse.org/aspectj/doc/released/progguide/language-joinPoints.html"&gt;join points&lt;/a&gt; in the AOP jargon and sets of them that the aspect writer wants to work with at once are called &lt;a href="http://www.eclipse.org/aspectj/doc/released/progguide/language-joinPoints.html"&gt;pointcuts&lt;/a&gt; (yes, no whitespace). At each join point, &lt;a href="http://www.eclipse.org/aspectj/doc/released/progguide/language-advice.html"&gt;advice&lt;/a&gt; is the executable code that an aspect invokes either before, after or both before and after ("around") the join point.&lt;/p&gt;
&lt;p&gt;Note that the most powerful AOP language, AspectJ, let's you advise join points like instance variable reads and writes, class initialization, instance creation, &lt;i&gt;etc.&lt;/i&gt; The easiest join points to advise are method calls and many AOP systems limit themselves to this capability.&lt;/p&gt;
&lt;h4&gt;2. Introduction&lt;/h4&gt;
&lt;p&gt;Introduction is the ability to add new state and behavior to an existing class, object, &lt;i&gt;etc.&lt;/i&gt; For example, if you want to use the Observer pattern with a particular class, you could use an aspect to introduce the logic to maintain the list of observers and to notify them when state changes occur.&lt;/p&gt;
&lt;h4&gt;3. Inspection&lt;/h4&gt;
&lt;p&gt;We need to be able to find the join points of interest, either through static or runtime analysis, preferably both! You would also like to specify certain conditions of interest, which I'll discuss shortly.&lt;/p&gt;
&lt;h4&gt;4. Modularization&lt;/h4&gt;
&lt;p&gt;If we can't package all this into a "module", then we don't have a new modularization scheme. Note that a part of this modularization is the ability to somehow specify in one place the behavior I want and have it affect the entire system. Hence, AOP is a modularity system with nonlocal effects.&lt;/p&gt;
&lt;br/&gt;
&lt;p&gt;Okay. How does pure Ruby stack up these requirements? If you're a Java programmer, the idea of Interception and Introduction, where you add new state and behavior to a class, may seem radical. In languages with "open classes" like Ruby, it is trivial and common to reopen a class (or Module) and insert new attributes (state) and methods (behavior). You can even change previously defined methods. Hence, Interception and Introduction are trivial in Ruby.&lt;/p&gt;
&lt;p&gt;This is why Ruby programmers assume that AOP is nothing special, but what they are missing are the complete picture for Inspection and Modularization, even though both are partially covered.&lt;/p&gt;
&lt;p&gt;There is a rich reflection API for finding classes and objects. You can write straightforward code that searches for classes that "respond to" a particular method, for example. What you can't do easily is query based on state. For example, in AspectJ, you can say, "I want to advise method call X.m when it is called in the context flow ('&lt;code&gt;cflow&lt;/code&gt;') of method call Y.m2 somewhere up the stack..." Yes, you can figure out how to do this in Ruby, but it's hard. So, we're back to the argument I made earlier that you would really like your language to match the semantics of your ideas.&lt;/p&gt;
&lt;p&gt;For modularization, yes you can put all the aspect-like code in a Module or Class. The hard part is encapsulating any complicated "point cut" metaprogramming in one place, should you want to use it again later. That is, once you figure out how to do the &lt;code&gt;cflow&lt;/code&gt; pointcuts using metaprogramming, you'll want that tricky bit of code in a library somewhere.&lt;/p&gt;
&lt;p&gt;At this point, you might be saying to yourself, "Okay, so it might be nice to have some AOP stuff in Ruby, but the Rails guys seem to be doing okay without it. Is it &lt;i&gt;really&lt;/i&gt; worth the trouble having AOP in the language?" Only if AOP is more applicable than for the limited set of problems described previously.&lt;/p&gt;
&lt;h4&gt;Future Applications of AOP??&lt;/h4&gt;
&lt;p&gt;Here's what I've been thinking about lately. Ruby is a wonderful language for creating mini-&lt;a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language"&gt;DSLs&lt;/a&gt;. The &lt;a href="http://wiki.rubyonrails.com/rails/pages/ActiveRecord"&gt;ActiveRecord&lt;/a&gt; DSL is a good example. It provides relational semantics, while the library minimizes the coding required by the developer. (AR reads the database schema and builds an in-memory representation of the records as objects.)&lt;/p&gt;
&lt;p&gt;Similarly, there is a lot of emphasis these days on development that centers around the &lt;a href="http://domaindrivendesign.org/books/index.html"&gt;domain&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Feature_Driven_Development"&gt;features&lt;/a&gt; of the project. Recall that I said that AOP is about modularizing the intersection of multiple domains (and recall my previous &lt;a href="http://blog.objectmentor.com/articles/trackback/4765"&gt;blog&lt;/a&gt; on the AOSD 2007 Conference where Gerald Sussman remarked that successful systems have more than one organizing principle).&lt;/p&gt;
&lt;p&gt;I think we'll see AOP become the underlying implementation of powerful DSLs that allow programmers who are &lt;i&gt;not&lt;/i&gt; AOP-literate express cross-cutting concerns in domain-specific and intuitive languages. AOP will do the heavy lifting behind the scenes to make the fine-grained interactions work. I really don't expect a majority of developers to become AOP literate any time soon. In my experience, too many so-called developers don't get objects. They'll never master aspects!&lt;/p&gt;
&lt;h4&gt;Shameless Plug&lt;/h4&gt;
&lt;p&gt;If you would like to hear more of my ideas about AOP in Ruby and aspect-oriented design (AOD), please come to my &lt;a href="https://www.cmpevents.com/SDw7/a.asp?option=G&amp;amp;V=3&amp;amp;id=447368"&gt;talk&lt;/a&gt; at SD West, this Friday at 3:30. I'm also giving a &lt;a href="http://web4.cs.ucl.ac.uk/icse07/index.php?id=90"&gt;full-day tutorial&lt;/a&gt; on AOD in Ruby and Java/AspectJ at the ICSE 2007 conference in Minneapolis, May 26th.&lt;/p&gt;</description>
      <pubDate>Wed, 21 Mar 2007 13:21:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3cf85795-7574-4b92-89a5-49b78f80f4ce</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/03/21/aop-and-dynamic-languages-contradiction-in-terms-or-match-made-in-heaven</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>design</category>
      <category>AOP</category>
      <category>AOD</category>
      <category>Ruby</category>
      <category>Dynamic Languages</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/5268</trackback:ping>
    </item>
    <item>
      <title>AOSD 2007 Conference</title>
      <description>&lt;p&gt;Last week, I attended the &lt;a href="http://www.aosd.net/2007"&gt;Aspect-Oriented Software Development 2007 Conference&lt;/a&gt; in Vancouver, BC, where I gave a &lt;a href="http://www.aosd.net/2007/program/tutorials/T2-aod.php"&gt;tutorial&lt;/a&gt; on aspect-oriented design and a &lt;a href="http://www.aosd.net/2007/program/industry/I6-AspectDesignPrinciples.pdf"&gt;paper&lt;/a&gt; in the &lt;a href="http://www.aosd.net/2007/program/industry/index.php"&gt;Industry Track&lt;/a&gt;, also about design.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;AOSD&lt;/span&gt; and this particular conference are still mostly academic projects with some notable industry traction, especially in the Java world. It is also a technology that needs to break through to the next level of innovation and applicability, in my opinion.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://www.aosd.net/2007/program/industry/index.php"&gt;Industry Track&lt;/a&gt; had a number of interesting papers, including, for example, a paper that describes how aspects are used in the innovative &lt;a href="http://terracotta.org/"&gt;Terracotta&lt;/a&gt; JVM clustering tool. Also, the last keynote by Adrian Colyer of &lt;a href="http://interface21.com/"&gt;Interface 21&lt;/a&gt; recounted his personal involvement in the AspectJ and Spring communities, as well as the impact that aspects are having at major Interface 21 clients. It&amp;#8217;s worth noting that many of the important Java middleware systems, &lt;i&gt;e.g.,&lt;/i&gt; Spring, JBoss, and Weblogic have embraced aspects to one degree or another.&lt;/p&gt;
&lt;p&gt;&lt;span class="caps"&gt;AOSD&lt;/span&gt; tools like AspectJ and Spring &lt;span class="caps"&gt;AOP&lt;/span&gt; (AO &lt;i&gt;Programming&lt;/i&gt;) solve obvious &amp;#8220;cross-cutting concerns&amp;#8221; (CCCs) like object-relational mapping, transactions, security, &lt;i&gt;etc.&lt;/i&gt; in Java. However, I feel that &lt;span class="caps"&gt;AOSD&lt;/span&gt; needs some breakthrough innovations to move beyond its current role as a useful niche technology to a more central role in the software development process. I get the impression that industry interest in &lt;span class="caps"&gt;AOP&lt;/span&gt; has reached a plateau.&lt;/p&gt;
&lt;p&gt;The academic community is doing some interesting work on fundamentals of &lt;span class="caps"&gt;AOSD&lt;/span&gt; theory (type theory, modeling, categorizing types of aspects, &lt;i&gt;etc.&lt;/i&gt;), on &amp;#8220;early aspects&amp;#8221; (&lt;i&gt;e.g.,&lt;/i&gt; cross-cutting requirements), and on tooling. There is also a lot of minor iterating around the edges, but that&amp;#8217;s the nature of academic research (speaking as one who has been there&amp;#8230;). Most of the research work is a long ways from practical applicability.&lt;/p&gt;
&lt;p&gt;However, I&amp;#8217;m seeing too much emphasis on extending the work of AspectJ-like approaches in statically-typed languages, rather than innovating in new areas like new applications of aspects and the nature of &lt;span class="caps"&gt;AOSD&lt;/span&gt; in dynamic languages.&lt;/p&gt;
&lt;p&gt;My recent work with Ruby has made me think about these two topics lately. I&amp;#8217;ll blog about these topics later, but for now, I&amp;#8217;ll just say that I anticipate a fruitful growth area for &lt;span class="caps"&gt;AOSD&lt;/span&gt; will be to facilitate the implementation of powerful DSLs (Domain Specific Languages), a popular topic in the Ruby community.&lt;/p&gt;
&lt;p&gt;Here are some other observations from the conference.&lt;/p&gt;
&lt;h3&gt;All other engineering disciplines recognize cross-cutting concerns&lt;/h3&gt;
&lt;p&gt;Gregor Kiczales (the father of AspectJ and one of the fathers of &lt;span class="caps"&gt;AOSD&lt;/span&gt;) made this remark in a panel discussion on &amp;#8220;early aspects&amp;#8221;. He cited the examples of electrical engineers considering systemic issues in circuit design, like capacitance, current leakage, &lt;i&gt;etc.&lt;/i&gt; and mechanical engineers who evaluate the stresses and strains of the entire structure they are designing (buildings, brake assemblies in cars, &lt;i&gt;etc.&lt;/i&gt;).&lt;/p&gt;
&lt;p&gt;Gregor also remarked that CCCs that are evident in the requirements may disappear in the implementation and &lt;i&gt;vice-versa&lt;/i&gt;.&lt;/p&gt;
&lt;h3&gt;Gerald Sussman keynote&lt;/h3&gt;
&lt;p&gt;In the first keynote, &lt;a href="http://www-swiss.ai.mit.edu/~gjs/gjs.html"&gt;Gerald Sussman&lt;/a&gt; argued that robust systems are adaptable for uses that were not anticipated by their designers. These systems often have multiple organizational ideas and a &amp;#8220;component&amp;#8221; structure that promotes &amp;#8220;combinatorial behavior&amp;#8221;.&lt;/p&gt;
&lt;p&gt;He doesn&amp;#8217;t like formalisms such as Dykstra&amp;#8217;s &lt;i&gt;A Discipline of Programming&lt;/i&gt;. Provable correctness and rigor aren&amp;#8217;t very compatible with rich programs. Sussman prefers a more &amp;#8220;exploratory&amp;#8221; model, very much analogous to Test Driven Development (TDD), and what he calls &lt;i&gt;Paranoid Programming&lt;/i&gt;, which he defined as &amp;#8220;I won&amp;#8217;t be at fault if it fails.&amp;#8221;&lt;/p&gt;
&lt;h3&gt;More on &lt;span class="caps"&gt;AOSD&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;I will blog further about aspect-oriented design and aspects in dynamic languages.&lt;/p&gt;</description>
      <pubDate>Wed, 21 Mar 2007 09:41:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:766fed9c-5a60-457d-a7b5-80aca0ae4692</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/03/21/aosd-2007-conference</link>
      <category>Dean's Deprecations</category>
      <category>aspects</category>
      <category>AOSD</category>
      <category>design</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/4765</trackback:ping>
    </item>
    <item>
      <title>Liskov Substitution Principle and the Ruby Core Libraries</title>
      <description>&lt;p&gt;There is a spirited discussion happening now on the ruby-talk list called &lt;a href="http://www.ruby-forum.com/topic/97965&lt;a href="""&gt;&amp;gt;Oppinions on &lt;span class="caps"&gt;RCR&lt;/span&gt; for dup on immutable classes&lt;/a&gt; (sic).&lt;/p&gt;


	&lt;p&gt;In the core Ruby classes, the &lt;code&gt;Kernel&lt;/code&gt; module, which is the root of everything, even &lt;code&gt;Object&lt;/code&gt;, defines a method called &lt;code&gt;dup&lt;/code&gt;, for duplicating objects. (There is also a &lt;code&gt;clone&lt;/code&gt; method with slightly different behavior that I won&amp;#8217;t discuss here.)&lt;/p&gt;


	&lt;p&gt;The problem is that some derived core classes throw an exception when &lt;code&gt;dup&lt;/code&gt; is called.&lt;/p&gt;


Specifically, as the ruby-talk discussion title says, it&amp;#8217;s the &lt;em&gt;immutable&lt;/em&gt; classes (&lt;code&gt;NilClass, FalseClass, TrueClass, Fixnum,&lt;/code&gt; and &lt;code&gt;Symbol&lt;/code&gt;) that do this. Consider, for example, the following &lt;strong&gt;irb&lt;/strong&gt; session: 
&lt;pre&gt;
irb 1:0&amp;gt; 5.respond_to? :dup
=&amp;gt; true
irb 2:0&amp;gt; 5.dup
TypeError: can't dup Fixnum
        from (irb):1:in `dup'
        from (irb):1
irb 3:0&amp;gt; 
&lt;/pre&gt;
If you don&amp;#8217;t know Ruby, the first line asks the &lt;code&gt;Fixnum&lt;/code&gt; object &lt;code&gt;5&lt;/code&gt; if it &lt;em&gt;responds to&lt;/em&gt; the method &lt;code&gt;dup&lt;/code&gt; (with the name expressed as a symbol, hence the&lt;/a&gt;). The answer is true, becuase this method is defined by the module &lt;code&gt;Kernel&lt;/code&gt;, which is included by the top-level class &lt;code&gt;Object&lt;/code&gt;, an ancestor of &lt;code&gt;Fixnum&lt;/code&gt;.

	&lt;p&gt;However, when you actually call &lt;code&gt;dup&lt;/code&gt; on &lt;code&gt;5&lt;/code&gt;, it raises &lt;code&gt;TypeError&lt;/code&gt;, as shown.&lt;/p&gt;


	&lt;p&gt;So, this looks like a classic &lt;a href="http://www.objectmentor.com/resources/articles/lsp.pdf"&gt;Liskov Substitution Principle&lt;/a&gt; violation. The term for this code smell is &lt;em&gt;Refused Bequest&lt;/em&gt; (&lt;em&gt;e.g.,&lt;/em&gt; see &lt;a href="http://www.soberit.hut.fi/mmantyla/BadCodeSmellsTaxonomy.htm"&gt;here&lt;/a&gt;) and it&amp;#8217;s typically fixed with the &lt;em&gt;refactoring&lt;/em&gt; &lt;a href="http://www.refactoring.com/catalog/replaceInheritanceWithDelegation.html"&gt;Replace Inheritance with Delegation&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;The email thread is about a proposal to change the library in one of several possible ways. One possibility is to remove &lt;code&gt;dup&lt;/code&gt; from the &lt;em&gt;immutable&lt;/em&gt; classes. This would eliminate the unexpected behavior in the example above, since &lt;code&gt;5.respond_to?(:dup)&lt;/code&gt; would return false, but it would still be an &lt;span class="caps"&gt;LSP&lt;/span&gt; violation, specifically it would still have the &lt;em&gt;Refused Bequest&lt;/em&gt; smell.&lt;/p&gt;


	&lt;p&gt;One scenario where the current behavior causes problems is doing a deep copy of an arbitrary object graph. For immutable objects, you would normally just want &lt;code&gt;dup&lt;/code&gt; to return the same object. It&amp;#8217;s immutable, right? Well, not exactly, since you can re-open classes and even objects to add and remove methods in Ruby (there are some limitations for the immutables&amp;#8230;).  So, if you thought you actually duplicated something and started messing with its methods, you would be surprised to find the original was &amp;#8220;also&amp;#8221; modified.&lt;/p&gt;


	&lt;p&gt;So, how serious is this &lt;span class="caps"&gt;LSP&lt;/span&gt; issue (one of several)? When I pointed out the problem in the discussion, one respondent, Robert Dober, said the following (edited slightly):&lt;/p&gt;


	&lt;p&gt;&lt;i&gt;
I would say that &lt;span class="caps"&gt;LSP&lt;/span&gt; does not apply here simply because in Ruby we do
not have that kind of contract. In order to apply &lt;span class="caps"&gt;LSP&lt;/span&gt; we need to say at a point we have an object of class &lt;code&gt;Base&lt;/code&gt;, for example. (let the gods forgive me that I use Java)&lt;/i&gt;
&lt;pre&gt;
void aMethod(final Base b){
   ....
}
&lt;/pre&gt;
&lt;i&gt;and we expect this to work whenever we call aMethod with an object
that is a Base.
Anyway the compiler would not really allow otherwise.&lt;/i&gt;
&lt;pre&gt;
SubClass sc;  // subclassing Base od course
aMethod( sc ); // this is expected to work (from the type POV).
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;i&gt;Such things just do not exist in Ruby, I believe that Ruby has
explained something to me:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;OO Languages are Class oriented languages&lt;/li&gt;
		&lt;li&gt;Dynamic Languages are Object oriented languages.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Replace Class with Type and you see what I mean.&lt;/p&gt;


	&lt;p&gt;This is all very much &lt;span class="caps"&gt;IMHO&lt;/span&gt; of course but I feel that the Ruby
community has made me evolve a lot away from &amp;#8220;Class oriented&amp;#8221;.&lt;/i&gt;&lt;/p&gt;


	&lt;p&gt;He&amp;#8217;s wrong that the compiler protects you in Java; you can still throw exceptions, &lt;em&gt;etc.&lt;/em&gt; The &lt;span class="caps"&gt;JDK&lt;/span&gt; Collection classes have &lt;em&gt;Refused Bequests&lt;/em&gt;. Besides that, however, he makes some interesting points.&lt;/p&gt;


	&lt;p&gt;As a long-time Java programmer, I&amp;#8217;m instinctively uncomfortable with &lt;span class="caps"&gt;LSP&lt;/span&gt; violations. Yet, the Ruby &lt;span class="caps"&gt;API&lt;/span&gt; is very nice to work with, so maybe a little &lt;span class="caps"&gt;LSP&lt;/span&gt; violation isn&amp;#8217;t so bad?&lt;/p&gt;


	&lt;p&gt;As Robert says, we approach our designs differently in dynamic &lt;em&gt;vs.&lt;/em&gt; static languages. In Ruby, you almost never use the &lt;code&gt;is_a?&lt;/code&gt; and &lt;code&gt;kind_of?&lt;/code&gt; methods to check for type. Instead, you follow the &lt;em&gt;duck typing&lt;/em&gt; philosophy (&amp;#8220;If it acts like a duck, it must be a duck&amp;#8221;); you rely on &lt;code&gt;respond_to?&lt;/code&gt; to decide if an object does what you want.&lt;/p&gt;


	&lt;p&gt;In the case of &lt;code&gt;dup&lt;/code&gt; for the immutable classes, I would prefer that they not implement the method, rather than throw an exception. However, that would still violate &lt;span class="caps"&gt;LSP&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;So, can we still satisfy &lt;span class="caps"&gt;LSP&lt;/span&gt; and also have rich base classes and modules?&lt;/p&gt;


	&lt;p&gt;There are many examples of &lt;em&gt;traits&lt;/em&gt; that one object might or should support, but not another. (Those of you Java programmers might ask yourself why all objects support &lt;code&gt;toString&lt;/code&gt;, for example. Why not also &lt;code&gt;toXML&lt;/code&gt;...?)&lt;/p&gt;


	&lt;p&gt;Coming from an &lt;a href="http://aspectprogramming.com"&gt;&lt;span class="caps"&gt;AOP&lt;/span&gt;&lt;/a&gt; background, I would rather see an architecture where &lt;code&gt;dup&lt;/code&gt; is added only to those classes and modules that can support it. It shouldn&amp;#8217;t be part of the standard &amp;#8220;signature&amp;#8221; of &lt;code&gt;Kernel&lt;/code&gt;, but it should be present when code actually needs it.&lt;/p&gt;


In fact, Ruby makes this sort of &lt;span class="caps"&gt;AOP&lt;/span&gt; &lt;a href="c2.com/cgi/wiki?AspectsAndDynamicLanguages"&gt;easy&lt;/a&gt; to implement. Maybe &lt;code&gt;Kernel&lt;/code&gt;, &lt;code&gt;Module&lt;/code&gt;, and &lt;code&gt;Object&lt;/code&gt; should be refactored into smaller pieces and programmers should declaratively mixin the &lt;em&gt;traits&lt;/em&gt; they need. Imagine something like the following:
&lt;pre&gt;
irb 1:0&amp;gt; my_obj.respond_to? :dup
=&amp;gt; false
irb 2:0&amp;gt; include 'DupableTrait'  
irb 2:0&amp;gt; my_obj.respond_to? :dup
=&amp;gt; true
irb 4:0&amp;gt; def dup_if_possible items
irb 5:1&amp;gt;  items.map {|item| item.respond_to?(:dup) ? item.dup : item}
irb 6:1&amp;gt; end
...
&lt;/pre&gt;
In other words, &lt;code&gt;Kernel&lt;/code&gt; no longer &amp;#8220;exposes the &lt;code&gt;dup&lt;/code&gt; abstraction&amp;#8221;, by default, but the &lt;code&gt;DupableTrait&lt;/code&gt; module &amp;#8220;magically&amp;#8221; adds &lt;code&gt;dup&lt;/code&gt; to all the classes that can support it. This way, we preserve &lt;span class="caps"&gt;LSP&lt;/span&gt;, streamline the core classes and modules (&lt;a href="http://www.objectmentor.com/resources/articles/srp"&gt;&lt;span class="caps"&gt;SRP&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://www.objectmentor.com/resources/articles/isp.pdf"&gt;&lt;span class="caps"&gt;ISP&lt;/span&gt;&lt;/a&gt; anyone?), yet we have the flexibility we need, on demand.</description>
      <pubDate>Sat, 17 Feb 2007 14:20:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:767b95e8-95d2-45dd-8aa0-09dfb4fe92ec</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/02/17/liskov-substitution-principle-and-the-ruby-core-libraries</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Ruby</category>
      <category>object-oriented design</category>
      <category>Liskov Substitution Principle</category>
      <category>LSP</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/188</trackback:ping>
    </item>
    <item>
      <title>Phantom (Menace) Dependencies</title>
      <description>&lt;p&gt;We&amp;#8217;ve been working on new course materials lately. The other day we were discussing the bad dependencies in code that can occur when the &lt;a href="http://www.objectmentor.com/resources/articles/isp.pdf"&gt;Interface Segregation Principle&lt;/a&gt; (ISP) is violated.&lt;/p&gt;


	&lt;p&gt;We started calling these dependencies &lt;i&gt;phantom dependencies&lt;/i&gt;.  Here&amp;#8217;s why&amp;#8230;&lt;/p&gt;


	&lt;p&gt;In the simplest example of an &lt;span class="caps"&gt;ISP&lt;/span&gt; problem, several client components depend on a &amp;#8220;server&amp;#8221; component, but each uses completely independent facilities provided by that component. There are no direct or indirect connections between clients. So, none of the clients knows anything about the other clients, nor cares to, yet each client has an implicit dependency on the other clients.&lt;/p&gt;


	&lt;p&gt;Why? Because if a change in one client forces a change in the server component, the other clients are affected. At the very least, a rebuild may be required.&lt;/p&gt;


	&lt;p&gt;The solution is to hide the server component behind segregated interfaces, one tailored for each client, and have the server component implement those interfaces. Even better, if the features really are independent, then segregate the server component, too!&lt;/p&gt;


	&lt;p&gt;Anyway, we sometimes use the term &lt;i&gt;back-channel dependency&lt;/i&gt; for this situation. However, while thinking about it, I remembered an article that I had read a few days before on the phantom pain phenomenon that amputees sometimes experience, where they feel pain in limbs that are no longer there. The &lt;span class="caps"&gt;ISP&lt;/span&gt; case is analogous; there is no traversable (&amp;#8220;real&amp;#8221;) connections  from one client component to another, yet each &amp;#8220;feels&amp;#8221; the presence of the others. Hence, the term &lt;i&gt;phantom dependency&lt;/i&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 26 Jan 2007 18:07:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:a7476b0f-1ca7-4c94-9b5b-f24e573f4b95</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/01/26/phantom-menace-dependencies</link>
      <category>Dean's Deprecations</category>
      <category>design</category>
      <category>ISP</category>
    </item>
    <item>
      <title>Protecting Developers from Powerful Languages</title>
      <description>&lt;p&gt;Microsoft&amp;#8217;s forthcoming C# version 3 has some innovative features, as described in this &lt;a href="http://tomasp.net/articles/csharp3-concepts.aspx"&gt;blog&lt;/a&gt;. I give the C# team credit for pushing the boundaries of C#, in part because they have forced the Java community to follow suit. ;)
&lt;/p&gt;
&lt;p&gt;A common tension in many development shops is how far to trust the developers with languages and tools that are perceived to be &amp;#8220;advanced&amp;#8221;. It&amp;#8217;s tempting to limit developers to &amp;#8220;safe&amp;#8221; languages and maybe not all the features of those languages. This can be misguided.&lt;/p&gt;

&lt;p&gt;Java is usually considered safe, but Java Generics are suspect. Strong typing is safe, but dynamic typing isn&amp;#8217;t controlled enough. Closures and continuations sound too advanced and technical to be trusted in the hands of &amp;#8220;our team&amp;#8221;.&lt;/p&gt;
&lt;p&gt;To be fair, larger organizations have more at stake and caution is prudent. Regrettably, it is also true that many people in our profession are &amp;#8230; hmm &amp;#8230; not that well qualified.&lt;/p&gt;
&lt;p&gt;However, I find that I&amp;#8217;m far more productive and less likely to make mistakes using Ruby iterators with closures than writing more verbose and inelegant  Java.&lt;/p&gt;
&lt;p&gt;I used to be a strong believer in static typing, but it has become a distraction, as I have to worry more about the types of method parameters and return values, rather than just worrying about the &lt;i&gt;values&lt;/i&gt; themselves. I realized that, on average in a typical section of code, the actual type of a variable is unimportant. The variable is just a &amp;#8220;handle&amp;#8221; being passed around. The name is always important, as it is a form of documentation. There are places where the type &lt;i&gt;is&lt;/i&gt; important, of course, when the variable is read or written in some way.&lt;/p&gt;
&lt;p&gt;Finally, static typing offers less security than at first appears. At best, it only confirms that variables of particular types are used consistently. Your unit tests also do this. However, static typing can&amp;#8217;t confirm that the &lt;i&gt;usage&lt;/i&gt; of the &lt;span class="caps"&gt;API&lt;/span&gt; is correct. This is analogous to testing the &lt;i&gt;syntax&lt;/i&gt; but not the &lt;i&gt;semantics&lt;/i&gt; of the program. In fact, only unit tests (or alternatives, like &lt;a href="http://rspec.rubyforge.org"&gt;rspec&lt;/a&gt; ) are effective at testing both.&lt;/p&gt; 
&lt;p&gt;So, it&amp;#8217;s prudent to be reticent about newer languages and features, but make sure the decisions you make about them are backed up by careful evaluation and don&amp;#8217;t forget to train your team appropriately!&lt;/p&gt;</description>
      <pubDate>Mon, 15 Jan 2007 21:57:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:bbefa294-6ed3-4a00-aa59-25b369d7c408</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/01/15/protecting-developers-from-powerful-languages</link>
      <category>Dean's Deprecations</category>
      <category>Java</category>
      <category>Ruby</category>
      <category>languages</category>
      <category>development</category>
    </item>
  </channel>
</rss>

