Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Is duplicate code always bad?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is duplicate code always bad?

    Hello, over the last days i thought about possible advantages from having duplicate code.

    Lets assume we have two functions that do the same: F1 and F2

    Lets assume that later in time, we want to implement F3, which is very similar to F1 (and F2). If the programmer had deleted F2, he will have to write F3 from scratch, but if he had kept F2, he will have to alter fewer lines of code to implement F3, since some of the code needed was already in F2. Hence it would have been better if the programmer had not deleted F2.

    What do you think about this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,776
    Thanks
    54
    Thanked 2,340 Times in 2,311 Posts

    Default Re: Is duplicate code always bad?

    What would be wrong with copying F1 and pasting it as a starting point for F3?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Is duplicate code always bad?

    Why would you have to write the third from scratch when you still have the first one?

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is duplicate code always bad?

    What if copy paste wasn't available? Would it be worth keeping both?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Is duplicate code always bad?

    Quote Originally Posted by DarkFalz View Post
    What if copy paste wasn't available? Would it be worth keeping both?
    What? Whether copying and pasting are available or not has nothing to do with repeating code.

    Duplicate code is always bad, or at least a sign of a bad design. Why are you trying to argue for it?

    If you want to continue this discussion, I highly suggest coming up with a concrete example with actual code instead of imagining scenarios. What if your space bar falls off? What if your electricity goes out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is duplicate code always bad?

    If two methods are exact copies of each other then there's no reason to keep both. At the very least for legacy reasons one method could simply call the other one. This will make maintenance much easier.

    However, just because two methods appear to take the same inputs and produce the same outputs doesn't necessarily mean they must arrive at the solution the same way. This is a good way to test out new solutions which may work better (i.e. looks cleaner, runs faster, or uses less memory). It's also entirely possible that both methods have their useful inputs. For example:

    Consider the naive primality checker. It's quite inefficient, but for small numbers it's extremely fast. However, for larger numbers it's much quicker to check using a probabilistic primality checker. These methods have a larger overhead so it's not really great to use them with small numbers, but for larger numbers this overhead is significantly smaller than time spent performing useful computation. Different data structures are also a good example (e.g. linked lists vs. array lists).
    Last edited by helloworld922; June 12th, 2012 at 11:41 AM.

Similar Threads

  1. How to make set to allow duplicate values... Please help me!!!
    By JavaKiddo in forum Collections and Generics
    Replies: 9
    Last Post: January 4th, 2012, 08:22 AM
  2. Duplicate elements in 2 Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 01:56 AM
  3. Issue with code. Does not detect duplicate names in file
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 02:13 AM
  4. check duplicate key and value in hashmap
    By starmandell in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 9th, 2011, 04:25 PM
  5. duplicate t:dataTable
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: August 4th, 2010, 11:35 AM