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 10 of 10

Thread: What is the best way to avoid duplicating code?

  1. #1
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default What is the best way to avoid duplicating code?

    Hi all.

    I have several times encountered this problem, but now I want to get to the bottom of it. Basically, if you have two methods that looks almost completely the same, aside from maybe one line, what is the best way to refactor it into one method? I know that you can use OOP, which is what I tend to do. Let me explain with an example of my problem.

    In my current project I encountered a situation where it would just feel like overkill. I had a method that I needed to change one line in while still having the option to use the old code. But since I hate to duplicate code. So instead of creating two almost identical methods I used an interface. The method would then take a parameter with an object that implemented that interface. That object would then be used on the line to do the part I needed to change yet keep. It looks like this:
        private String getResult(Indexer i) {
            String result = data;
            int index = i.find(data); // THIS IS THE INTERESTING LINE
            if (index > -1) {
                result = data.substring(0, index);
                data = data.substring(index);
                hasMore = !data.isEmpty();
            }
            else {
                hasMore = false;
            }
            return result.trim();
        }
    The line with the comment is the one I am talking about. Anyway, the thing is that this method is only used by one other method. And the interface is only used by this method. So it feels a bit overkill to use this solution, but I didnt know what the best way to solve this would be. Is this the proper way to do it? Or is there a better one?

    What is the best way to avoid duplicating code in cases like this?

    Take care,
    Kerr


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is the best way to avoid duplicating code?

    Could you pass index vs Indexer to the method. i appears to only be used in the one statement setting the value of index.

  3. #3
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: What is the best way to avoid duplicating code?

    Do´h. Now why didnt I think of that, lol?!?!

    Anyway, it was more or less meant as an example. Have had this issue before. The issue with duplicate code, that is. Like when you have two or more methods that are almost exactly the same. What is the best way to deal with those situations?

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: What is the best way to avoid duplicating code?

    Possibly you could add a boolean parameter, if true use new code if false use old code for that line?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: What is the best way to avoid duplicating code?

    Try to use modular approach to avoid duplicating the code. Divide your problem into as many sub modules as you can. Modular approach gives you the reusability of the code.

  6. #6
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: What is the best way to avoid duplicating code?

    Yeah, its just that I have no experience with writing modular code.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: What is the best way to avoid duplicating code?

    Didn't you ever write functions(modules)?

  8. #8
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: What is the best way to avoid duplicating code?

    Are you talking about methods when you say functions? Sorry, sometimes I get stuck on words .

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: What is the best way to avoid duplicating code?

    Quote Originally Posted by Kerr View Post
    Are you talking about methods when you say functions? Sorry, sometimes I get stuck on words .
    Yes, i mean methods!!!!!

  10. #10
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: What is the best way to avoid duplicating code?

    Ok . I use methods, of course. Trying to use them whenever a peace of code is used in more then one place. It is just hard to figure out how to do it (organizing the code and all, like should this method belong in this or that class, should it be static or not, how should I pass data to it, and so on) and I have never really thought of it as dividing the code into modules.

Similar Threads

  1. Duplicating array in C code
    By FearTheCron in forum Java Native Interface
    Replies: 1
    Last Post: April 6th, 2013, 09:33 PM
  2. Duplicating Elements through Recursion
    By vk999 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 01:22 AM
  3. How to avoid the Deadlock in the below program
    By murali1253 in forum Threads
    Replies: 0
    Last Post: April 15th, 2010, 05:35 PM
  4. Develop Superfast Programs - Avoid Threads
    By freespirit in forum The Cafe
    Replies: 2
    Last Post: March 18th, 2010, 01:49 AM
  5. duplicating output
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 3rd, 2010, 01:11 AM