2
Apr
2013

What’s your problem?

Google don’t do Test Driven Development, so I don’t see why I should either
Countless software devs

We live in a world where people love to apply quick fix solutions to problems. Lose 10kg in 2 weeks! No cash until payday? Borrow £100 at 30000% APR! We crave these simple solutions, and deeply want to believe that they can make our lives better.

It’s a good idea to look at the context we are in, then work on a way to change the system that governs that context. Once we understand the system we can then look at adopting tools to help us.

For both of the examples I gave in the introduction a good question would be: What’s your food intake and exercise like? How are you spending your money currently? But people don’t do this. They are far more interested in the next miracle fix. Sadly in the IT world we are just as susceptible to quickly grabbing a new tool that worked for someone else, then either blindly going with it or quitting in absolute disgust.

We can improve the process we use to select tools to remedy our problems. The key to this is to stop only focusing on how a tool works and its benefits and start by asking what problem the tool solves. The first two parts are important, but useless without knowing if it’s solving a problem you have.

I suggest we use this checklist when considering any new tool or practice:

  • What problem does it solve?
  • How does it work?
  • What are the benefits?
  • Will this work in my context?

A lot of people have a strong reaction to BDD and Cucumber so let’s use that as an example. We’ll use a shortened summary of the checklist items then consider a recommendation for two different personas.

Cucumber checklist summary

Cucumber solves a number of problems often seen in software development where misunderstandings are rife. In particular it solves the problem of the business speaking a different domain language from the development team by asking them to collaborate on acceptance tests together in a simple natural language script.

Its benefits include living documentation, automated acceptance tests, and encouraging an outside in view of software development.

Is Cucumber right for Angus?

Our first persona will be Angus. Angus had an idea for a mobile app and hired two people he trusts to help him build it. Angus has heard great things about Cucumber at conferences and wants to try it out with his team.

Angus doesn’t have a major problem with miscommunication within his team. If he tries Cucumber he’ll probably decide that he doesn’t like “the extra layer of indirection” Cucumber introduces. As the readers of the tests are all technical, he could get the same benefit from just writing outside-in integration tests in the testing framework he’s already using.

Angus should look for other methods, or invent his own, that can give him some of the other benefits that Cucumber brings.

Is Cucumber right for Bruce?

Bruce works for a medium sized digital agency who specialise in online shopping sites for a wide variety of clients. His customers like to run discount campaigns during different seasons. Currently he gets the client to write the pricing rules down in an email which he turns into code.

The product owner for Bruce’s project is not part of the team. By using Cucumber’s acceptance criteria with the client he could quickly discover problems with his understanding of the pricing code and also open up other conversations with his client.

Bruce should look into adopting Cucumber.

Context is king

When you hear about a tool you need to look at the problem it solves, the other benefits it brings and consider if this makes sense in your own context. The misapplication of tools leads to pessimism and waste. We should be optimistic about new tools, but grounded in our current context. Context is king.

1
Apr
2013

Everyday Git Aliases

Git gives you as much flexibility in how you construct your VCS workflow as it does for the commands you use on your local repo. In your gitconfig file you can add alises for your favourite commands, in this article I’ll talk about mine. You can see my gitconfig on github.

git standup

alias for git log ––since yesterday ––author joe

Just about to head to a standup but you can’t remember everything that you did yesterday? This command will come to your rescue. It only lists what you did in the last 24 hours.

3400455 - fixed the whizzbang (Mon, 1 Apr 2013 13:57:37 +0100) 
5dae0a0 - whizzbang feature (Mon, 1 Apr 2013 13:57:32 +0100) 

git purr

alias for git pull ––rebase

In Git you have public and private branches, public branches are the ones on github (or your own git host), private branches are in your local git repo and are setup to be tracking or topic. A tracking branch is linked to a public branch and a topic is only in your local git repo.

You have two options when you want to integrate changes between branches, either you merge or rebase. By default when you do a pull on a tracked branch it performs a fetch then a merge. If you’ve made changes locally and someone else has pushed changes to your git host then git will automatically merge these together with a merge commit.

