Chris Worfolk's Blog


Testing multiple conditions in if statements

December 2nd, 2011 | Programming

This is one of the geeks among you. I’m currently working on a new open source project which uses a series of database models derived from a base model which includes some standard methods and I was having one problem in particular to do with updating data values in an object.

if (
    !$object->setName($d["name"]) ||
    !$object->setSlug($d["slug"]) ||
    !$object->setDateByArray($d["date"]) ||
    !$object->setContent($d["content"])
) {
    $this->setMessage($object->getMessage());
    return false;
}

As you can see, this does an if statement and then runs all the updates – the idea being that if any of them come back as false, it can set the error message and return false to the controller.

This worked as expected but with one problem – as soon as one of them appears false, PHP stops looking at the other conditions in the if statement. This is a problem because it means the user only gets one piece of feedback at a time. Whereas, it would be much better if we could give them all the fields that were wrong at once.

However, this doesn’t seem possible in the above implementation. Even if I switched it round to check that everything is true, and everything must be true, that suffers from the same problem that once something isn’t, PHP will stop checking the conditions.

I considered whether I could implement it using exceptions – putting all the checks inside a try block and then have the update operations throw an exception if they were invalid. I could then catch this exception and add the error to the list of errors. However, this suffers from the same problem – as soon as the first invalid value triggers an exception, the rest of the try block is ignored.

The solution I have eventually come to is this:

$writes = array (
    $object->setName($d["name"]),
    $object->setSlug($d["slug"]),
    $object->setDateByArray($d["date"]),
    $object->setContent($d["content"])
);

if (in_array(false, $writes)) {
    $this->setMessage($object->getMessage());
    return false;
}

All the operations are run, and their values are fed into an array. That way all the checks get run no matter what value they return. I then search the array for any values set to false and if any of them were, I know there has been a error and can flag it up with the user.

Mark Edon tells it like it is

December 1st, 2011 | Events, Humanism

Mark Edon recently gave a talk to West Yorkshire Humanists about “talking to creationists.” I’ve already seen Mark talk three times before but never the less it was interesting as ever, with plenty of fresh material.

Charity quiz

November 30th, 2011 | Humanism

As part of Non-Prophet Week, Atheist Society recently held a charity quiz to raise money for the very much worthwhile cause, Medicines Sans Frontiers. It was an enjoyable night and I came away with a pair of tickets to the West Yorkshire Playhouse so a good night all round.

Intelligence of Genetics

November 29th, 2011 | Events, Science

Recently, I visited Headingley Cafe Scientifique for the first time for a talk entitled “Intelligence of Genetics.”

I had never attended the Headingley Cafe before but it seems very well attended. It was standing room only by the time the event kicked off and there were plenty of seating available – so they probably had 50+ people there.

The venue was the New Headingley Club which looked very fancy on the website but turned out to be significantly less fancy in the flesh. I got plenty of change from my round at the bar though, so will approve of that!

The event itself was somewhat disappointing. I came away from the talk not really feeling that I had learnt anything – other than that we have a one in three chance of getting Alzheimer’s disease and this massively varies depending on our genetics. There were lots of stats, but a lot of these weren’t really in much context – am I supposed to be impressed by that number? I don’t know what an average sample size for your area of science.

Still, it was good to finally make it down to the Headingley Cafe.

SocietasPro v0.3

November 28th, 2011 | Foundation

Just six days after we released version 0.2 of SocietasPro, we’re pleased to announce that we’re announcing the third iteration, which is now available to download on Github.

Here is what we’ve changed in this version:

  • Events can now be displayed on a calendar
  • Added style to the system pages
  • Improved visual editor
  • High contrast admin version added
  • Custom columns are now exported in the CSV
  • You can now change a member’s password
  • You can now reset your own password
  • Version checker added to admin panel
  • Added support for custom selects
  • Members import now supports custom columns
  • HTML Purifier now cleans dangerous HTML
  • Locations are now shown on the events page
  • Mac line endings are now handled on imports
  • Audit trail now works with deleted members
  • Deleting members now cleans up their custom datas as well
  • Duplicate members are now filtered out on imports
  • You now get feedback if your login fails
  • Events are now sorted by date
  • Added extra stats to the control panel

Where will the project be going next? We’re going to put some work into the front end and then it should finally be ready to demo! Stay tuned to our updates on Twitter for further updates.

Food stocks

November 28th, 2011 | Life

I don’t keep a great deal of food at home. My freezer is always full of stuff, and I have some backup tins in my cupboard, but beyond that, I have to specifically go out and buy food to put in my fridge. The reason is, it never really seems to get eaten.

Taken my recent weekend for example. On Saturday afternoon I went for lunch with Raby. We hit up Las Iguanas which is always a pleasure and it was great to catch up with Raby as it’s been quite a while since we’ve met.

In the evening, we hit Sam’s Chop House, then on Sunday afternoon, I headed over to parents for lunch before hitting up Humanist Community on Sunday evening – which may well have set a new record for attendance.

Sam’s Chop House

November 27th, 2011 | Food, Friends, Reviews

