Quotes
Martin Fowler, Kent Beck:
Number one in the stink parade is duplicated code.
Robert C. Martin:
Duplicate code is the root of all evil in software design.
Duplicate code always represents a missing abstraction.
A quite different opinion from dzone:
Copy and paste programming isn’t right or wrong. It’s a tool that can be used properly, or abused.
Hm, things like that you can say about everything ...
Anonymous developer:
No matter how it looks, main thing is that it works.
This is the most negative statement about software development I've ever heard. We should write code for humans, not machines.
The current web considers copy & paste programming to be
StackOverflow abuse.
There seems to be a problem with IPhone app programmers ...
but their reduced keyboard with just Ctrl C
and V
is a hit:-)
Phrases
Agile principle:
DRY ("Don't Repeat Yourself")
From c2 Wiki:
Once-and-only-once
About
It's about having single responsibilities. Small methods that do just one thing, without side-effects. Small classes that have just one responsibility. This goes well with separation of concerns.
But it's also about service monopolies, having just one utility that sorts a list, just one that searches collection elements, just one that delivers the current date/time, and so on. Freedom of choice in source code is counterproductive, who needs five different sort algorithms, seven different loop syntaxes, fifty ways to exit an application?
Of course it is about to not duplicate bugs. If you have 20 copy & paste occurrences of code that contained a bug, you have 20 bugs, but you may notice just a few of them, because most are in rarely used application areas. As soon as you fix the known bugs, their code can not be associated any more with its siblings by copy & paste detection tools. Repeated boring efforts. It's really evil, like a virus.
Avoiding duplications is writing professional source code, programming in a concise way. That application will be very easy to maintain, extend, and fix. Nevertheless it is more work to do, and lasts significantly longer than to copy & paste together a stack-overflow app. You can compare it with adhering to speed limit. Code duplication is the most widespread software weakness because most people consider it a trivial offense to exceed the speed limit.
It's about software quality!
DRY is Hard
I have seen skilled programmers copy code without hesitation. Asking them I was told that "Java doesn't support DRY programming". I think Java has everything for DRY, but you must think about it. It takes time, experience and patience, and may look tricky in the end. I do not know a language where code reusage is easy. For sure you won't achieve DRY by "write-less-do-more" languages. You just make your copy & paste sections shorter when using lambdas.
Understanding object-oriented principles is absolutely necessary to achieve DRY. I mean class inheritance, and object-delegation for multiple inheritance. But this is only the beginning of the reuse journey. Next comes the "Factory Method" design pattern to achieve reusability of whole class graphs. The functional features added to Java 8 are another dimension of reusage capabilities. I mean passing methods around like objects. Refactor, refactor, refactor is the way. 30% of software production efforts.
Related
In my next Blog I will give an example of Java code that is not so easy to refactor to DRY, and how I would do it.
Worth reading:
Keine Kommentare:
Kommentar veröffentlichen