4ffe733 - Merge branch 'development' of github.com:YourCompany/project into master

On an active project with other colleagues using pull will generate a load of these noisy commits in your projects history. I only like merge commits to be in the history when a topic branch has been reintegrated. Tracking branches should have a linear history.

When you do a git pull ––rebase, git fetches the changes from your remote repo and then perform a rebase rather than the default merge. A rebase resets the HEAD of your local branch to be the same as the remote HEAD, then replays your local commits back into repo. This means you don’t get any noisy merge messages in your history. As well as giving a linear history, this also helps when using bisect.

git st

alias for git status -sb

Git gives a verbose output when you perform a status which is excellent when you are getting started with git. As you become used to the output you want a shorter version. The output of this alias shows a single letter that represents the change type and reports how far ahead of the remote branch you are.

## master...origin/master [ahead 2]
A  g
D  gitignore

git ready

alias for git rebase -i @{u}

Once you’ve committed a few local changes you’ll want to share them with your team by pushing to your git host. Before I push I always run the git ready alias to see what’s going to be pushed so I can reword commit messages and squash related commits together. git ready performs an interactive rebase on your unpushed commits.

Let’s say I’ve pushed two commits that are related to a new feature and I have another where I made a spelling mistake in the commit message. When I run git ready I get dropped into vim with this input.

pick 7f06d36 whizzbang feature - adding fizzbuzz
pick ad544d0 whizzbang feature - minor refactoring to fizzbuzz
pick de3083a spelling mizzztake

I want to squash the two whizzbang feature commits together. So I change pick to say s to squash the two together into a single commit. I also want to reword the commit with the spelling mistake. To do this I make the file look like:

pick 7f06d36 whizzbang feature - adding fizzbuzz
s ad544d0 whizzbang feature - minor refactoring to fizzbuzz
r de3083a spelling mizzztake

This gives me two new commit messages to edit, which I update. Now when I push the remote repo host receives two commits

3400455 - spelling mistake
5dae0a0 - whizzbang feature 

git lg

alias for git log ––pretty=format:’%Cred%h%Creset -%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’

When I’m viewing history I just want to see the SHA, the commit message, who made the commit and how long ago. This custom log output gives me:

3400455 - spelling mistake (20 minutes ago) 
5dae0a0 - whizzbang feature (28 minutes ago) 
efaea80 - Removing trailing space on save (4 days ago) 
c351700 - Copying to system clipboard in vim (5 days ago) 

I’d be interested to hear about your favourite git aliases too.

25
Mar
2013

An example driven guide to regular expressions

The problem

You have a string of text that needs to be checked to see if it fits a validation pattern or to extract information from it.

In the case of validation you might want to know if a given input is a valid currency amount like £100, so you can prompt the user to enter a valid amount before you process a transaction.

For parsing you might want to get the version of a users web browser given from a User Agent string like these:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0

To solve both problems we either need to either split the text and manually check for conditions in our code or we can use a regular expression.

What is a regular expression?

Regular expressions are a DSL which consist of two parts, a target string and the regular expression itself. The regular expression part is kind of like the patterns you use to search for files with wildcards. They look very scary at first but you only need to know a few rules to get the most out of them.

Given a target string of ‘Mississippi’ and a regular expression of /s/ we would get a match back as the target string contains at least one ‘s’. Though this is a quite simple example, usually they use a number of regular expression features like: /^d\w[uiop](in|vi)[^a-f]*$/ which matches ‘driving’.

How does it work?

A regular expression is made up of literal characters, metacharacters and escape sequences.

A literal is like in the Mississippi example above, the /m/ literally means this contains an ‘m’ anywhere in the target string.

A metacharacter is used within a regular expression for special characters that don’t have a literal meaning, for example a caret sign indicates this regular expression must match from the start of the line. Meaning /^s/ would no longer match Mississippi but /^m/ would.

