Monday, May 10, 2010

MPS is simple

If you have ever seen or tried MPS and it didn’t make a lot of sense, do not believe your first impression, because meta-programming in MPS is simple.

There are only two things you have to know to create something useful in MPS:

  • editors and
  • generators.


Editors is the way you interact with your language in MPS. The thing is that MPS doesn’t edit text files. It works with abstract syntax tree. When you edit a program in MPS, you edit AST. Here is AST for hello world program written in Java:

To define a language you describe types of nodes that can be in your language AST. In MPS this is done with concepts (they are green).

In a way concepts are classes for AST nodes and AST nodes are instances of concepts.

For each concept you define an editor.

Editors allow you to edit and view AST nodes in a text-like form. In a way editors that you define for concepts are “editor classes” and the area in which you actually edit programs consists of “editor instances”.

If you like MVC pattern and its flavours, you can think about AST nodes as a model and editors as view and controller. You can read more about concepts here and here and about editors here.


While having AST and being able to edit it is great, usually you would like to produce something useful like java code or text files. This is what generators are for. They take AST and transform it into real files on your hard drive.

Generators can transform AST directly into real files:

…or they can transform AST into another AST. This is useful when you want to build your language on top of another language. For example, in MPS there a bundled language called base language which is basically Java extended with features like closures. You can create generator to translate your language into base language and generator from base language will automatically transform it into java files:

This way you have a chain of AST transformations and don’t have to deal with file generation.

You can read more about generators here. There are also quite good generator demos.

What’s next?

There are a lot of other interesting things in MPS like type system, IDE support and much more, but to do something useful you don’t have to know anything except editors and generators. In my view, the best way to get started is to go through MPS tutorial and generator demos. Do not just read them and look at screenshots, install MPS and try to repeat all the steps. Be aware, that tutorials are not too easy and might cover too many topics to understand at once. Don’t get frustrated if something is not clear. After tutorials you might read parts of user guide or try doing something simple yourself. It doesn’t take a lot of time, but it’s rewarding and it’s the best way to understand what MPS is good for.

Thursday, April 15, 2010

Pomodoro in practice

Do you ever feel at work like you’re doing thousand things at the same time but never finish anything? You start writing some code, then a bug report comes in and you have to respond because it stops QA from testing. You go back to code, but then colleague comes with a question. Then yo go to meeting. You go back to code again, but it doesn’t go very well so you start reading blogs. Then you receive an email you have respond to. And before you know the day is over and you don’t feel like you have really accomplished anything.

If you experience anything like this, stop doing what you’re doing and try pomodoro.

What is pomodoro?

It’s a focus (and time) management technique or put more simply “a way to have less distractions”. In its basic form pomodoro is like this:

  • you do a task for 25 minutes avoiding any interruptions;
  • after 25 minutes you take 5 minute break;
  • if despite all your efforts, you’re distracted during 25 minutes, you reset timer and this pomodoro doesn’t count.

(Notice, that “pomodoro” term is overloaded, because “pomodoro” is not only the name of the technique, it also means 25 minute interval.)

The main reason to do it is that any non-trivial task requires a lot of mental effort to get into the state of flow (a state of mind when you’re really productive). Getting into the flow is quite expensive so you should avoid all the internal (checking email, reading RSS) and external (a colleague taking to you, instant messenger pop-up) interruptions.

On the other hand, working for 10 hours in a row is also not productive. It’s also important to have small breaks. I remember quite a few examples when after hours of hectic coding I realized that I had created a mess, while there is a much simpler solution to my problem, or even worse that the problem was wrong.

There is more to it, but in a nutshell pomodoro is like a vise:

  • forces you to concentrate on the task by having less interruptions;
  • forces you to have breaks.

(Another metaphor I like “pomodoro is your personal Scrum sprint”.)

Pomodoro in practice

I’ve been using pomodoro for about 4 months now. In three words “it is great”. In ten words “It’s not a silver bullet, but totally worth doing”.

What works

  • I get more satisfaction. Having small tasks and accomplishing them is somehow more satisfying than doing the same amount of work without tasks (I think, it’s also one of the ideas in Getting things done). It works especially well on the most insane days because doing small focused tasks makes them less insane and at the very least at the end of the day you can look at the paper where you track pomodoros and see that you have actually accomplished something.

  • makes the cost of context switching explicit. It’s explicit for me because I know that I’m doing a certain task and if I’m distracted I’ll have to reset timer. It’s explicit for others because they know I’m doing pomodoro and they can see my timer. (I use Focus Booster on Windows and Pomodoro on Mac OS.)

  • less procrastination. Before each pomodoro I choose a task (or several tasks if they’re small) that I’m going to accomplish. If something has to be done to accomplish the task, I do it immediately. With 25 minute “sprints” there no time to procrastinate. It works especially well with tasks which are not exciting but just have to be done.

  • time tracking. I used to track how much time I spend on tasks before using pomodoro. With pomodoro time tracking became easier and better. Easier because I do it anyway whenever I use pomodoro. Better because my time is tracked with 100% focus factor.

  • focus factor. “Focus factor” a term from Agile (see XP and Scrum from trenches). What I mean here is how much time a day you can spend without being interrupted. With pomodoro you get your personal focus factor for free. On average I can do 8 pomodoros or 4 hours of uninterrupted work. This is 50% focus factor. (It may seem low, but it’s better to know the truth than to avoid it.)

  • having breaks is relaxing. It helps a lot to keep focus while doing boring tasks because I know I’ll have a break after all :)

  • it’s fun. It’s like a game, but really useful one.

What doesn’t work

  • often 25 minutes is not enough. I haven’t figured out the reason yet. Probably, it depends on the time of the day and kind of task. When I do something really exciting, I tend to skip every second break.

  • obeying the timer. When I started using pomodoro, I would actually start or stop doing task when timer rang. I don’t know whether I adopted pomodoro to my needs or I’m just lazy, but now I don’t care about timer that much. I can stop few minutes later or have a longer break if I feel like doing it.

  • exploratory tasks. Pomodoro is great for goal oriented tasks but being goal-oriented is not always a good thing (see awesome talk by Daniel Pink). Sometimes you may want to enjoy yourself without thinking of time or how much you can accomplish. I don’t watch movies or read books using pomodoro.

  • breaks can be distracting if you involve in a discussion.

  • some people don’t care about pomodoro. Even though they know what it is, even though they see timer, they keep distracting me anyway. It’s not a big deal, just the way it is.

  • since pomodoro is like a game, you may start cheating yourself to get better results and more satisfaction. For example, I noticed that I reset pomodoro less often than I used to three months ago.


  • I feel happier. Even if have to do something really boring.

  • I have less distractions:

    • I turned off email and instant messenger’s notifications;
    • I check instant messenger only when decide to do it, not when someone sends me a message;
    • I broke email addiction. I don’t check email or read RSS feeds when I feel like I’m bored.
  • I became more realistic in task estimates (at least I want to think so).

  • I became more productive.

Try pomodoro now!

I started using pomodoro by simply typing “pomodoro timer” in google and choosing a timer. I installed Focus Booster and started my first pomodoro without knowing too much about it. You’re not going to lose anything, but there is a chance to get rid of distractions and become happier :)

Sunday, April 4, 2010

Meta Programming System: generator for Miss Grant's controller

This is the second part of Martin Fowler’s state machine example in MPS. The first one was about creating editor for Miss Grant’s Controller and at the end of it there was a nice editor like this:

While this is cool, this editor doesn’t help to create anything useful like Java code which you could actually execute. So this part will be about code generation for Miss Grant’s Controller.

What it’s going to be like in the end?

There will be java code based on existing set of java classes. Something like this:

Importing java classes

In introductory example Martin Fowler shows some code and a diagram to explain what the state machine is like. To reproduce this state machine framework I copied all the code from introductory example into IDE. (There are some problems with code, so if you try doing it yourself, don’t expect it to compile out-of-the-box.) I also didn’t feel ok copying diagram or linking to it (after all it’s a part of the book), so here is a diagram I got in IntelliJ after copy-pasting code and making some minor changes:

In a way this diagram is very similar to AST from the previous part. StateMachine receives Events and changes its State. States can have several rules called Transitions describing which State StateMachine will be in after receiving some Event. When state machine transitions to a new state, it executes Commands associated with new State. There is also StateMachineFactory which creates StateMachines.

First off, let’s create generator and empty template.

In order to use existing java classes in template, we need to add these java classes to MPS project:

After adding classes to language properties we have to import them into template. There are two ways to do it. The simple one is to press Ctrl+M and start typing package name:

The more complicated way is to add package in generator properties:

After importing the package we’ll be able to use all the state machine classes in template code:

Now that we have simple template let’s map StateMachine concept to this template…

… and see if it’s working

Adding initialState

Before going further it worth noticing that there is probably a missing part in Martin Fowler’s example. There is a notion of resetEvents which return state machine to its initial state and there is even code which does it…

… but state machine DSL doesn’t have any constructs to specify initial state. Let’s fix it by adding reference to StateMachine concept:

Let’s also configure initial state to idle, as it is in the introductory example.

Generating Events, Commands and States

First off, let’s make generated class name more descriptive by prefixing it with state machine name. In editor state machine name is the topmost line:

We add it as property macro and replace spaces in state machine name, otherwise it will be invalid java class name.

Let’s add generating code to create Event, Command and State objects. It will cover the following areas of the state machine DSL (note that State objects won’t be fully configured after this step):

Let’s start writing template by adding some code as if it was written in plain java. Code for creating Event object will be like this:

Now we can add macros to loop over all the events:

Inside the $LOOP$ macro we add property macro to change variable name for each loop cycle:

Similarly we wrap with property macros event “name” and “code” parameters. After writing similar macros for Command and State, template will look like this:

Let’s see what this template generates…

Generating initial state

So far we’ve been generating code which instantiates StateMachine with null initial state. Let’s add macro to initialize it with real state. We replace null with reference to state variable and wrap this variable with reference macro:

Reference macro can return node or string so we just copy initial state name (which is string) from StateMachine node:

After generation StateMachine will be constructed with idle initial state.

Adding transitions and actions

Event and Command objects are now fully constructed but States has yet to be configured with transitions and actions. Let’s start by writing template code for adding transition as if it was plain java:

What we want now is to copy that line for each transition in each state. In pseudo code it will look like this:

At the end equivalent template code will be:

We start by creating outer loop…

… and then inner loop

Parameterizing event and target state is easy. We can simply take names of triggerEvent and targetState references:

Getting name of the current state is more complicated. The problem is that inner $LOOP$ macro doesn’t know anything about outer $LOOP$ macro. There is a way to do it using mapping label (see this thread) but in this case we can do it by taking Transition node’s parent which is a State node:

When generated, the code will look like this:

We do similar thing to generate addAction() statements.

Reset events

The only missing part now is reset events. This is pretty straightforward. We loop over resetEvents and use reference macro to generate parameter names forv addResetEvents() method.

Setting up output path

It’s great to have generated code, but we need to be able to actually use it. Let’s save generated files in the folder with source code from introductory example. It can be done in Solution properties by specifying generator output path:

Now java project for introductory example can be configured to use src_gen folder as a source folder, so whenever state machine changes in MPS, generated files will be seen by IntelliJ.

What’s next?

While having all this code is great we still need:

  • unit tests for state machine
  • unit tests for state machine DSL