A few weeks ago, we headed to Sam’s Chop House for a meal while Kat was in Leeds. I had been there once before and had had a great experience, so I was looking forward to it.

Unfortunately, I didn’t feel they really delivered. The steak was good, but not great. It also took a really long time for it to turn up and though I was hoping they could recover with desert, that proved similarly disappointing.

Afterwards we headed over to Brown’s to enjoy their fine range of cocktails.

The age of maturity

November 26th, 2011 | Religion & Politics, Thoughts

Ben brought up an interesting topic in this month’s Humanist Community of Leeds meeting. The topic was the discrepancy between the age of sexual consent and the age at which you can vote.

I’ve heard the argument before, and similar ones that I don’t buy in to – for example, you can pay tax at 16 but you can’t vote until you’re 18, therefore it could be suggested it is unfair that you pay tax to a government you can’t vote for.

I don’t find this a credible argument because you don’t necessarily pay tax as a way of gaining a vote in democracy. You’re vote in democracy is a guaranteed right, even if you’re not paying any tax at all – it simply isn’t available until you’re ready to make an informed decision. The reason you pay tax is primarily to pay for public services such as hospitals and schools, which you almost certainly have been making use of at the age of 16.

The argument for having a different age for sexual consent and voting is a less clear cut one though. Indeed, Ben made a very powerful argument that I think may be winning me over.

The reason we don’t let people vote until they are 18 is because we’re worried they would vote for the wrong political party – a lot of them might vote for the Monster Raving Looney Party, or the BNP, or the Greens, or one of the many other fringe parties and being the pretentious grown up real adults we are, we don’t approve of such free spiritedness.

But it’s very hard to make the case that they can do more damage with a vote, than they can do by having sexual relations.

Actually, having a vote probably won’t make any difference. Voter turn out is low in young people anyway, let along even younger people and at the end of the day, it’s only one vote and there are lots more people aged 18 or over than there are aged 16 or 17.

Sex however, can be quite damaging. Initially it could appear this is primarily damaging to themselves which is perhaps why we allow it (whereas voting for the wrong political party would be damaging to society and is therefore not allowed), but of course sexual relations can be incredibly damaging to society.

Unwanted children are a huge problem because they don’t get properly parented and therefore become out of control kids and eventually grow up to become criminals, breaking into your house and filling up those prisons that your tax money pays for. Not to mention the possibility of ending up in care, which our tax money also pays for.

In fact, one of the biggest reductions in crime has come from legalising abortion[1], simply because most of the unwanted pregnancies that would have previously been born and grown up to become criminals are now getting aborted. Unwanted pregnancies cause problems, as do STIs which are also prevalent with young people who engage in regular exual intercourse with multiple partners.

Therefore, giving how damaging it is in society, it is very difficult to justify having a higher age for voting than you do for sexual consent.

Karaoke congregations

November 25th, 2011 | Thoughts

music-audience

There are only two places you are allowed to sing in public – church and karaoke bars. Unfortunately, many people feel uncomfortable doing karaoke and you never get to sing anything good at church.

The solution – karaoke congregations! It’s a meeting, where you all turn up and do karaoke as a group. There are a number of different songs to suit people’s tastes (or as the movement grows separate meetings with different genres) and you come along and everyone sings along together. This way, you can sing popular songs and you don’t have to be embarrassed because everyone is singing so nobody can really hear you anyway.

It’s also important to point out that it is karaoke. That is an important term because it stresses that it is fun and you don’t need to be able to sing. As opposed to a term like choir where there is a focus, or at least an interest in signing properly, in a formal way and performing together, these meetings would just be fun meetings where you can turn up and sing badly because it’s just about having a laugh, just like when you go to a karaoke bar with friends.

Thoughts?

Saying goodbye to a hero

November 24th, 2011 | Thoughts

Earlier this month, Sir Jimmy Savile sadly passed away.

The reactions of the Leeds community showed just how much of an impact that he had. Having re-watched the Louis Theroux documentary, Jimmy estimated he has raised over £40,000 for charity. Now I like to think I’ve raised quite a bit in my time and yet, I’m fairly sure that you compared it as a percentage, it would be 0%.

It is no wonder, however. There doesn’t seem to be a person I can find who has a bad word about him. Indeed, the more people you ask the poor you seem to get a heartwarming story. Whether it was when he took time out to take a photo with Casual Dave or sign an autograph for my mum. Or the time he went to visit my dad while he was in the hospital or the time he gave my grandfather a lift.

Unfortunately, I was unable to go down and pay my respects as they moved the public viewing back from 9:00 to 9:45 and I had to be at work by then. Still, I’m never the less starting or getting involved with the following campaigns:

  • A Statue for Sir Jimmy. If Don Revie is getting a statue (http://donreviestatue.com/), who was someone who I’m sure what a great guy, but I had to ask my dad about, I think it’s the least we can do for Jimmy.
  • A Song for Jimmy. Jimmy is the new Princess Diana (the King of Hearts, if you will), I think it would be a fitting tribute for Elton John to write a song about him.
  • The Jimmy Savile Arena. We’re building a brand new arena in Leeds, so why not name it after him? Why not indeed?