3 ms·
For a long time I've thought that Don't Repeat Yourself is the most important software engineering rule. I still do, but in line with this article, I've learned
by jerf 10y ago
For a long time I've thought that Don't Repeat Yourself is the most important software engineering rule. I still do, but in line with this article, I've learned that some things that appear to be duplication if you just look at the literal code actually aren't. Just because you've got a chunk of ten or twenty lines that are identical right now doesn't mean that they are actually identical.
It's hard to give concrete examples of such an abstract concept, but one I encounter with some frequency is some chunk of code that grabs something from a URL. Assuming you're using some half decent library that takes care of the brute mechanics, it's easy to end up with repetitive code to deal with setting up a form and putting on a couple headers and dealing with authentication or whatever, and it's tempting to try abstract on that yet further. Yet I find all the attempts to do so make the problem worse, because the superficially-similar code is actually quite dissimilar. The pressures on the instances are different. Here I may be deeply concerned about SSL, there I may be adding header hacks for some broken auth scheme, over there I've got some sort of serialization problem. Trying to put them all behind the same abstraction is just pain. It turns out the level of abstraction offered by these often years-old, mature HTTP libraries is more correct than may initially meet the eye.
For code to be "duplicated", the full context of the code need to be duplicated, not just have superficial visual similarities. It's important that all the code with the same context, the same evolutionary pressures, the same likely change planes end up in one place, and it is equally important that code that lacks that similarity not end up bound together. In the end, it's the same error.
- TeMPOraL 10y agoI agree. I follow the DRY principle very strongly, but not on the literal code level - just one level above, on the "what is this doing" level. So in your example, if I see a dozen places where the program is using that URL library to e.g. send requests to get a different dataset from the same API, I'll quickly DRY it into a function. That's because all those places do the same thing - "fetch a dataset named $name from API XYZ". But I'll leave alone all the other places that use the same URL library, even with pretty much identical parameters, if they're doing different things. They may look the same now, but they may start to differ later. And even if they don't, placing them under a common abstraction when they represent different things is basically a lie. I don't like it when the code lies to people.
- thangalin 10y agoThere's a reason why Torvalds said, "Talk is cheap. Show me the code."[1] Quite often when people write about abstract concepts in software that violate well-known principles, they cannot come up with concrete examples that show a good time to break form. Because they are few and far between. Take URLs for an example. > Here I may be deeply concerned about SSL On web pages, instead of codifying "http://" http://" and "https://" https://" everywhere (a form of duplication), it is possible to drop the protocol to use "//"; the browser will fetch subsequent files using the security of the original page. (Mixing secure and insecure content is an architectural mistake.) An application shouldn't be concerned with HTTP vs HTTPS. In Java, that concern is made transparent by HttpURLConnection (and its subclass of HttpsURLConnection), for example. > a chunk of ten or twenty lines that are identical When seemingly identical chunks of code are repeated, they can often be abstracted and parameterized. Lambda expressions help reduce duplicated code snippets. Of course, when I say "parameterized" I don't mean "pass in a metric ton of parameters" as there are techniques for shortening long parameter lists (such as multiple methods and the builder pattern). Lines 70 to 89 are an example of duplicated code[3] (here are four lines, but you get the idea): this.namespacesAware = xmlNode.getAttribute("namespacesaware"); this.prunetags = xmlNode.getAttribute("prunetags"); this.hyphenReplacement = xmlNode.getAttribute("hyphenreplacement"); this.booleanAtts = xmlNode.getAttribute("booleanatts"); These should be a map of properties rather than a volley of String instance variables: private String namespacesAware; private String prunetags; private String hyphenReplacement; private String booleanAtts; It might not seem like much duplication at first, until you look at the code that uses those parameters[4]: final String namespacesAware = BaseTemplater.evaluateToString(elementDef.getNamespacesAware(), null, context); if (namespacesAware != null) { properties.setNamespacesAware(CommonUtil.isBooleanTrue(namespacesAware)); } else { properties.setNamespacesAware(false); } final String hyphenReplacement = BaseTemplater.evaluateToString(elementDef.getHyphenReplacement(), null, context); if (hyphenReplacement != null) { properties.setHyphenReplacementInComment(hyphenReplacement); } final String pruneTags = BaseTemplater.evaluateToString(elementDef.getPrunetags(), null, context); if (pruneTags != null) { properties.setPruneTags(pruneTags); } final String booleanAtts = BaseTemplater.evaluateToString(elementDef.getBooleanAtts(), null, context); if (booleanAtts != null) { properties.setBooleanAttributeValues(booleanAtts); } The entire code block could be reduced to one line of code had properties been used instead of instance variables. (Maybe a loop and two lines of code, but either way the duplication could be easily mitigated.) Without seeing source code examples, it is extraordinarily difficult to identify problems in the code and explain how the code could be improved. Hence the Torvalds quote. > the same abstraction is just pain Sometimes code is poorly designed, not designed for extensibility, or has hard-coded assumptions that should be exposed as properties. Take line 52 of the HtmlToPlainText class[2]: private static final int maxWidth = 80; That one line forced duplicating the entire class, which was otherwise perfect for my needs. But the class did not expose behaviour to change the maximum width of a line. An incorrect assumption was made at the design level: 80 characters ought to be enough. The correct solution would be to make it an attribute and send a pull request to the author. :-) [1]: https://lkml.org/lkml/2000/8/25/132 https://lkml.org/lkml/2000/8/25/132 [2]: http://grepcode.com/file/repo1.maven.org/maven2/org.jsoup/jsoup/1.6.3/org/jsoup/examples/HtmlToPlainText.java http://grepcode.com/file/repo1.maven.org/maven2/org.jsoup/js... [3]: https://sourceforge.net/p/web-harvest/code/HEAD/tree/trunk/webharvest-core/src/main/java/org/webharvest/definition/HtmlToXmlDef.java https://sourceforge.net/p/web-harvest/code/HEAD/tree/trunk/w... [4]: https://sourceforge.net/p/web-harvest/code/HEAD/tree/trunk/webharvest-core/src/main/java/org/webharvest/runtime/processors/HtmlToXmlProcessor.java https://sourceforge.net/p/web-harvest/code/HEAD/tree/trunk/w...
- deleted 10y ago[deleted]
- mavelikara 10y agoI agree. I my mind, DRY really is not about similar looking lines of code. I think of it as an attempt to strive for a concept-to-implementation-ratio of 1.
- dozzie 10y ago> For a long time I've thought that Don't Repeat Yourself is the most important software engineering rule. It's not. "Don't introduce unnecessary dependencies" and "keep dependency graph without cycles", "keep things modular" and "all operations in a unit (function, module, ...) should be from the same abstraction level" are just as important, if not more, but they're a little more high-level, and thus less actionable. DRY is just simple to enforce, that's all to it.
- Rexxar 10y agoI think the concept is formally identical to database normalisation but applied to code. If the code is duplicated and you know the different part will have to evolve the same way: factorize now. If the different part are just formally similar by accident wait a little before factorize. Similarly in a database you create a table for users but you don't create a table for "family names" and "first names" even if you could factorize them : when Cassius Clay become Mohammed Ali you don't want to rename all Cassius to Mohammed and all Clay to Ali.