Wednesday, December 22, 2010

Coding practices, part 1

.. hold on tight

Various have been the places I have worked at and even more so the people I have worked with. Sometimes it was a good experience where both parties benefited, but sometimes it was a struggle.
One of the downsides is that I have had to work with code produced to varying standards if there was something like a standard at all. In this little series of blog posts I will highlight some of the examples that I have encountered over the years. Most of these are related to C# but probably apply to more situations. If so, you have my condelances.
Make me search like forever
One of the great things of freedom in structuring classes, codes and files is that you can do things in all kinds of ways. That is exactly the downside as well. You can put things all over the place without any logical connection between them:
  • Put multiple public classes in one file.
  • Name the file different from the class that’s inside.
  • Don’t use the namespace according to the folder the file is in.
  • Name the project different from the name of the assembly.
  • Put in a folder with yet a different name.
  • Cherry on the cake: make it completely different in source control where possible.
I only hope that this covers all possible obscurity.
Taking for granted that we now know every possible option
Let's consider a simple enumeration giving us a choice of direction.
public enum Direction
{
    Left,
    Right
}
Now let's say you are at least clever enough to consider using switch-statement instead of a if-statement to implement this in your code. Still, you have multiple ways to make this work or not.
Suppose you need this in a method that returns something based on the direction.
Good
Be sure to have a default option.
switch (direction)
{
    case Direction.Left:
        return SomethingLeft();
    case Direction.Right:
        return SomethingRight();
    default:
        return null;
}
Better
Maybe returning a null-value is not desired or maybe even SomethingLeft() can return null then it's probably better to throw an exception.
switch (direction)
{
    case Direction.Left:
        return SomethingLeft()
    case Direction.Rightt:
        return SomethingRight();
    default:
        throw new ArgumentOutOfRangeException("direction");
}
Bad
Somepeople think that writing less code is better.
switch (direction)
{
    case Direction.Left:
        return SomethingLeft();
    default:
        return SomethingRight();
}
What now if someone thinks we shoud be able to go 3D?
public enum Direction
{
    Left,
    Right,
    Up,
    Down
}
A right-handed Cartesian coordinate system, il...Image via Wikipedia
Or maybe an old sailor becomes architect and decides to use this:
public enum Direction
{
    East,
    West,
    North,
    South
}
We could do something like global search and replace or a refactor action, but the bad code will probably not do what we want.
Seen all these
I have seen the above examples and have frowned at them. I have improved the code where possible and have given up on trying to educate some of the people who wrote the code.
More examples will follow.
Enhanced by Zemanta

No comments:

Post a Comment

Thanks for you comment. I will probably have to moderate it, so it could take some time to see it appear on the blog, but I am usually quite fast at that.

When I feel that you are commenting just to get some link spam to your own site,you will probably never see it appear ..