Tuesday, February 22, 2011

Math.Round in .NET is buggy!

I was trying a piece of code and I was amazed to see that Math.Round(4.5) results to 4! 1.5 leads to 2, 2.5 leads also to 2, 3.5 leads to 4, as does 4.5. According to the MSDN documentation on Math.Round, The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction. To control the type of rounding used by the Round method, call the Math.Round(Double, MidpointRounding) overload. That's nice and everything, but it contradicts the description of the method on the same page: Rounds a double-precision floating-point value to the nearest integral value. It also contradicts the behaviour of Javascript's Math.round in Microsoft's browser Internet Explorer.

The solution for this is to use the overload Math.Round(value,MidpointRounding.AwayFromZero). Math.Round(4.5,MidpointRounding.AwayFromZero) equals 5. If you don't know that, like I did after 5 years of .Net, you are pretty much screwed. So be wary of helpful mathematical functions.

Monday, February 21, 2011

Making a switch-like construct with types in C#

Update: this is more of an exploration post, not something serious on how to achieve the desired result. For a construct that actually works like a switch, check this post out: Creating a C# construct that acts like a more complex switch/case

Many a time I had to select a different behaviour based on the type of a boxed object. The usual solution for this is an ugly stream of if statements that check for the type and then return if found. A while ago I was writing about the dynamic keyword and how it can be used to declare a method for each specific type that is then executed from a simple method having the boxed object as a parameter. Some people liked it, some did not. What would have been nice is a switch statement that works with types, something like:
switch(obj.GetType()) 
{
case typeof(MyClass1):
// do something with obj
break;
case typeof(MyClass2):
// do something else with obj
break;
default:
throw new NotSupportedException();
}
Alas, this is impossible in C# due to the fact that typeof(class) is not considered a constant, but a method call.

What I am about to present to you is in no way a best practice, but something that I've cropped up in my wild musings on code: why not use the try/catch blocks as a switch statement? The idea is simple: create a generic Exception class then throw it based on the type of the object, then catch the specific generic Exception class. Here is the code, enjoy:
class Program
{
public class BaseClass
{
}

public class InheritedClass1 : BaseClass
{
}

public class InheritedClass2 : BaseClass
{
}

public class TypeCatchBlockException : Exception
{
public static void Throw(object obj) 
{
doThrow((dynamic)obj);
}

private static void doThrow<T>(T obj)
{
throw new TypeCatchBlockException<T>();
}

/*public static void Throw(object obj)
      {
        var type=obj==null?null:obj.GetType();
        var exceptionTypeName=typeof(TypeCatchBlockException)+"`1";
        Type generic=Type.GetType(exceptionTypeName).MakeGenericType(new[]{type});
        throw (Exception)Activator.CreateInstance(generic);
      }*/
}

public class TypeCatchBlockException<T> : TypeCatchBlockException
{
}

static void Main(string[] args)
{
showObjectType(new InheritedClass2());
showObjectType(new BaseClass());
showObjectType(new InheritedClass1());
showObjectType(new object());
Console.ReadLine();
}

private static void showObjectType(object obj)
{
try
{
TypeCatchBlockException.Throw(obj);
}
catch (TypeCatchBlockException<BaseClass>)
{
Console.WriteLine("BaseClass object");
}
catch (TypeCatchBlockException<InheritedClass1>)
{
Console.WriteLine("InheritedClass1 object");
}
catch (TypeCatchBlockException<InheritedClass2>)
{
Console.WriteLine("InheritedClass2 object");
}
catch (TypeCatchBlockException)
{
Console.WriteLine("Unsupported object");
}
}
}


So there is the simple class TypeCatchBlockException that has a Throw method. You have the dynamic implementation as well as the reflection implementation in the commented region. You execute Throw with an object inside a try block and then you add a catch block for each generic version of the exception for each supported type. The last block is reached if none of the supported types were encapsulated into the exception object.

Executing the console application in the code block results in:
InheritedClass2 object
BaseClass object
InheritedClass1 object
Unsupported object


Update: Some people didn't understand this was mostly a joke, so I needed to clarify it. We do need type switches and someone should implement them as a language construct. Throwing exceptions is expensive and the solution in this blog post would not be recommended for any production software.

But here is a nice solution for type switching using lambda expressions: Switching on Types.

Saturday, February 19, 2011

Moving to Chrome

I am the kind of guy that uses a piece of software until he has a really good reason to change it. I don't try new stuff if I am satisfied with what I've got and I am all for second chances when mistakes are made. After all, I am not immune to the occasional error. However, from this day on I am fully switching from Internet Explorer to Chrome.

Wipe that smirk off your face, I happen to think that IE is a reasonable browser and, given the hypothetical situation where Google would have made their browser compatible with Internet Explorer rather than the standards, you would all curse at FireFox right now. Besides, this is not about the standards at all. Firefox is still not on my list of available choices. It is sluggish, pretentious and buggy.

The thing is that parameters have changed. I now use the browser to quickly get answers to questions or to open RSS feed blog articles. Before I was using it to make ASP.Net applications and web applications and I still think Internet Explorer 8 developer tools are nicer and easier to use than Firebug, not to mention the horrible confusing developer tools on Chrome. Now I am free (partially) of web development and I can rejoice. Also, sites were more compatible with IE than any "standard" browser and it is not the case any more.

The downloading window from Internet Explorer always made more sense to me: have an option of Open, Save and Cancel and do it in another process so you can close the windows that spawned the download. Then it opens or you just go to the file and use it, you know where you saved it. I think this is the only thing I will miss from Internet Explorer, though.

Let's summarise:
  • Chrome opens instantly, letting you write something in the address bar immediately and having autocomplete for search term as well as web addresses. Internet Explorer blocks the CPU for a second or two and, even if it lets you write something in the address bar, it hickups just so that what you wrote is truncated. Firefox is so keen to update every fucking extension that it takes forever to start. Yeah, I know, FF4 is way better. Not compared to Chrome it isn't.
  • Internet Explorer is supposed to support the new Silverlight wonder technology. Unfortunately, every single SL video I open is sluggish, blocks my mouse, doesn't respond to simple commands like Space to pause and arrows to skip and every Silverlight app is alower than their Flash counterpart. I've just wasted one hour today to uninstall with a special tool the older Silverlight version as the installer for the new one brutally stopped with a "generic error message". Well, screw that! I would rather download the videos and run them in a "generic" video player
  • Javascript: it works 8 times faster on Chrome that on Internet Explorer 8. I know they've just released Internet Explorer 9, but it won't work on Windows XP. I have an old computer, it works fine, I want a browser for it without buying a trillion dollar Windows 7 license. You can't provide it for me, Microsoft!
  • HTML5: it is supported in Chrome. If I ever want to start web dev again, I would go with that
  • Extensions: I am not a big fan of extensions as they slow the browser down. I do prioritize speed. However, I have AdBlock Plus installed on Chrome and I couldn't install it (even if I have tried emulating it) on Internet Explorer. Having no flashing nonsense on a page when I am reading the text on it is wonderful. Try it!


I could find even more reasons if I think about it longer. Like a long relationship going bad, you start noticing all the things you hated only after you break up, but break it up I do. So there it is, found me a new girlfriend and she is chrome shiny.

Friday, February 18, 2011

Take that, RedGate!

As you know, the uberuseful tool Reflector, originally developed by Lutz Roeder, was bought by Red Gate, with promises that the tool will always remain free for its users. Of course, the first thing they did is to create a commercial version of it with some extra features, but they did keep their word by allowing people to use a free version. Recently they changed their minds again, practically saying "Fuck you, developers!" and asking for money for any version of Reflector. The community was outraged.

Enter Jetbrains, my favourite company, the one that would have replaced Google and Microsoft at the top of the development world if they weren't so busy actually developing. They are also the makers of ReSharper, which you should download and use if you are any decent C# programmer. What did they think of this Reflector fiasco? They've decided to build a version of their own! They did bundle it in ReSharper 6, which is a commercial tool, but then... they promised to make it a free standalone tool! Thumbs up, guys!

Here is the blog entry announcing this: ReSharper 6 Bundles Decompiler, Free Standalone Tool to Follow

Tuesday, February 15, 2011

Friday, February 11, 2011

Image.FromStream or Image.FromFile locking streams and files

You may want to create an image from a Stream or from a file on the system. You would want to use the static methods Image.FromStream or Image.FromFile to do it. However, you soon realize that this would not work if you immediately dispose the stream. Also, trying to delete the file would result in a locked file error. Could it be that some reference of the stream or file is kept in the resulting Image object? The answer is yes.

Microsoft explains it like this: GDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image. Additionally, even after the image has been decoded, GDI+ may determine that it is more efficient to discard the memory for a large Bitmap and to re-decode later. Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object. And they have a fix to this, here: Bitmap and Image constructor dependencies

As you can see in the link above, their solution is different from indexed versus non indexed images. You can determine if an image is indexed or not by looking at the PixelFormat property. Also, you need to do some pretty weird stuff in the indexed case and there is no code on the Microsoft page. So here is a helper class I've cropped up to get images from Stream, File or byte array without locking any of the original resources:

/// <summary>
/// Helper for Image objects
/// </summary>
public static class ImageHelper
{
/// <summary>
/// Get an image from a byte array
/// </summary>
/// <param name="iconBytes"></param>
/// <returns></returns>
public static Image GetImageFromBytes(byte[] iconBytes)
{
if (iconBytes == null || iconBytes.Length == 0)
return null;
using (MemoryStream ms = new MemoryStream(iconBytes))
{
try
{
return GetImageFromStream(ms);
}
catch
{
return null;
}
}
}

/// <summary>
/// Get an image from a file without locking the file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static Image GetImageFromFile(string fileName)
{
try
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return GetImageFromStream(stream);
}
}
catch
{
return null;
}
}

/// <summary>
/// Determines if an image is indexed (with a color palette)
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static bool IsImageIndexed(Image image)
{
switch (image.PixelFormat)
{
case PixelFormat.Format1bppIndexed:
case PixelFormat.Format4bppIndexed:
case PixelFormat.Format8bppIndexed:
case PixelFormat.Indexed:
return true;
}
return false;
}

/// <summary>
/// Get an image from stream without locking the stream
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static Image GetImageFromStream(Stream stream)
{
try
{
using (var image = Image.FromStream(stream))
{
var srcBitmap = image as Bitmap;
var destBitmap = new Bitmap(image.Width, image.Height,image.PixelFormat);
if (IsImageIndexed(image)&&srcBitmap!=null)
{
Rectangle srcArea = new Rectangle(0, 0, image.Width, image.Height);
BitmapData srcData = srcBitmap.LockBits(srcArea, ImageLockMode.ReadOnly, image.PixelFormat);
Rectangle destArea = new Rectangle(0, 0, image.Width, image.Height);
BitmapData destData = destBitmap.LockBits(destArea, ImageLockMode.WriteOnly, image.PixelFormat);

IntPtr srcPtr = srcData.Scan0;
IntPtr destPtr = destData.Scan0;
byte[] buffer = new byte[srcData.Stride*image.Height];
Marshal.Copy(srcPtr, buffer, 0, buffer.Length);
Marshal.Copy(buffer, 0, destPtr, buffer.Length);
srcBitmap.UnlockBits(srcData);
destBitmap.UnlockBits(destData);
destBitmap.Palette = srcBitmap.Palette;
} else
{
using (var graphics = Graphics.FromImage(destBitmap))
{
graphics.DrawImage(image, 0, 0);
}
}
return destBitmap;
}
}
catch
{
return null;
}
}
}

Wednesday, February 02, 2011

Ridiculous self importance

From early ages we learn to listen to the people around us, but only at a later time we get to understand that what those people are saying may not be true or in our advantage. I believe this sets us up for the rest of life to partially believe even the most ridiculous claims for the sole reason that someone has uttered them. And who better to take advantage of this than politicians and lawyers.

Check out these stupid ideas:
  • Ad blocking may not be entirely legal - In other words, if someone were to help you personally to avoid all the annoying flashing and blinking and popping ads on a site, they would infringe some law of revenue. While this may sound preposterous, try googling for AdBlockPlus and you will see a myriad pages acusing successful ad-blockers of diminishing web site revenues. And if you think this is just the lament of sore losers happy to distribute their content at almost no cost on the Internet, but wanting to take money frmo you unencumberred, or if you think that being your browser, your computer and your money paying for the connection you should have some sort of rights, check this out: CoralQQ author arrested in 2007.
  • Free Internet porn is unfair competition to pay sites! - I may soon be accused of distributing free content on my blog as unfair competition to the ad ridden ones, if that is the case. I sure wouldn't want to hurt those, no sir. I find it terribly funny that the case against free porn was dismissed as an action designed to censor free speech (in legal terms a SLAPP).
  • Making software that allows one to make better use of software or a device, be it a computer, XBox, PlayStation or IPhone, is illegal! - As you can see from the Wikipedia link someone wrote in the first paragraph "The distribution and use of cracked copies is illegal in almost every developed country". Major arrested development! You may have heard that recently the unhackable PlayStation3 has been hacked. The funny thing is that with each new version, Sony seems to remove functionality, making PlayStation one of the few devices that are better old than new. When they found out about the hack Sony started a major law action against sites that post the hack solution for their crappy console. Apparently, their perspective is that you never owned the console, but you "rented it", while spending time to make software that allows you, the user, to do what you want with the thing you paid good money for, but is still not yours, it's illegal. They phrase it in a very funny way, too, saying that the hacking actions "are circumventing effective protection methods". Well, they aren't very effective, now, are they?
  • Major sites that distribute large content like video should pay extra; people watching should pay extra, too; ISPs should pay a lot; the Internet should be split into low rent and high rent areas. One might argue that since one does pay for the Internet connection at both ends, it should not be an issue what they are doing with it. That may be too late for Canadians, who always seemed very decent to me, as they got a new law voted on the 25th of January allowing Usage Based Billing for ISPs. People are now outraged and are protesting rather than swallow it, but already the coolness of Canada has been forever sullied in my eyes.
  • Using encryption of your own data on your own device is illegal! - The Indian government demanded that Blackberry find a way to allow authorities to read the encrypted data on their devices or at least disable the option for Indian devices, else face a national ban. Of course, they "reasoned" it is all for the sake of the people and against terrorist acts. It now seems unlikely they will proceed with the ban, but still, the whole thing begs the question: how come they didn't ask this of other device manufacturers? Also, it may be of some interest to the reader that all major instant messaging tools like Yahoo, MSN (now Windows Messenger) or GTalk do not feature encryption. Skype does, but guess what? They also provide some eavesdropping tools.


The list could go on and on and on, but these were both startlingly ridiculous and pretty recent. Apparently, the DRM bug, the one that made people sell you things and then consider it's still their property, is still alive and thriving. Most such beliefs come from the power we give other by not reacting. It is easier for us to do nothing than do something, so it is easy to take advantage by positioning the average human so that he has to do something to thwart your plans.