Process Based Development

I have come across recently a way of developing software in an “agile” way. Stories are written, encapsulating some functionality and subsequent stories enhance or build on those areas. They are tested and deployed for review during and at the end of the sprint. Everything is working as you would expect. You work on a process till it is “finished”.

Is it really finished though?

However, what if there are dependant processes down stream? Can we say that the 1st process is done? It is like wiring a house without ever checking that switch A turns on light A. You wait till the whole house is built and decorated before finding out a) it isn’t finished and b) you need to make a hole in that wall to get into the wiring again. (I don’t know if this is strictly how you would fix that issue, i am not an electrician! Hopefully the analogy holds though).

An alternative process would be to develop part of the 1st process, then implement it’s usage in process B to verify a) it works and b) assumptions on it’s structure/format/workflow are correct. For instance we have a process to define some business rules, set parameters, data to be used in another process. We then implement a basic usage of those business rules to show them being applied and verify that amending them gives us the desired results. This is kind of like Unit Testing (or even Test Driven Development) for the application from a end to end point of view. We write enough code to add or edit some of these business rules, then enough code to test that their usage is correct. We can keep iterating over this adding more features and testing their usages.

This approach can catch unexpected behaviour and quickly highlight where additional refinement or changes need to be made. We also have a fully working system (basic at first, gaining more business value over time).

A step into a larger world

As Obi-Wan Kenobi once told Luke. (Clip)

That’s good, you have just taken your first step into a larger world

Recently at the Code Club i help out at, someone was struggling to work out why their python code didn’t run. He called me over, we looked at the code the project was asking him to write and what he had written. It looked right, no typos, no missing declarations or wrong variable names. We tried saving it and re-opening in case Trinket (online editor for python) had failed parsing it correctly, same result. So i called over another volunteer, they checked it…nope it looks right. A 3rd volunteer is free (we are lucky we have at least 4 of us every week), they checked it too. It looks right, so we decided to disregard the sample code and work out what we thought it should do. 2 changes later and after a corrected import statement and while loop parameter and it is fixed. SUCCESS!!! 10 lines of code, 2 bugs.

The student thanked us for our help and then proceeded to say

Those 2 bugs wasted 10 minutes of my life

I said “I know, welcome to coding in real life, you are lucky if it only takes 10 mins and never believe any sample code on the internet”.

“…you have just taken your first step into a larger world” 🙂

1st day at school

first-day-of-school-frugal-coupon-living-1024x682
Yesterday I helped out at my first Code Club, so it was like my first day at school. I had prepared (a bit) by going through some of the first few Scratch and Python projects on the Code Club site and doing them myself. Having not really done loads with either language I think it helped as I fell into some of the same pitfalls that the kids did (and some they didn’t!). It helped when they had questions or call me over to help as their code didn’t run and they couldn’t work out why. I was definitely more comfortable helping with the Python than Scratch, probably due to it being closer to what I do every day.

I am amazed by how quickly they picked things up and got through the projects, even adding their own touches to the designs as they went, with no fear or apprehension. It is great to see how enthusiastic they are to dive in and get things working. They even pick up the developer trait and not saving often enough when they are in full flow. 🙂

A big thanks to all the other volunteers for spreading a calm demeanour that helped the club run smoothly. Can’t wait for next week.

Code Club

Code Club

Very recently I got asked to help in running a Code Club at a local primary school, it is something i have been interested in for a while now. My daughters have both tried coding with scratch at home on weekends and loved it. Especially the Frozen and Star Wars projects on code.org. I tried numerous times to get my eldest’s school interested in running a club, even though my eldest isn’t quite at the right age yet. The projects and resources are targeted at 9-11 year olds. However my 7 year old has done some logic puzzles and coding in her IT lessons on both desktops and iPads. (She got me to download the app so she could teach the 3 year old how to code!)

Anyway i digress, the school were unresponsive and so I accepted an offer to help at another school nearby who have an existing club set up. There are around 4 or 5 volunteers and a class size of 20. The clubs run once a week during term time after school, are free, use school equipment and resources provided through the Code Club website. (Resources)

The projects start by using Scratch and get more advanced, moving into HTML/CSS and then finally Python.

The fact that the clubs run directly after school obviously creates a problem for those volunteers with jobs, luckily for me the company I work for slicedbread are lovely enough to allow me to have a break in the day of the week the club runs. I am honestly very excited about seeing what they produce and the ideas they come up with. Speaking to the other volunteers it is a thoroughly rewarding experience, mentoring the next generation of coders and digital creatives.

If this had existed when I was at primary school I am sure I would have jumped at the chance, I learnt to code when I was 18 (and already on a computing course at university!). Most children are so comfortable with digital technology now, it is in everything they touch and do pretty much. To give them the chance to understand what goes into that and how it works is amazing, it may even inspire them to go and create new apps or technologies themselves.

