Skip to content
I love GMail's 'Archive' feature. I wanted the same in Outlook which I am currently forced to use. So...

Sub Archive()
Set myItem = Application.Explorers.Item(0).Selection.Item(1)

If myItem.Class = olMail Then
myItem.UnRead = False
myItem.Move (Application.Explorers.Item(1).CurrentFolder().Parent.Folders("Archive"))
End If
End Sub


This little macro marks an email read and moves the current selected item to the 'Archive' folder (that you will need to create at the top level).

I also added a button to run the macro and called it "&XArchive" so I can go Alt+X to archive the currently selected mail item.

Works for me..
If anyone can be bothered to get it to work for all selected items that would be cool.

Oh, I'm using Outlook 2000. You may need to change the macro for other version.
Let me know :)

As I mentioned in an earlier post, Scala offers many alternatives for unit testing. With its Java interoperability using JUnit or TestNg is no more difficult than using the purpose built testing frameworks for Scala such as ScalaTest, Rehersal, specs, and ScalaCheck.

My personal favourite is specs. The specs testing framework, is a Behaviour-Driven-Design testing tool written in Scala by Eric Torreborre.

Below is a very simple example of a specs test.


package com.gigantiq.thing
import org.specs._

object ThingSpecification extends Specification {
  "A Thing" should {
    "have a name equal to the passed value" in {
      val thing = new Thing("myThing")
      thing.name mustMatch "myThing"
    }

    "throw an IllegalArgumentException if passed a blank name" in {
      new Thing("") must throwA(new IllegalArgumentException)
    }
  }
}

The import org.specs._ statement is importing everything from the import org.specs package. The _ is equivalent to * in a java import statement.

Next we declare a specification with the following syntax…


object ThingSpecification extends Specification {

...

}

They object key word in Scala declares a singleton object. So in this case we have declared a singleton called ThingSpecification that extends the abstract class called Specification as provided by the specs library.

The specs library utilizes Scala’s flexibility to define a DSL that allows us to construct a very human readable specification.


  "A Thing" should {
    "have a name equal to the passed value" in {
      val thing = new Thing("myThing")
      thing.name mustMatch "myThing"
    }

    "throw an IllegalArgumentException if passed a blank name" in {
      new Thing("") must throwA(new IllegalArgumentException)
    }
  }

Looking at this example we can see clearly that we are expecting the following behavior from our application that we are writing.

  • A Thing should have a name equal to the passed value.
  • A Thing should throw an IllegalArgumentException if passed a blank name.

Before we can run this Specification we need the Thing class otherwise our tests will fail. So in the conciseness of Scala, here it is:


package com.gigantiq.thing

class Thing(val name: String) {
  require(name.length > 0)
}

We can now compile our application. Once we have done that we can run our specification using the scala command.


andytrigg$ scala -cp specs-1.3.1.jar com.gigantiq.thing.ThingSpecification

Upon execution of this specification we receive the following output to our console.

Specification "ThingSpecification"
  A Thing should
    + have a name equal to the passed value
    + throw an IllegalArgumentException if passed a blank name

Total for specification "ThingSpecification":
Finished in 0 second, 49 ms
2 examples, 2 assertions, 0 failure, 0 error

Just for completeness I would like to show what happens in the event of a test failing. If we remove the precondition check(require(name.length > 0)) from our Thing class and rerun our specification, we will get the following output.

Specification "ThingSpecification"
  A Thing should
    + have a name equal to the passed value
    x throw an IllegalArgumentException if passed a blank name
      java.lang.IllegalArgumentException should have been thrown (ThingSpecification.scala:12)

Total for specification "ThingSpecification":
Finished in 0 second, 49 ms
2 examples, 2 assertions, 1 failure, 0 error

In the above examples I illustrated the use of two of the matchers that specs offers out of the box. Matchers are used for specifying the assertion being made in the specifications. The ones we have used here are:

      mustMatch: used for asserting matching strings
      must throwA: used to evaluate that an expected exception is thrown

In addition to these, specs offers many matchers and it is very easy to write your own if none of the standard matchers suffice.

As you can see, unit testing doesn’t get much simpler than that. I love those specs. :)

If I intend on writing even a smallish application there are two things I look for in regards to the tool support of the language.

  1. I hope the language has reasonable IDE support. By reasonable it should offer me a moderate number of code refactorings as a minimum.
  2. The language should integrate nicely with an automated build system so that i can incorparate the build in to a continuous integration environment.

One of the factors that can make or break the success of a language is the IDE support. Currently Scala’s IDE options are not great. Nothing that i have seen in regards to Scala’s IDE support has rocked my world. The main two I have explored is IntelliJ’s Scala plugin and the Scala plugin for Eclipse.

Normally my editor of choice would be IntelliJ for authoring Java. When it comes to writing Scala the IntelliJ plugin available is buggy at best and completely unusable on the Mac. I am sure this will improve over time … I am quietly hopeful that by the time IntelliJ 8 is released, many of the issues surrounding the Scala plugin will be resolved.

The Eclipse plugin, while quite limited in features seems less buggy than the IntelliJ equivalent. It is still not great though, so be prepared for some ugliness.

Both of the IDE’s mentioned above offer little more than the most basic refactoring support for Scala.

Fortunately Scala fairs better when it comes to an automated build environment. Scala offers great support for Apache Ant out of the box.

To illustrate an Ant build, lets get a simple application running. Here is a hello world application.

package com.gigantiq.helloworld

object HelloWorld extends Application {
  println(â€�Hello world!â€�)
}

To show this application running we can either use the Scala compiler called “scalac� or the Scala compiler daemon called “fsc� to compile the application. For example:

andytrigg$ fsc com/gigantiq/helloworld/HelloWorld.scala
andytrigg$

Now that the application has been compiled to Java byte code, we can run the application by using the scala command:

andytrigg$ scala com.gigantiq.helloworld.HelloWorld

The output looks something like this…

andytrigg$ scala com.gigantiq.helloworld.HelloWorld
Hello world!
andytrigg$

Now that we have proven that the application runs, lets write a simple Ant script to automate this.

<?xml version="1.0" encoding="UTF-8"?>
<project name="hello world" basedir="." default="run">
  <property name="scala.home" value="/Users/andytrigg/dev/scala/scala-2.7.1.final"/>
  <property name="scala-compiler.jar" value="${scala.home}/lib/scala-compiler.jar"/>
  <property name="scala-library.jar" value="${scala.home}/lib/scala-library.jar"/>
  <property name="build.dir" value="${basedir}/build"/>
  <property name="src.dir" value="${basedir}/src"/>

To make use of the built in Ant targets that Scala offers both “scala-compiler.jar” and “scala-library.jar” need to be on the classpath.

  <path id="scala.classpath">
    <pathelement location="${scala-compiler.jar}"/>
    <pathelement location="${scala-library.jar}"/>
  </path>

  <path id="build.classpath">
    <pathelement location="${scala-library.jar}" />
    <pathelement location="${build.dir}" />
  </path>


Add the Scala task definitions to this Ant project.

  <taskdef resource="scala/tools/ant/antlib.xml">
    <classpath refid="scala.classpath"/>
  </taskdef>


Use the “scalac” compiler to build the project.

  <target name="-compile">
    <mkdir dir="${build.dir}"/>
    <scalac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" force="changed">
      <include name="**/*.scala"/>
    </scalac>
  </target>


Once the project is compiled to Java byte code, the application can be executed as a java application.

  <target name="-run">
    <java classname="com.gigantiq.helloworld.HelloWorld" classpathref="build.classpath">
    </java>
  </target>

  <target name="--run" depends="-compile, -run"/>

  <target name="run" depends="--run"/>
</project>

Now that we have an Ant build we can run the following from the command line:

andytrigg$ ant run

The result looks like this …

Buildfile: build.xml

-compile:

-run:
[java] Hello world!

--run:

run:

BUILD SUCCESSFUL
Total time: 0 seconds
andytrigg$

As you can see it is pretty simple to get a Scala application being built using Ant.

For quite some time now a number of people whom I respect highly have been talking quite consitently about the Scala programming language. It has also been mentioned a number of times on “The Java Posse� pod cast. As a result my interest in Scala was sparked and I figured it is time I get my hands dirty and explore why there is so much buzz about the language.

My plan is to document what I learn about the language on my blog as a self-reference. Hopefully others will find my postings useful… or at the very least spark more interest.

What is scala?
Scala is a Sca(lable) la(nguage). Many sites about Scala talk about the language being scalable since it can lend itself to writing small scripts or to building large systems. To be honest I am not convinced I would use it for writing small scripts. I think there are better suited technologies available. When it comes to building larger systems I think it has a lot of promise … more so than Ruby. Ruby has some serious issues that makes writing large systems difficult. I am not going to discuss those issues here though.

An application written in Scala compiles into Java byte code and runs inside the JVM. As a result it offers interoperability with Java that few other languages offer.

Scala is both an object orientated, and functional language. Taken from the scala web site (http://www.scala-lang.org/)

Scala is object-oriented: Scala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits. Classes are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.

Scala is functional: Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala’s case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages.

While Scala is statically typed, its type system is highly advanced and resolves many of the issues that people site as problems of type strong languages. I shall discuss a little more about this later.

Through the use of a unique combination of language mechanisms Scala is highly extensible. Its ability to add new language constructs makes it ideal for developing domain-specific languages.

Why scala?

For me, Scala’s main attraction is its interoperability with Java. From Scala you can call Java methods, access Java �elds, inherit from Java classes, and implement Java interfaces. Scala code can also be invoked from Java code. This level of interoperability opens up a huge world or open source and commercial libraries that are available to the Java community also available to the Scala community. It also means that existing Java systems can be extended with Scala with out much effort.

Like Ruby and Groovy, Scala allows programs to be written concisely and expressively. A common example for illustrating Scala’s conciseness is the declaration of a class.

Consider the following Java class:

class Person {
  private final String firstName;
  private final String lastName;
  private final int age;

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

If we were to rewrite this class in Scala, it could be written like this:

class Person(firstName: String, lastName: String, age: Int)

As I have mentioned already, Scala’s type system is highly advanced. The local type inference mechanism of Scala means redundant type information does not have to be explicitly annotated.
For example:
val y: Int = 1 + 3
val y = 1 + 3

These two statements are equivalent. In both cases y is an integer but on the second line the type of Int is inferred by its usage.

For me another good reason of why Scala is worth investigation is its testability. With its close interoperability with Java, well established test frameworks such as JUnit and TestNg can easily be used for unit testing. In addition there are a number of Scala specific testing frameworks such as ScalaTest, Rehersal, specs, and ScalaCheck.

In recent days I have started to use specs as my unit testing framework of choice and will discuss its use in a future blog about unit testing Scala.

You're hiring... you make a list of mandatory skills and some preferred ones. You interview someone... they don't get ticks in all the mandatory fields... so easy decision. They are mandatory! You have to say no.

Now... some time passes... you are interviewing people, but no-one seems to be passing the bar. Time is running out. You have to staff your project or it won't be viable. What do you do?

What would you do?

My thought for the day..

Staffing a project with the wrong people WILL LEAD TO FAILURE! Usually a slow, expensive failure. Having the project canceled because it wasn't viable is a much better business decision, and in the long run may save the company (and your job).
Our company holds an annual conference where customers from all over the country come to see and hear the latest goings on with our rather large software product. I get up and give a couple of presentations at this event,...

I've just released version 1.3.0 of Roodi. It contains two new checks - a CaseMissingElse checks that ensures you have a default path through your case statements, and an AssignmentInConditional check that looks out for things like if foo = 1 which are likely to be mis-typed equality checks. I've also DRY'ed up the code a bit, but the major new feature is the ability to provide your own configuration file.

If a config file is not provided, Roodi now configures itself with via a YAML one that ships inside the gem which looks like this:

AssignmentInConditionalCheck:    { }
CaseMissingElseCheck:            { }
ClassLineCountCheck:             { line_count: 300 }
ClassNameCheck:                  { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
CyclomaticComplexityBlockCheck:  { complexity: 4 }
CyclomaticComplexityMethodCheck: { complexity: 8 }
EmptyRescueBodyCheck:            { }
ForLoopCheck:                    { }
MethodLineCountCheck:            { line_count: 20 }
MethodNameCheck:                 { pattern: !ruby/regexp /^[_a-z<>=\[\]|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/ }
ModuleLineCountCheck:            { line_count: 300 }
ModuleNameCheck:                 { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
ParameterNumberCheck:            { parameter_count: 5 }

It's basically a list of checks, each with a hash of options for that check. You can take this file as a starting point and remove existing check, add your own custom new checks, or change the default values on some of them. I've intentionally been strict on the values on some of the checks with thresholds, setting high standards that I'd expect to see on my own projects. If you're working through a long list of warnings and want to eliminate some of the noise, or if you decide the thresholds should be different, you can save this in your own config file and use it to configure roodi.

To use your own config file with roodi, pass it in as a value to the -config parameter on the command line like this:

roodi -config=my_roodi_config.yml "rails_app/**/*.rb"
Check out this simple introduction to metaprogramming. I have an article bubbling away in my head about why in .Net I need tools like NDependencyInjection, but in ruby you don't.

You really do need tools like NDependencyInjection in .Net by the way.. I'll explain that too.
Some guys at Google came up with this idea of specifying a set of problems as two stages processes.

Step 1: Map (come up with a mapping function that takes a key value pair and maps it to a new set of key value pairs)
** do this for all key value pairs **
Step 2: Reduce (for each of the new key values, take all values and reduce them down to a single value)

Any problem that can be broken down into these steps can be parallelized really easily and scales well by adding more boxes.

An example may be 'Count word occurrence on the web'
Mapping: [word, url] => for each occurrence of 'word' in document@url add an item to an output hash for value 'word'
Reduce : [word, values] => number of items in value list 'values'

Anyway... lots of problems can be defined in this way... the code that runs your map/reduce functions for you can solve all sorts of problem handling networking issues, machine loading, etc...

Blaine Cook wrote a version in ruby here. Or see mapreducerb@github

I can't wait to have a play with this.
Finally someone has got some scripts together to maximise windows in osx.

OSX has this weird idea that you want to see lots of windows at once. "Maximizing" a window therefore means make it big enough to show the content of the window, but not too big. This leaves the application to decide how big is big enough. Some apps do this fine... others (like Preview) keep getting it wrong.

Grab these scripts form git hub. From a terminal type "rake install", then type "rake install_fastscripts" so you can map the scripts to keys easily. Once you have this working map Maximise to something like Ctrl + Apple + M and you can now make one app take over the whole screen.

THANK YOU TopFunky THANK YOU!

A couple of the scripts are hardcoded to use Terminal, so I have forked the repository for my own edification. I also want to fix a problem with the scripts expecting the draws to be on the right hand side of a window.. which it isn't always.