Mark Thomas

Deceptively random thoughts

Archive for August, 2008

Scala: Type safe duck typing

with 5 comments

If it walks like a duck and quacks like a duck, I would call it a duck.

Duck typing uses the set of methods and properties of an object, rather than its position in a type hierarchy to decide what you can do with it.

Scala’s structural typing feature provides something close to duck typing in a type safe way. A structural type is close to a .NET anonymous type; a type with a set of methods and properties but with no name. An example in Scala based on one of Wikipedia’s examples of duck typing:

class Duck {
  def quack = println("Quaaaaaack !")
  def feathers = println("The duck has white and gray feathers.")
}
 
class Person {
  def quack = println("The person imitates a duck.")
  def feathers = println("The person takes a feather from the ground and shows it.")
}
 
def inTheForest(duck: { def quack; def feathers }) = {
  duck.quack
  duck.feathers
}

The inTheForest function declares that it will accept any object that has a quack and feathers method, both of which take no parameters and return Unit (like void in Java/C#). When executed, the code works as expected:

scala> inTheForest(new Person)
The person imitates a duck.
The person takes a feather from the ground and shows it.

scala> inTheForest(new Duck)
Quaaaaaack !
The duck has white and gray feathers.

To demonstrate the type safety, lets try using a string:

scala> inTheForest("Duck")
:6: error: type mismatch;
 found   : java.lang.String("Duck")
 required: AnyRef{def quack: Unit; def feathers: Unit}
       inTheForest("Duck")
                   ^

When is this useful? Mostly in situations where you are consuming classes that follow common conventions but have no shared interface, in this case you can define the parts you are interested in by using the structural type as an implicit interface.

Finally, if you find that you are using a structural type a lot, it can define it in one place:

object UsefulTypes {
  type Duck = { def quack; def feathers }
}

inTheForest can then be declared as:

import UsefulTypes.Duck
 
def inTheForest(duck: Duck) = {
  duck.quack
  duck.feathers
}
Share

Written by Mark Thomas

August 27th, 2008 at 6:40 pm

Project Cancellation

with one comment

A well run software project should have a set of objectives that it is trying to meet. Sometimes projects will fail or be cancelled when it becomes clear that those objectives cannot be met within constraints, such as time-scale or budget, that form part of the project’s business case.

When a project is cancelled, it can have a profound impact on the team. It is important to manage the process of cancellation so that team members can smoothly transition in to their next project.  How this is handled can impact team members’ productivity now and in the future, as well as their perception of the company and its managers.

Team members should understand the reasons for project cancellation and believe them, if possible the teams should be part of the discussions around cancellation even if they can’t influence them.  Once team members leave the project they should be rolled on to new projects as soon as possible and given productive work to do.

It is important to realise that project cancellation is often the right action.  Projects are planned based on imperfect information; as projects progress and teams learn more then the decisions on project viability can change too.  Often it is difficult to act on this changing information, particularly in public sector projects, as it could directly impact the reputation of the project sponsors, and undermine the strong emotional engagement that team members have made in delivery.  To make the best decisions, projects should be monitored continually, and early and regular feedback obtained from all project stakeholders so that informed decisions can be made and openly communicated.

At the end of a project, a project retrospective should be run to give the team an opportunity to take stock and take away lessons for the future.  Retrospectives should be balanced, looking at the good and the bad and providing a basis for team members’ future work and a celebration (cue the beer) of the teams achievements.

Share

Written by Mark Thomas

August 22nd, 2008 at 2:40 pm

Ever wondered where your time goes?

without comments

Slife is a great free app for the Mac that sits in the background and logs how much time you spend in each app that you have running.

By telling it when you start a new activity, it will give you an idea of how you spent your time during that activity.  So, for example if you are trying to ‘Implement a basic Rails app’, you can find out how much time you spent reading e-mail, browsing the web or editing your source.  I found it quite enlightening and it made me think more about ways that I can stay more focused.

If you want to understand where you time goes and you have a mac, Slife is the app for you.

Thanks to Floyd for the tip.

Share

Written by Mark Thomas

August 22nd, 2008 at 12:22 pm

Moving from Java to C#

with one comment

After about 10 years working primarily in Java, I’m a few weeks in to my first commercial C# project. The transition was much smoother than I’d expected and after some head-scratching around the syntax additions in C# 3, I feel productive.

I’m surprised how little time I have needed to spend learning the .NET framework libraries and exploring the .NET open source community.  As a business app, most of the code I am writing models the domain and interacts with other parts of the domain; it is all written by the team.  With the exception of things like the collections classes, most of the framework/library interaction is on the fringes of the system.  That said I’ve done my fair share of Googling things like ‘How do I parse XML in .NET’, but the community is large and so it’s easy to find solutions to problems. A harder part is learning C# idioms, the Manning book C# in Depth was great for this (see later) as were chats with my teammates and pair.

Along the way I’ve picked up a few useful resources:

Resources

Other thoughts

  • Get Resharper
  • If you are familiar with Java IDEs like Eclipse or IntelliJ and regularly re-factor then you will need Resharper. The out of the box support for refactoring is minimal even in VS2008.

  • Remember there is an eco-system outside of Microsoft
  • Many companies having selected .NET will automatically favour Microsoft solutions for everything. Even though the open-source community doesn’t appear as vibrant as for Java, it does exist. Check out alt.net too.

Share

Written by Mark Thomas

August 20th, 2008 at 3:45 pm

Polyglot programming with IntelliJ 8

without comments

Artima interviewed JetBrains’s Dmitry Jemerov at JavaOne 2008 back in June, the interview notes are here: http://www.artima.com/lejava/articles/javaone_2008_dmitry_jemerov.html.

Dmitry makes some interesting comments around Polyglot Programming and how Jetbrains plan to add new language support to IntelliJ. I’m glad to see a move towards more modularisation and particularly a way of specifying which features to enable (e.g. Java, SVN etc) and even for disabling Java when using another language. This change should go a long way to addressing the bloat, and sluggishness, of earlier IntelliJ releases.

The most exciting feature for me is Scala support. I’m a big fan of Scala, but I see two barriers to adoption: the available documentation is limited, particularly good examples; and it’s quite a complex language. Dmitry touched on this last point but I don’t really agree that it’s as complex as C++. Artima have a Scala book in the works which is a fantastic way to pick up the language.

The IntelliJ EAP releases can be downloaded from http://www.jetbrains.com/idea/nextversion/.

Share

Written by Mark Thomas

August 19th, 2008 at 5:35 pm

Lotus Notes 8.5 won’t launch on Mac OS X 10.5.3/10.5.4

with one comment

IBM have released a public beta of Lotus Notes 8.5 for the Mac. It seems a lot better than version 7, but after applying the OS X 10.5.3 update I found that I could not start Notes. The only feedback of a problem was in the system log:

[0x0-0x34034].com.ibm.lotus.Notes[546] maclauncher: unrecognized option `-psn_0_213044′

The workaround I’m using is to run the ‘Notes’ application from inside the application bundle. From the terminal:

/Applications/Notes.app/Contents/MacOS/Notes

There is a discussion on the IBM forums covering this so I hope it will be fixed in the next public beta: http://www-10.lotus.com/ldd/nd85forum.nsf/DateAllThreadedWeb/d1ef5d3d0a322049852574600046e61e?OpenDocument

Share

Written by Mark Thomas

August 15th, 2008 at 4:17 pm

Posted in Feed to Planet TW