Wednesday, March 22, 2006

C# Reversioned

She says she likes my face
She says she owns the place
Forget about it, 'cause she's a
Hotel detective
-- They Might be Giants "(She was a) Hotel Detective"
See Sharp Dot Net Two Point Oh has only recently been released officially and there are already talks about the features to be in Three Point Oh. Even though I have not been able to dig into the lastest virgin, err... I mean, version, I do have some beefs about what I've heard has been added. Using the MSDN and ONDotNet articles regarding C# 2.0 I am going to lay out my own opinions below. But before we get into that let me preface my opinions by stating: I fully realize these language additions are not required. I don't have to use them. I can ignore them. Unfortunately though, other people can and will use them, so I am required to learn and understand them.

Why am I so opinionated about these changes? Because some of them just don't make sense. There are three factors of performance when considering any developmental task: Development, Runtime, Maintenance. Will speeding up any of those three hurt any of the others. I feel some of the additions will seriously hurt in the maintenance area. YMMV.

I am not describing what these functions do, I am sharing my opinion about them. For more information I would suggest the previously linked articles and personal experience. I feel that even though I have no experience with 2.0, my extensive 1.1 and a decade plus of software development experience is enough to allow for the following interpretations. I fully expect my opinions to change as my experience with these additions increases. You can skip to the bottom to see what I think should be added to the language, framework, and IDE.

Partial Types
Basically, you can define a class or structure across multiple pages. You could do this in C++ and I've collaborated on a couple projects in the past with a guy that loved doing this and I've always hated it. I just hate the thought of having to manage multiple files for one class (forms and their associated resources excluded). Really, to each his own and I do feel you really shouldn't rely on the IDE too much so I see this as an acceptable addition to the overall .Net development experience. People have different organizational requirements and goals so I have no real problem with this. And I should note, having dealt with classes automatically generated by the IDE based on WSDL files, I see a very crucial need for this feature.

Static Classes
To me, this where the onus of responsibility gets obscured. One of the many things I learned from the illustrious Chief Software Architect (L-3 AR) and all around incredible guy, Bill Hertz, was that the compiler should not be relied upon to detect coding mistakes. It seems to me, that's all this feature does. If you can't keep your own developers from adding non static methods to a properly documented class intended for it, what makes you think you can stop them from removing the static clause and doing it anyway? This hardly seems like a useful addition to the language.

Global Namespace Qualifier
Well, its definitely a good thing, but I don't care for the syntax. I can deal with that though. On the up side, I have never encountered a need for it as I have never had to deal with a class written at the global level.

namespace MyApp
{
class MyClass
{
public void MyMethod()
{
::MyClass obj = new ::MyClass();
obj.MyMethod(); // Traces "Hello" instead of recursion
}
}
}

public class MyClass
{
public void MyMethod()
{
Trace.WriteLine("Hello");
}
}

It just doesn't feel like good programming to do something like this without a good reason. I just can't find that reason.

Property and Index Visibility
Yeah, I really like this one. This one is a good one and one that I wish I had access to now. I can see many uses for a public get or set and a protected opposite. Sure, the property can be readonly publicly, but I should be able to write to it internally. This is a good one.

Anonymous Methods
I am going to hold off on my evaluation of this until I can actually see it in action. At first I had a knee-jerk reaction to it. I didn't like the idea of it. I still don't, but I want to give it a chance before I slam it. For now I will just say, I don't see what I gain from this addition.

Generics
This is probably the best feature in the next version. Excellent.

Iterators
I don't know enough about this yet and will be spending more time to figure out how to best implement it, but at first glance, it looks promising.

And now on to the Three Point Oh language additions.

MSDN and CodePost have pretty good descriptions of the pending feature additions.

Implicit Type Declarations
Even though (thankfully) these are to be local only, it is assinine letting the compiler decide the appropriate datatype for a particular variable. I just can't believe this is a proposed "feature". Take this for instance:

// x is not local and is defined outside
// the scope of the mthod calling this loop
for(var i = 0; i < x; i++) { // do stuff }
for(var i = 0; i > x; i--) { // do stuff }

Will the compiler be smart enough to match the datatype of 'x'? I hope so, if x is a long and it types i as an integer, this is going to have some serious implications. And before you say "but this will help with database access where you don't always know... blah blah blah". Go to hell. Try doing your damn job.

Object Initializers
This one isn't too bad really, it is a kind of shorthand that will allow a little less redundant coding, provided you have properties defined for the fields you want to assign:

public class Point
{
int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}

public int X { set { this.x = value; } }
public int Y { set { this.y = value; } }
}

Point p = new Point { X = 1, Y = 1 };
Point q = new Point(1, 1);

I don't know yet, I can see where this can be handy, but you have to know the class. If the property is readonly, this won't work. What happens if I want to require that both x and y are initialized by the caller? This feature will allow me to ignore this requirement.

Anonymous Types
Another brain dead concept, only this time, we're combining Implicit Types with Object Initializers and this is what the children look like:

var x = new { Name: "Jim Bob", Number: -1 };

I see nothing but chaos coming from this mess.

Extension Methods
To me, this just defeats the purpose of subclassing. And how does this inherit anyway?

Lambda Expressions
To me, I think these will only confuse the code further but I want to understand it better before I slam it too hard.

LINQ
Now, this one I do like. And I do understand that many of the previous features that I see as boneheaded are used to make this possible.

And now for what I would like to see in the language.

foreach Indexing
In PHP you can iterate through a collection like

foreach(items as value)

Which will return each value in the items collection. C# does this and it works just fine.

foreach(items as index=>value)

Now this will not only give you the value, but also the index or key of that value in the items collection. This would be very useful.

Unfortunately, I've run out of time. I will add to this though; count on it!

0 Comments:

Post a Comment

<< Home