Why Do You Blog?

I’ve been writing this blog for a few years now (since June 2005 in fact) and in that time I’ve been asked this question quite a few times, typically from people who I think like the idea of writing their own blog but get put off by what a lack of confidence and doubt.  Questions like “what would I say?” or downer statements such as “I don’t have anything interesting or original to say” are typically what I hear.

So, if you don’t mind indulging me for a few minutes, let me give you some of the reasons why I blog.  If these reasons help you consider your own blogging or lack thereof afresh then I’d be pleased, but that’s not really the point of this post as you’ll see.

By the way, in case you were wondering, there’s nothing radically new or earth shattering in this post.  Seriously.  It’s all been said before by other people with much more eloquence than I can put together.  In fact, if you want, go and read a few of those posts (just Google for reasons to blog or check Atwood’s or Hanselman’s blogs as they’ll probably have something prosaic and witty to say about it).  So you may ask why would I write this post at all then?  Let me give you the first of my reasons as an answer:

To Clarify My Thinking

Shock. Gasp. Awe. Yawn. Told you it wasn’t earth shatteringly original :-)  I sometimes write blog posts as a way of helping me get my view on things straight and clearing up my understanding.  The simple act of writing something down and trying to do so in a lucid, structured and clear manner instead of resorting to “grawk, nhhh, brrrp” explanations combined with hand movements and dance simply helps me get things straight in my head.  This particular post is to remind myself why I blog and why after a lengthy break (due to major household renovations) it’s worth getting back into.  When I write clarity posts I’ll normally publish them but sometimes I don’t, especially if I think it’s worthless drivel, but even so, the mere act of writing something down helps me understand where I’m at.  If I realise I’m still not clear on a subject it’ll usually just save it as a draft until clarity comes.

OK, With that out of the way here’s a few more reasons…

It Helps Me Improve

Yep.  Putting something on screen and posting it on the internet for any and everyone to see can be a bit scary at times, but that in itself is a challenge to help me improve not only my understanding of a subject but also my ability to explain concepts and techniques.

Sometimes it works, sometimes it doesn’t, but regardless it helps me improve, little by little, and for someone who blathers on about continual improvement this is just one of many mechanisms for doing it.

I Enjoy Teaching and Helping

I actually enjoy helping people understand something they didn’t before.  Now this is where most people start in with the “nothing original” excuse.  My response?  “Yeah, so?”.  Big deal if someone has written about it before.  Who cares if it’s something ScottGu or DHH has blogged about.  If someone’s reading a blog post of yours then it’s probably because they’re either interested in you as a person and want to hear what you have to say or because they found it via a search engine.  If it’s the latter, then you’ve probably written something from a slightly different angle than others and that’s something that’s going to help them.  That’s a good thing.

For The Fame and the Money!

Of course!  Well, maybe not.  Yes I’ve got some ads on the blog and feed (please click them and help me feed my starving children!!) but the amount of cash they bring in is only enough to cover a coffee once a week (if that).  And if I was doing it for the fame then I’d be delusional.  I’m glad you’re reading this post, seriously I am, but with my visitors a month count being in the 3,000-4,000 range and with the regular readers who follow via the feeds being in the hundreds I’d be considered extremely lucky to rate a mention globally.  If I wanted to get lots of hits then I’d have to blog about trendy subjects and unless I have an interest in it myself, then why would I? A blog should be about you and what interests you.  Of course I could write a killer app and be famous the world over, but that flash of inspiration still hasn’t hit :-P

Before I move on, I’ll be honest with you, a well received blog post does help scratch the ego and I like to get positive feedback, but if that was the main reason for me doing it, it would be a pretty crappy reason.  I can get my ego scratched much better by just visiting http://ourstereo.com/compliment/ and clicking refresh a few times.  Or playing a game with my kids who adore me.  Or, heck, I could just go look in the mirror… LOL. Yeah, okay.  Reality check!  I can proceed.

It’s a Record of What I Learn

Yep. Boring.  Move along.  Actually, I should just say that I often hit my blog and search for things I’ve written about a while back and where I’ve either forgotten the details or don’t have them handy.  Being able to quickly look up some info from the blog has helped me many, many times.

Finally, It’s A Way To Connect

This one is probably the least thought about, but to me it’s still something that’s quite important.  Blogging helps me connect with people, just like twitter and IM can, but instead of it being a social connection with people typically know me personally or are one or two connections removed, it’s more a connection around common interests and whatever my blog posts have been about.  Through some of my posts I’ve made connections with people I admire from around the globe that I would never have met in person, and who would never have encountered me via social networking alone.  Even though the connections are typically loose and short lived I think they’re still invaluable, and who knows, some of those connections have a tendency to stick and can become people you connect with over the long term.  How good it that!

 

So that’s enough reasons for me.  What about you?

 

Do you want an Aussie Alt.Net Conference?

We’re currently trying to gauge the level of interest in having an Australian Alt.Net conference on the day before Tech.Ed AU.

Putting a conference together (even an open spaces one) takes quite a bit of effort so before we go down that path please help us out by responding to the poll on the sydney.ozalt.net site (it’s in the right hand column).

 

Attention to Detail

Sometimes you just have to wonder how the things that show a lack of attention to detail can make it to production.  Especially when there are sooo many eyeballs that would have looked over something before it’s release.

Notice anything suspect about the latest Zune HD marketing shots?  Hint: It’s inside the circled area :-)

image

A font clipping problem? Seriously?! Why wouldn’t you just reduce the font size by a tiny fraction so that the default text actually fits cleanly.  It’s not that hard a problem surely?  And now that I’ve seen that I’m wondering if the device is actually too narrow and whether other software will also experience the same clipping problem.

And then I start wondering, why on earth would you use an image with clipping problems as a promotional picture?  I can’t imaging Apple letting something like that slip through.

 

My Visual Studio Environment (R# and more)

At the most recent Oz Virtual Alt.Net meeting I gave a run through of the various things I have installed on my box for making Visual Studio work the way I want it to, and making myself much more productive in the process.  The recording of that session can be seen either down below or over on the OzAlt.Net blog.

For reference here are the links to the various things I was showing off:

There’s actually more stuff I have installed, but since I didn’t show them off I won’t list them now.  Maybe in another blog post I’ll go through what those things do.

 

 

Basics: Adding Lambda Support To Your Classes

I talked recently about Linq and Extension methods and showed how you could refactor For Each loops into simple linq statements.  When using Linq one of the things you will often come across are Lambda functions (anonymous methods declared inline).

If you’re a curious type, then you might ask yourself “How can I make it so my classes can use Lambda functions?”.  If that’s you then read on :-)

In this scenario I’m going to try and do a few things.  I want to create a simple class that has a collection of strings in it and I want to have a method in that class to print those strings.  But here’s the catch, I don’t want to actually code up that printing method itself in my class.  It doesn’t really belong there and instead  I want someone else to handle the printing – I just want to send them the strings in the class and let them worry about it.

Now I could do it using Dependency Injection techniques, but that’s a different discussion.  Here and now, I want to have someone supply me a function and I’ll just call it.

Test First

Let’s start by creating a test for it first (forgive me for not putting any asserts in the code).  Here’s what it looks like

[TestMethod]
public void TestMethod()
{
StringHolder stringHolder = new StringHolder();
stringHolder.Strings.Add("string 1");
stringHolder.Strings.Add("string number 2");
stringHolder.PrintUsing(Console.WriteLine);
stringHolder.PrintUsing(p => Console.WriteLine(p.ToUpper()));
}

Nothing too complex.  Add some strings and call the method named PrintUsing.  But have a look at those two PrintUsing statements.  What do you see?


The first one says we want to do the print using Console.Writeline.  That’s easy enough to read, but what about the second one?  The second one has one of those funny => operators – it’s a lambda function.  A method defined in line.


What this method is saying is that for each string to be printed, pass it in as a parameter named “p” to a method that has no name (it’s declared inline so needs no name).  That method will call Console.WriteLine, passing to it the string once it has been converted to Uppercase.  Pretty cool.


The StringHolder Class and Action<T>


So let’s implement the class

public class StringHolder
{
public StringHolder()
{
Strings = new List<string>();
}

public List<string> Strings { get; private set; }

public void PrintUsing(Action<string> action)
{
foreach (var part in Strings)
{
action(part);
}
}
}