Finally, an escape sequence is used to convert a metacharacter into a literal for when the need arrises. For example the dollar sign ‘$’ has a special meaning so to literally search for it we’d escape it by putting a slash at the front like /\$/.

Capture groups

The first metacharacter we’ll get to know properly is the capture group, as this is what allows you to extract a substring of text from a target string.

Let’s say we wanted to find the version of the IE web browser a visitor was using which has a target string of ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0′. In here we are looking for the ’8.0′ following the MSIE. A simple way to achieve this would be to do /MSIE (8.0)/

See this in action

Boolean ‘or’

If you wanted to search for one string or an alternative. You could do this with a pipe sign allowing you to match two different versions of IE: /MSIE (8.0|9.0)/

See this in action

Matching any single character

Searching for just 8.0 or 9.0 is quite limiting though, so let’s use the dot ‘.’ metacharcter to search for any version number with a length of three characters with /MSIE (…)/

See this in action

Iteration metacharacters

Matching on exactly three characters using the dot is working well for us here, but we know IE 10.0 is coming soon and many later versions after that. What we want is a variable amount of matching characters. To do this regular expressions allow us to put an iteration metacharacter right after a literal or other metacharacter to say how many times we’d like it to match.

Regular expressions give us curly brackets to do this. Say we expect exactly three occurrences of the previous pattern we could do: /MSIE (.{3})/

See this in action

The curly brackets also allow us to use a range to match a minimum or maximum amount using /MSIE (.{3,4});/. In this example we’ve added the literal semi-colon to indicate the end of the version in the target string.

See this in action

In we wanted to future proof to even larger version numbers of IE we could leave the second value blank: /MSIE (.{3,});/.

See this in action

The regular expression creators realised this is quite a common task, so they made metacharacters to support the common ranges:

  • ? : Zero or one {0,1}
  • + : 1 or more {1,}
  • * : Zero or more {0,}

The question mark is particularly useful when dealing with pluralization. So you could match a target string of ‘game’ or ‘games’ with /games?/.

See this in action

Positioning

We’ve been looking for regular expressions that occur anywhere within a string so far. When you want your expression to match an entire line you use the $ and ^ signs. ^ means from the start of the line and $ means from the end of the line. This prevents your expression matching characters you don’t want it to.

For example /^brown fox$/ matches ‘brown fox’ but not ‘brown fox jumps away’.

See this in action

Character classes

Up to now we’ve searched for literal matches or used the dot wildcard. Sometimes you want to search for a string that matches a list of possibilites. Square brackets allow you to do this /[01234556789]/.

See this in action

Within a range the hypen, ‘-’, becomes a metacharacter that allows you to specify a range. So the previous example can become /[0-9]/

See this in action

As well as numbers you can do the same with letters with /[A-Z]/ for uppercase letters and /[a-z]/ for lower case.

See this in action

These ranges can be combined to search for alpha numeric characters with /[A-Za-z0-9]+/

See this in action

Finally, you can invert the selection by placing a caret at the start of the range to search for the opposite /[^0-9]/

See this in action

Shorthand character classes

Just like with the range shorthands, the regular expression creators realised that character classes are a common occurance as well. To help with this they added some shorthand versions of popular tasks:

  • \d : digits [0-9]
  • \w : alpha numeric search for [0-9A-Za-z]
  • \s : searches for spaces, tabs, and other whitespace

All of these letters can be made upper case to search for the opposite, just like the caret did previously with ranges. So \D means not a number.

Capture groups extended

If you don’t want to capture the contents of a group you can put a question mark and a colon at the start of the group. This is useful when you need to use groups but don’t care what their contents are. For example /the cost of the (?:grey|gray) sofa is £(\d+)/ will handle the different spellings of the colour grey, but only capture the price.

See this in action

If you’d like to add text into the capture group to make it readable, called a named group, you can use angle brackets within the group. For example to extract a date naming each part you could use /(?<month>\d{1,2})\/(?<day>\d{1,2})\/(?<year>\d{4})/ on the target string ‘Today’s date is: 10/23/2012′ to get ‘month 10, day 23, year 2012′

See this in action

