Welcome to Dream.In.Code
Become a Java Expert!

Join 150,429 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,083 people online right now. Registration is fast and FREE... Join Now!




How to avoid duplicate code?

 
Reply to this topicStart new topic

How to avoid duplicate code?

C:/Syntax
17 Apr, 2008 - 09:59 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 23



Thanked: 1 times
My Contributions
I made a picture of Mondrian. Now I have 3 buttons, going to three methods. In those 3 methods, the picture of Mondrian must change to different colors.
Lets say in One method I would have to change one box that is blue to green, and so on the other boxes.
My teacher took of one point for duplicate code in the the methods (9/10).

MY CODE:
CODE
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* Class MondrianPictureOnly - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class MondrianPictureOnly extends JApplet
  implements ActionListener
{
  private JButton choice1Button, choice2Button, choice3Button, intialButton;
  private Canvas drawSpace;
  private JPanel choicePanel;
  public void init()
  {
    choice1Button = new JButton("Choice One");
    choice2Button= new JButton("Choice Two");
    choice3Button = new JButton("Choice Three");
    intialButton = new JButton("Click here First");
        intialButton.addActionListener(this);
        choice1Button.addActionListener(this);
        choice2Button.addActionListener(this);
        choice3Button.addActionListener(this);
    
    Container window = getContentPane();//window
    //window.setLayout( new FlowLayout());
    BorderLayout layout = new BorderLayout();
    
    choicePanel = new JPanel();
    choicePanel.setLayout(new FlowLayout());
    choicePanel.add(intialButton);
    choicePanel.add(choice1Button);
    choicePanel.add(choice2Button);
    choicePanel.add(choice3Button);
    
    window.add(choicePanel, BorderLayout.NORTH);
    drawSpace = new Canvas ();
    drawSpace.setBackground(Color.black);
    window.add(drawSpace, BorderLayout.CENTER);  
  }
  public void actionPerformed( ActionEvent ae){
    
    if(ae.getSource() == intialButton){
      doIntial();
    }else if(ae.getSource() == choice1Button){
      doFirst();
    }else if(ae.getSource() == choice2Button){
      doSecond();
    }else if(ae.getSource() == choice3Button){
      doThird();
    }
  }
  
  public void doIntial()
  {
    Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.white);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.blue);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.white);
    g.fillRect(493,420,20,60);
    g.setColor(Color.red);
    g.fillRect(150,50,363,359);
    //(above)make the background(window) black, while boxes yellow
  }
  public void doFirst()
  {
          Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.blue);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.yellow);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.white);
    g.fillRect(493,420,20,60);
    g.setColor(Color.red);
    g.fillRect(150,50,363,359);
    
  }
  public void doSecond()
  {
          Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.green);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.blue);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.white);
    g.fillRect(493,420,20,60);
    g.setColor(Color.magenta);
    g.fillRect(150,50,363,359);
    
  }
  public void doThird()
  {
          Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.white);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.yellow);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.magenta);
    g.fillRect(493,420,20,60);
    g.setColor(Color.red);
    g.fillRect(150,50,363,359);
  }
  
  private static int stringToInt(String strObject){
    return Integer.parseInt(strObject.trim());
  }
}


This is what he means by duplicate code:
CODE
  public void doFirst()
  {
          Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.blue);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.yellow);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.white);
    g.fillRect(493,420,20,60);
    g.setColor(Color.red);
    g.fillRect(150,50,363,359);
    
  }
  public void doSecond()
  {
          Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.green);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.blue);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.white);
    g.fillRect(493,420,20,60);
    g.setColor(Color.magenta);
    g.fillRect(150,50,363,359);
    
  }
  public void doThird()
  {
          Container window = getContentPane();//window
    Graphics g = drawSpace.getGraphics();
    drawSpace.update(g);
    window.setBackground(Color.black);
    g.fillRect(25,50,488,504);
    g.setColor(Color.white);
    g.fillRect(25,50,114,142);
    g.fillRect(25,214,114,193);
    g.setColor(Color.yellow);
    g.fillRect(25,421,116,133);
    g.setColor(Color.white);
    g.fillRect(153,420,334,134);
    g.setColor(Color.yellow);
    g.fillRect(495,495,18,59);
    g.setColor(Color.magenta);
    g.fillRect(493,420,20,60);
    g.setColor(Color.red);
    g.fillRect(150,50,363,359);
  }

