Finding out my "Hacked to Properly" Ratio

Spent quite a big chunk of this evening discovering that code you write during a GameJam is not actually the most solid of stuff. I've reached the point where I'm piling dirty hacks on top of horrible code to try and get something almost working. Oh well, it looks like the time for a total rewrite is here.

The good news is that I can take all the ideas and stuff over to the new design. The bad news is that I'm going to have to do it. Mind you, at the end I can probably work out my "Hack to Properly" ratio. This is the ratio of time it takes to hack something together vs the time to do a proper job. The code took around a day to write first time. Lets see how long it takes to write the new one.....

Windows Phone, Wedding Lights and Bluetooth

Windows Phone connected Wedding Lights. Eight synchronized lights which can be controlled over Bluetooth. Each light contains an Arduino processor and a 16 NeoPixel ring.

I've finally finished it. I was going to write an article about my Windows Phone Controlled Wedding Lights. But instead I thought I'd do something different. So I fired up Adobe Premier and I made a video about them instead. It only lasts a couple of minutes, but boy was it complicated to make. Anyhoo, feel free to take a look and let me know what you think.

I've done something else I've not done before (and I feel a bit guilty about this one). I've put the Bluetooth code for Windows Phone 8.1 and Windows 8.1 up on Codeplex. I'm ashamed to admit that this is the first code that I've ever posted there. I really should have been posting stuff up there earlier. I'm determined to post more stuff as I come up with it.  

You can find a sample project (my Bluetooth Printer) and the BluetoothManager class that I used to communicate with the embedded Bluetooth controller. There are also details of how to configure the Bluetooth device and send and receive data. 

Wrestling with Pygame and Cheese

This pictures is entitled "Unsuccessful Cheese Movement Number 1"

This pictures is entitled "Unsuccessful Cheese Movement Number 1"

The "Wrestling with Python" sessions on Pygame are progressing well. Today we got some cheese moving around the screen under keyboard control. Next week we will add some more sprites and get some proper game action going. You can see on of the first attempts at moving things above. Perhaps we should clear the background after each draw.....

It occurred to us (in a rather nervous-making way) that we are exactly four weeks away from the big event on campus when folks are going to come in and make and show off games. But everyone is learning stuff and having fun, so actually I'm quite looking forward to it. 

If you want to find out what we have been doing you can download the content from here

Making Screencasts the Hard Way

Having the house to myself this morning I thought I'd record a video of the 08120 Programming 2 Exam walkthrough. I try to remember to make these when I've finished marking the papers. I can cover lots of the questions that folks might have about the right answers and I like to do this when the marking issues are still fresh in my mind. 

Of course it wasn't as simple as I expected. I hadn't done a video for a while, so I had to find the headphones and the microphone, install Camtasia (my favourite program for recording stuff), find that the headphone and microphone weren't working and that sound playback was also broken. Fix all that, set the screen size to work properly, create a PDF that I can browse through and finally, after half an hour of faffing around, get to sitting down and recording something. 

And I thought that modern technology was here to make life easier....

Making a quick Batch File

batchFile.PNG

It's not often that I get to show off my Command Prompt smarts. But now and again they are useful. The Command Prompt is the place in Microsoft operating systems where you type, er, commands. In the days of MS-DOS it was the only way that you told the computer what to do and it has survived, mostly unscathed, all the way into Windows 8.1 

Of course, in these days of touch and windows you don't often have to give commands at the console, but every now and then you do, and today I got to show off a trick that I've used for years. We were typing in long commands to start up services in the Microsoft Robotics Framework and I suggested that we make a batch file rather than type everything in each time. You can use the "up arrow trick" (press up arrow to get the previous command) but that doesn't work if you close the command prompt and re-open it, as this clears the command buffer. 

A batch file is simply a sequence of console commands that are stored in a text file and can be executed just by giving the name of the file. It has the language extension ".bat". You can run them from within Windows too just by clicking on them. 

What you can see above is a way that you can take a long and complicated command that you have typed in and convert it into a batch file.  You use a few tricks. First trick:

copy con doit.bat

This connects the console (your keyboard and screen) to a file called doit.bat. Essentially, everything I type from now on will go into this file. 

Second trick:

(press up arrow to get your command)

Remember that I said you can press up arrow to get back things you have previously typed. This works when you are copying into a batch file too. So to my recover my echo command in the above screenshot I just pressed up a few times. Once I had the command on the screen I just had to press Enter to put the text into the batch file. Note that if you want to enter a sequence of commands that you have typed into your batch file you just have to recover each one in turn. 

Final trick:

^Z

This is CTRL+Z. It tells the command prompt you've finished entering stuff into the file and want to return to typing commands. The destination (in this case the file doit.bat) is closed and then you can just run the batch file by giving the name. 

The console is a powerful thing in the command prompt. You can copy things to it (which can gives a behaviour similar to typing a file) and you can copy things from it. You can also do stupid things:

copy con con

This works, I'll leave you to figure out what it does. And how to get out of it. 

C# Code Snippets on Demand

Snippets.PNG

This is kind of neat. Rather than have to jump out of your IDE to find samples of how to use language and API features, you can just put the question into a comment and hit tab. 

It is a bit hit and miss at the moment (and doesn't know anything about XNA) but it would be a very interesting future development of my favourite place to work.... Have a play yourself here

What does ?? mean in C#

WP_20130829_17_11_38_Pro__highres.jpg

I sent out a tweet asking folks if they knew what the ?? operator in C# does. Quite a few people did, which was nice. For those of you that haven’t heard of it, here is my explanation.

If you are a C# programmer you will know about references. The way that C# works, a variable can be managed by value or reference. A variable of value type is stored in a location in memory. For example:

int i;

This creates an integer variable called i. When the program runs the system will decide where in memory the variable should live. You can think of computer memory as a whole bunch of numbered locations. Because that is what it is. Perhaps the variable i could live at memory location 5,000. Then when we write the code:

i=99;

The effect of this is to put the value 99 in memory location number 5,000

So, if I write code like:

j = i;

This would copy the value held in memory location 5,000 into the location where the integer variable j is stored.

This is how value types work. Whenever we do an assignment of a variable managed by value what happens is that the value is copied from one memory location to another.

So, what about values managed by reference? Well, in these there is a level of indirection between the variable and the actual data. Suppose we have a class called Account, which is managed by reference.

Account a = new Account();

This statement makes a new account variable and then sets the variable a to refer to it. The variable a will hold a reference to the new Account. Perhaps the new Account will be located at memory location 10,000 which means that the variable a (which might be stored at location 6,000) will hold the number 10,000 – because that is where the Account instance is stored. The Account class might have a Name property, so I can write code like:

a.Name = "Rob";

When the program runs it will go to location 6,000 (where a is stored) and read the number out of there to find out where the Account is. In this case the variable a holds the number 10,000 and so the program will go to the Account there and set the name.

So if write code such as:

Account b = a;

This creates a new Account reference called b which refers to the same Account instance as a, in other words it will refer to location 10,000.

So, in the case of the value the information is just there in memory, but for a reference we have to go where the reference refers. With references you can also set them to null:

a = null;

This has the effect of putting a “magic value” in the variable a that indicates it really points nowhere.  It is a way of saying “this reference does not point to any object”.

The null reference is often used in programs to indicate that the thing you asked for could not be found, or hasn’t been made yet, or doesn’t matter.

This “nullability” is so useful that people wanted to be able to make values “null” as well. So they invented one.

int? ageValue;
The addition of the question mark makes an integer variable (ageValue) that can be made null. For example, the bank might store the age of a customer when it is important (when the age is less than 20 say) but once a person reaches a certain age, from then on the age is completely irrelevant to the system. They can mark the ageValue as null to indicate this.

ageValue = null;

Programs can also test for null

if (customerAge != null)
{
// Stuff you do if the age matters
}

In XNA you often find nullable value parameters being sent to method, so that the method can know to ignore them.

So, I’ve been writing for what seems like ages, and I’ve still not explained what ?? does.

Well, ?? provides a convenient way that I can map the null value of a nullable variable onto a specific value

int actualAge = customerAge ?? -1;

It saves us having to write code that tests for null and sets a value appropriately. The above statement sets the value of actualAge to the value in customerAge unless the value in customerAge is null, in which case it sets it to –1.

if (customerAge == null)
actualAge = -1;
else
actualAge = customerAge;

In other words ?? is a short form of the above test.

Going to Hacked

image

Next weekend we are off to Hacked. I’ve managed to get hold of a couple of tickets so myself and number one son will be taking our places with lots of other developers who want to make something silly/awesome/fun. I’ll be taking my lovely Lumia 920 along and targeting the Windows Phone platform, along with a bunch of gadgets and other things that we can use if mood takes us.

I’m really looking forward to this. The plan is to try and last through Saturday night. But I’ve booked a room in a nearby hotel just in case…..

Yellow Book Translations

DSCF3436_7_8.jpg

Every now and then someone emails me and asks if it OK to translate my C# Yellow Book into their native language.

Of course it is.

I’ve added some notes about translation to the home page for my programming resources. Essentially you can translate into any language you like as long as you keep the colour of the cover yellow.

If you want to sell your translation that's fine too, but you are not allowed to charge more than the price of a translation of the novel "Pride and Prejudice" by Jane Austen in your language.

If you do manage to get all the way to the end of the book, let me know and I’ll link through from my pages to your translation.

What use is a structure?

DSCF1594.jpg

It is exam season here in Hull. Later in the week we have our programming exam. And I’m getting quite a few questions through about C# and stuff. Today I got asked the question “What use is a struct?”. I’ve been spending a lot of time talking about classes and references and how clever they are, and someone wants to know why, if classes are so wonderful, some objects in a program are managed by value. The answer is simple enough. It is because sometimes you really want a value which you can just copy around easily.

Consider the Rectangle type in XNA. We can use this to represent the position and size of something on the screen. And this is a struct, not a class. Take a look at this code:

a = b;
a.X = 99;

If a and b are both Rectangle variables the effect would be to set a to the value in b and then move the X position of a to 99. This would not affect the position of b.

If the Rectangle type was a class then we would have two references, a and b, that both refer to the same object in memory, so moving a would affect b as well. If we want to place objects in lots of different positions on the screen, and we don’t want them to be linked in any way, then structures managed by value is the way to do it.

Note that things like Textures are managed by reference. This makes a lot of sense too. An image is a large thing, and it is often very useful to be able to share one image for a whole bunch of things. Think multiple sprites in a space shooter. They will have a Rectangle value to give them a unique position and then a reference to a texture that they all share, to give them an appearance.

The XNA framework is full of objects that are actually structures because they work better this way. For example Color, Point and the Vector objects are all structs so that we can manage them by value. In fact, now that you know how it works, you should be able to look at any XNA type and figure out whether it is a class or a struct, just based on how it is used.

Class Design for Shops

IMG_6213_4_5.jpg

At the moment lots of our students are working on their first year coursework. This year they can make either a game, which I’ve called “Space Killers” or a management system for a record shop, which I’ve called “Vinyl Destination” (turns out that there actually is a shop with that name – which only goes to show that great minds think alike sometimes). By the way, a record is what very old people used to buy music on. They are pressed out of plastic, usually 7 or 12 inches in diameter, easy to scratch and becoming fashionable again, which is nice.

Anyhoo, one of the central design decisions that has to be made is just how to store information about the records that the shop has in stock. We have been discussing this in the lectures, and it is interesting to reflect how the business needs of the customer affect the organisation of their data. Lots of shops have things to sell, but the way that you would hold their stock information varies from one business to another. I’ve broken these down to three broad categories which I’ve called “Art Gallery”, “Car Showroom” and “Supermarket”.

Art Gallery: In an art gallery each item of stock is unique. Or at least it better had be. Turning up with a second copy of the “Mona Lisa” would probably raise one or two eyebrows. Every item in the gallery has properties such as the artist or artists that made it, the date, the description and the price, but this information would be held for each individual piece in stock. To store the data for this application we would design a class, perhaps called “StockItem”, and then create an instance of this class for each of the pieces that is held in stock.

Car Showroom: This situation is slightly different. The showroom will hold a large number of cars, but they will be of particular models. They will hold a large number of “Ford Fiestas”, but each of them will have different trim levels, colours, ages and prices. If we held the data about our cars in the same way as we hold the data in the art gallery we would store a lot of the same data multiple times. Every individual StockItem would hold its own copy of the name, the manufacturer and lots of other information common to all Ford Fiesta models. A better way to design the system might be to have a class, perhaps called “CarModel”, which holds all the information about the type of car (all the “Ford Fiesta” parts) and then have the “CarModel” class hold a list of all “StockItems”. The StockItem class would hold all the information about a particular car, including things like colour, mileage and price. The CarModel class could hold a list of the StockItems of that particular model. For example, if the garage had a red Ford Fiesta and a blue Ford Fiesta there would be a CarModel object for Ford Fiesta which holds a list containing two items, a StockItem for the red one and a StockItem for the blue one.

Supermarket: In the case of a supermarket which is selling cans of beans, all the cans of beans of a particular type are identical. In this case we don’t need to store information about any individual can, instead we just want to know how many we have in stock. So for the supermarket we just need an object that holds a description of the item and also contains a counter that tells the system how many of that item are in stock.

If you are building a system for a customer, it is worth considering which of the three arrangements makes most sense. In terms of Vinyl Destination I reckon that because the you can get records of different quality it is more like a garage or an art gallery. The shop may have several copies of “Abbey Road” by The Beatles, but some will be in pristine condition (and therefore worth more) than others.

Making Sample Data Can Be Fun

image

I’ve just released the first year coursework specification. We are going to be creating a system for a record shop which will track customer orders and suggest new records to buy. I think the only reason I went for a record shop was so I could call it “Vinyl Destination”, but that is by the by.

The program needs to be able to track huge numbers of records and customers, along with their orders. The problem is that, at the start, we don’t have any customers or orders, or anything. And anyone that thinks the way to solve this problem is to sit down and enter some details when the program has been built is very, very wrong.

I make the test data right at the start. I use a program to do it. All it takes is a few loops. Now, I could give the customers names like “Customer1”, “Customer2” and so on. But I can do better than that. All I need are some names to start with:

string [] firstNames = new string [] { "Rob", "Fred", "Jim", 
  "Ethel", "Nigel", "Simon", "Gloria", "Evadne" };
string [] surnames = new string[] { "Bloggs", "Smith", 
 "Jones", "Thompson", "Wooster", "Brown", "Acaster", 
  "Berry", "Ackerman" };

Here we have two arrays, one of first names and the other of surnames. I can now make myself a whole bunch of reasonable looking customers:

foreach (string surname in surnames)
{
    foreach (string firstname in firstNames)
    {
        Customer c = new Customer(firstname + " " 
                                  + surname);
        result.Customers.Add(c);
    }
}

This makes us a whole bunch of customers, from “Rob Bloggs” all the way to “Evadne Ackerman”. We can do something similar with the names for each of the recording artists:

string[] artist1 = new string[] { "Pink", "Flying", "Random", 
    "Singing", "Uptown", "Family", "Floating" };
string[] artist2 = new string[] { "Chicken", "Circus",
    "Banana", "Kitchen", "Groats", "Monkey", "Collective", "Pyjamas" };

I can combine the words to make random artist names.

string artist = artist1[rand.Next(artist1.Length)] + 
    " " + artist2[rand.Next(artist2.Length)];

This picks a random word from the first list and adds it to the second one (rand is just a random number generator that I use to make my test data). So I can have “Pink Pyjamas” and “Singing Groat”, among others. I do something similar for the track names, except that I have three parts. As you can see in the screenshot, this makes for some quite funny combinations, that liven up testing no end.

Don’t Make the Recipe Static

DSCF0719_20_21.jpg

Today it was time to talk about static class members in the first year programming course. I always have problems with static. If you are not careful you start thinking that static means “doesn’t change much”. This is reasonable in real life, but not in C# programming. In C# programming static means “always present, part of a class and not of an instance”. At this point I thought an example would serve well, so we started talking about contexts where static would make sense.

Consider that you are implementing a system for a fast food store. You have a class called “Dish” and the system stores an instance of Dish for each of the items in the menu. A Dish instance holds the list of ingredients for the dish, the sale price, and the name of the dish. For example, there might be a dish called “Chicken and Chips” which held two ingredients (chicken and chips) and a price value (perhaps four pounds 50 pence). I asked everyone for some suggestions for static class members for the Dish class. Someone suggested that recipe would be a good candidate for a static member.

Not so. The idea of a static member is one that exists as part of the class. Which means that recipe could only be static if every dish was cooked in the same way (i.e. the recipe exists once for the entire class). Each dish needs its own recipe, so this must be a non-static member of the Dish class. However some things, for example the maximum and minimum price of dishes, the maximum number of ingredients that a dish can have all make sense as static members of the Dish class.

Simon has a sign on his office wall, “Consider the context”. When trying to work out what makes sense as static and what doesn’t, this is very good advice. And a good starting point is that “a recipe should not be static”.

Delegates and Dice

IMG_5589_90_91.jpg

I’ve been marking the First Year “Space Cheese Battle” implementations and it has been great fun. The aim of the work is to implement the board game “Space Cheese Battle”, which is a bit like Snakes and Ladders crossed with Ludo. Using rockets. And cheese.

The program uses a dice to control movement and in the final implementation this must of course be completely random. However when the program is being tested a random dice is exactly what you don’t want. What you want is a way of getting particular numbers so that you can test the program more easily. I first hit this kind of problem when I wrote a pontoon (blackjack) playing program and I had to wait for ages for the game to deliver a King followed by an Ace, so I could test the part of the program that deals with that. Eventually I figured out that a good way to speed things up was to allow me to type in the card being dealt, rather than have it picked randomly. Later on I worked out that it is even easier to test if you have a “pre-set” list of cards that are dealt automatically.

We can do the same thing with the dice for Space Cheese Battle. I can make three dice methods:

static Random rand = new Random();

static int RandomThrow()
{
    return rand.Next(1, 7);
}

This is the “production” dice throw method. It just delivers a value between 1 and 6.

static int InputThrow()
{
    int result = 0;

    while (true)
    {
        Console.Write("Enter throw value: ");
        try
        {
            result = int.Parse(Console.ReadLine());
            break;
        }
        catch { }
    }
    return result;
}

This is the “manual entry” dice method. It returns whatever the user types in. Note that I’ve not limited the values at all. It is sometimes useful to put in very large dice throws so that you can move the player onto particular squares for testing.

static int[] diceValues = new int[] { 
    3,4,5,4       
};

static int throwPos = 0;

static int FixedThrow()
{
    int result = diceValues[throwPos];
    throwPos = throwPos + 1;
    if (throwPos == diceValues.Length) throwPos = 0;
    return result;
}

This is the “fixed sequence” dice. I can create a whole set of values and then have them replayed by the dice. This is great for automated testing.

Of course the difficultly comes when I have to write my program and I need to switch between these dice behaviours. The students had hit this problem too, and some had created tests that checked the dice mode and then called the appropriate method when the program ran. These work, but the best way to select the kind of dice that you want to use is to create a delegate which refers to a particular method.

delegate int DiceThrow();

A delegate is a type of variable that can refer to a method with a particular signature. The statement above creates a delegate type called DiceThrow which can refer to a method that accepts no parameters and returns an integer, just like the dice methods above.  I can create a variable of type DiceThrow.

static DiceThrow ActiveDice;

The variable ActiveDice can refer to any method that accepts no parameters and returns an integer. I use it like this:

ActiveDice = new DiceThrow(RandomThrow);

The variable ActiveDice now refers to a new delegate instance connected to the RandomThrow method. Which means that the statement:

Console.WriteLine(ActiveDice());

- will call the RandomThrow method and print out what it returns.  In other words I can use the ActiveDice method exactly as if it was a method that accepts no parameters and returns an integer. When the delegate is called it will follow the reference to the method it has been connected to and then run that method. The great thing about my game code is that I don’t need to change the code that uses the dice to make it pick up new dice behaviours. I just have to call the delegate.

Note: The C# implementation also incudes a Delegate class (note the capital D) which intellisense might try to get you to use. This does something different and confusing. Make sure you use the delegate with the lower case d.

Debugging Conditions

IMG_5091_2_3.jpg

We had our first programming lab proper today. Conditions and statements. Great fun. The aim of the exercise is to decide whether or not a given customer can see a movie at your multiplex. The inputs to the program are the age of the customer in years (age), and the number of the movie that the customer wants to see (filmNo). Movie number 1 is Looper (which is apparently quite a good film). In the UK you have to be at least 15 years old to see the film. Quite a few students wrote code like this, which makes very good sense:

if (filmchoice == 1 && age >=15)
            Console.WriteLine("Enjoy the film");

The condition will fire when the customer has chosen film 1 (Looper) and their age is greater than or equal to 15, which is just fine. However some folks then decided to improve the program to add the message that says “You are too young”. And they used the else keyword to achieve this:

if (filmchoice == 1 && age >=15)
      Console.WriteLine("Enjoy the film");
 else 
       Console.WriteLine("Access denied - you are too young");

This seems to make sense, but we now have a problem when we finish off the program. People start being denied access to films when they shouldn’t be. In fact they see lots of denied messages, which is very confusing.

The problem occurs because the program can enter the above statement in one of four states:

  • Film is Looper and age is greater than or equal to 15
  • Film is Looper and age is less than 15
  • Film is not Looper and age is greater than or equal to 15
  • Film is not Looper and age is less than 15

The if condition above will fire if the program is in the very first state (which is what we want if the customer can see Looper) but the else part will fire in all the other three states, leading to “Access denied” messages when we really don’t want them. One way to solve this is to split the program into two tests, so that the age test only takes place once the program has decided that the customer is seeing the Looper film.

Actually, there are lots of ways of addressing this issue. For me the interesting thing is that you need to be careful when you take a tiny piece of code that works fine (the original test) and then add a bit extra to it. Your program must handle all the input possibilities, not just the ones that constitute the “Happy Path”.

Sneaky First Year Programming Lab Fun

IMG_4899_900_901.jpg

We had all the First Year students in for their first lab today. As part of the fun we ask the students to find the bug in this piece of C# code, which is supposed to add two numbers together.

   1:  static void Main()
   2:  {
   3:      Console.WriteLine("This program adds two numbers together");
   4:      Console.Write("First Number : ");
   5:      string number1Text = Console.ReadLine();
   6:      int number1 = int.Parse(number1Text);
   7:      Console.Write("Second Number : ");
   8:      string number2Text = Console.ReadLine();
   9:      int number2 = int.Parse(number2Text);
  10:      int result = number1 * number2;
  11:      Console.WriteLine("Sum is : " + result );
  12:  }

Lots of people found the error straight away. But some people (usually the more advanced programmers) didn’t. I had reports describing problems with number parsing, the range of the input values, crashes caused by entering text instead of numbers, and all sorts of things like that. But the real problem is much, much simpler.

The program says it adds two numbers. But the statement at line 10 which works out the result actually does a multiplication rather than an addition. Which means that the program is completely correct, runs fine, but does the wrong thing. This is a surprisingly common problem with programs. You can write a program that is perfect, works a treat, but doesn’t do what the customer wants. And you will not get paid/get fired as a result.

The nice thing about this “sneaky” lab for me is that it worked on two levels. Those learning how to program can see how the computer follows a sequence of statements, executing each in turn. If the statement is wrong, the output is wrong. Those who can program a bit have hopefully learnt that it is a good idea to read the specification when you start writing code…..

On Broken Software

IMG_4097_8_9.jpg

Both Scott Hanselman and John Batelle have been having problems with their software over the last week. Both their posts are well worth a read.

Years and years ago Gerry Weinberg wrote "If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilisation". Not much has changed since.

It's always more fun (and more lucrative) to make and sell new stuff than it is to mend broken old stuff. And software lets you sell stuff that isn't finished. Unlike things like bridges: "Hey, that doesn't reach the other side!", and buildings: "Hey, where's the top floor?", many faults in programs take a lot longer to show up.

With a program you can leave out error handling, load testing and all the other boring bits that make a program properly useful and still ship a product that will sell by the boatload. With a bit of luck not that many people will notice. Of course, you could spend a lot more money and time getting it right. Snag is, I don't think that anyone would stay in business today making completely perfect software that took ten times as long to write as the current state of the art. People will go for new and shiny over old and working most of the time I'm afraid. I don't think I've ever seen an Engadget post about a new version of a product that is exactly the same as the old one, but works properly.

I’ve been writing software for an unfeasibly long time and some of it has wound up in the hands of proper users. I pre-date objects, Test Driven Development and pair programming. I wasn’t there when the loop was invented, but I I think I saw it in the papers. When I write a program I worry about everything, particularly what could go wrong. To me “The Happy Path” is an aside. I’m spending all my time fretting about “What happens if the response never comes back?” or “What if I get millions of these when I only asked for five?”.

It drove me nuts when I found out that the standard input/output libraries in C didn’t actually check the length of what was given to them, making the potential for buffer overrun part of the run time experience. I wrote my own input validation suite. I put it in all my products. I added timeouts everywhere. I didn’t particularly do this as part of a methodology, I just did it because it seemed sensible at the time, rather like a builder would make the ground floor before starting second floor. My programs hardly ever went wrong. Even the really big ones.

As a person who also teaches programming I try very hard to make sure that students take this approach when they write code. We start with defensive techniques and move on from there. As soon as you have a need for a number (say perhaps the age of your customer) then start to worry about how it might go wrong, become negative, very large, or change by more than 1 after a year. I don’t see this as tied to any particular methodology, I just see it as common sense, and I really want my students to have the same mind-set when they write their programs.

Modern development environments give you a lot more tools for making products that can be more reliable. Of course, the flip side is that the products can also do a lot more and that the demand for new, innovative solutions delivered in record time has never been greater. For me the only really good news is that where it is important to get code really right, for example in cars, airplanes and nuclear reactors, the software industry does seem to be able to deliver properly working systems, albeit slowly, and at great expense.

For the rest of us, I think it is as much our fault as anyone else’s that we are in this situation. We are keen to queue up for the next iPhone when the one that we have doesn’t actually work properly. Until we start only buying software that really works (and probably paying more for it) then this is how things are going to stay.

20 Programming Opinions

IMG_3373_4_5.jpg

Today I was reading a post from James which linked through to a list of “20 Controversial Programming Opinions” that folks had posted on Stack Overflow. It is worth a look, if only to foster debate about the subject.

I’ve not got a problem with any of the statements to be honest. Some of them make more sense in particular contexts, but none of them are outright daft. I think most of them boil down to:

If you are using something make sure you are doing it for the right reasons and for the right effect. Adding comments, drawing use case diagrams, writing unit tests etc etc should not be things that you do “Because you have to”. In the right context these techniques are crucial. If I’m meeting a customer for the first time and I need to have something that will serve as the basis of of discussion a Use Case Diagram is essential. But if I’m writing a quick, single purpose, application then there is no need for that level of detail.

The bottom line is that before you use something you should consider why you are using it and how it will add to what you are building.  Don’t feel bad because you haven’t used a design pattern, or written thousands of unit tests. Just ask yourself how much the user likes what you have made, and how you can make it work better.

C# Yellow Book 2012 Now Available

Yellow Pages

The latest version of the C# Yellow Book is now available for free download. You can get it here, or you can press the spiffy new short cut on this page.

There are a few changes. I’ve fixed all the mistakes that have been sent in (and probably added a few more). The section on Graphical User Interfaces now covers XAML rather than Windows Forms. And the text now mentions “The Wizard of Oz”