Software Development

Rant of the day: Learn to frickin' count!

I was in a shop recently and I bought 6 items at £5 each. A total price of £30, even I can manage that mental arithmetic without resorting to a calculator. However, the till decided that the total price was £30.01. For a penny I really can’t be bothered to argue, but it got me thinking about code quality and wondering about what awfulness must be sitting in that system to create such a simple basic mistake.

My colleagues are probably all aware of my views on code quality. I rant daily whenever I see examples on ineptitude by people that are paid money to write code. I read and respond on forums in order to help others learn their craft, or just get unstuck when they accidentally dig themselves in a hole. However, I see on an almost daily basis these days people posting their homework questions with no apparent attempt to at least try to work it out from themselves.

Take this example I found on Code Project a while ago:

I need to know how to do some simple things with arrays please help with any!
1.Find largest or smallest value
2.Count how many times a given value is in the array
3.Count the number of even or odd integers in the array
4.Add up the sum and compute the mean
5.Create another array of the same size containing the same values in reverse order

This is very obviously an exercise from an introductory course on the language they were studying. They just want someone to give them an answer that they can copy and paste. If this is what they are like now, imagine what they will be like years down the road writing commercial software.

I’ve seen lots of evidence over the years of people writing software by copy and pasting examples from the internet without thought of what is actually going on. This results in slow, bloated, inefficient code that is integrated very badly with the rest of the system, hard to read, hard to debug, and is just generally a complete mess.

If you are tempted to copy and paste some code snippet from the internet for your application then stop and think first. Do you actually understand the code? If not, then don’t copy and paste it. If you don’t understand it, how will you debug it?

I would say that if you are tempted to copy and paste from the internet that you create a very small test application first, paste it in to that and learn how it works. Once you understand how it all fits together and how it works you can then write a version that will integrate in to your application.

While you are at it, write some unit tests to go with it. Make sure you test for edge cases, make sure you test for some normal cases too. If you ever get a bug, then add a test that replicates the bug. So if someone suddenly discovers your software things that 5 times 6 equals 30.01 you can add a test for it, fix the bug and redeploy the system. Hopefully, this would have been caught before the public get a chance to see the glaring error and write blog posts about it.

Software Development

It's that time of year again

It seems to be that time of year when lots of students are on forums trying to persuade people to do their homework for them. Since I’ve found difficulty in finding good quality software developers in the past there is no way I’m going to contribute to producing any more debased lazy Muppets claiming to be programmers. The rules of the forums I frequent most often are very simple, and I’d imagine that most forums will have the same set of rules. Basically the person requesting help has to show what they’ve done already then they can expect to get advice on how to fix the problem. If the question comes in and it is just a set of questions with a plea that their homework assignment is due the next day then they are likely to feel the wrath of several professional software developers that have most likely had to clean up the excrement left by previous generations of these inattentive indolent laggards.

I would like to make it perfectly clear for any student that is genuinely struggling with an assignment that forums are an excellent place to seek help. However, you must show that you’ve made an attempt yourself, that you’ve attempted to research the topic and that you are genuinely seeking help and not just a free ride. If you are willing to work through the problem yourself (with some assistance from folks on forums) then you will attain a greater understanding and you are more likely to reach that AHA! moment where everything clicks in to place in your mind. And what a joy that feeling is!

In the workplace

Training Developers

Software development is a very fast moving business and it is therefore vitally important to keep up with what is happening with technology. If you don’t you could be left behind pretty quickly. When the proverbial hits the fan you don’t want to be left behind.

If you remember back to your school days you might remember Æsop’s fable The Ant and the Grasshopper. The grasshopper did no work in the summer while the ant worked hard in preparation of the lean times. When the lean times came the ant was able to provide for himself, while the grasshopper starved. A lot of software developers are like the grasshopper, they are not planning for their future. You might think it unfair of me to tar all developers with that brush, but I don’t mean to. I did say “a lot” not “all”. So why do I think that?

Last year I spent three months trying to hire a developer. I was shocked at the number of developers that I saw had the attitude that they didn’t need to learn anything unless their employer put them on a training course. Some seemed surprised when I asked how they kept up to date with the industry they are in. Most just mumbled something about reading books and articles online. Given the answers to other questions I don’t think they spent all that much time reading.

Software development knowledge fades faster than ever these days. It seems like only yesterday that I was learning .NET 1.0, yet .NET 4.0 is just around the corner. In a couple of years I’ll be using it commercially. Technologies like LINQ to SQL which has only been released will be dead soon, if recent reports are to be believed. That’s a lot of new information to take in only for it to go stale. This week I’m immersing myself in ASP.NET MVC. I’ve been to a talk on the subject at it looks like that’s the way forward so I want to be at the head of the pack. I’ll also be taking a first look at Subversion this week also. On top of that I’ve got a stack of other things to get through.

When I left university I had the arrogance to think that I knew it all and for a few years I worked on that principle. But that attitude eventually caught up with me and I spent some time unemployed partly as a result of that attitude. I am determined not to let that happen again. These days I am acutely aware of what I don’t know. I think that now I am more aware of my limitations I am a better developer. Because I’m more aware of what I don’t know I strive to fill those gaps by ensuring my own education – I don’t let an employer dictate what I should be learning. I am pro-active and go out there and ensure I’m educated. Of course, if my employer wants to put me on a specific training course I’ll accept it.

I did a little calculation last weekend that surprised me. Actually it shocked the heck out of me. Last year my employer spend roughly £200 on training materials for me. However, I spent a lot more. Of the things that I can remember (so I suppose the things that were worth it) I spent £2500 on my own education.

So, who gets the benefit out of this? For the most part my employer gets the benefit. While I enjoy developing software, it is my employer that profits from it at the end of the day.

Most employer attitudes to training developers are that it isn’t worth it because the developer will just leave with the additional knowledge they’ve received for a higher salary else where. Well, I suppose they will if the employer has that kind of cynical attitude. Employers think that they have to find a replacement soon so why bother training a developer only for another company to take advantage of that. But the a lot of the knowledge that is walking out the door cannot be replaced easily. A lot of the knowledge walking out the door is about the employer’s business and that is unique. You can’t hire that in.

There are a number of “solutions” to this problem. Some employers go for the stick method. “If I train you up and you leave within X months then you have to pay part of the training costs back.” The disturbing thing is that I used to think that was an acceptable solution. But it really isn’t. It is a form of financial handcuffs, and the developer being trained is going to resent it. Quite possibly they’ll be off as soon as the handcuffs are removed or simply not accept the training.

The more reasonable attitude, I think, to take about training is to realise that the developer that’s just been trained has increased their market value. So, pay market value for the developer. You don’t necessarily have to do it straight away, give the training time to sink in if you want, but do increase the developers salary within a few months and don’t tie it to anything else. Don’t roll it in with inflation based increase, an annual increase, or other bonus schemes. Make it very clear that the increase is solely as a result of the greater ability or productivity as a result of the training previously received.


Solution: A simple challenge

For personal reasons I’ve not been very active in the last week or so. Therefore the solution I promised has been a bit late in coming.

Here is the “reference solution” for the simple challenge that I set last week. It is by no means the only solution, nor is it necessarily the best solution (depending on how you define “best”)

class Program
    private static void Main(string[] args)
        int width = Convert.ToInt32(Console.ReadLine());
        int height = Convert.ToInt32(Console.ReadLine());

        DrawBox(width, height);


    private static void DrawBox(int width, int height)
        // Work out the interior width and height (i.e. the width
        // and height of the space inside the box)
        int interiorWidth = width - 2;
        int interiorHeight = height - 2;

        // Work out what the top and bottom of the box should look like
        string topAndBottom = new string('*', width);

        // Work out what the interior rows should look like
        string interiorRow = string.Concat(
            "*", new string(' ', interiorWidth), "*", Environment.NewLine);

        // Work out the entire interior using the "trick" of defining
        // a string with a repeating character for as many rows as the
        // interior needs to be, then replacing each of those characters
        // with the pattern for a row of the interior.
        string interior = new string('-', interiorHeight);
        interior = interior.Replace("-", interiorRow);

        // Write the box to the console.
Technorati Tags: ,,,,

A simple challenge

A homework question was posted on a forum recently. As usual it was met with the cries of “We won’t do your homework for you” (We will, of course, help if you get stuck. But don’t expect any assistance without at least trying first)

The assignment was to write a console application that accepted a width and a height from the user and then created a rectangle using asterisks. e.g. This might be the result of the program

*      *
*      *
*      *

Now the query was asked how to complete this using loops (or conditional statements, we weren’t really sure – he said loops, but proceeded to talk about if/else statements)

One of the responses was along the lines of “I can do this without loops”, modified to add “Or conditional statements”, and modified again to add “Or recursion”

So, my challenge to you is to write a small program above without loops, conditional statements or recursion. It sounds difficult, but it isn’t really. I have a solution (from which the example output above has been taken) which I’ll post this evening.

Technorati Tags: ,,,,

Parsing OS Grid References

If you have ever been to an agile coding session you may have come across the concept of the coding kata. It is an exercise designed to improve coding skill by making you more aware of the different ways of building the same solution. It also tends to lend itself extremely well to TDD.

I was just looking at ways of converting OS National Grid References from their alphanumeric form to a purely numeric form and it occurred to me that it might make an excellent project for a coding kata.

So, what’s the deal with OS National Grid References. Well, they consist of two letters followed by a number of digits. For example NT2474. NT relates to a square 100km along each side. The first two digits represent eastings within that square, and the second two represent northings within the square. The complete reference gives you a square that is one kilometre along each side. Of course, you can modify this to produce larger or smaller squares. NT, NT27, NT245746. As The actual coordinate the grid reference resolves to is the south west corner of the square. Also, there are optional spaces between parts, so NT245746 could be written as NT 245 746.

There is a more detailed guide to the national grid on the Ordnance Survey website.