Bobby,
First off, thanks for posting your classes from AU - I'm sure a lot of people will benefit from it, as will the board.
Hi Glenn,
You're welcome. And I only hope that someone, anyone, will benefit half as much from it as I have.
Let me make myself perfectly clear here: You should always write code that perfoms the best it can, irrrespective of the fact
that the .NET CLR will clean up behind you.
This is by no means meant as a dig at anybody, I'm just trying to help everybody learn.
First, no digs detected. I enjoy discussing differing views without digging into each other.
Second, I respectfully disagree. I did another class at AU this year and its main goal was to show the exact opposite of what you are saying. I code with three goals in mind:
Goal #1 - Write code that works. If it doesn't work as desired by the end user then I get fired.
Goal #2 - Write code that is "right". I like to say that this is really goal #1.5.
Goal #3 - Write code that is fast. And I also like to say that this is a distant 3rd behind the first two goals.
The majority of the class focused on Goal #2, and there are a
lot of things that make code "right". If I have to sum it up, "right" code screams its intent at the top of its lungs. To me, testing with the
is keyword and then casting again is much clearer in its intent than using the
as keyword and testing for null. Creating "right" code trumps creating fast code and I will aways write "right" code first.
Now, if testing proves that this function, and in particular this double casting, is a bottleneck then, and only then, I would look at tuning it. This is probably what I would try first:
**Untested code below**
private static void OnObjectAppended(object sender, ObjectEventArgs e)
{
if (IsLine(e.Object))
{
newLine.UpgradeOpen();
if (newLine.IsWriteEnabled)
{
newLine.Layer = NEW_LINE_LAYER_NAME;
}
}
}
private static bool IsLine(object appendedObject)
{
Line newLine = appendedObject as Line;
return (newLine != null);
}