Finally blocks

Finally blocks are underrated. They’re really very useful for removing duplication in code with multiple exit points. Consider the following:

public int foo(boolean condition1, boolean condition2) {
setMouseBusy();
if (condition1) {
doOne();
setMouseNormal();
return 1;
}
if (condition2) {
doTwo();
setMouseNormal();
return 2;
}
setMouseNormal();
return 0;
}

Compare with this:

public int foo(boolean condition1, boolean condition2) {
try {
setMouseBusy();
if (condition1) {
doOne();
return 1;
}
if (condition2) {
doTwo();
return 2;
}
return 0;
} finally {
setMouseNormal();
}
}

Why is the second form better? Because no matter how the method exits (including throwing exceptions), the finally block will be executed. In the first form, if either of the ‘do’ calls throw an exception the call to setMouseNormal will be skipped, possibly leaving the application with a misleading hourglass cursor.

No, please no!

This is just horrible! How many characters needed? I can feel my wrists ache just reading it (which is also strenuous due to all the XML taggage). XML is designed to be easily parsed by machines, not people! What is the point of a language where ‘XML is the underlying construct, not ASCII’? Call me a luddite, but I mean really, what does it offer over good old fashioned text?

Cool Programming Language Concept: SuperX++.

Cool Programming Language Concept: SuperX++

This is neat: Super X++.  It is a language where XML is the underlying programming construct as opposed to ASCII.  And, yes Virginia, it is Open Source.[_Go_]

Here is a simple example from the FAQ:

How do I code “Hello World!” in Superx++?
The following code will be a full Superx++ program that returns the string “Hello World!” to the Superx++ client (whatever process calls a Superx++ program):

<xpp>
   <xout>Hello World!</xout>
</xpp>

And here is a complex example:

How do I define a class?
A class in Superx++ is defined using the <class> statement. An example follows:
<class name=”XTree” inherit=”XPlant”>
   <construct>
      <scope type=”public”>
         <Chlorophylic>yes</Chlorophylic>
      </scope>
   </construct>
   <scope type=”public”>
      <func type=”string” name=”GetChlorophylic”>
         <body>
            <return>
               <eval object=”Chlorophylic” />
            </return>
         </body>
      </func>
      <func type=”void” name=”SetChlorophylic”>
         <parm type=”string” name=”sVal” pass=”val” />
         <body>
            <eval object=”Chlorophylic”>
               <eval object=”sVal” />
            </eval>
         </body>
      </func>
      <func type=”int” name=”GetAge”>
         <body>
            <return>
               <eval object=”this” member=”Age” />
            </return>
         </body>
      </func>
      <func type=”void” name=”SetAge”>
         <parm type=”int” name=”sVal” pass=”val” />
         <body>
            <eval object=”this” member=”Age”>
               <eval object=”sVal” />
            </eval>
         </body>
      </func>
   </scope>
   <scope type=”protected”>
      <var type=”int” name=”Age”>0</var>
   </scope>
</class>

The statement above declares a class called XTree which inherits from the class XPlant which contains an object called Chlorophylic. Every time an object of class XTree is instantiated it will be instantiated along with a contained object called Chlorophylic. The class XTree also defines four methods and one member variable. For more details on classes click here.

Thanks to Dr. Dobbs Journal for turning me on to this.

[The FuzzyBlog!]

Language Lessons

Joe’s Jelly

…it’s a new year now and I’m looking for a new language. So what do I learn?

Smalltalk has been recommended but I think I may have already learned some of it’s most important lessons through Ruby.

From chatting to colleagues, I’ve narrowed the list down to:

  • LISP (with CLOS and MOP)
  • Scheme
  • Haskall
  • Self

So, which one should it be? How will they change the way I think? What will you be learning this year?

Last year I looked at:

Python, Ruby, Lisp, Scheme and Haskell. I bought books about 2 of them. I actually ended up learning Perl. Which, if you’re a zealot, means you learned the best bits of all of them. Not my place to comment on that though. I also dabbled a bit in Smalltalk, to the extent I can now read it, if not yet code fluently in it.

From your list, Joe, I’d say go for Scheme if you feel like exploring just how much can be done with just lists and heaps of recursion. Or try Haskell for some truly functional coding. Both paradigms are significantly differerent from (ahem) run-of-the-mill OO development.

My current thoughts for this year are that I want to try Ruby or Haskell or something truly off the wall like OCAML or Erlang.

I’ll probably end up learning Python as I’ve started getting into Marathon Man, which uses Jython to script its tests. I find learning a new language without a context to apply it to somewhat dry and rather unfulfilling. Its similar to how much more quickly you learn a foreign tongue when you are in that country than you do from books and lessons.