Posts in category .net - page 29

Handling nullable value types in .NET 1.x

One of the great new features in .NET 2.0 is support for nullable types – especially important when dealing with value types such as Int or DateTime values coming from a database. Previously you were forced to either set and track another binary flag or to use a “special” value to represent null that sooner or later would turn up in real data.

In C# 2.0 thanks to a bit of syntactic sugar we can do this;

int? nullable1 = 1;
if (nullable1 == null) nullable1 = 2;

Which the C# compiler actually turns into the following:

Nullable<int> nullable1 = new Nullable<int>(1);
if (!nullable1.HasValue) nullable1 = new Nullable<int>(2);

This makes use of the new Nullable generic structure to wrap the value with an additional boolean HasValue property.

That looks surprisingly similar to the syntax of the open-source NullableTypes project you can use with .NET 1.1 (or 2.0):

NullableInt32 nullable1 = new NullableInt32(1);
if (nullable1.IsNull) nullable1 = new NullableInt(2);

Disclaimer: I’ve worked on the NullableTypes project specifically implementing the IXmlSerializable, NullableGuid & NullableTimeSpan.

[)amien

Bad programming advice – don’t use exceptions

Kristian Dupont Knudsen has written a top 10 of programming advice not to follow with his rationalisation for each one. Being a programmer I thought I’d chip in with my 2 bits.

Joel recommends not using exceptions because they are “invisible in the source code” and that they create “too many possible exit points”.

His recommendation is to return error values when thing go wrong just like C does. This solves neither of the two problems he mentions and creates some new ones;

  • Passing an extra ‘error’ parameter to a function or use part of its return value
  • Keep track of the various error codes and what they mean
  • Newer libraries may return new errors that your code wasn’t written to handle and so your code will continue ignorantly
  • Forget an error and you’ll continue as if nothing went wrong…

I have to agree with Kristian on this one – Joel’s advice just makes no sense. Exceptions are a much superior solution to a difficult problem.

While I think Java went overboard in each method declaring what it might throw and needing to ensure you accept that C#’s balance seems just right.

Throw exceptions when your method can’t do what it says on the tin but exceptions should be just that – the exception. Don’t ever throw them to signify some additional or inconsequential condition.

[)amien

Microsoft XNA

Microsoft have announced their XNA platform for game development.

The platform is based around C# and the .NET 2.0 CLR and frameworks, specifically tailored for the requirements of game developers. This might possibly mean much of the common language specification (CLS) elements are removed for performance.

Initially the platform will target both Windows machines and the Xbox 360 although Microsoft have touted cross-platform compatibility and with the recent shared-source version of the .NET 2.0 CLR source code ROTOR release there are hopes for Mac OS X, FreeBSD and Linux.

XNA Studio is the central IDE for the platform and is based around a modified version of Visual Studio 2005 Team Services.

Currently the only tool available is XNA Build which is a modified version of MS Build with additional tracking features to ensure obsolete game assets are suitably pruned. Microsoft have modified their MechWarrior 2 game source code to build using XNA Build and were surprised to realize that around 40% of the textures they shipped on the CD were in fact no longer used having been artifacts from previous builds.

In my opinion there’s a few reasons why Microsoft is going this route..

Flash

Microsoft want developers to get away from Flash – at least on the Xbox 360. Some of the Live titles, notably Hexic, are Flash games. Flash is okay for 2D games and their run-time has been modified to utilize the Live! and 360 controller but adding 3D would be non-trivial and it’s still a non-MS platform.

Isolation of home-brew code

It’s no secret that developers at home love to get code running on their favorite console platform be it to port existing games or emulators (such as ScummVM) or simply to hack around.

The problem with letting developers run your own code is that it becomes quite easy for other people to use this to boot pirated “warez” versions of games professional developers just spent a few million dollars developing.

By giving developers a byte code based language such as Java or .NET you can control what they do. Specifically you can give them no mechanism to launch native code such as that comprising a commercially developed Xbox 360 game…

Flesh out Xbox Live!

Xbox Live! seems to be a hit and Microsoft have put up titles from smaller indie development studios. This is all well and good as both the studio and Microsoft make money from the purchase but the Xbox 360 dev kits aren’t cheap. This probably explains why a number of those games are Flash…

With Sony’s announced PSP Live serving up highlights from the original PlayStation back-catalog and Nintendo hinting at emulation of older systems on the Revolution the 360’s catalog is going to look a slim by comparison.

If Microsoft get XNA out there and let developers write and distribute Windows versions how they see fit they can grab the cream of the crop for publishing on Xbox Live.

Either way it looks like good news for Xbox 360 owners and small developers. Microsoft have already stated back-bedroom coders will be able to get in. :)

[)amien

Web applications in Visual Studio 2005

One of the things that annoyed me with Visual Studio.NET 2003 and Visual Studio 2005 is the web “project” type.

The main problem is that these projects are not treated as traditional applications, but in order to please the ad-hoc web developer crowd they are treated as collections of files with no specific project options or compilation process – instead compiled on-the-fly by the web server.

Now for managing day-to-day web sites I can understand this. You don’t want a small change to one page bringing down the whole site because of a compilation error.

But there are other scenarios whereby you are developing a web application, perhaps as a front-end to your main system and you want that compile error to show up here and now. You want compilation options because you’re not intending on shipping the source out to other people.

Thankfully I’m not alone in this scenario and Microsoft have released a second beta of the VS 2005 Web Application Project. They have also updated the beta of the VS 2005 Web Deployment Project to work with it.

The Web Deployment Project lets you right click and built to remote and test servers…. Nice.

You can keep an eye on the developments over at ScottGu’s excellent blog.

[)amien