Here we see something interesting.  Have a look at the parameter for the PrintUsing method – it is looking for an Action<string>.  An Action<T> is a .NET type for a lambda function that returns a void.  The <T> type is the parameter to pass to the method.  If we used Action<string,int> we would be wanting two parameters; a string and an integer.


What we also see is that inside the method we simply call the action passed in as if it were a normal method, including the passing of the parameters.  Quite elegant and simple.


Functions vs Actions


So how about we now expand things a little and add a few extra lines to the end of our test as so

var averageLength = stringHolder.CustomFunction(l => l.Sum(p => p.Length)/l.Count);
Console.WriteLine("Average length: " + averageLength);

We now want to allow people to supply a custom function so that they can apply if to our entire list.  So instead of giving them one string at a time, we’re going to send them the whole string list and they can manipulate it as they choose.


In this case we’re going to work out the average length of the strings in the list.  To do it we add up the length of all the strings using Linq’s Sum method, and then divide it by the number of strings in the list.


Now what do we have to add to our class in order to support this?  As it turns out, we have to add just 3 lines:

public T CustomFunction<T>(Func<List<string>,T> function)
{
return function(Strings);
}

Notice the Func<List<string>,T> type on the parameter. It’s just like the Action<T> we looked at earlier, but with a slight difference. We now have two types being listed.  The parameter types and the return type.  The return type is always the last type in the type list.


Now that it’s declared as a parameter we can call function() just like any other method and then do something with it’s return value.  In our case, we’ll just return it.  I should also point out that we’re using generics here – the type <T> after the CustomFunction method name says that T is a generic type.  In other words the function we pass in can actually return any type it likes, and we’ll pass that back to the calling code.


Default Actions and Overrides


How can any of this be useful? Well, let’s consider our string class and think about showing a total of the number of strings in the class.  I might want a default behaviour, but also allow people to override that behaviour by supplying a different method to use into one of my classes, and I want to be able to do it at run time.


Here’s how it might work.  Let’s start with a test as follows:

[TestMethod]
public void CallAnAction()
{
StringHolder stringHolder = new StringHolder();
stringHolder.MyAction();
}

I simply call the MyAction method on the stringHolder object and it does something.  In this example we’ll get it to write the number of strings out to the console as follows (I know, incredibly complex stuff)

public void MyAction()
{
Console.WriteLine("Number of strings: " + Strings.Count);
}

But I can’t override this behaviour.  I could potentially sublcass it and do an overload on the method (assuming I made it virtual), but that’s not really what I want to do.  As a different approach I can refactor my class slightly and make the MyAction method a property as shown:

public class StringHolder
{
public StringHolder()
{
Strings = new List<string>();
MyAction = () => Console.WriteLine("Number of strings: " + Strings.Count);
}

public Action MyAction { get; set; }

We’ve now made MyAction an Action property and we supply the default method signature in the constructor.  The () at the start of the lambda declaration indicates that there are no parameters for the method.  The good thing is that our test remains unchanged and everything works.


Now we can take this a step further and from our user code supply an alternate behaviour, overriding that defined by the StringHolder class as shown here:

[TestMethod]
public void CallAnAction()
{
StringHolder stringHolder = new StringHolder();
stringHolder.MyAction = () => Console.WriteLine("Alternate " + stringHolder.Strings.Count);
stringHolder.MyAction();
}

We set the property value to be the new lambda function we’ve defined and we’re all set.  This time when we call the MyAction() method the program will use the newly supplied lambda method we’ve supplied and show the alternate output.


Conclusion


What we’ve done is overridden the behaviour of our StringHolder class at runtime avoiding the hassle of subclassing, dependency injection or any other compile time techniques we might use.  It’s an easy and simple way to provide hooks into your classes where you want to have a standard behaviour that can be overridden.

 

Welcome

As you might have guessed, this is a tech blog. I normally write about development and things that can help us do it better, specifically in the .NET space, and occasionally i'll throw in a post about whatever happens to be in my head at the time. Enjoy your visit!
And in case you were wondering, I'm a Principal Consultant for Readify, I run the Sydney Alt.Net group and I play a bit of LOTRO in my spare time (feel free to look me up)


 

My Open Source Projects

Labels

Blog Archive