2011-09-05

Extensions...

Here's a little extension on Object in C#

public static bool EqualsAny<T>(this T obj, params T[] compareTo)
{
  for (int i = compareTo.Length - 1; i >= 0; --i)
  {
    if (obj.Equals(compareTo[i]))
    {
      return true;
    }
  }

  return false;
}
If the original code would have looked something like this before:
int nowHour = DateTime.Now.Hour;

if (nowHour.Equals(10) ||
    nowHour.Equals(12) ||
    nowHour.Equals(15) ||
    nowHour.Equals(17) ||
    nowHour.Equals(19) ||
    nowHour.Equals(23))
{
  //Happy hour!
}
the after code would look like this:
if (DateTime.Now.Hour.EqualsAny(10, 12, 15, 17, 19, 23))
{
  //Happy hour!
}

2011-09-04

Lazy mans guide to code syntax highlighting on Blogger

Having just moved some old blog entries to a brand new blogger.com blog from Wordpress I was looking for an easy way to publish highlighted code in my posts.

There are several different tools freely available such as SyntaxHighlighter, highlight.js and Prettify which I decided on using.

2011-03-15

Using the Stopwatch class in C#

During the development of any kind of C# application you might find you need to measure how long some part of your code take to execute. Perhaps your customer demand an operation to finish in a maximum amount of time or you might be puzzled what part of a function is making it run slower than expected.

2011-03-09

XOR enciphering data for medium security

Sometimes when developing systems that involve sending or storing semi-sensitive data as a string there might not be an easy option or enough time to implement conventional encryption even though there might be some need to hide the contents of the data in some way. Such situations may include where you want to send scrambled messages between servers on the interwebs, store data in places where others may have access to it (on shared environments with nosy admins etc) or where you want to be able to easily unscramble data in something like javascript or some other language that lack libraries for conventional encryption.

2011-02-22

Optimizing the for loop for fun and profit

The three-expression, C style for loop is one of the most useful iteration statements in many programming languages including C#, javascript and perl. However it is commonly used in a sloppy manner and can often be optimized for better performance which is why I have set out to try and explain the inner workings of the for loop in this blog post. This is pretty basic stuff but it seems a fair share of developers has overseen the details of how the for loop really, really works when learning it.