I hope to keep posting throughout the school year about the club and the activities we undertake. Also my efforts to learn Python.

Python

Is this the end of The Once-ler?

I don’t know if everyone is familiar with The Once-ler from the Dr Seuss story, The Lorax. If not some reading – The Once-ler

Now we all know who he is, he tells the Lorax in the story that

You never can tell what some people will buy.

However, due to the advent of Machine Learning and Big Data, you CAN tell what people will buy! Cloud platform providers like Azure and Amazon offer services you can build upon to process and interpret large data-sets to draw conclusions or make recommendations based on what the data says. Companies like IBM or SAS offer the insight and reporting solutions without you having to develop your own applications.

Does this mean that the human element of taking a chance of a product or service based on past experience and “gut feel” is coming to an end? Let’s look a bit closer at the Once-ler’s quote.

The Once-ler invents and sells a Thneed, the Lorax mocks him till more and more people buy Thneeds and the Once-ler’s business grows (to the detriment of the land and environment surroundings, a great warning for current worldwide issues, but that would be a whole different topic). The Once-ler is operating on the possibly now outmoded assumption that people’s buying habits can be unpredictable, however massive amounts of funding, research and computing hours are being spent in order to remove this unpredictability. Businesses need to be able to predict what you will buy and target you with these products and services. Their ability to stay competitive in the modern marketplace relies on them understanding what will sell and what won’t, and to whom.

So what do you think? Could the Once-ler survive in business now without this insight?

Decorator or Russian Dolls (part 2)

It has taken a few weeks, but i have finished an implementation of “extras” in the making a cuppa instructions app. Part 1, for those that missed it. All the code can be found on github, https://github.com/chrishey/CupOfCoffee-DecoratorPattern

So ideally i wanted to keep a similar style to getting the extras, passed in via the command line. I also wanted to be able to specify via the command line what beverage i was making. So lets tackle that last one first.

First i brought in a command line args parser (see the git repository to see how it works) to read the arguments and give me an dictionary of arguments and their values to work with. In our initial Main method now we can read the “drink” argument and from there initialise the right beverage type class.


if (args.Length == 0)
{
Console.WriteLine("Sorry, we couldn't find your beverage");
}
else
{
var commandLineArgs = new CommandLineArgsParser(args);

switch (commandLineArgs["drink"].ToLower())
{
case "tea":
var tea = new Tea();
tea.CuppaFather();
break;
case "coffee":
var coffee = new Coffee();
coffee.CuppaFather();
break;
default:
Console.WriteLine("Sorry, we couldn't find your beverage. Would you like a cuppa Tea, Father?");
break;
}
}

We can now run the program to give us instructions to make us a cup of tea

makebrewdrink

Moving swiftly on past that small hurdle, I started thinking (and hacking) about how to get extras in. I wanted extras, whatever they were to be an IStep, so i could chain them. I also need a quantity potentially on an extra i.e. spoons of sugar. I tried a few ways to do this and after several failed attempts to make it work 100% cleanly i ended up with the following.

An extra always has a quantity, so let’s create a class to hold that.


public class Extra
{
protected readonly int Quantity;

public Extra(int quantity)
{
Quantity = quantity;
}
}

We can now use this as base for all extras and inject the quantity in via the constructor. I also wanted the extras to implement IStep so i can keep calling .Do() if another IStep is injected into it.


public class AddSugar : Extra, IStep
{
private readonly IStep _step;

public AddSugar(IStep step, int quantity):base(quantity)
{
_step = step;
}

public void Do()
{
Console.WriteLine(string.Format("Add {0} spoons of sugar", Quantity));

if (_step == null)
return;

_step.Do();
}
}

As you can see here, we take an IStep instance and a quantity in the constructor of an extra. We pass the quantity to the base, print out our instruction and then see if we have a subsequent step to call. I now needed to put this together with the command line arguments along the lines of Make.exe -drink tea -sugar 2. Due to the quantity constructor parameter it wasn’t just a case of chaining the extras into the Tea/Coffee class, also there might be multiple extras and i need to know their names and what instances to create.

My solution was to read any extras off the command line arguments.


var commandLineArgs = new CommandLineArgsParser(args);

var extras = GetExtras(commandLineArgs);

Where GetExtras is as follows:


private static Extra[] GetExtras(CommandLineArgsParser commandLineArgs)
{
var extras = new List();

if (commandLineArgs["sugar"] != null)
{
var sugar = int.Parse(commandLineArgs["sugar"]);
extras.Add(new AddSugar(null, sugar));
}

return extras.ToArray();
}

Not ideal and it does “itch” with me a bit, but it works, which some of my other attempts didn’t. In this example we aren’t passing in another step, just the amount of sugar off the arguments. I also have to know the name of the extra to deal with it, i couldn’t see how to do this dynamically, this might be part 3! 🙂