User is offlineProfile CardPM
+Quote Post

hallizh
RE: How To Avoid Duplicate Code?
17 Apr, 2008 - 10:16 AM
Post #2

New D.I.C Head
*

Joined: 9 Mar, 2008
Posts: 37


My Contributions
Here ya go biggrin.gif

java

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* Class MondrianPictureOnly - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class MondrianPictureOnly extends JApplet
implements ActionListener
{
private JButton choice1Button, choice2Button, choice3Button, intialButton;
private Canvas drawSpace;
private JPanel choicePanel;
Graphics g;
public void init()

{

choice1Button = new JButton("Choice One");
choice2Button= new JButton("Choice Two");
choice3Button = new JButton("Choice Three");
intialButton = new JButton("Click here First");
intialButton.addActionListener(this);
choice1Button.addActionListener(this);
choice2Button.addActionListener(this);
choice3Button.addActionListener(this);

Container window = getContentPane();//window
//window.setLayout( new FlowLayout());
BorderLayout layout = new BorderLayout();

choicePanel = new JPanel();
choicePanel.setLayout(new FlowLayout());
choicePanel.add(intialButton);
choicePanel.add(choice1Button);
choicePanel.add(choice2Button);
choicePanel.add(choice3Button);

window.add(choicePanel, BorderLayout.NORTH);
drawSpace = new Canvas ();
drawSpace.setBackground(Color.black);
window.add(drawSpace, BorderLayout.CENTER);
}
public void actionPerformed( ActionEvent ae){

if(ae.getSource() == intialButton){
doEmAll(Color.white, Color.blue, Color.white, Color.yellow, Color.white, Color.red);
}else if(ae.getSource() == choice1Button){
doEmAll(Color.blue, Color.yellow, Color.white, Color.yellow, Color.white, Color.red);
}else if(ae.getSource() == choice2Button){
doEmAll(Color.green, Color.blue, Color.white, Color.yellow, Color.white, Color.magenta);
}else if(ae.getSource() == choice3Button){
doEmAll(Color.white, Color.yellow, Color.white, Color.yellow, Color.magenta, Color.red);
}
}

public void doEmAll(Color A, Color B, Color C, Color D, Color E, Color F)
{
Container window = getContentPane();//window
g = drawSpace.getGraphics();
drawSpace.update(g);
window.setBackground(Color.black);
g.fillRect(25,50,488,504);
g.setColor(A);
g.fillRect(25,50,114,142);
g.fillRect(25,214,114,193);
g.setColor(cool.gif;
g.fillRect(25,421,116,133);
g.setColor©;
g.fillRect(153,420,334,134);
g.setColor(D);
g.fillRect(495,495,18,59);
g.setColor(E);
g.fillRect(493,420,20,60);
g.setColor(F);
g.fillRect(150,50,363,359);
}

private static int stringToInt(String strObject){
return Integer.parseInt(strObject.trim());
}
}


Could be a code to minimize it even further, not sure how though smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: How To Avoid Duplicate Code?
17 Apr, 2008 - 10:18 AM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(C:/Syntax @ 17 Apr, 2008 - 10:59 AM) *

I made a picture of Mondrian. Now I have 3 buttons, going to three methods. In those 3 methods, the picture of Mondrian must change to different colors.
Lets say in One method I would have to change one box that is blue to green, and so on the other boxes.
My teacher took of one point for duplicate code in the the methods (9/10).


I guess he's right an probably would have like something like that:

java

public void actionPerformed( ActionEvent ae){

if(ae.getSource() == intialButton){
doAny(Color.WHITE, Color.BLUE, Color.YELLOW, Color.RED);
}else if(ae.getSource() == choice1Button){
doAny(Color.BLUE, Color.YELLOW, Color.WHITE, Color.RED);
}else if(ae.getSource() == choice2Button){
doAny(Color.GREEN, Color.BLUE, Color.WHITE, Color.MAGENTA);
}else if(ae.getSource() == choice3Button){
doAny(Color.WHITE, Color.YELLOW, Color.MAGENTA, Color.RED);
}
}

public void doAny(Color c1, Color c2, Color c3, Color c4)
{
Container window = getContentPane();//window
Graphics g = drawSpace.getGraphics();
drawSpace.update(g);
window.setBackground(Color.black);
g.fillRect(25,50,488,504);
g.setColor(c1);
g.fillRect(25,50,114,142);
g.fillRect(25,214,114,193);
g.setColor(c2);
g.fillRect(25,421,116,133);
g.setColor(c1);
g.fillRect(153,420,334,134);
g.setColor(c3);
g.fillRect(495,495,18,59);
g.setColor(c2);
g.fillRect(493,420,20,60);
g.setColor(c4);
g.fillRect(150,50,363,359);
//(above)make the background(window) black, while boxes yellow
}


I haven't test it though so may be I missed a color somewhere.
User is online!Profile CardPM
+Quote Post

Martyr2
RE: How To Avoid Duplicate Code?
17 Apr, 2008 - 10:21 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
To build on what hallizh is showing you, it is the process known as "refactoring" and it is a trick you will want to learn well because you will need to use it a lot. What you do is first identify a few places that look like they are doing almost the exact thing (maybe with a few minor variations) like your three function. Then you create a function which basically abstracts it, like a template where the slight variations would be the variables you pass in. Like the the only difference between your three functions is the color you use for setColor. So you would pass in the colors to the function and replace the variations with your variables. Now everywhere you use one of the three functions you would just call one function instead.

What this does is reduces 30 lines that are roughly the same and boils it down into one function that is 10 lines and may variate ever so slightly.

It reduces lines, it reduces complexity, it increases speed, and it makes it easier to maintain because now you just need to change one line in one function instead of three exact same lines in three different functions.

smile.gif
User is offlineProfile CardPM
+Quote Post

m2s87
RE: How To Avoid Duplicate Code?
17 Apr, 2008 - 11:24 AM
Post #5

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 3 times
Dream Kudos: 1225
My Contributions
Irw, guess i made a bug of some kind, when writing a fix. Anyhow i wanted to show how code could be designed if there where a lot more then 4 buttons to generate different images.

CODE

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
* Class MondrianPictureOnly - write a description of the class here
*/
public class MondrianPictureOnly extends JApplet implements ActionListener {

    private static final long serialVersionUID = 4521234837503565212L;

    public class exButton extends JButton {
        private static final long serialVersionUID = 4676425712555477294L;
        private Color[] _colors;

        public exButton(String name, Color[] colors) {
            super(name);
            this._colors = colors;
        }

        public Color[] get_colors() {
            return _colors;
        }

        public void set_colors(Color[] _colors) {
            this._colors = _colors;
        }
    }

    private ArrayList<exButton> buttons;
    private ArrayList<Rectangle> rects;
    private Canvas drawSpace;
    private JPanel choicePanel;
    private Container window;

    public void init() {
        buttons = new ArrayList<exButton>();
        rects = new ArrayList<Rectangle>();
        ArrayList<Color[]> C = new ArrayList<Color[]>();
        String[] msgs = { "Choice One", "Choice Two", "Choice Three",
                "Click here First" };

        C.add(new Color[] { Color.BLUE, Color.YELLOW, Color.WHITE,
                Color.YELLOW, Color.WHITE, Color.RED });
        C.add(new Color[] { Color.GREEN, Color.BLUE, Color.WHITE, Color.YELLOW,
                Color.WHITE, Color.MAGENTA });
        C.add(new Color[] { Color.WHITE, Color.YELLOW, Color.WHITE,
                Color.YELLOW, Color.MAGENTA, Color.RED });
        C.add(new Color[] { Color.WHITE, Color.BLUE, Color.WHITE, Color.YELLOW,
                Color.WHITE, Color.RED });

        this.rects.add(new Rectangle(25, 50, 488, 504));
        this.rects.add(new Rectangle(25, 50, 114, 142));
        this.rects.add(new Rectangle(25, 214, 114, 193));
        this.rects.add(new Rectangle(25, 421, 116, 133));
        this.rects.add(new Rectangle(153, 420, 334, 134));
        this.rects.add(new Rectangle(495, 495, 18, 59));
        this.rects.add(new Rectangle(493, 420, 20, 60));
        this.rects.add(new Rectangle(150, 50, 363, 359));

        int i = 0;
        for (String msg : msgs) {
            buttons.add(new exButton(msg, C.get(i++)));
        }

        choicePanel = new JPanel();
        choicePanel.setLayout(new FlowLayout());
        for (JButton B : buttons) {
            B.addActionListener(this);
            choicePanel.add(B);
        }

        drawSpace = new Canvas();
        drawSpace.setBackground(Color.black);

        window = getContentPane();
        window.add(choicePanel, BorderLayout.NORTH);
        window.add(drawSpace, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent ae) {
        Object x = ae.getSource();
        if (x instanceof exButton) {
            doPaint(((exButton) x).get_colors());
        }
    }

    public void doPaint(Color[] c) {
        int i = 0, j = 0;
        Graphics g = drawSpace.getGraphics();
        drawSpace.update(g);
        window.setBackground(Color.black);

        doPaintRect(g, i++);
        g.setColor(c[j++]);
        doPaintRect(g, i++);
        doPaintRect(g, i++);
        g.setColor(c[j++]);
        doPaintRect(g, i++);
        g.setColor(c[j++]);
        doPaintRect(g, i++);
        g.setColor(c[j++]);
        doPaintRect(g, i++);
        doPaintRect(g, i++);
        g.setColor(c[j++]);
        doPaintRect(g, i++);
        g.setColor(c[j++]);
        doPaintRect(g, i++);
    }

    public void doPaintRect(Graphics g, int index) {
        Rectangle x = this.rects.get(index);
        g.fillRect(x.x, x.y, x.width, x.height);
    }
}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: How To Avoid Duplicate Code?
17 Apr, 2008 - 11:47 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,291



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Argree with prior. I just like to refactor. tongue.gif

Here's another attack. I took the colorizing from pbl, blame him. wink2.gif

I don't really like lots of large scope variables floating about, so I tend to stomp them. Here we make the button itself responsible for holding it's color data. We can even go so far as to make it do our drawing. This means we don't have to test the button, just check it's class and call it's method.

java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MondrianPictureOnly extends JApplet implements ActionListener {
private Canvas drawSpace;

private class ColorButton extends JButton {
private Color c1, c2, c3, c4;
public ColorButton(String buttonName, Color c1, Color c2, Color c3, Color c4) {
super(buttonName);
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
this.c4 = c4;
}
public void draw(Graphics g) {
g.setColor(Color.black);
g.fillRect(25,50,488,504);
g.setColor(c1);
g.fillRect(25,50,114,142);
g.fillRect(25,214,114,193);
g.setColor(c2);
g.fillRect(25,421,116,133);
g.setColor(c1);
g.fillRect(153,420,334,134);
g.setColor(c3);
g.fillRect(495,495,18,59);
g.setColor(c2);
g.fillRect(493,420,20,60);
g.setColor(c4);
g.fillRect(150,50,363,359);
}
}

private JButton initButton(String buttonName, Color c1, Color c2, Color c3, Color c4) {
JButton button = new ColorButton(buttonName, c1, c2, c3, c4);
button.addActionListener(this);
return button;
}

public void init() {
JPanel choicePanel = new JPanel();
choicePanel.setLayout(new FlowLayout());
choicePanel.add(initButton("Click here First", Color.WHITE, Color.BLUE, Color.YELLOW, Color.RED));
choicePanel.add(initButton("Choice One", Color.BLUE, Color.YELLOW, Color.WHITE, Color.RED));
choicePanel.add(initButton("Choice Two", Color.GREEN, Color.BLUE, Color.WHITE, Color.MAGENTA));
choicePanel.add(initButton("Choice Three", Color.WHITE, Color.YELLOW, Color.MAGENTA, Color.RED));

Container window = getContentPane();
window.setBackground(Color.black);
window.add(choicePanel, BorderLayout.NORTH);
drawSpace = new Canvas ();
window.add(drawSpace, BorderLayout.CENTER);
}

public void actionPerformed( ActionEvent action) {
Object src = action.getSource();
if (src instanceof ColorButton) {
Graphics g = drawSpace.getGraphics();
drawSpace.update(g);
((ColorButton)src).draw(g);
}
}
}


Hope this helps / makes sense.

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month