The unbearable lightness of vendor bloating…

It’s been a busy year so far! Let’s see… My kid is almost 1 year old and that’s where most of my spare time gets sucked into. At the office, we’ve finally launched the new version, so if you’re around the UK or ROI enjoy your movie session. I’ve also been catching up on my reading (if only I started paying attention to Mr. Skeet anytime sooner) and finally, I’ve decided to take the Scrum Master certification course, which will happen by the end of this week.

Busy days indeed…

That’s why I hate wasting time. Especially cleaning up the laptop on account of that major mess called Superfish from Lenovo. When I first read about it, I reacted with disbelief, but when I later confirmed that I was actually one of the victims… my jaw dropped. I ended up doing a clean install following this great tutorial!

Typewriter and cofee

The bottom line is, manufacturers sell their computers loaded with bloatware, that affects performance, the user experience and even compromises sensitive data along the way! The performance perception you get from a notebook, filled with those wonderful bits of code is depressing. Only one of the parts seems to benefit from this… But honestly I think they’re taking it too lightly! Even Microsoft. Ask these users how they feel and you’ll get something like: OMG! Why is Windows soooo slow!!!! But in some cases (most?) this has nothing to do with the OS. Continue reading →

The Ghost PixelFormat

There is a method, that given an input image file, saves its thumbnail to disk. The code works for, pretty much, all the images tested and then there’s one that makes it throw a System.OutOfMemoryException.

The method could be something like this:

private void LoadImage(string fileName)
{
  using (Image img = Image.FromFile(fileName))
  {
    Image thumbnail = new Bitmap(150, 200);
    using(var graphic = Graphics.FromImage(thumbnail))
    {
      var rectangle = new Rectangle(0, 0, 150, 200);
      graphic.DrawImage(img, rectangle);
      thumbnail.Save("Thumb_" + fileName);
    }
  }
}

I started debugging my code and it turns out the failing image used a CMYK color model (which is the wrong format for a web image) and this seems to be a somewhat random bug that depends on windows version. This was showing in the image properties where the property PixelFormat displayed a value of 8207, because this value is not one of the defined Enum values. I fixed the code by testing the PixelFormat property and when Enum.IsDefined(typeof(PixelFormat), img.PixelFormat) is false we fallback to a RGB color mode. Let’s leave it at that, it’s not this part that got me interested…

PixelFormat_blog

What really got me puzzled is how the Image object displayed a PixelFormat value of 8207. This didn’t make any sense. The value is not one of its defined values. So how could it be assuming this value? Does C# allow values outside of the defined Enum to be assigned to a enum object? Let’s find out:


enum GhostPixelFormat
{
 First = 1,
 Second = 2
}

static void Main(string[] args)
{
 GhostPixelFormat enumObject;
 if (Enum.TryParse("Second", out enumObject))
 {
   Console.WriteLine("Second was parsed to a valid GhostPixelFormat: " + enumObject);
 }
 if (!Enum.TryParse("Third", out enumObject))
 {
   Console.WriteLine("Third was not parsed to a valid GhostPixelFormat");
 }
 if (Enum.TryParse("1000", out enumObject))
 {
   Console.WriteLine("1000 was parsed to a valid GhostPixelFormat: " + enumObject);
 } 
 if (!Enum.IsDefined(typeof(GhostPixelFormat), "1000"))
 {
   Console.WriteLine("1000 is not defined in GhostPixelFormat Enum");
 }
 Console.ReadLine();
}

And the output is:

Second was parsed to a valid GhostPixelFormat: Second
Third was not parsed to a valid GhostPixelFormat
1000 was parsed to a valid GhostPixelFormat: 1000
1000 is not defined in GhostPixelFormat Enum

Even though we couldn’t parse the invalid Enum value “Third”, we could do it with an integer (1000) which is outside the defined values. On the other hand, the Enum.IsDefined() is false for 1000.

This is explained by looking at the way that Enums are implemented in .NET:

From Enum C# Reference:

“The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int.”

Basically an enum is treated as the underlying type, so being an Int32, you can store any valid int value and the conversion will be valid. But even if this is true, do we want this? Well, most of the times, no!

From Enumeration Types:

“However, you should not do this because the implicit expectation is that an enum variable will only hold one of the values defined by the enum. To assign an arbitrary value to a variable of an enumeration type is to introduce a high risk for errors.”

Is it all bad? Not exactly, think of BitFlags for example, if enum conversions were explicit it wouldn’t be possible.

So, I’m definitely not the first to stumble on this, but at least now I found that ghost.

Back to the future

A bit later than I had planned, but I got one! I’m finally using a windows 8 machine.

Lenovo

It’s a Lenovo IdeaPad Flex 2 14 Notebook (the i3 Haswell version), which is not exactly the performance behemoth you would expect in a technology blog, but it’s a cool little machine that was bought for portability and battery-life. The small form-factor seems to be the decisive factor for my latest gadgets. I’m trying to figure what these new Hybrid computers are all about, before investing in a proper development machine. So far I’m loving it! Like Rob wisely put it: it’s not a new gadget, it’s an investment! Brilliant!

About Microsoft’s new OS, most of my favorite tech bloggers have done their share of windows 8 posts a long, long time ago… But hey, I warned you I needed to catch up. For anyone starting with Windows 8, this video seems like a good place to start.

I also bought a Nokia Lumia 925 a few months ago and I absolutely love the Windows Phone OS, reliable, responsive and easy to use, so I want to find out what the new Universal App model means for us developers. Hyper-V won’t be an option unless I upgrade to Windows 8.1 Pro, so no phone emulator for me. This might not be an issue, as long as I get an unlocked phone.

Stay tuned!

The first of many, but no code yet

First of all, who am I and how did I get here?

The answer is simple: I’m a software developer and I’m a software developer!

It’s been almost 10 years working on the field and after all this time, I’m telling you, it’s never been so easy and so hard at the same time.

I mean, these are definitely exciting times. Every developer with an idea, has the resources and technologies at hand to make it happen, from the comfort of his home, and he might not even need to spend any money to do it. Even Visual Studio is free now!

The hard part is keeping up! There are lots of people complaining about this! And don’t worry, I won’t do another 10-things-you-need-to-start-doing-to-keep-up kind of crap articles. There’s so much going on in the web development world at the moment, that I just can’t put myself up to date. Even if I wasn’t working 8+ hours a day, I wouldn’t have enough time to cover all of it.

This is me pledging to do something about it, to learn about things, trying to have a place I can go to from time to time to recall about some problem I solved, or some opinion I had.

It’s go time!