Managed Environments and ASP.NET

I started a new job on Monday.  This job involves working with ASP.NET and C#.  Actually, I could choose to use VB, but that way lies madness.

I’m not going to comment on my work or what I do, other than to say that I really like the people and the ethos of the company.  But ASP.NET and C# are technologies that are worth commenting on.

There are many good things to be said about both technologies.  ASP.NET does pretty well at the separation of code and design: you create a pseudo-html (actually a superset of XHTML) file to describe the page, including elements that can be manipulated programmatically such as an <asp:Button> tag, and then write a separate file for the code that does the magic.  Creating forms and reading input from them is a doddle.  As is doing different things depending on which button was pressed, or doing magic when a control is changed.  The backend code is object-orientated, and comes with such useful features as events (essentially a signal/slot mechanism).

In short, PHP eat your heart out.

Of course, it’s not all that straightforward.  PHP wins hands down on text processing.  The overhead of ASP.NET is not to be sniffed at.  And writing something very simple can be a faff, because you have to do it “the right way”.

So, that’s ASP.NET.  How about C#?  In particular, how does it compare to C++?

Well, it removes some of C++’s annoying quirks.  C++ has many of these.  A complete lack of consistency about whether definitions end with a semicolon or not is one, and another is the “explicit” keyword which you almost always want.  Exceptions come as standard, as does reflection (although it doesn’t seem as easy as Java’s reflection system).  Useful features like the foreach construct are welcome, as are the “out” and “ref” parameter keywords (thank you, Pascal).

And, of course, C# is a managed environment, with everything that entails.  Garbage collection.  Having to try really hard to get invalid pointers or references.  Unpredictable desctructors.

Wait.  What was that last one?

Well, say you’re working with a file.  Or a database connection.  Or any other such resource that requires tearing down in some way.  In C, you had to do it manually.  In C++, we did away with that with the advent of destructors.  Create your file object on the stack, then let it go out of focus.  Desctructor gets called, file gets closed, hey presto.

Not so with managed environments like C#.  There is no concept of a stack.  Everything is on the heap.  If you create a file object, it stays around until no-one references it any more, and then (and this is the important bit) at some unspecified later time the garbage collector eats it.  Notice the unspecified bit.  If you depend on the destructor, you don’t know when your file will get closed, and therefore when you’ll be able to open it again.  So you’re back to doing it manually.

There’s really no getting around this.  If variable scope guarantees destruction, you can’t do things like returning objects from functions.  And managed environments are supposed to get you away from petty things like keeping track of memory.  C#’s solution is to introduce a using keyword (well, an overload, since it’s already used like C++’s using keyword) which gives a fixed scope to such objects.  At the end of that scope, the resources they reference are cleaned up (but the now-useless object will hang around until some time after it’s not referenced any more).

That’s one of the irritating quirks of C#.  Another is restrictions on the switch() statement, where you can’t execute some code before falling through to the next case statement.  The reasoning is obvious – many a subtle bug has arisen by people doing this unintentionally.  But it’s annoying when you do intend it.

These are just the issues I’ve found in four days, mind.  No doubt I’ll have other gripes later.  Lack of determinism is one of my pet peeves, though.  That’s the mathematician in me, I guess.

One of these days I must get around to learning ADA.  I suspect I’ll like that.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: