DEV Community

Cover image for Code Smell 46 - Repeated Code
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

2 1

Code Smell 46 - Repeated Code

DRY is our mantra. Teachers tell us to remove duplication. We need to go beyond.

Problems

  • Code Duplication

  • Maintainability

Solutions

  1. Find repeated patterns (not repeated code).

  2. Create an abstraction.

  3. Parametrize abstraction calls.

  4. Use composition and never inheritance.

  5. Unit test new abstraction.

Sample Code

Wrong

<?
class WordProcessor {
function replaceText(string $patternToFind, string $textToReplace) {
$this->text = '<<<' .
str_replace($patternToFind, $textToReplace, $this->text)
. '>>>';
}
}
final class Obfuscator {
function obfuscate(string $patternToFind, string $textToReplace) {
$this->text =
strlower(str_ireplace(
$patternToFind, $textToReplace, $this->text));
}
}
view raw repeated.php hosted with ❤ by GitHub

Right

<?
final class TextReplacer {
function replace(
string $patternToFind,
string $textToReplace,
string $subject,
string $replaceFunctionName,
$postProcessClosure) {
return $postProcessClosure(
$replaceFunctionName($patternToFind,
$textToReplace,
$subject));
}
}
// Lots of tests on text replacer so you can gain confidence.
final class WordProcessor {
function replaceText(string $patternToFind, string $textToReplace) {
$this->text = (new TextReplacer())->replace(
$patternToFind,
$textToReplace,
$this->text,
'str_replace', fn($text) => '<<<' . $text . '>>>');
}
}
final class Obfuscator {
function obfuscate(string $patternToFind, string $textToReplace) {
$this->text = (new TextReplacer())->replace(
$patternToFind,
$textToReplace,
$this->text,
'str_ireplace', fn($text) => strlower($text));
}
}
view raw composed.php hosted with ❤ by GitHub

Detection

Linters can find repeated code.

There are not very good finding similar patterns.

Maybe soon machine learning will help us find such abstractions automatically.

For now, it is up to us, humans.

Tags

  • Duplication

Conclusion

Repeated code is always a smell.

Copying and pasting code is always a shame.

With our refactoring tools, we need to accept the duplication remove challenge trusting our tests as a safety net.

Relations

More info

Don't repeat

Credits

Photo by Sid Balachandran on Unsplash


Copy and paste is a design error.

David Parnas

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay