Thursday, March 25, 2004

Why is code maintenance so difficult?

The more complicated your code base is the less likely other people will be able to understand it. So how do you make it simpler? Well, there is no easy answer to this, even when you try to write understandable code different people with different ideas may find it incomprehensible. That said here are some things to think about when writing code.

Stay away from form inheritance. Languages like Delphi have the concept of form inheritance (creating a base window and then inheriting off it). Although this can be great in the initial creation of applications, allowing you to create many forms quickly, it is hard to maintain. What if there is a button that is no longer needed, and that button lives on the base form. You can not delete the button because that would delete it on every descendant form. So you end up hiding it. Now you have controls that in design time clutter your designing window. The problem gets worse when you need to radically change your window’s visual appearance. Now what do you do? Hide more controls or start over? Well in this case the two options are not ideal. You will end up with more confusing un-maintainable code or you will waist time rewriting the window. I think rewriting it to the way to go. Rewrite it to the way it should have been in the first place, with out inheritance.

Try not to inherit more then three levels deep. Unless your objects are created perfectly and no one will need to maintain it, inheritance is not your friend. When it comes time to extend functionality it becomes very confusing to figure out where the code belongs. Does it belong in the base or at what level? Fixing bugs can also be difficult; trying to follow the call stack as it jumps from one level to the next can be tricky. There are times that each level is very straight forward however most times the lines get blurred and functionality ends up existing at levels it shouldn’t.

Minimize the amount of code files. If you have inherited objects that exist in multiple files think about putting them into one file. If someone need to make changes it is easier because everything is in one place. Organization can go a long way. Another solution is to create a directory that holds common files.

Try not to wrap objects for the sake of making things simpler. Of course there are times that you need to wrap DLL’s or COM objects. However, if you are trying to create easier access to code by writing more code this is probably not a good thing. The less code the better. Unless you have a really good reason to do it, don’t do it.

If you have a frame-work, document it well or better yet buy one. If in your project you try to make thinks easier by writing a frame-work that you can just plug-in objects, this may seem like a good idea, just make sure that you document it well. Everyone must understand it and be able to maintain it. If you can buy a framework you are probably better off. Developers are more likely to adopt a third-party solution then one created in house.

Diagram your software. A picture is worth a thousand words and a code picture is worth a thousand lines of code. Diagrams can make things easier to understand they can show relationships very well, and can go along way in helping others to understand what your code does.

Try to teach others your coding ideas. If no one understands what you are trying to do then know one will be able to maintain your code. Also in the process of documenting or teaching others you may find code that could have been written better. This is a great opportunity to make your code more understandable.

No comments: