Year of Code – The Road to Rachmaninoff

As many people know, I’m a musician as well as developer. I love playing and writing music just as I love creating code. As well as a developer I also work in higher education where I teach programming. First of all, I want to say I fully support initiatives which encourages people to learn to program (such as the Raspberry Pi foundation and code clubs) as I think people (adults and children alike) should be given this opportunity to learn how to program as it not only increases computer literacy, it also provides them with valuable skills which can be used in many aspects of life that involve problem solving and logical methodologies/analysis.

However, I have a serious problem with year of code: and this problem starts with the very top of the yearofcode.org website:

START CODING THIS YEAR
IT’S EASIER THAN YOU THINK

That’s a very bold statement, and potentially very damaging. How hard do people “think” it is? The reason I mention music is because I think it has a lot in common with programming. It is very dangerous to trivialise programming, and the Year of Code programme seems to do just that. This means that people could come into this with a false expectation that, in an afternoon, a month, or even a year they will be able to write an amazing web site or app. It’s like saying to someone “Starting learning the piano this year, it’s easier than you think”, and people may expect to be able to play Rachmaninoff within a year. I’ve been playing for 20 years and I still struggle with Rachmaninoff!

The thing is though, do people think this about learning an instrument? Probably not. Why? Because people already have some awareness about what it takes to learn to play an instrument. However, it still does not stop people from giving up after 3 months claiming “I was rubbish”. I can see this being considerably worse for people getting involved in the “Year of Code” because how far do they expect to come in a year?

Teaching syntax of a programming language is the easy bit, as is teaching the notes on a piano. I can tell you what notes are, and how they relate. Great! Does this give you the skills to be able to play a Rachmaninoff sonata? Eventually. We all started off with the basics. However, to become a great (or even good) developer/musician requires many years of learning and experience, and some would argue (myself included) you never stop learning.

Trivialising programming will just lead to false expectations and disappointment when those expectations aren’t met, and all we will get from it is even more people saying “I can’t do programming” because they have now tried it. Learning to code is a marathon not a sprint, and not only that the goal posts are continually moving as new technology and methodologies come out. I really do encourage people to learn to code, but you must be aware of the challenge that you may face and the amount of time and effort it will take.

When Objects Act Like Arrays

I’m a big advocate of object-oriented programming, even though only a few years ago I hated it and thought it was “over engineering”. I don’t believe, however, that object orientation is suitable for every circumstance. Anyway, that’s for another blog post. Objects are great, but not always the best when handling data, particularly things like data collections.

One of the nice things about storing collections of data in arrays rather than objects is the fact that you’re able to easily loop through the collection, and extract data from the collection depending on the position. Sometimes though we may want to manipulate the collection, and store more information than just the data in the collection. This is where SPL comes in.

As of version 5.0 PHP has included the SPL (Standard PHP Library). I am going to look at a couple of interfaces from this library in this blog post: ArrayAccess and SeekableIterator. These interfaces allow us to do a couple of things: SeekableIterator allows us to iterate through the object (eg. a foreach() loop), and the ArrayAccess allows us to extract data from the object as if it was an array. All this in a way that is completely controlled by the programmer.

Definition for the SeekableIterator:

SeekableIterator extends Iterator {
/* Methods */
abstract public void seek ( int $position )

/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}

Definition for the ArrayAccess:

ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}

The way these work is that they require a variety of methods to appear in the class. With SeekableIterator there is generally a “position” property which is just an integer holding the current position in the collection (so normally defaults to 0). The methods that are required are: seek, current, key, next, rewind and valid. The “seek” moves the position to the number passed in as an argument, “next” method normally increments the position by one, and “rewind” resets the position back to 0. “current” returns the item at which the position property is at, “key” tells you the current value of position and “valid” checks to make sure an item exists at that position. You can then put the object in a foreach loop as if it was an array!

foreach($iterableObject as $item {
     ...
}

ArrayAccess requires fewer methods, and some of them are similar: “offsetExists” is the same as “valid” but rather than getting the position from the internal position property, it is passed in as a parameter. “offsetGet” returns the item at a given position and “offsetSet” sets an item to a given value. You can then treat an object like an array by using standard array notation:

$item = $arrayAccessObject[0];

A single class can implement both of these interfaces without conflict, and SPL contains many more things like this. I have not written this as a tutorial as the PHP Docs are incredibly good on this subject and easy to follow.

http://php.net/manual/en/class.seekableiterator.php
http://php.net/manual/en/class.arrayaccess.php

Installation of Memcache in PHP on Ubuntu/Debian

Happy new year all! Just a quick post about how to install Memcache in PHP on an Ubuntu or Debian server (I think it will apply to Redhat based servers, just substitute the apt-get for yum).

Log on to your server as root (either directly or using sudo su), then install the Memcache daemon through apt:

apt-get install memcached

Once installed you need to install the Memcache module into PHP. This is done through PECL which in turn is installed through PEAR. When they are both installed, you need to run the command:

pecl install memcache

This will install the memcache extension, and once done will tell you to add the the line “extension=memcache.so” into your php.ini file. I prefer keeping these in separate files (so that the php.ini file can be updated). The easy way to do this is:

echo "extension=memcache.so" > /etc/php5/conf.d/memcache.ini

And finally, reload Apache

/etc/init.d/apache2 reload

If successful, Memcache will now appear in your phpinfo().

HTML 5 Preparation

When setting up an HTML 5 web page, there are a couple of things which need to be done. First of all you need to standardise the CSS for the new HTML 5 elements. This can be done at the top of your CSS stylesheet by making all the new elements be displayed as “block” elements:

article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary{
  display: block;
}

Unfortunately there is still a range of browsers which still don’t render HTML 5 objects. This means we need to add a JavaScript hack. Rather than downloading or creating your own JavaScript file, you may as well just link to a file that has already been created for you on Google Code. This means that it will always be kept up to date. Also as it is only needed for IE browsers up to version 8, you won’t need other browsers to load it:

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

You are then free to start creating your HTML 5 web page!