Blog

Back to basics string vs StringBuilder

This is simple stuff but is something I see people easily miss by just not thinking about it.

A string is an immutable object, which means once created it can not be altered. So if you want to do a replace or append some more text to the end a new object will be created.

A StringBuilder however is a buffer of characters that can be altered without the need for a new object to be created.

In the majority of situations a string is a perfectly reasonable choice and creating an extra 1 or 2 objects when you append a couple of other strings isn't going to make a significant impact on the performance of your program. But what happens when you are using strings in a loop.

A few weeks ago one of my developers had written some code that went through a loop building up some text. It looked a little like this:

1string foo = "";
2
3foreach (string baa in someSortOfList)
4{
5 foo += " Value for " + baa + " is: ";
6
7 var aValue = from x in anotherList
8 where x.name == baa
9 select x;
10
11 foo += aValue.FirstOrDefault().value;
12}

Everything worked apart from the fact it took 30seconds to execute!

He was searching through convinced that the linq expressions in the middle was what was taking the time, and was at the point of deciding it could not go any faster without a new approach.

I pointed out not only had he used strings rather than a StringBuilder, but the loop also created around 10 string objects within it. The loop which repeated a couple thousand times was therefore creating 20000 objects that weren't needed. After we switched froms strings to a StringBuilders the loop executed in milliseconds.

So remember when your trying to work out why your code may be slow, remember the basic stuff.

Image resizing

Using 3rd party components while essential, can also be a headache. All to often you can find yourself spending hours following the setup instructions to the letter only for it to not work. Other times you know it's the exact component you want to use but it seems while the guys may have spent a huge effort in writing to component they seemingly never got round to writing anything to tell you how to use it.

Every so often though I come across a component that works first time and does exactly what you want. One such component that I've found this with recently ImageResizer.

I needed a simple way for images to be resized for a product listing on an eCommerce platform written in Classic ASP. Image resizing can be a real pain, there's the decision of do you do it when the images are uploaded or on the fly, if you do it on the fly there's a a huge number of mistakes you can make and nearly every example of how to do image resizing includes a couple of them. Then lastly in either scenario you need to make sure the end result is actually decent quality.

With image resizer I was done in 5 minutes! It's actually a .NET component rather than Classic ASP but that wasn't an issue. You create a bin folder, add a couple of simple lines to your web.config file then put an extra extension on the image path plus parameters for the size you want and that's it!

If your doing a .NET project it's also available on NuGet making it even easier to get up and running.

Improve productivity by ignoring emails

Do you find your most productive at the very start of the day following all those lovely hours of sleep? But do you also find you spend the first hour or 2 at work reading through emails only to then discover its coming up to lunch and you haven't achieved much actual work?

This is a problem many of us face and often it can lead to having a feeling of being behind for the rest of the day, which ultimately lowers our performance for the rest of the day.

The solution is simple, Don't open your email! Ok obviously at some point you will have to, but rather than first thing, try getting an hour of worm done first. This way you've got something done that you can feel good about before trawling through all those emails followed by a break for lunch.

Sill organising email into folders? Then STOP!!!

About a year ago I was listening to a podcast to do with work and productivity and one of the subjects being discussed was email management and not letting it take over you life.

At work we all receive a lot of emails, in-fact recent stats shows light users receive 180 emails a week, heavy users receive 2100 emails a week. Now most of these go in the trash or get picked up by spam filters, but a lot is still emails from colleges needing action, some can be deleted and some just need to be kept for the future. With the ones needed for the future a lot of us file them into folders (often organised by projects) so that they can easily be found in the future.

But when that future date occurs you discover that your folders are actually full of hundreds of emails. Worse still a lot of them were items that could probably have been deleted a couple months after reading them. Now theres no way of finding the one you wanted by looking through the list, so your only option is to use a search to find the email you wanted. This is ok though as searching emails actually works quite well now. But if searching actually works well now and we're going to do it anyway, what was the point in all that time you spent filing emails into individual folders?

So here's my recommendation. Create 2 folders one called Keep Forever and the other called Keep for 6 Months. Set a rule on the 6 month folder so that anything older than 6 months get automatically deleted (if your concerned you can always make the rule for a year). Then when emails come in if it needs actioning leave it in your inbox, if you need to keep it forever and move it to the forever folder and if it's something that might be needed over the next few weeks put it in the 6 month folder. This way you inbox is kept small with things you need to reply to, you still have all your emails to search in the future, and the crap that is only relevant for the next few months will be deleted automatically in 6 months time.

Going Further....

In Outlook you also have the ability to tag an email with categories (or multiple categories), each category is also colour coded. An extra step you can take is to choose a category for each email that comes in that your going to keep or need to action. e.g. I tag my emails with a client category.

This is much simpler than organising into folders as you can do it as you receive/read emails (not when filing), you get the benefit of being able to find emails to action in your inbox visually by colour, and when you need to search in the future you also have the power of the category filter.

Give it a try for a few months and then wonder why you spent all that time filing emails.