Chris Worfolk's Blog


Mustache

June 27th, 2012 | Limited, Programming

Mustache is a logic-less templating system, written by people who apparently can’t spell moustache correctly.

It has been ported to almost every popular language ranging from PHP to C++, though it has its origins in Ruby. It isn’t just useful for HTML either – you can also use it for things like config files.

The idea behind it is that templates are logic-less. It divided the traditional view into two parts – a view which is a class in your host language, and a template which is the Mustache markup. The view class can also be an associative array, like you would pass with a traditional templating system, but using a class allows you to easily make use of functions.

It produces very clean mark-up – you simply place your variables in curly braces (or double moustaches if you will) like {{so}} and they are swapped out. Repeating sections are handled by {{#sections}} that repeat based on lists {{/sections}} or are just used once to display or not, instead of using if statements.

The advantage is that you get templates which are almost logic free. I saw almost because of course you do still have repeating elements and tags in there designed to replicate if statements. But there are no logical statements in there – just tags!

The disadvantage is that it means doing a bit more work with your views. MVC already splits out the stack into three components and Mustache divides the view one step further.

The sweet taste

June 26th, 2012 | Photos

One of the drinks I missed most since I stopped drinking alcohol is Rekorderlig. It’s like Kopparberg, but a lot nicer, there is more of a fruit taste in it. Luckily, when you are in a country full of alcoholics, they cater for such a niche.

Rekorderlig

Alkoholiton is Finnish for alcohol free. It’s amazing! Devastatingly, since returning to the UK I had a search around on the internet to see if anyone imports it, but it seems not.

Using Apache Bench to load test your website

June 25th, 2012 | Life, Tech

Apache comes with a cool load testing script called Apache Bench, which lets you throw loads of requests at a URL. It comes bundled with Apache, so if you’re running the LAMP stack, you probably already have it.

The syntax is simple.

ab -n 1000 http://www.google.com/

This will throw a thousand requests at Google and then produce a report on how long it look. You can also use the -c option to set the number of concurrent requests. There are lots more options too – see the Apache docs for full details.

Finland

June 25th, 2012 | Travel

Now that I am self employed, I hate holidays. It’s fine to say, well I budget for that in my earnings, but when it actually comes to taking time off and doing the maths surrounding it, it’s very hard to make that decision.

Therefore, given I was forced to take a four day weekend for the Jubilee, we decided to fit in a cheeky trip to Finland to go see Elina’s family.

As if I didn’t hate flying enough, it turns out Finnair were running a new programme trial in which instead of feeding us, they didn’t feed us. Rubbish,

I had previous met her mum who provided us with a warm welcome and some amazing cooking. Indeed, she cooked so much that we ended up having beef and chicken for breakfast the next day too! I’m hoping it will become the new traditional Finnish breakfast.

It’s a really beautiful country and not called the land of a thousand lakes without good reason.

We had to drive through quite a bit of forest to get to the lakes. No wonder Finland produces so many rally drivers, it was ace.

On Monday we went to Turku, one of the larger cities where Elina went to university. It was mostly full of sex shops that offered live shows and a river that you would never want to drink from, but was nice never the less.

Then, in the evening, we travelled up north to Rauma so we could eat at a restaurant that served reindeer steak.

It was a lovely traditional restaurant, and the food was excellent.

After a long day of adventuring, Moomintroll was very tired.

Thank you so much to Elina’s mum and brother who put us up for the three days.

Grammarian

June 24th, 2012 | Public Speaking

At the most recent Toastmasters meeting, I took on my second role as Grammarian. This involved introducing the word of the day, which I had chosen to be jubilant (after giving a short rant about how we should abolish the monarchy), as well was watching out for good uses of grammar and interesting phrases.

New website

June 24th, 2012 | News

Jenny's Photo Blog

Following on from the success of Sarah Jones’s blog, Worfolk 18 is pleased to announce the launch of Jenny’s photo blog. New updates every month!

Hirst’s Yard

June 23rd, 2012 | Friends, Life

Hey, do you remember SoBe? Probably not as it’s new. Well, forget about it anyway because it’s gone. It’s now called Hirst’s Yard. Confusing, it’s also located in Hirst’s Yard, along with a load of other bars.

Myself, Elina, Norm, George and Kat were recently out for post-Nando’s cocktails when we received a buy one get one free flyer, so decided it was worth a go. Perhaps they decided that the SoBe look was simply a bit too bright and happy for Leeds, but the essentials remained the same – quality, properly mixed cocktails, at reasonable prices (which changes to very reasonable when you have two for one vouchers).

It was reasonably quiet when we went in at around 9pm, though picked up soon after that, so head down early if you want a seat.

We followed that up with a trip to the ever-popular Call Lane Social where we ventured up to the tiki hideaway upstairs. You can question the wisdom of lighting drinks on fire when you have a tiki-style straw roof that is highly flammable, but we survived to tell the tale, so all is well.

At this point, myself and Elina left as we had to be up early the next morning, but I presume the night ended in a strip club.

Would you like me to change my gloves?

June 22nd, 2012 | Thoughts

I popped into Subway for lunch today and noticed they had a staff notice up in their baking area that asks “what as your position’s today?” I’m fairly sure there shouldn’t be an apostrophe in that.

Anyway, one of the people in front of me in the queue asked for a vegetarian sandwich and the artist asked her if she would like her to change her gloves. The customer said yes. But what is the point of all this?

For me, even as a vegetarian, I just don’t see the point in offering to change your gloves for vegetarian sandwiches. But maybe I’m missing the reason entirely, as I can only think of two reasons.

Firstly, it’s a principle issue. You don’t want gloves which have touched meat to touch your sandwich. But this is just stupid. You’re not advocating the slaughter of animals just because someone uses the same gloves to prepare your sandwich as they did to prepare someone else’s. Besides, where does it end? Why just change your gloves, why is it OK for the same person to prepare a vegetarian sandwich if they have previously been in contact with meat?

Secondly, it’s in case the sandwich then tastes of meat because the gloves have some kind of meat smell on them. This doesn’t make sense either though, because why would you just do it for vegetarian sandwiches? Someone ordering a chicken sandwich doesn’t want their sandwich to smell of meatballs for example.

Why then, does it matter if someone changes their gloves at Subway?

Motivating unit testing with a reduction in functional testing

June 21st, 2012 | Programming

The problem with unit testing is that developers don’t want to write them. No surprise, there, they aren’t the most exciting thing in the world, unless you’re actually a tester and they are your pride and joy.

So, the challenge for creating a good development and testing cycle is how to ensure your developers are motivated to actually write the unit tests. Test driven development is a good way to go about this, and helps stop developers over-engineering too, but it requires a radical shift in your workflow.

Perhaps a less extreme modification, and one developers are more likely to buy into is to replace functional testing with unit testing.

Back in the old days, when you were working on a web application for example, you would write some code, you would then load it up in your browser and see if it worked. If it did you would move onto the next thing and if it didn’t you would go back and fix it.

Then the rise of unit testing for web applications came along and developers were asked to write the code, functionality test it and then write a unit test for it. But none of them did because they didn’t want to write a boring unit test.

However, recently I was writing a model for a web application. Having made my changes, I then wrote a new test for it in our set of unit tests to ensure that it worked as it should. I ran the tests, and I did. But, being pressed for time, I never actually ran a functional test on it. But it worked. Which is no huge surprise, given it passed the unit test.

But what if that was policy? All you had to do was to write the unit test and for it to pass that, you didn’t have to do a functional test. Developers might buy into that, because it takes roughly the same time (probably a little more, but not too much to worry about) and you don’t lose too much. Here is why:

You can’t get away from functional testing. At some point, someone is going to have to do a functional test and at that point they will see if it works or not, and knock anything that doesn’t work back to the developers. This isn’t the case for unit tests – you can go without them, it just means you end up producing pretty rubbish unreliable code and end up spending ages tracing issues down later. But it isn’t an in your face blocker, so developers often ignore it.

Of course, I wouldn’t recommend this for organisations who already have a testing culture embedded. But for organisations that don’t currently unit test, relaxing your rules on functional testing could help you take that first step down the road.

Using the Symfony2 Validator component outside the framework

June 20th, 2012 | Limited, Programming

Symfony2 has a very nice Validator component for data validation. As with most of the components, it can be used outside of the framework, but unfortunately, the documentation on how to do this is rather lacking. Indeed, even using it within Symfony but outside the controller can be difficult and this is made especially difficult if you’re using Propel because you can’t use annotations.

However, it can be done, and this tutorial will show you how.

When using the validation component somewhere other than in a Symfony2 controller (be that in an entirely different project, or just in a custom class inside the framework), the problem is that you don’t have access to the service container. So we need to create the object for ourselves.

Lets start by importing the namespace into our class.

use Symfony\Component\Validator as Validator;

This will give us quick and easy access to the Validator classes. Now we can use the ValidatorFactory to generate a validation object.

$factory = Validator\ValidatorFactory::buildDefault();
$validator = $factory->getValidator();

This is good. It means we now have a validation object that we can run against annotated classes. But what if we are using Propel and need to specify our validation rules in a YAML file?

$yamlFile = "../src/Acme/DemoBundle/Resources/config/validation.yml";
$factory = Validator\ValidatorFactory::buildDefault(array($yamlFile), false);
$validator = $factory->getValidator();

Now we can use the rules we specified in the YAML file to validate our Propel classes.

$violations = $validator->validate($model);

Splendid, we’re done. One further trick I’ll throw in with this post – what happens when you need to validate multiple objects and give a combined list of errors back? Because the list we get back is a custom object, we can’t just array_merge the two $violations list. But luckily, there is a function in the object to do this.

$violations = new Validator\ConstraintViolationList;
$firstViolations = $validator->validate($firstModel);
$secondViolations = $validator->validate($secondModel);

if ( count($firstViolations) > 0 ) {
	$violations->addAll($frstViolations);
}

if ( count($secondViolations) > 0 ) {
	$violations->addAll($secondViolations);
}

That will return you a single ConstraintViolationList that you can iterate through, containing errors from both models.