Wednesday, 18 March 2009

Broken Records coming to New Zealand...

Not exactly technology related, but I thought a little more promotion wouldn't go amiss. Since leaving the UK, my brother became a member of a band, Broken Records and I've never yet managed to see them live which is a real shame. They have recently been signed by 4AD and are currently recording their first album which is due for release on 1st June 2009. Good luck lads!
Oh, by the way, they aren't physically coming to New Zealand, just via my blog... Shame, I think it would be a good idea! :)

Partly for my own reference, but also as a shameless plug, here are some of their website links:
YouTube
Facebook Group
MySpace

Here are some videos that are worth watching too:

Broken Records Studio Video Diary - behind the scenes of their album recording:



Official promo for 'Lies' - the 3rd single from Scottish band, Broken Records



A Channel4 review:

Sunday, 1 February 2009

Using language features for readability and understandability

Whilst pairing recently, we came across a situation that I believed was best solved using LINQ. I've not used LINQ much other than reading about it in blogs so it was an opportunity to see what it was all about. In the end, we managed to solve the problem very nicely with very few lines of code. As we continued programming, yet another opportunity arose where LINQ could have solved the problem but instead we chose to write a foreach statement and this made me stop and think about language syntax in general. Why do we chose to use a certain syntax over another? Is it readability? Is it understandability? It is about knowledge? This post explores a few thoughts around this topic.

I can think of a few reasons that might affect why a developer would chose to use a certain syntax:

  • readability
  • understandability
  • fewest lines of code
  • existing knowledge of the syntax
  • they only know one way to solve the problem
  • technical constraint (platform, deployment, etc)

These are just a few choices of the top of my head and I'll examine the readability and understandability first. These two terms are very closely related but they do have distinct differences. I'm not going to discuss them here, but you can read further on Wikipedia on readability and understandability. Suffice to say that the choice of syntax can enhance both these concepts. Let's look at an example:

public string Name

{

    get

    {

        if (name == null)

            return "No name";

        return name;

    }

}

 

public string Name

{

    get { return name ?? "No name"; }

}

These two property statements are identical in functionality but obviously differ in syntax. However, if you do not understand the '??' operator, you will be at a loss for the second statement. However, if you did understand the operator, which of the statements has a higher readability index? I've come round to thinking that they are both readable but one could argue that the first one is more understandable because it uses an explanatory if statement. However, the '??' operator explains the same thing albeit that you need knowledge of the operator to understand its function. Further, you could also argue that the second implementation is more readable because it is only one line of code to read.

Let's look at another example this time using LINQ:

public IList<Customer> GetCustomers(string startsWith)

{

    IList<Customer> customersStaringWith = new List<Customer>();

    foreach (Customer customer in customers)

    {

        if (customer.Name.StartsWith(startsWith))

        {

            customersStaringWith.Add(customer);

        }

    }

    return customersStaringWith;

}








public IList<Customer> GetCustomersLinq(string startsWith)

{

    var selectedCustomers =

        from customer in customers

        where customer.Name.StartsWith(startsWith)

        select customer;

 

    return new List<Customer>(selectedCustomers);

}



Again, these two methods return the same result but which one is more readable?



Drawing a conclusion from this is quite difficult as readability and understandability are generally opinionated views. However, what I find interesting is that there are some language syntaxes that are adopted readily whilst others are left behind. Take generics, for example. I don't know any developer that doesn't use generics if they are using .NET Framework 2.0 or above. What is it about the syntax that attracts its adoption? Are developers becoming lazy about learning new syntaxes because they can still accomplish their goals using "standard" syntax? Does using a more recent syntax (Linq, Lambda) reflect on the developer's skill or is it just demonstrating knowledge?



So that I'm not left behind, I'm trying to use these new syntaxes where I get a chance as I believe that the more you use them, the more readable they become - to you. Sometimes, developers fear new syntaxes because they don't understand them well enough to use them effectively and therefore they write them off if they can accomplish the same result in another way. If a new syntax makes for more concise code and it doesn't impair its readability, then I believe it is the right choice. One way to expose yourself to various syntaxes (and code styles) is to take part in an open source project and explore the source code.



I've only scratched the surface in this post about readability and understandability but the concepts of each are extremely important to grasp in order to write good code, IMHO.