Now the Tea and Coffee classes need to accept these extras to add the instructions.


public class Tea : BrewUp
{
private readonly Extra[] _extras;

public Tea(Extra[] extras) : base (new FillKettle(new BoilKettle(new AddTeaBag(new AddMilk(null)))))
{
_extras = extras;
}

public new void CuppaFather()
{
base.CuppaFather();

if (_extras.Any())
{
foreach (var extra in _extras)
{
var step = extra as IStep;
if (step != null) step.Do();
}
}
}
}

We move through the ISteps via the BrewUp method of CuppaFather and then if we have extras we loop through them and call any subsequent ISteps chained onto them.

makeabrewwithextras

We can now get extras like sugar printed out in our instructions.

Any feedback or possible tweaks to the solution are very welcome. The code is there on Github, fork it and send a pull request or get in contact.

Thanks for reading….i am off to make a brew!

Decorator Pattern or Russian Dolls

We were discussing at work a scenario where you would want to be able to execute behaviours from a controlling block of code and pass in many types of common behaviours. My mind went straight to abstract and base classes to solve it. However someone said it can be done using composition over inheritence. It is not something I had considered before and was interested to find out more and see if i could implement something similar in C#.

So given that i am partial to a hot beverage i decided to use the steps to prepare different types of these as they have shared steps or behaviours. i.e. boiling hot water, adding sugar, milk and pouring hot water into the cup. I started with the idea of an interface to represent the step.


public interface IStep
{
void Do();
}

I then create a class with one method on it to call the Do method on a step. The CuppaFather method being inspired by Mrs Doyle from Father Ted. The class constructor take an instance of IStep and the CuppaFather method calls the Do method on it.


public class BrewUp
{
private readonly IStep _step;

public BrewUp(IStep step)
{
_step = step;
}

public void CuppaFather()
{
_step.Do();
}
}

I can then implement IStep for as many steps as i can think of for making say a cup of coffee. So fill the kettle, boil it, add coffee to the mug, pour the hot water in and add milk. (Or milk first, i am not getting into THAT debate though) So here is the code to fill the kettle for instance. Note how it also has a constructor taking an IStep instance. When its Do method is called we check to see if we got an IStep instance and if so we call it’s Do method, thereby chaining the steps/behaviours together.


public class FillKettle : IStep
{
private readonly IStep _step;

public FillKettle(IStep step)
{
_step = step;
}

public void Do()
{
Console.WriteLine("Filling the Kettle");

if (_step == null)
return;

_step.Do();
}
}

This is then pull together into a console app by passing in new instances of each step to the previous one.


class Program
{
static void Main(string[] args)
{
var brewUp = new BrewUp(new FillKettle(new BoilKettle(new AddCoffeeToCup(new AddMilk(null)))));
brewUp.CuppaFather();
Console.ReadKey();
}
}

This gives us an output of the steps to make a basic cup of coffee…not very exiciting so far.

beveragemaker

What we really want is to be able to input the hot beverage we want steps for and get those steps back reusing the common steps, maybe even add in some “extras” like chocolate sprinkles or sugar (and the number of spoons).

That is for another time…

Part 2 now published Decorator or Russian Dolls Part 2

Taking the lead

I used to lead a team at an agency in the Cheshire countryside before my latest move took me to Altrincham. I was therefore directly responsible for people’s careers and shaping how back end code was written on software projects. I know you can say people are responsible for their own careers, but I think senior people need to help steer others in the right direction and offer advice. You can’t make people learn, but you can inspire them and keep them on the right track.

Recently I began thinking about my role there, since now I have my own projects to deliver and in terms of mentoring I am not directly responsible for other developers careers, at least officially. Back when I was responsible we managed to get the team built out to enable me to help out of projects that needed support and spend time pairing with others, making sure we use the right technology for the project and have the necessary time to complete the work. I hope this allowed me to spend valuable time with the 5 developers (all lovely lads!) I had in the team and work with the other “leads” to make sure we collaborated properly to build the best solutions efficiently. I believe this will have benefited everyone involved as I got their feedback and could discuss how and why we could solve a problem better, plus we grew to be a tight unit as a team. By this I mean we would pick up each others work easily and want to put in the extra effort for each other in times of tight deadlines. It also gave me the chance to see the bigger picture and introduce TDD, code coverage and continuous integration and deployment.

This wasn’t always the case, at first when the team was small, the workload wasn’t much smaller. I had projects to deliver myself as well as help out on others, plus I wanted to give them space to develop their skills with out fear of failure. At times I got too into my project (especially as it was a big client and had very tight deadlines) and wasn’t able to spend the time I needed with others in the team when they really needed me. I still maintain to this day that this impacted badly on 2 the members of the team I had at the time. They were left to fend for themselves and although they did their best they needed direction and support.

