Monday, June 10, 2013

Bitching about poor pattern I see everywhere

So when I started this blog I decided I was going to try and avoid it being about complaining about shitty code that I saw or ruminations on software process, but I am going to break my bitching rule because I wanted to complain about a almost universal pattern I see in web development.

So in .Net you see of lot of inheritance. In WebForms, every page descends from Page and every control descends from Control. In MVC you have a new set of base objects like Controller. One common pattern is to develop your own base class and then put a lot of stuff in it. Now having a common base class for your Controllers, Pages, or whatever makes a lot of sense, but it is overused. You have a situation where any code used in two different Controllers can be dumped into the base class easily so you dump an assortment of code that doesn't need to be there. When you ask what a class does and then name of that class does not give you any idea, well, in my mind, that is a code smell. Now don't get me wrong, these objects are useful and it is valuable to put code into a base object, but it is easy to abuse this and dump tons of unrelated code here. It is far better to generate objects with a bunch of static methods or extended interfaces than to use your base classes as a dumping ground.

In the project I am working on there is legacy WebForms code and new MVC code. A great deal of the legacy code is tightly coupled to Pages and Controls when it simply does not need to be. Now this is an example of over using inheritance which has helped give inheritance a bad name. If a method is not intrinsically attached to a set of data go ahead and put it elsewhere.

Objects exist for Polymorphism, Encapsulation, and Inheritance, and the least of these is Inheritance. So spaketh my mouth!

No comments:

Post a Comment