Get your .NET code ready for other platforms#

Although .NET is mainly targeted at Windows right now, it's obvious from the Mono Project (now in beta 2 phase!) and other initiatives like the SSCLI that running .NET code on other platforms is either already possible, or within reasonable reach. So how do you take that into account when developing in .NET?

Personally, I knew I was doing the "right thing" all along because it just felt right, but it's nice to see that reflected by 'trusted sources' once in a while: Scott Hanselman takes some gems away from nGallery's Mono compliance and sprinkles them with a little of his own fairy dust. Also, Clemens Vasters just noticed that there's a dasBlog port on mono. These accomplishments are both pretty impressive.

So here's the aggregated wisdom, with a personal topping to cover it up:

  1. Use constants like System.Environment.NewLine and System.IO.Path.DirectorySeparatorChar - everywhere and always.
  2. Furthermore, use platform helper methods as much as you can. Don't outsmart the system, it's not because you know from your daily Windows usage how to append a filename to a directory like so:

    string fullPath = directoryName + @"\" + fileName;

    that this wisdom will help you on all platforms. (By the way, did you check to see if directoryName didn't already end in a backslash? And add a myriad of other checks here as well.)

    Use

    string fullPath = System.IO.Path.Combine( directoryName, fileName );

    in stead. Guaranteed to be correct (regardless of the platform), more readable and meaningful, and less code (if you'd add the necessary checks to your own code of course)!
  3. Make sure sure all I/O operations (files, directories, ...) are case sensitive.
  4. Make sure you don't compile your assemblies with the /incremental flag.

I'd like to see that first point supported in the framework just a little better though, since not using Environment.NewLine is just that tiny bit easier. Compare

Console.WriteLine( "Exception details:\r\nMessage: {0}\r\nStack Trace:\r\n{1}", e.Message, e.StackTrace );

to

Console.WriteLine( "Exception details:{0}Message: {1}{0}Stack Trace:{0}{2}", Environment.NewLine, e.Message, e.StackTrace );

Arguably, the second one does look a bit cleaner, but it's still more cumbersome with the extra parameter. So, an extra escape character indicating a new line (regardless of the platform) would be nice. Who cares about those 'return' and 'newline' codes anyway these days.

Other than that: stick to the rules and on one beautiful day, you just might find yourself running on Linux. And why not, why not indeed.

All content © 2010, Jelle Druyts
On this page
Get your .NET code ready for other platforms

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 348
This Year: 2
This Month: 0
This Week: 0
Comments: 525
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In