Sunday, September 25, 2011

C# Extension Methods

One of my favorite features in C# is Extension Methods. Let me explain why.

Did it ever happen to you that you needed to add a method to a class you cannot edit, and inheritance couldn't help as well?
This can happen in various opportunities, especially when working with libraries and SDKs that uses a certain class for its internal operations, and exposes this class to the user as well.

In such cases, in languages that doesn't support extension methods, the only way to add functionality to that class, is to write a "helper" class or method, and use it.

C# Provides a better way.
Let's say you have a blogging system, and you have a blog summary page, that displays only the first 100 characters of each post, as a teaser. There could be many ways to get the shortened text, but image you could have your own String class, that provides a method that displays the entire post if it's less than 100 characters long, and displays only the first 100 characters and "..." if it is longer.

Have a look at the following extension method:


public static class StringExtension
{
        public static String ShortenText(this String str, int length)
        {
                if (str.Length > length)
                {
                        return str.Substring(0, length) + "...";
                }
                return str;
        }
}


Simple. This code needs very little explanation. All it does is create a new extension method for the String class that implements the logic described above. Here's a sample usage for this new method:

Console.WriteLine("12345678".ShortenText(5));


The output of this would be "12345...", as you'd expect.

The possibilities of extending built-in classes provided by C# and the various libraries, such as ASP.NET MVC, are endless. Just make sure you don't abuse it.

The full source code of the sample given above is available on my bitbucket samples repository. It compiles on Mono as well as on MS C# compiler.

Friday, September 23, 2011

Ubuntu 11.04 and Unity

For a long time I've been a 10.10 user. Probably too long. To tell you the truth, I couldn't care less. Everything was so customized to my own needs, and everything worked perfectly.
Moreover, since most of my development nowadays is done on a Windows box, I didn't feel the urge to try something new on Linux.
Finally, there were so many bad reviews about Unity, and "thanks" to those I had second thoughts about replacing the desktop environment.

Last week I took the "risk", and you know what? I love it!
I had 11.04 installed both on my workstation and my laptop, and I can easily say it's one of the best Ubuntu versions I used.

On my laptop, I rarely connect a mouse, and I hate touchpads. This means I use the keyboard almost exclusively. Unity seems to be adapted to this, so it provides great keyboard shortcuts. Now I need the mouse even less.

On the workstation, even though I have a big screen with a "big resolution", I always try to find the best utilization for the screen space real-estate. If you ever seen a those 30" mac wide monitors that have the mac dock dragged to the left edge, you know what I'm talking about.
Unity also knows what I'm talking about.

Also, performance is great, as ever. Far better than using Windows. Ubuntu never misses this one.

I wish I didn't wait so long to try Ubuntu 11.04 and Unity. On the other hand, since this version is quite mature, there's a chance I saved myself some of the glitches and bugs it had 5 months ago.

Sunday, September 18, 2011

Trello and Blogging

If you're following TechCrunch Disrupt, or have been reading the recent news from Joel Spolsky, you've probably heard by now about Trello.

If you haven't, let me bring you up to date. Trello is a note taking application aimed to replace ordinary to-do lists and sticky notes used in project management. Every note taken, dubbed "Card", can be much more than a sticky note, as it can contain background text, votes, embedded objects and even someone "assigned" to the card. Cards are organized in lists which are organized in boards.
If you've ever seen agile project management techniques, such as Scrum, you'll be familiar with the board filled with columns of sticky notes, and sticky notes being able to "move" between columns. That's exactly like that.

So far, in order to see what Trello is worth, I've been using it to manage ideas for this blog.
Up until now, ideas for posts where mostly in my head, with some starred items in my inbox that contains the research I did. Now, I have a board with "Ideas", "Collecting information", "Writing", "Follow comments" and "Done" columns. Finally, my ideas are organized and I have a place save the information and even order/rank the ideas before writing posts.

I can see now that I have two ideas I should be working on. If you have more ideas for stuff you want me to blog about, leave a comment, and I'll treat it when this post will reach the "Follow comments" phase.