On Reflection

3 Nov 2015 | programming and c#

I’ve been looking back at my code (you could say reflecting on my code, even…) and trying to see patterns and designs that I may have inadvertently come up with or techniques that I use frequently. There are a few reasons I’m doing this:

  1. To find what works and what doesn’t in hindsight
  2. To see if I can abstract out or make some utilities
  3. To understand how my brain works

Most programmers will understand the first two easily, but the third point is probably a bit unusual. What I mean by “understand how my brain works” is that all developers approach problems in a particular way, and I’m no exception. In most cases we don’t even realise how or why we do certain things or approach things a certain way.

Anyway, what I’ve noticed is that I use reflection more than people I work with or even more than code I’ve seen out in “the wild.” Now, I understand the down sides of using reflection, the biggest ones being performance and ambiguity, and I do my best to avoid both. And, before you go and label me a crappy developer, I never use it to replace good design or object-oriented principles.

What I’ve found is that I use it to avoid duplication, and above all, I use it out of laziness. That is to say, I hate writing repetitive if-then-else or switch statements, or having to modify half-a-dozen things because I added a new property to a class. So, I use reflection to turn what would normally be code into data. This way, my “business logic” remains small and succinct and thus, easily understandable. Or, at least, that is my hope.

An example - let’s say that we wanted to display some market data for a stock. There are multiple prices for any given stock, so we’ll need a structure to hold them all in:

class Quote
  double LastTradePrice { get; }
  double AskPrice { get; }
  double BidPrice { get; }
  double MidPrice { get { return (AskPrice + BidPrice) / 2; } }

Our users might want to see only one of those prices at a time, and so traditionally I’d have written a switch clause to get the return the one that the user wanted to see.

void ShowPrice(Quote q)
  if (UserPreference == "LastTradePrice")
  else if (UserPreference == "AskPrice")
  // ... etc ...

Now this would get tedious quickly and every time something changed in the Quote class, I’d have to make sure that my ShowPrice() method was changed to support it. Will I remember to do that amongst all the other changes I’m likely to be making? Probably not.

And so I “cheat” by using reflection:

void ShowPrice(Quote q)
  var p = typeof (Quote).GetProperty(UserPreference);
  var val = (double) p.GetValue(q)

One of the biggest problems with reflection is everything ends up being an object and so, it’s easy to abuse and make errors. The cast to double is required. On the plus side, my code is a lot shorter, and once you get how reflection works, it’s easy to follow. Just in case you don’t understand what is happening in the above code - the first line obtains a “handle” to the property that is to be read from the Quote object, for example the LastTradePrice property, and the second line invokes the getter of that property on the instance of Quote referenced by q.

The other issue to consider when writing code like this is the performance implications. Reflection is slow. And not just a little slow - it’s up to 200x slower.

Luckily, all is not lost. One simple way to speed this code up is to keep hold of that handle that is obtained in the first line. Still, it will still be a lot slower than if-then-else method above. This is because that 2nd line where we invoke the getter is also a lot slower than a direct property access. So what can we do?

This is the first part of my thoughts on reflection. I’ll will update this post with links to the future posts when they are available.

→ Social Anxiety

25 Oct 2015 |

Brent Simmons on social anxiety:

But if you raise your hand for a high-five, or hold out a fist for a bump, it won’t even occur to me to wonder why you’re doing those things. You’re just doing those things. Why would there be a reason?

It’s not that I don’t understand the gestures — of course I do — it’s that I have to keep reminding myself that gestures have meaning and intent.

Everyone who meets me quickly realises that I’m geek/nerd/etc. The stereotype that is associated with that kind of personality is that they are introverts1, and I would consider this true for myself. The quote above resonated with me as I often find myself having a slower-than-average time in reacting to common physical interactions like a high-five or fist-bump.

It’s good to know I’m not the only one.

  1. In many circles, calling yourself an introvert has a negative connotation. However, I take introvert to mean that a person gets their energy or motivation from within themselves. On the other hand, an extrovert would be someone who gets their energy from the people they are surrounded by.

The Journey Begins... Again

20 Oct 2015

This is the umpteenth time I’ve tried writing online - somehow I’ve never managed to keep it up despite trying pretty hard (okay, probably not that hard.) My previous attempts have resulted in me doing a comprehensive design for the site, turning it into a theme for the fashionable blogging platform at the time and then, by the time it came to writing, I was already bored. I’ve always found the design more interesting than the writing itself.

So what’s different this time? Well, nothing really. I spent a weekend and a bit on the technology and design and am keeping it simple by using Jekyll and a basic that design draws heavy inspiration from the very simple and elegant Harmony theme. I’m not bored yet.

My aim is to write something once a week, whether it’s technology related or some other random interest. There are a few of those, so I should have something.

Try, try and try again…

All that blurb above begs the question as to why I’m trying again. There are a few reasons, but the primary one is that, for the past year or so, I’ve been listening to a large number of podcasts. Hosts such as Casey Liss of the Accidental Tech Postcast, Myke Hurley of Relay FM and the infamous CGP Grey have been discussing their humble beginnings and I’ve been inspired by their stories.

I secretly (and now, not so secretly) would like to be part of the circles they are in. It’s not that I want to be “famous” – I don’t think I could take the kind of spotlight that someone like Marco Arment is given. What’s exemplified by these people is there is something to be said for having a brand for yourself, no matter your background.

The second, and probably more practical reason for me, is that while listening to The Rule of Two, Grey made an excellent point about diversifying your income that stuck with me. It’s hard to describe exactly why that’s so since I’ve always been quite content with my “day-job.” I’ve been vaguely exploring starting my own venture of some sort for a while, but have never really given it serious thought. I’m hoping writing here will start the flow of ideas and lead to some interesting opportunities.