Link

Incentives Over Process

I read this article and it really hits home for me. I work in an organisation where process is everything. Even to the point of preventing little things.

I often think if we gave individuals an incentive for achieving a goal then things would run smoother. Members of my family work in organisations where an incentive is given if they reach certain targets, they work their bums off to get to that target. It’s still hard work and working late happens on occasion but they have something tangible at the end.

It’s like training a dog… 

Reordering Lists

‘OMG how do we reorder this list in an efficient way…’

One nifty little tip I picked up reading CouchDB: The Defeinitive Guide but never used (until now) was to use a float / decimal instead of an integer when you need to store an order of some sort (… get it…sort… don’t hate).

The reason for doing this is is when it comes to reordering the items in that list and eventually saving the changes.

Say you have a list of objects with a display order as an integer:

...
[
{ name: 'Mary', order: 1},
{ name: 'Tanya', order: 2},
{ name: 'Mel', order: 3},
{ name: 'Jan', order: 4}
]
...

Lets say you move Mel from position three to position one so in affect Mel has order = 1.

...
[
{ name: 'Mel', order: 3},
{ name: 'Mary', order: 1},
{ name: 'Tanya', order: 2},
{ name: 'Jan', order: 4}
]
...

Then when we commit to storage we are going to need to adjust the order field for some (if not all in more complex examples) meaning we will be touching many records instead of just the one record we essentially moved.

Enter the float.

Lets change the order data type from an integer to a float:

...
[
{ name: 'Mary', order: 0.2},
{ name: 'Tanya', order: 0.3},
{ name: 'Mel', order: 0.4},
{ name: 'Jan', order: 0.5}
]
...

Now lets Jan from position four to position two.

...
[
{ name: 'Mary', order: 0.2},
{ name: 'Jan', order: 0.5},
{ name: 'Tanya', order: 0.3},
{ name: 'Mel', order: 0.4}
]
...

Now when we need to redefine the order fields but in this case we don’t need to touch every record, just the one we moved. How we achieve this is by looking at the surrounding records of our new position and from those we find the median of the two order values.

(0.2 + 0.3) / 2 = 0.25

And viola we have our new order value and when we commit this to storage we are only touching one record.

...
[
{ name: 'Mary', order: 0.2},
{ name: 'Jan', order: 0.25},
{ name: 'Tanya', order: 0.3},
{ name: 'Mel', order: 0.4}
]
...

If you are moving the item to the start of the list you can use 0 as your first value and if moving it to the end of the list you can use 1 as your second value in the above formula.

Please note that we are working with complete lists here. If you are only seeing a portion (or page) of the list then you need to take this into account when you move an item to the start or the end of your portion. I’ll let you decide how to do that.

However…

As you may have guessed if you are constantly reordering the list then your values may get a little crazy (0.123332324522333395 etc…). This isn’t a bad thing but eventually your precision might be affected. So it might be worth scheduling a routine that takes the complete list and redefines the values to something more civilised. Food for thought.

Happy coding.