The Cat’s Favourite Spot on a Dark Morning.Something struck me yesterday. Something Evil. It comes out of the fact that you can use C#’s Null-Coalescing operator – that’s ?? to most people – with reference types as well as nullable (value) types.
Most C# devs I dare say will be familiar with C#’s nullable types. They’re really System.Nullable<T> but C# allows you to use the nice shortcut of just putting a ? after the type declaration…
class ExampleClass
{
int? exampleField;
System.Nullable<int> isTheSameAsExampleField;
}
You quite often see ?? used with examples of nullable types but it can actually be used with any reference type too. String is the most obvious[1]. For instance there’s a very common gotcha of performing some operation on a string that could actually be null. For instance,
if(someObject.someString.Contains("SOME TEXT"))
{
If someString is NULL (as opposed to "") this will throw a NullReferenceException
Note that although the logical result of these two options is the same the way it accomplishes that result is subtly different. In the first example if the string is null the Contains will never be executed. In the second it will be executed against the "" object (which clearly doesn’t contain "SOME TEXT"). The result in both cases is that the if condition as a whole evaluates to false, but it does so for different reasons.
…and so now here’s a way you can use these powers for evil. Let’s say you need to add something to a list, the problem is that you don’t know if the list has been instantiated yet, but if it hasn’t you need to instantiate it. This type of situation actually happens quite a lot and this solution works, ho-hum.
List<string> list = null;
...some code...
(list ?? (list = new List<string>()) ).Add("blah");
It works because C#’s assignment operator (single =) produces a result, the result is the value that was assigned. So this code says "if list is instantiated then use it for the Add. If not then instantiate a new List<string> and assign the value to list then take the result of that assignment (the newly instantiated object) and use that for the Add".
In summary then, you can use ?? against any type that could have a null value, not just System.Nullable<T>, but be careful with it because it’s a power than can be easily abused.
[1] string is actually a reference type, but it behaves like a value type
An accidental feature has just bitten us in the butt. I thought it worth a quick mention because it’s one of those things that happens quite often in the software industry and it’s more complex than it might first appear.
In this case it’s the ordering of a list – the customer had noticed that the list was always in a particular order. They wrote their training notes and indeed their operational procedures based around the fact that this list was always in that order.
We didn’t explicitly program the list to be in that order, we didn’t declare it as a feature that it’d be in that order, nevertheless it so happened that it was. Then we fixed a bug and as a result the ordering of the list changed slightly.
Now we have a bug report complaining that we’ve broken the list.
There are certain elements of the software development world that now would be waving their in-depth specifications at us crowing about how bad Agile is at this kind of thing and how if we’d “done it properly” then it would have been clear from the start that the list was in no guaranteed order.
They would be mockingly waving those specification documents if a) they could move them without a fork-lift and b) they hadn’t been put out of business years ago by Agile software houses like Seed.
Would it really have helped though? Perhaps – maybe the customer wouldn’t have relied on the ordering of the list or maybe they’d have spotted that it wasn’t ordered and asked for a change in the specification before the initial delivery.
Actually that isn’t so likely. Such specifications are often so complex and confusing that they never get much distribution within the customer organisation and if they do the number of people who understand them is limited (often to zero). The likelihood that the people who needed to know that the list was not in a guaranteed order actually knowing this is rather far from 100%.
Naturally there is also the risk that no matter how detailed the specification that particular detail was omitted.
So it is by no means certain that having an in-depth specification would have prevented this from happening. The only thing it is likely to do is make the contractual situation unequivocal.
That’s not the real question though, what we have to consider is what the result of us batting it back to the customer with the comment “well we never said it would do that in the first place” would be. Even with a water-tight contract such a response would have to be carefully considered.
The software industry today relies on good customer relations, repeat business and reputation. We want the customers that are using our software to continue using our software, not least because they pay an annual licence fee. Even if you’re an app developer for smartphones you have to be careful – if you annoy your customers they post bad reviews and there’s only so many bad reviews you can face down before you app dies in favour of someone else’s that has better reviews.
From a purist software development point of view this list ordering is a feature request, indeed that’s what we’ll call it internally. The relationship with the customer however is a far more complex matter – every customer tries to get at least some of their feature requests through as bugs in the hope that they’ll either get them for free or that they’ll be treated with a higher priority. If you let the customer walk all over you then you’ll be in trouble. On the other hand the age of profiteering has long passed and if the customer thinks your trying to charge them unjustly to fix genuine bugs then you could find your reputation damaged and your business will suffer as a result.
We may think of Computer Science as a logical, scientific profession but the reality of being a good software developer is that you not only need to understand logic, but you need a good grasp of politics too.
There’s almost a throw-away comment in my last article, The Internet and the Office Roof, “I was prepared”. We all knew about the St Jude storm and we knew fairly well when it was going to hit. But what does “prepared” mean? What did I do and what do I have that made me well prepared?
In many ways that story is far more important than me jumping on and off the office roof trying to find some mobile Internet. There’s some kit I have that’s useful but let’s start here because it’s simpler;
Things I Did Before the Storm
Loose objects outside are problematic. They can get blown around and damage themselves, other things or simply get blown away. So the day before the storm I had a wander around. Bins are a classic problem but ours have a little pen they live in and I thought them safe.
Garden furniture is another problem but ours is folding, so I collapsed it all and weighted it down. It’s quite heavy so in that state I was confident it wouldn’t budge.
I also surveyed the structures around the house – the decking, the fencing, the bin pen, the Wendy house. Were these in good condition? Were there any parts loose or vulnerable that might need fixing down?
Water Container
I took the time to draw some water into the camping container. Some large scale power outages were possible and there are a lot of electric pumps in the water network. Although we didn’t lose water there were disruptions nearby.
Rechargeable devices are great, but useless if they’re not charged. So the day before the storm I made sure that everything that I use and all out battery packs were on charge well in time for the storm.
I had a good look at the house too – did everything look in good order? Again anything loose or vulnerable could cause big problems. Chimney pots for instance are pretty big and heavy, you don’t want a loose one crashing through your roof.
Things I Have that Might Help
An “A” rated freezer like mine is a far better place to store cold food – even if it’s turned off – than a cool box. However the greater the thermal mass in the freezer the longer it will remain frozen. If you have a load of cool blocks it’s worth chucking them in the freezer a day or two before the storm (which I did).
I always keep a stock of tinned and packet food in as well because this is the country and things happen. A few days worth of relatively nonperishable food is a good thing to have regardless. I make sure that I always have rice, lentils, dried egg noodles, soy sauce and spices. You can make a stir-fry with all sorts of things, most notable vegetables which you can store for a good period of time without a fridge and you can always make lentil curry.
I have a lot of camping gear – stove, lights etc. I made sure they were all easily accessible. It’s nice to have a kettle rather than just boil water in a saucepan, but it’s by no means essential!
Batteries
A lot of camping things are battery powered rather than rechargeable so I make sure that I always have a small stock of (the right) batteries.
Three of my favourite (portable) lights are these.
Nightsearcher Trio
The NightSearcher Trio is available from a number of places. It’s a rechargeable LED torch and it’s exceedingly bright. It has a big handle that’s easy to grab and a flat bottom which means that it can be placed upright – pointed at the ceiling. In that mode it will happily light an average sized room. It claims to work for up to 23 hours. I can’t vouch for that but I can vouch for the fact it lasts many hours indeed.
UltraFire Torch
The Ultrafire Q5 is another torch, but it’s a pencil variety. It can run off its own special battery (expensive) or a pair of AAs. It’s nothing like as bright as the Nightsearcher but is pretty impressive for the size.
Tilley Lamp
A Tilley Lamp – this is a pressurised paraffin lamp so you really need to know what you’re doing to use (and maintain) one of these safely. It throws off a reasonable light and will last several hours on a tank of paraffin (kerosene). Its big advantage is that it also throws off a lot of heat as well so if you’re stuck with neither light nor heat it’s ideal.
The most dangerous thing about these is getting one lit. After that – with reasonable caution – they’re safe to use indoors.
Powergen
The Powergen is a mobile phone power pack. It’s essentially just a battery with 3 USB ports on it that provide power. It also has a LED torch on it. It’s good for about 4 charges of a modern smartphone.
Old Corded Phone
A corded telephone – when the electricity fails generally a cordless phone base station stops working. The resilience of the UK Telephone network is actually quite good and the chances are that even if the power is out the phone will actually still work – as long as you have a phone that doesn’t need any power other than from the phone line itself. This particular phone lived in a workshop for years, hence the sawdust.
Nokia 6310i
A Nokia 6310i. It’s an old, stupid mobile phone but from a disaster-coping view it stands out in 2 ways.
The transmitter and receiver on this model were particularly good.
It has a genuine 3 week standby time.
This gives me a pretty good chance of being able to communicate even if everything else goes wrong – and for some time too. Although I still have to sit on the office roof (or walk up the nearby hill) to use it.
Tea Light
Tea Lights – small self-contained candles that apparently can last up to 8 hours. They last 4, but they’re cheap and can be used relatively safely without the need for candle-sticks.
12V Power Pack
A 12V power pack. These are actually designed to start cars that have a flat battery but they come with a range of accessories, notably torches and car accessory sockets. We have one primarily to keep our smart-phones topped up for a week or so when we’re camping well away from power. There are a lot of things you can run from a car accessory socket. Some power packs even have inverters so you can use normal household appliances with them (although probably not for long).
Pure One Portable Radio
A battery powered radio – in this case actually a DAB and FM / AM radio. Local radio is often the most reliable source of information when all other forms of communication are failing.
Bombay Sapphire
Gin – it’s good for cleaning wounds and in dire circumstances it can be used as fuel. Apparently some people drink it, too.
Work Related Things
Half of your county might be without power but the rest of the world doesn’t stop. You’ve got to try to continue to work as best you can. Naturally I have back-up plans.
My main machine is a high-end laptop with an SSD that I normally use docked. This has two advantages.
It’s effectively its own UPS.
I’m mobile. I can work anywhere I can put the laptop down without having to move a lot of stuff.
I have a good quality laptop backpack bag. This means I can cycle easily with my laptop – useful if for whatever reason the car in unavailable or, let’s say, the local roads are strewn with broken branches, fallen trees and other debris.
I did consider getting a mobile broadband deal but I end up without Internet / WiFi so infrequently that it just makes no sense. For the amount that I need it I can tether my phone instead.
My parents-in-law are in easy cycling distance and they’re on a different telephone exchange and use a different ISP which means that the chances of the Internet failing in both locations is fairly small. There are also (actually) a few WiFi hotspots in the local town and there’s another town that’s not too far away that has loads.
I have a SIP phone which means that I have a telephone – the same telephone – wherever I can find some Internet with half reasonable bandwidth.