Duplicate Code (2024)

Signs and Symptoms

Two code fragments look almost identical.

Duplicate Code (1)

Reasons for the Problem

Duplication usually occurs when multiple programmers are working on different parts of the same program at the same time. Since they’re working on different tasks, they may be unaware their colleague has already written similar code that could be repurposed for their own needs.

There’s also more subtle duplication, when specific parts of code look different but actually perform the same job. This kind of duplication can be hard to find and fix.

Sometimes duplication is purposeful. When rushing to meet deadlines and the existing code is “almost right” for the job, novice programmers may not be able to resist the temptation of copying and pasting the relevant code. And in some cases, the programmer is simply too lazy to de-clutter.

Treatment

  • If the same code is found in two or more methods in the same class: use Extract Method and place calls for the new method in both places.
    Duplicate Code (2)
  • If the same code is found in two subclasses of the same level:
  • If duplicate code is found in two different classes:
    • If the classes aren’t part of a hierarchy, use Extract Superclass in order to create a single superclass for these classes that maintains all the previous functionality.
    • If it’s difficult or impossible to create a superclass, use Extract Class in one class and use the new component in the other.
  • If a large number of conditional expressions are present and perform the same code (differing only in their conditions), merge these operators into a single condition using Consolidate Conditional Expression and use Extract Method to place the condition in a separate method with an easy-to-understand name.
  • If the same code is performed in all branches of a conditional expression: place the identical code outside of the condition tree by using Consolidate Duplicate Conditional Fragments.

Payoff

  • Merging duplicate code simplifies the structure of your code and makes it shorter.
  • Simplification + shortness = code that’s easier to simplify and cheaper to support.
Duplicate Code (3)

When to Ignore

  • In very rare cases, merging two identical fragments of code can make the code less intuitive and obvious.

Tired of reading?

No wonder, it takes 7hours to read all of the text we have here.

Try our interactive course on refactoring. It offers a less tedious approach to learning new stuff.

Let's see…

Duplicate Code (2024)
Top Articles
How to Reheat Roast Potatoes (Oven, Air Fryer & More!)
Top Gainers Today - NSE Top Gainers Stocks Live, BSE Top Gainers Shares Today
Friskies Tender And Crunchy Recall
Gomoviesmalayalam
Nfr Daysheet
Comforting Nectar Bee Swarm
Black Gelato Strain Allbud
Delectable Birthday Dyes
Ribbit Woodbine
Parks in Wien gesperrt
Does Pappadeaux Pay Weekly
Why Is Stemtox So Expensive
Robert Malone é o inventor da vacina mRNA e está certo sobre vacinação de crianças #boato
8 Ways to Make a Friend Feel Special on Valentine's Day
Kinkos Whittier
Costco Gas Foster City
Radio Aleluya Dialogo Pastoral
Cbs Trade Value Chart Fantasy Football
Craigslist Free Stuff Santa Cruz
Does Breckie Hill Have An Only Fans – Repeat Replay
Roll Out Gutter Extensions Lowe's
Cta Bus Tracker 77
Mccain Agportal
Dallas Craigslist Org Dallas
Evil Dead Rise Showtimes Near Pelican Cinemas
Dtlr Duke St
Inbanithi Age
Directions To Nearest T Mobile Store
Marquette Gas Prices
Milwaukee Nickname Crossword Clue
Margaret Shelton Jeopardy Age
Danielle Moodie-Mills Net Worth
Robotization Deviantart
Myra's Floral Princeton Wv
Watchdocumentaries Gun Mayhem 2
All Things Algebra Unit 3 Homework 2 Answer Key
Metro 72 Hour Extension 2022
Best Weapons For Psyker Darktide
Best Workers Compensation Lawyer Hill & Moin
Keeper Of The Lost Cities Series - Shannon Messenger
Www Usps Com Passport Scheduler
Simnet Jwu
Sound Of Freedom Showtimes Near Amc Mountainside 10
Timothy Warren Cobb Obituary
Europa Universalis 4: Army Composition Guide
Rescare Training Online
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Haunted Mansion Showtimes Near Millstone 14
Tito Jackson, member of beloved pop group the Jackson 5, dies at 70
18443168434
Craigslist Monterrey Ca
The Ultimate Guide To 5 Movierulz. Com: Exploring The World Of Online Movies
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 5846

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.