Style! Pattern vs double code what is good?
i have a lot of similiar looking small pieces of code. E.g. parsing config files with jdom, coverting the into regex patterns. It's all stuff that is done in 10 lines. Writing some abstract meta-monster that does all this would be very complicated.
Now I always here people crying about double-code. Is this in my usecase really such a bad thing? Having the code similar makes it easy to understand and maintain. There is no big interrelation开发者_Go百科 of functions.
Am I doing the right thing?
Over-engineering is an antipattern. If you don't need abstraction, don't use it. Abstraction and patterns are the most useful when your project is large or is supposed to grow. If it that isn't your situation, then Keep It Simple and Stupid.
It's also a matter of taste. Personally, even if it is sometimes discouraged, I prefer using patterns and abstraction even in simple situations if I feel that it might be useful in the future, because I hate rewriting the same lines of code twice. In addition, design patterns also help you to avoid errors because they put order into your code and class relations.
No, having very similar code makes it hard to maintain, if you've got more than, say, three of those pieces of code. When you catch a bug (or get a specs change) that affects all or several of those pieces of code, you have to try and spot the differences. It may even be harder to fix then when they're all exactly the same.
The least you can do is try to lift out some commonalities and make a tiny library of well-named helper functions. Lifting out the tricky bits is more important than how many lines of code you save.
It really depends on what those 10-lines look like. Some cases that don't seem to warrant a proper abstraction, can be solved with a simple loop.
精彩评论