Monday 16 July 2012

Is It Really That Complicated?

I've recently been reading Fabien Sanglard's blog, particularly his fascinating code reviews on the Doom and Quake source engines. Apart from making me feel a bit stupid not understanding how BSP trees work, something else strikes me regarding my own work:

What I do isn't really that complicated.

At the heart of my job I write code to make a website work, and if I consider the bare essential tasks that it needs to do I think I'd have the following shortlist:
  1. Get data from a database
  2. Do something to the data
  3. Put that data into a webpage and send back to the client
  4. Profit
By now it's safe to assume we've figured out how to do all of these steps easily - except perhaps the last one! There are a ton of ways to read and write to a database from code, either using ADO.NET or a variety of ORM frameworks, just as many ways to visualise that data (WebForms, MVC, Silverlight etc), and the only real work you should do in theory is the logic that sits between those parts.

Yet if this is all I need to do, why does it still seem so complicated to get anything done? Why does it still take me ages to implement a new feature when "code-reuse" should be helping me? I mean, it's not like I'm writing a game engine with crazy 3D maths and physics to calculate in nanoseconds, attempting to cure cancer or even writing software for spacecraft. Apart from some technical challenges that do exist, I think it mainly boils down to two things.

Other People

Your customers usually don't tend to think in the same way as you do. They might say "hey, can you add in the ability to search our website like Google? In fact, can we not just use The Google on our database or something? And possibly email all the search results to us? It can't take that long, right?" I like to try and keep things as simple and generic as possible, yet find that when "other people" get involved it tends to spiral out of control.

Take, for example, the financial sub-system I inherited from my predecessor. At work we have a system which calculates how many units each of our clients uses based on what assessments have been completed and how many reports have been generated. These units are then used by account managers to translate into real money to calculate invoices, such as 1 unit = £10 for instance.

So this sounds simple, right? Surely it's just adding up numbers and computers love adding up numbers. Did I mention though that if component A was generated before component B then you only add units for B, but if C and B are combined into one then we halve the units of A. Oh, I forgot, if D is generated between B and A then we add C and E together but only cap to a maximum of 6 (which can be changed based on which country you are in) and then if you bring F into the mix... and this kind of logic goes on and on in an endless spiral of doom.

Why does the system work this way? Because that is how other people decided it should work, because apparently business rules should never be simple. It also didn't help that this evolved, as most products do, into a big mess which means that business rules end up circling around each other without you even being fully aware of it.

When I first started this job I had to spend 3 months understanding all the many intricacies of this system because only my predecessor knew how it truly worked - despite being told by the managers what they wanted. It has now gotten to the point where only I know how every item is charged for and even the management have to double check with me.

And to be fair, there is one other thing that tends to complicate matters just as much as other people...

Myself

I tend to get very attached to the code I write. In an ideal world I feel as though my source code should be beautiful to gaze upon, to be able to admire it's sheer simplicity whilst also marvelling at how it can handle so many complex tasks at once. Sometimes I look back on code I wrote maybe a month or two ago, which at the time I was perfectly happy with, yet now I feel it is ugly and horrible and what was I thinking? This simply has to go because now I know exactly how it should be written...

But this is the real world and experience has taught me that sometimes I have to just "get it done" no matter how it ends up. And if we're being honest who else apart from me really cares about what my code looks like? The other day I decided to bash-out three quick features for my website as I felt that customers really needed them; I created them as quickly as possible (since I do have other projects to complete) so the code wasn't pretty, sometimes even copied-and-pasted with some changes. I look back on it now and I don't like how it looks. Yet my customers won't care, because what they see are three new features that make their life easier and they work, which at the end of the day is the most important thing in your product.

Keep It Simple, Stupid

Simplicity is what I strive for in life. I genuinely feel that the simplest solution is always the best as there are fewer complications to consider and less chance of anything going wrong, and I really try to impress that upon people. Yet at the end of the day outside factors have a way of creeping in and adding complications to your best laid plans, and I've built up enough work experience to realise that although the simplest solution may be the best, it's not necessarily what people want.

Friday 6 July 2012

Becoming a Father: One Year On - Part 2

This is the second part of my retrospective on my first year as a new father. To view all the parts, please click on one of the links below:

Part 1 - The Beginning
Part 3 - Getting the hang of things
Part 4 - The Accident

This post focuses on...

The First Two Weeks

The day after my son was born I woke up bleary eyed and called my parents and in-laws - now forever know as "the grandparents" - to tell them the good news; they had been calling all night but I was a little tied up at the time. I had breakfast, a quick shave and headed back to the hospital to pick up my wife and son.

My wife was fortunate enough to get a single room for the night - usually you can get at least 6 new mothers and their babies in a ward room. Even so my wife didn't get that much rest due to my son needing regular feeding and nurses popping in and out constantly to check on mother and child. So I think she was glad to be getting out of there and back home, plus it seemed the hospital kind of wanted the room back; it was never said, just implied since this is the NHS we're talking about, always under strain. On a side note at least she wouldn't have had to eat the lunch I saw them bring in; the NHS is a great institution but they never have got the hang of meals anyone would willingly eat.

So, new baby in his new car seat and me carrying a metric ton of samples of various baby related paraphernalia, I managed to finally get everyone in the car and get them home. And then the realisation dawned on me and my wife: what the hell do we do now? Because you spend a lot of time thinking about how it will be and reading a lot of books to learn how to look after a baby, but nothing says "trial by fire" more than bringing home your first-born and having to figure out everything you need to do to look after them by pure guesswork.

Breastfeeding

Breastfeeding in particular stressed my wife out a lot. You would assume, maybe from seeing people doing it or just the general impression that the idea gives, that it would be an act that both mother and child instinctively know how to do, like they are hard-wired to "just do it". Nothing can be further from the truth. Of course a newborn baby does have a natural suckling instinct, just no way of knowing where to apply it. And it's no easier for the mother - it's very easy to get the baby up to the breast but it's not easy to see whether they are latched on correctly due to the positioning of everything, with no idea how much milk they have drunk, if any. Add to that the pressure of midwives always telling you that "breast is best" and my wife refusing to try any other way, it resulted in a very stressful time.

That first day I don't think my son got much milk down him due to both our inexperience, my wife getting very upset by the end of the day. In the end he did sleep almost through the night - which helped my wife who was still exhausted from childbirth - but we were still worried he wasn't actually getting any milk. Fortunately the midwife who came to visit on the second day explained to us that a) you can express breastmilk and give it to him in a bottle and b) give him formula milk at the same time whilst learning the breastfeeding technique. So we resolved to go out to the supermarket and get some formula milk - our first trip out so it probably did us all good.

Chaos

Babies change everything. Suddenly all your little routines that govern your life are thrown out of the window and you are forced to re-learn everything. So apart from having to know how to change nappies, how to feed them, how to burp them, how to dress them and a thousand other little things that you never thought about before, you also have to try and find order within the chaos that a newborn brings into your life. As a programmer, who relishes procedures, order and routines, I found this very hard.

Take, for example, the night feeds - because your job as a parent doesn't end when the sun goes down, no siree! Every 3 hours my son needed feeding - and I mean every 3 hours, morning and night. Before he was born I insisted to myself that I would do my share of everything - nappy changing, cleaning him, the works - and since my wife was starting to express milk it also meant I could help with feeding him. This also meant getting used to a new sleep routine (or lack of it). As a result, we both basically went to bed at around 8pm for nearly 3 months because we were too exhausted to stay up any later!

There was also the night of the third day when he just would not sleep and cried and cried all night. We tried everything we could think off and were getting to our wits end when we realised that letting him suck our finger helped, but take it away and he would start crying again. It got to around midnight, when I was sitting up in bed with him sucking my finger and not being able to get to sleep like this, when I declared to my wife "I'm going to buy some dummies. Now".

Another thing you learn as new parents is to let go of all your pre-conceptions. My wife had many ideas that she thought would be good for our son such as breastfeeding, no dummies ("too unsightful") leading all the way up to baby-led weaning. We reluctantly learned the hard way that a baby doesn't care one bit what you think is best for them, it will do exactly what it likes and there is nothing you can do about it but adapt to them. So I had to drive to the midnight supermarket to get some emergency dummies, which let him at least suck on something while he slept in his crib and gave me my fingers back.

I Don't Like You

Perhaps the thing I personally and emotionally found hardest to accept in my two weeks off work was the fact that my own son seemed to hate me. Well, perhaps "hate" is too strong a word but he definitely did not like me. No matter how I held him or cuddled him he would just cry and cry at me, only stopping once his mother took hold of him; he was inside my wife for nine months, knew her like the back of his hand, whilst he only met me a few days ago and did not like what he saw.

This is a very hard thing to take, the fact that your own child does not seem to like you. I had an idea that it would be tough but I also imagined that he would at least just lie in my arms looking all cute and all, but there was none of that. I even looked after him for a whole afternoon on my own while my wife went to get a haircut; I insisted to her that some time alone for her would do her some good. It also meant that I had an entire afternoon of him crying at me, pretty much non-stop. Add on top of that the sleepless nights, trying to keep the house in order (though I soon gave up on that notion) and keeping up a repetitive 3-hour cycle of the same tasks - feed, burp, change nappy, prepare the next feed - I have to admit it almost broke me.

But It Gets Better

So far I'm making fatherhood sound like hell on earth, but overall it isn't. What people reading this who don't have children of their own must understand is that the shock of changing your life from free-and-easy to instant parent is incredibly severe. Everyone says that raising children is tough but my words alone cannot convey just how tough it is, the only true way of understanding is to experience it for yourself.

And yes the first two weeks of my son's life were incredibly tough, for me and my wife, I cannot lie about that. But every parent would also tell you that things get easier. At the time, when you haven't slept all night and every minute drags on forever, you can't see the end but it is there. And things did (very slowly) start to get better; towards the end of my paternity leave my son was starting to get a bit more used to me, partly by me realising that he wanted to be moving at all times - that meant rocking him in my arms non-stop, which wore me out physically but at least he wasn't crying! And once you start to see a pattern in his routine - such as when he appears to sleep, when he last had his feed so you can gauge when the next one should be - then you can get into the swing of things.