“Generic” code - part 2 April 23, 2008
Posted by Wille in Software Development.trackback
A previous post raised a misunderstanding for one reader, thinking that I was somehow opposed to reuse.
That could not be further from the truth, I am by nature a “lazy” developer - I don’t like solving the same problem more than once and therefore always strive to refactor into a single unified solution when I come across a similar problem a second time. To me, writing as little code as possible to meet requirements is a virtue.
BUT, what I was railing against in my previous post was people trying to solve problems they don’t have, with “solutions” that have vague responsibilities and roles. To add insult to injury, people often try to justify these vague “solutions” by throwing around the word “generic” (hence the word in the post title).
If there are classes in a codebase to which you cannot clearly and succintly state what its responsibilities are, it is code bloat, I don’t care how much someone tries to say that it is “generic” and solves all manner of non existent problems, it is bloat all the same.
Many a years ago when I was just a young lad coming into development, a senior guy on my then team put me against the wall for some classes I had written and asked me (for each class): “What is it’s responsibility? What does it do?”. At the time I couldn’t answer the question succintly, and we rightly cut the code. Ever since, I have in turn asked the same question of people I have worked with when I have come across classes of dubious nature or purpose, almost always to good effect (either cutting the code, or refactoring to make responsibilities clearer).
As a rule of thumb, never try to solve problems you don’t have. IF (and that is often a big if) you eventually come across the problem you predicted - refactor.
99% of the time you’ll find it easier and a lot less effort to refactor a clean codebase to solve the problems when they come along rather than try to predict things you do not have a clear picture or understanding of.
Obviously some people around you misuse the word generic, as it has a completely different meaning in a software engineering context.
As you say, it is not a good idéa to try to predict and solve problems before they occur. Instead focus should at all times be on delivering business value, which is easier with a robust architecture and nice design and of course, with the use of generics.
Yes Generics is quite a good addition to the Java language (and part of C# since some time).
It’s easy to see both sides. I’ve seen plenty of over-engineered stuff out there, and I have to admit over-engineering is something I’ve been guilty off a couple of times in the past. On the other side of the spectrum or those zillions of occasions I’ve seen people copy ‘n paste stuff, being to lazy to avoid code duplication. I’m not sure what the bigger evil is
The ideal software engineer imho is eager to produce but can take a step back to think what ‘the real’ problem is and fix that instead of always going for the short cuts.