Category Archives: Technology

Posts about software development and technology

Break the Line, Change the Sign

Ipswich Civic College [(c) EADT]

Ipswich Civic College [(c) EADT]

Shirt and tie, green tank top, brown jacket with leather patches on the elbows and horn rimmed spectacles. The image of my college maths tutor yelling “break the line, change the sign!” is still burnt into my brain.
Nevertheless De Morgans’s Law is one of those things from my days as an electronics research technician that’s still useful today – so it was worth it.

I needed to change an if statement around. Originally it was like this;

if (null == cert || !cert.HasPrivateKey)
   doMainPart()
else
   doElsePart()

But I wanted to switch the main and else part around, so I wanted to reverse the result of the condition. I could have done this…

if ( !(null == cert || !cert.HasPrivateKey))

But instead I employed De Morgan’s Law. To invert the meaning of the entire condition the first step is to invert the meaning of each individual term of the condition. So:

  • null == cert becomes null != cert
  • !cert.HasPrivateKey becomes cert.HasPrivateKey

The second step is to change the operators that combine the terms, so OR becomes AND and vice-versa.
Thus (null != cert && cert.HasPrivateKey) gives the exact opposite result to (null == cert || !cert.HasPrivateKey).

if (null != cert && cert.HasPrivateKey)
   doElsePart()
else
   doMainPart()

It’s really easy to tie yourself in knots with this kind of stuff, remembering De Morgan’s law can save a lot of heartache.


If you’re wondering why it’s “break the line, change the sign” this is because of the way boolean logic is written is electronics: if you want to invert the meaning of something you don’t put a ! in front of it, you draw a line over the top of it.
So our condition would have started as:

null == cert || cert.HasPrivateKey

We then want to invert the entire operation, so we draw a line right over the top…

null == cert || cert.HasPrivateKey

Now we apply De Morgans’s Law, we break the line and change the sign.

null == cert && cert.HasPrivateKey

Of course the two inversions on the right-hand side cancel each other out, so we finish with:

null == cert && cert.HasPrivateKey

Of in C#

null != cert && cert.HasPrivateKey

Social Personality

Singapore

Singapore

I made a mistake when I joined The University of Hull back in 2008. I don’t mean that joining university was a mistake, it was one of the best career decisions I’ve made. I made a mistake with the way I used social media.
Part of the role at The University was to be an ambassador, to represent and promote the university and the field of computer science. “No problem,” I thought, “I’ll just use my existing social media accounts.” That was the mistake.

It led to two problems. The first is that my online persona changed. I suddenly became aware that I used the account for professional purposes. That changed the image of the account, it became my professional persona and it became very difficult for me to be the person that my friends know outside of the professional environment.
The second problem is that I started adding professional “friends” and followers.  That reinforced the first problem.
I went from someone who’d been very active on social media to someone who only posted the most carefully filtered content.

If you are a professional you have to be somewhat circumspect, once you’ve hit the “send” button you’ve lost an element of control – it’s out there. Even friend-locked posts and old blog articles can resurface at inopportune moments.

At the last interview I was at one of the interviewers let slip something that they couldn’t have known unless they’d read my blog – and not only that but it wasn’t in the most prominent article either. As a professional your online presence matters.

Fortunately the Victorian image of professionalism is now fading. I maintain however that a professional image is important. It’s about giving your clients and peers confidence that when you turn up to work you’re going to do a good job. For instance, if your social media streams contain a disproportionate amount of pictures and stories of you partying with your friends to all hours that’s going to damage your professional credibility.
A balance still needs to be maintained.

For myself however I no longer work for The University, in fact being an ambassador for computer science is no longer an official part of my job at all. So you’d think it would be easy for me to rectify those mistakes I made back when I joined The University and revert my social media personas back to being more like the real me. You’d think that. It appears to be proving more difficult than I thought, however.

Avoid Constructors that Throw Exceptions

Even ones in the CLR itself…

It’s well know that throwing exceptions in constructors is a bit dodgy (mainly because of possible memory leaks), but things recently got a bit weird.

It worked fine on my machine, however on my colleague’s machine Visual Studio’s unhandled exception dialog kept popping up. After he hit the continue button everything seemed to work OK, but it was still unnerving.

To cut a long story short I’d written something a bit like this[1]

        private async static Task<TcpClient> GetConnectedClient()
        {
            try
            {
                return await Task.Run<TcpClient>(() =>
                {
                    return new TcpClient("127.0.0.1", 80);
                });
            }
            catch(SocketException ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }

That particular overload of the TcpClient constructor tries to open a connection and throws a SocketException if it fails. The exception should be marshalled through the await to the catch, but it was all going a bit strange.

So I changed it to something a bit like this[1] and it all started to behave properly.

        private async static Task<TcpClient> GetConnectedClient()
        {
            try
            {
                var client = new TcpClient();
                await client.ConnectAsync();
                return client;
            }
            catch(SocketException ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }

Underneath the bonnet (or hood, if you’re not British) ConnectAsync starts a Task to manage the older BeginConnect asynchronous mechanism. Nevertheless, exceptions throw in this scenario are marshalled properly with no weirdness.

Now ultimately both versions of the code seem to work correctly, but what is clear is that there is something different about the exception handling in constructors. So I’d recommend avoiding not just avoiding throwing exceptions in your own constructors but if you can, avoid using constructor that throws an exception.


[1]In order to make the point clear I’ve simplified these examples to the point where, as code, they’re not terribly useful in their own right. I certainly wouldn’t recommend using these as any kind of template.

Because Great Things Grow From Seeds…

Way Back When...

Way Back When…

I’d been working for Seed Software for a few short weeks when the manager announced that he was going snowboarding for a week. “Who’s in charge whilst you’re away?” I inquired, “You are!” came the reply.

Over a year ago I posted the story of how I came to work for Seed Software, but the story didn’t end there. There’s the small matter of what happened between then and me leaving Seed in October 2015. It was quite an experience. I’m not sure if people take me entirely seriously when I say that I learnt as much in Seed as any of the interns or students but it is nevertheless true.

So I’d just about worked out where the stationary cupboard was and suddenly I was being asked to run the business. This was definitely not in the job description for a software developer but before I made that point I took a moment to think about it. I’d spent the past few years in my previous company trying to convince the senior management that some software developers understood more than just matters technical. I’d had some success, but here was an opportunity for me to prove it by stepping right into the front line of running a business, if only for week.

Nothing much happened, it was rather an uneventful week. I don’t know if I was more disappointed or relieved. Nevertheless it cemented my position as being very actively involved in the running of the business of Seed Software.

After that I started getting down to trying to learn WPF and WCF, neither of which I’d used before, and trying to build a Command and Control system. I’d got out on the road a bit too, the C&C was very much developed with the Fire Service which meant frequent visits to site with the latest developments to make sure we were all heading in the same direction. I’d also been to a few of the other sites because although the other products were managed by the Seed Manager there was only one of him and we needed some resilience.

It was that need for resilience that soon bit us, “Tom,” the Seed Manager said, “Erm… I’ve double-booked myself. I don’t suppose you could cover a sales presentation next week? I’ve got the slides and everything.”

This wasn’t entirely unexpected. I’d known before I joined Seed Software that it was just a software development team. There were no sales, marketing or operations staff. A business can’t survive without these functions though which left only one conclusion: the development team were doing them. This is actually one of the things I found exciting about Seed, if the business was going to work and I was going to be successful within it I knew that I was going to have to get involved in these functions to a much greater level than I ever had been before. Sure I’d been to sales presentations, I was actually a bit of a regular, but I’d always been the “technical expert” that answered the questions that the salesperson couldn’t. I’d never actually delivered a sales presentation before.

As it turned out Seed’s presentation was part of a much larger event where several suppliers were pitching their wares at a group of senior fire officers from many services across the country.

This was a great learning opportunity for me – I was on relatively late in the day which meant that I had a lot of time to watch what the others presenters did and tune my own performance. I was expecting swish, professional salespeople to glide in and deliver polished shows that would make mine seem shambolic and amateurish.

That is not what happened. They were all professional enough but there was no performance, no spark, no charisma.

By my slot half the audience had been struck down with a nasty case of…

.

This is where it struck me how just how different Seed Software really was in 2009, there was nobody else like us there. I had some Powerpoint slides, just to get across some of the key information, but most of our sales presentations were live demonstrations of the kit. This, it transpired, was a breath of fresh air. I was able to engage with the audience, sleepy heads popped up and started asking questions. I ran out of business cards and had to start writing my details on the back of potential customers’ ones.

I was beginning to settle in to Seed, I was a lead developer, software architect, product manager, deputy business manager and occasional sales guy. The phrase “can do attitude” crops up in a lot of places and generally it means something it’s not supposed to but in 2009 / 2010 Seed Software embodied it in its true sense. It didn’t seem like there was anything that we couldn’t make work somehow.

2011 was not so kind to us. The business was growing too fast for The University to react and we were all having to put in way too much work just to keep our heads above water. To make matters worse the Seed Manager ran into a spot of bad luck – a couple of serious accidents ruled him out for extended periods of time. I found myself trying to develop the Command and Control, project manage delivery to the first control rooms and the subsequent go-lives and manage the business of Seed itself.

It was insane, one day I looked at my timesheet and I’d accumulated 28 extra days of time-off-in-lieu. I had to offload the management of the business to the department’s Enterprise Director, or I would have burnt out.

Despite the workload Seed was still a hugely positive, exciting place to work. What we’d achieved was pretty amazing too, two industry professionals and a bunch of students had successfully developed and delivered a mobilising system – the single most important computer system in a fire service – into two live control rooms.

The workload however was still out of control. Even with the Seed Manager back full time it was clear that we needed to make big changes. The Seed Manager position was actually a hybrid position, half developer, half manager. It was obvious that managing the business alone had now become a full time job. The role was therefore split into two, a senior developer and a dedicated business manager.

Preferring to retain a technical role, the Seed Manager left. I had also planned to leave – I knew that a chapter in the development of Seed Software was coming to a close. Seed was going to change, it was going to become more established, less dynamic. I also wanted to move back here, to Suffolk.

I did move, but I didn’t leave Seed. The University rather unexpectedly offered me a remote working contract. This threw up a whole myriad of new challenges. When I first became a remote worker I thought I’d be knocking out a steady stream of blog articles on what the problems were and how we were trying to solve them.

That’s a subject for another time however. As for the story, In October 2015 Seed was about 3 times the size it was in 2011, had a lot more products than it did an had begun to offer a support function. Brigid Command and Control is was well established as the primary mobilising system in the control rooms of 3 of the UK’s Fire and Rescue Services.

That just about brings my story at Seed to a close. But what about the future of Seed? The changes I predicted have certainly happened: I believe that Seed Software will become a highly successful business.

 

 

Because… PostScript!

Because it’s become somewhat of a tradition for me to do something silly on a Friday lunchtime, I thought I’d take on one of The Department‘s basic coding challenges.

The brief was simple;

I want a program that will print out the numbers 1,2,3,4,5,6,7,8,9 in a shuffled order. The order must be different each time the program runs. Note that the same number must be different each time. It should be possible to extend this to work with 52 numbers, in which case I can make a shuffled deck of cards.
You can use the Random number generator in C#, but you must make sure that the same number never appears twice, as a deck of cards which contains more than 4 aces has been known to raise suspicion.

As it was Friday lunchtime however I decided to make the solution anything but simple, firstly I replaced the numbers with the actual card names and secondly I thought I’d write it in PostScript because it demonstrates a totally different form of notation from the way we write the normal imperative languages like C# or Java.

/Suits [(Clubs)(Diamonds)(Hearts)(Spades)] def
/Cards [(Ace)(Two)(Three)(Four)(Five)(Six)(Seven)(Eight)(Nine)(Ten)(Jack)(Queen)(King)] def
/YCursorMax 720 def % 10 inches from bottom
/YCursor YCursorMax def 
/XCursorMin 72 def % 1 inch from the left
/XCursor XCursorMin def
/XColWidth 113 def % 1/4 of the printable page
/Helvetica findfont
12 scalefont 
setfont
/Deck [ 0 1 51 {} for ] def
0 1 50 {
    /SwapLeft exch def
    52 SwapLeft sub realtime rand mul exch mod
    SwapLeft add /SwapRight exch def
    Deck SwapLeft get
    Deck SwapRight get
    Deck exch SwapLeft exch put
    Deck exch SwapRight exch put
} for
0 1 3 {
    dup /Col exch def
    0 1 12 {
        Col 13 mul add Deck exch get
        dup 13 mod
        XCursor YCursor moveto
        Cards exch get show
        5 0 rmoveto
        (of) show
        5 0 rmoveto
        13 div cvi
        Suits exch get show
        /YCursor YCursor 20 sub def
    } for
    /XCursor exch 1 add XColWidth mul XCursorMin add def
    /YCursor YCursorMax def
} for
showpage

C#’s Parallel.Invoke is [Insert Hyperbole Here]

Freeside VTIt’s simple – we can’t make processors go any faster. But we can add more cores.
The OS can schedule different processes to different cores but to take full advantage of the potential we need to be writing applications that run on multiple cores.
Unsurprisingly there’s a lot of investment in making this easier for programmers – to the point that it’s now easier than falling off a log…

Parallel.Invoke(
    () =>
    {
        if (!GetSomethingFromTheDb(out something))
            something = MakeUpSomethingFromSomewhere();
    },
    () =>
    {
        complexResult = SomeComplexOperation();
    },
    () =>
    {
        someObjectGraph = ReadAndParseSomeFile(fileName);
    });
SomeReallyComplexThing(something, complexResult, someObjectGraph);

The individual actions will be run in parallel, or to be more precise may be run in parallel. The task scheduler takes care of whether or not to actually run them in parallel and the degree of parallelism.

When we arrive at SomeReallyComplexThing all the previous tasks will have been done.

That’s ace. It’s a lot easier than messing about with threads.

Even before the Parallel library was around it wasn’t actually difficult, but you needed some lateral thinking…

Task[] tasks = new Task[] {
    Task.Factory.StartNew(()=>
    {
        if (!GetSomethingFromTheDb(out something))
            something = MakeUpSomethingFromSomewhere();
        }),
    Task.Factory.StartNew(()=>
    {
        complexResult = SomeComplexOperation();
    }),
    Task.Factory.StartNew(()=>
    {
        someObjectGraph = ReadAndParseSomeFile(fileName);
    })
};
Task.WaitAll(tasks);
SomeReallyComplexThing(something, complexResult, someObjectGraph);

Ok, I admit, I’m simplifying. You still need to understand the basic problems of concurrent programming but the mechanics of writing code that can take advantage of parallelism are now trivial.

Shell Attacks!

Toad!

…because toads are lovely really

I’ve been having some networking issues recently so I was watching the router logs on my main gateway. I was genuinely amazed by the number of attempts to hack my ssh server, every few seconds I saw another line telling me that the firewall had rejected another attempted hack.

So I started pondering what I could do to try to stop this. Perhaps I could write a little script that works out who the appropriate admin is and emails them? Yes, but I think that might cause me rather a lot of trouble. I can’t exactly send the logs to my ISP or to the Police – simply attempting to connect to a ssh server isn’t exactly strong evidence of nefarious intent.

So instead I thought I’d just publish them – each day’s log is uploaded to the downloads section as a text file.
If I get bored and fancy writing some php then I’ll start stuffing them into a database so we can run some basic analysis on them – produce graphs and run some analysis.

The “Within” Pattern in C#

Ziggy StardustHere’s a little pattern that I use a lot. It happens quite often that I need to perform a set of tasks in C# that are different, but all need the same set up and tear down.

Now I could use two methods, e.g.

object someResource=PerformSetUp(someParameters);
try
{
    someTask(someResource);
    if(someCondition)
        reportSomeError();
    anotherTask();
    someResource.Conclude("It was aliens");
}
finally
{
    PerformTearDown(someResource);
}

That’s a totally reasonable way of doing it. There are a few disadvantages though, it splits up the allocation and release of resources which can lead to tracking problems. It’s also a pain if you’ve got several things that need to be set up and torn down. You either end up with a lot of local variables flying around all the time or you make an object to hold the resources you need.

So I tend to use a method to do both the set up and tear down and pass into it an Action.

So the above example would become;


WithinSetUpAndTearDown((someResource)=>
{
    someTask(someResource);
    if(someCondition)
        reportSomeError();
    anotherTask();
    someResource.Conclude("It was aliens");
});

WithinSetUpAndTearDown is defined thus;

void WithinSetUpAndTearDown(Action<object width="300" height="150"> action)
{
    Handle someHandle = new someHandle.Open();
    try
    {
        using(object someResource = someDisposableAllocation(someHandle))
        {
            action(semeResource);
        }
    }
    finally
    {
        someHandle.Close();
    }
}

It’s sort of a user-defined version of the using keyword.Of course it has other uses too, there’s one particular applications that all share a particularly complex set of error handling conditions. I’ve used this pattern there too.

There is one application where I use this pattern rather a lot. C#’s “lock” keyword is really handy if you’re operating in a multithreaded environment, its big drawback is that there’s no way of specifying a timeout. This is important for deadlock resolution – a deadlock always means there’s a bug somewhere but if your concurrent operations just sit there deadlocked it may cause some pretty serious problems that you may not be aware of for some time. So you want to know. Rather than use the “lock” keyword then you can use this…

public static void WithObjectLocked(object lockMe, int waitMsecs, Action action)
{
    if(null == lockMe)
        throw new ArgumentNullException("lockMe");
    if(0 >= waitMsecs)
        throw new ArgumentException("waitMsecs must be greater that zero");

    bool gotLock = Monitor.TryEnter(lockMe, waitMsecs);
    if (!gotLock)
        throw new TimeoutException("Timeout of [" + waitMsecs.ToString() + "] milliseconds exceeded");
    try
    {
        action();
    }
    finally
    {
        Monitor.Exit(lockMe);
    }
}

Its functionality is identical to that of “lock”, but it will throw a “TimeoutException” if the lock cannot be obtained within the specified time. This at least gives you a fighting chance of finding operations that are taking far too long or are deadlocked.

The Tube That Rocks the Remote Working World

Slash And Myles Rock!Moose and JRock of UK national (and international) radio station Team Rock Radio had a crazy idea. They were going to broadcast the breakfast show whilst on the Tube.
To be precise they were going to travel from their bunker in a secret location under an an anonymous office building in Central London to Westfield Shopping Centre in Stratford using the London Underground system.
They planned to travel during the songs and do the links live from the stations using the WiFi.

Today they’re doing it. In fact they’ve just left Liverpool Street Station. This might seem like just another silly radio stunt but actually there’a very serious point behind it for them, but moreover for a good proportion of our working population.

Until recently it’s been really difficult and expensive to arrange communications of the kind of quality that it would require to broadcast a live radio show.

So Moose and JRock wanted to prove – or perhaps really just test – the fact that this is no longer the case.

The programme isn’t over yet, but thus far it’s worked really rather well. There have been a few audio glitches, but nothing serious.

This proves to the Team Rock organisation that they can be much more responsive in covering events and festivals and indeed broadcast from anywhere in the world as long as they have a half reasonable Internet connection – which today isn’t a difficult thing to obtain.

The wider implications though are significant. I’ve been a remote worker for a little over 3 years so I know that technology has improved such that remote working (and indeed home working) is entirely possible for Software Developers like me. I also happen to work for the Computer Science Department at The University of Hull so they understand it too.

Not everyone happens to be a technologist or indeed work for a technical employer. What Moose and JRock have done is to demonstrate very publicly and very effectively to a much wider audience that the technology for remote working is now there.

I really hope this has the impact that it should.

PS: They’ve now added the show to their On-Demand section.

Using Rank In Case in SQL Server 2005, 2008, 2012, 2014 etc.

Here’s a quick tip for a Friday.

I have a table that records the history of certain items. I wanted to get a list of the updates between two dates – that’s easy enough. The problem was that, for each item, I also wanted the last change before the start of the time period.

A standard way to get the latest update for each item in a history table is use a nested pair of queries, the inner one using the RANK function to provide an order. The outer query the selects all the records with a RANK of 1 as these are the latest (or the earliest, or maximum, or minimum, depending on the order specified).

e.g.

select * from (
select ml.*,rank() over(partition by itemID order by [timeColumn] desc) rk
from someHistoricTable ml ) T1
where rk=1

So I started thinking – I wonder if I can make the inner query return the ranking for only part of its result set – when the results were in the time period I was interested in then I didn’t need them ranked, when they were before I did. So I wondered if I could us the RANK function within a CASE statement.
Yes, apparently you can…

select * from (
select ml.*,
case
when [time] < @startTime
then rank() over(partition by [itemID] order by [timeColumn] desc)
else 1
end rk
from someHistoricTable ml
where [timeColumn] < @stopTime ) T1
where rk=1 order by [time] desc

So the case / rank bit splits the time, it ranks records that were earlier than the @startTime whereas if they’re later it just returns ‘1’. The outer select then just takes all rows that have an ‘rk’ of 1, these being the records that are either during the time period or the highest ranked row before.