On Procrastination

Writing about procrastination seems like a good a way as any to pick up after a 7 year gap.

A lecture from Dr. Tim Pychyl was posted to Reddit recently, in which he discusses procrastination, its causes, and some strategies for change.

The lecture highlights some of the ways in which we deceive ourselves, mechanisms we use to give our brains those little dopamine hits to feel good now (writing to-do lists, distracting ourselves with something unrelated), whilst avoiding what we should really be doing (finishing that blog post).

Dealing with Procrastination

The main take away from the lecture for me was a very straight forward mechanism for dealing with it:

In situation X, I will do behaviour Y, to achieve subgoal Z.

By defining a task in terms a of a concrete start time, a well defined achievable task, and a reminder of why you are doing it in the first place, you remove a lot of the thinking involved in getting started in the first place.

Simply: The When, the What and the Why.

Agile

I like that this isn't a million miles away from what we try to achieve as good agile practitioners; planning work as a series of small, well defined increments:

As a <person in a situation>
I want <a defined behaviour>
So that <sub-goal is achieved>

This is also not a million miles away from defining acceptance criteria in the Given / When / Then format.

Original Video

Fluent NHibernate, Counting Entries In A Related Table

I have a simple Fluent NHibernate mapping setup where a parent table has a 'HasMany' relationship to the child table.

As part of the parent table, I want to include a property that indicates how many child entries there are, without having to map the child relationship. This is mainly because I don't want the additional overhead of bringing back each child record, but is also useful for being able to sort by the count.

Turns out this can be achieved quite easily by using the 'Formula' function on a mapped property:

Map(c => c.ChildCount).Formula("(select count(*) from childTable where childTable.parentId = id)");

Simple when you know how!

Vim and Linux C++ Development

My workday generally revolves around .Net and the Microsoft stack, so for a change I thought it would be interesting to do some development under Linux. Finding a suitable text editor was the first task, but it didn't take me long to settle on Vim, an incredibly flexible and popular text editor, available for most platforms. This post looks at working with Vim for C++ development under Linux.

Vim is very customisable, and a lot of tweaks can be made in the per user Vim.rc file, usually found in your home directory, so let's start there.

Vim.rc Tweaks

We can makes some tweaks to Vim.rc to make working with C++ a little more pleasant by dealing with indentation and tabs in a more sensible way.

Firstly, let's set up the auto-indentation:
set autoindent
set cindent

I prefer Visual Studio style tab settings of a 4 stop width and converting to spaces:
set softtabstop=4 shiftwidth=4 expandtab

Navigation

Coming from a Visual Studio background, I'm used to working with a full IDE and all it brings, including file navigation through a tree menu as part of the editor.

Vim is a world apart from Visual Studio, so despite the Project type plugins available for Vim, I'm trying to stay away from GUI like treeviews. CTags builds a list of all symbols in your project to enable quick navigation to function definitions, calls, methods etc. Ubuntu 10.10 doesn't seem to include it as part of a standard install, so start with:
sudo apt-get install ctags

Then run it against your code directory; using -R will run it recursively:
ctags -R *

Now you can use the following commands to leap around your source with glee:

Ctrl + ]Jump to selected symbol
Ctrl + OMove back in history
Ctrl + IMove forward in history

Note: in order for vim to find the tags file, it needs to be launched from the directory in which the file was generated.

Neat Commands

Learning Vim is almost like learning an instrument, commands can be combined in a way that makes text editing almost fun. There are a number of good introductions to Vim's commands, but here are a few that I find to be particularly useful:

gg=GReindent entire file
w / bNext Word / Prev Word
^ / $Start / End of line
cwChange word
viwcSelect inner word, change
😡Save changes and quit
bn / bpNext buffer / Prev buffer
bdClose buffer

Down The Rabbit Hole

This barely scratches the surface of what you can do with Vim, I haven't even looked at what you can do with buffers, tabs or any of the multitude of plugins.

So go get stuck in and see what works for you.

Concise Invoking into the UI Thread

It's a new year (and has been for a while), so time to get on and actually make use of this blog.

I often find myself having to Invoke execution back in to the UI thread, and thus far the most concise way I have come accross for doing this is as follows:

public void UIFunction(string aMessage)
{
MethodInvoker del = delegate{
lblUILabel.Text = aMessage;
};
Invoke(del);
}

This is nice as it gives you access to the function parameters inside the Invoked anonymous delegate without needing to add them to the invoke call, and also saves you from creating any more functions, whose only purpose is to deal with being invoked.

Obviously you may want to consider that anyway if the logic is complex, but I like that this method keeps things nicely encapsulated in a single function.

The MethodInvoker delegate comes from the Windows.Forms namespace, so if you want to use this method where it is unavailable, such as on the Compact Framework, simply create a new delegate type as follows:
public Delegate void MethodInvoker();