I was lucky that the company allowed me to build the team out and become less project dependant as the role of leading a whole team AND delivering projects 24/7 just wasn’t going to work, at least not in this case. By being able to put in the extra time and effort to bring others on we ended up happier, more efficient and better skilled as a team.

I would be interested to hear other developers views on this, if you are prepared to share them?

Thanks for listening!

Razor view anonymous models

I stumbled across this issue this morning…

'object' does not contain a definition for 'Error'

Description: An unhandled exception occurred.

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Error'

@if (Model != null) {@Model.Error}

Basically the anonymous type on the model is internal and can only be accessed by the assembly in which it is declared. Views get compiled separately and therefore can’t access the property.

How to fix it…

I found a nice little extension class referenced on stackoverflow


public static ExpandoObject ToExpando(this object anonymousObject)
{
IDictionary anonymousDictionary = new RouteValueDictionary(anonymousObject);
IDictionary expando = new ExpandoObject();
foreach (var item in anonymousDictionary)
expando.Add(item);
return (ExpandoObject)expando;
}

You then create your (very basic) anonymous model as follows:


const string error = "error message";

return View("viewname", new {Error = error}.ToExpando());

The razor view code is then as before:

@if (Model != null) {@Model.Error}

Prototyping with scriptcs and building in Nancy

I had a project brief to work with an API the other day at work and it wasn’t one I was familiar with. Looking at the documentation for it I knew I wanted to hit some of the endpoints, get the responses and analyse the data before I dived in and started writing anything. I am a NancyFx convert, I like how easy it is to build any kind of web/HTTP based application. The speed and lightweight feel of it just sits well with me. However in this instance I want to dive into making API requests even faster than opening Visual Studio, picking a NancyFX template and creating some routes to return data from the API via my browser.

So how to do it?

I happened to go into the scriptcs session at the NDC London conference done by one of the founding fathers Glenn Block. Could I use this?

Well the answer is yes I could and it was great fun into the bargain! I won’t go through how to install it, how cool Chocolatey is (it really is) as the guide on the site will do a better job. http://scriptcs.net

Yes, you have to know your way round what is in what namespace and get the right using statements (I have Notepad++ as my text editor, there is a plugin for scriptcs, which helps but doesn’t seem to bring in usings, I know there is a plugin for SublimeText maybe it is better? If so I would love to hear from you!). Personally I see this as a challenge to my own knowledge of the framework and part of my continued learning. Anyway it was nice to be able to jump into prototyping the API calls and seeing results quickly in the command line.

I put the following code into my text editor (removing the api key and location of the saved file on my machine) and saved it as ProjectsBoards.csx:


#r "Newtonsoft.Json.dll"

using System.Net;
using System.Collections.Specialized;
using System.IO;
using Newtonsoft.Json;

using (var client = new WebClient())
{
var data = new NameValueCollection();
client.Headers.Add("apikey", "<APIKeyValue>");

var response = client.UploadValues("http://kanbanize.com/index.php/api/kanbanize/get_projects_and_boards/format/json", "POST", data);
var responseString = Encoding.Default.GetString(response);
File.WriteAllText(@"<filelocation>\projects-and-boards.json", responseString);
}

To run this script (after installing scriptcs obviously), you open a command prompt navigate to the directory you save your file and type:

scriptcs ProjectsBoards.csx

Alternatively you could give the scriptcs command the full path to your script file.

Now I have a JSON file I can look at, analyse and determine how I can use it in my application. I can parametrise the script and pass in the API endpoint and file name of where I want the data to be saved.


#r "Newtonsoft.Json.dll"

using System.Net;
using System.Collections.Specialized;
using System.IO;
using Newtonsoft.Json;

using (var client = new WebClient())
{
var data = new NameValueCollection();
client.Headers.Add("apikey", "enter api key");

var response = client.UploadValues(Env.ScriptArgs[0], "POST", data);
var responseString = Encoding.Default.GetString(response);
File.WriteAllText(@Env.ScriptArgs[1], responseString);
}

I can call this script as follows:

scriptcs Parameterised.csx -- http://kanbanize.com/index.php/api/kanbanize/get_projects_and_boards/format/json path\to\file

Now I can hit any endpoint and write a file out with the results (reusable!). I passed each result into http://json2csharp.com/ to generate POCOs for use in the Nancy application.

I then switched into Visual Studio and created my project using the Nancy application template with ASP.NET hosting and Razor with the knowledge of what data I was working with and some POCOs to start me off. Also performing this rapid prototyping with scriptcs had got me to consider that the API call needs parametrising in my application and encapsulating correctly. I can also quickly try out optional parameters on the API calls without having to change my application and roll it back if I am not happy with the results.

By using this approach I haven’t created an application I then scrapped or refactored once I knew more about the 3rd party API I was using. Plus it was good fun to use something new and see if it improved the overall development experience.