You can also reuse a capture group using \1 to search for the same pattern again, this is called a backreference. For example with html tags you’d want to make sure that the closing tag matches exactly the same element name as the opening tag with /<(em|strong)>.*<\/(\1)>/ which makes sure a </strong> or </em> matches it’s opening tag.

See this in action

/^d\w[uiop](in|vi)[^a-f]*$/

Earlier on I said this regular expression can match the target string ‘driving’.

See this in action

Let’s break down what’s happening here. We’ve got positional anchors with the ^ and $ that let us know the entire contents of the string are within the regular expression. Let’s remove them to give us: d\w[uiop](in|vi)[^a-f]*

The first letter is a literal ‘d’. Followed by a single alphanumeric ‘\w’. Then we have a range of possible single characters [uiop], followed by a boolean or ‘(in|vi)’ and finally a range between 0 or infinite characters that doesn’t contain any letters between a-f. One word which matches these conditions is ‘driving’.

If you come across such crazy regular expressions I’d encourage you to put them into Regexper which will explain them for you. For this expression Regexper gives us this diagram:

driving_regex

Why would someone make this regular expression?

I’ve given you a crash course in what you need to know to solve most problems with regular expressions. Now see if you can put your knowledge to test by solving this regular expression crossword puzzle.

Resources

  • Rubular is a great testing tool for creating regex
  • For converting a regular expression into a human readable form use Regexper
24
Mar
2013

A compassionate approach to the Pycon incident

You’ve no doubt heard about the recent incident at Pycon where Adria Richards attempted to correct the behaviour of some other participants of the conference by publicly shaming them. This resulted in a dire consequences for both sides with job losses and groups with agendas online battling against both sides. It’s a sad story for everyone involved.

A few commentators online have been saying the approach to how Adria reported this incident was the only option available to be heard. I find this depressing as it’s encouraging people to use fear of public shaming to change behaviour rather than through compassion.

When realising her feelings and needs weren’t met I wish someone in the same situation would’ve confronted the people responsible and said:

When I hear you making sexual innuendos about dongles and forking people, I feel incensed and disheartened because I need this community to be inclusive and considerate for myself and others like me. Would you be willing to stop making these type of jokes while at community events?

How would you react to someone requesting you change your behaviour in this way? I feel this makes an emotional connection that properly expresses the needs that aren’t being met by the requestor.

It’s making an appeal to the compassion of the people involved. It isn’t trying to coerce people through fear, guilt or shame. It’s letting other people know about specific behaviours that are hurting your feelings and needs, coupled with a request for change.

Update: I would encourage people in this situation to blog / tweet / talk about their experience in this style so more people can hear about their needs that aren’t being met by community events.

16
Feb
2013

Agile is more about efficiency then effectiveness

At Lean Agile Glasgow on Wednesday we were discussing a recent event in London where a bunch of agile luminaries got together to discuss “How to build the right thing”.

The author of the article starts by saying (emphasis mine):

Agile basically solved the problem of how to deliver software. Most any company that applies an agile method and mindset can get working software out the door. Now, the biggest waste in software development seems to be building the wrong product, or the wrong features.
Henrik Kniberg

I’m interpreting this as the author saying that we’ve figured out how to efficiently create software with agile, but we are still working out how to effectively deliver it.

Reading over the headline statements on the agile manifesto and focusing on the items on the left you will have an efficient system for developing software. But are they focusing on effectiveness?

Individuals and interactions over processes and tools

Waste reduction by encouraging conversation over heavy process or tools

Working software over comprehensive documentation

Waste reduction by building something usable first

Customer collaboration over contract negotiation

More effectiveness focused than the others here, though also the least adhered too

Responding to change over following a plan

Waste reduction by removing detailed future plans that’ll be wrong or unwanted

Seeing three out of four items as efficiency focused is what leads me to think that agile is more about efficiently than effectiveness. The hope is that by being more effiecient you get more time building valuable and effective software, rather than just doing the wrong thing righter. The area of building the right thing is where we should now focus our efforts.