CS 134 Style Examples

Example 1

Original:

  if (red + blue + green < 240) {
    correct = darks;
  } else if (red + blue + green > 670) {
    correct = whites;
  } else if (red + blue + green >= 240 &&
             red + blue + green <= 670) {
    correct = colors; 
  }

Improvements:

Result:

  int colorValue = red + blue + green;
  if (colorValue < 240) {
    correct = darks;
  } else if (colorValue > 670) {
    correct = whites;
  } else {
    correct = colors; 
  }

Example 2

Original:

  if (ballGrabbed && hoop1.contains(pt)) {
      score = score + 1;
  } else if (ballGrabbed && hoop2.contains(pt)) {
      score = score + 1;
  } else if (ballGrabbed && hoop3.contains(pt)) {
      score = score + 2;
  }

Improvements:

Result:

  if (ballGrabbed) {
    if (hoop1.contains(pt) || hoop2.contains(pt)) {
      score = score + 1;
    } else if (hoop3.contains(pt)) {
      score = score + 2;
    }
  }

Example 3

Original:

  public void onMouseClick(Location point) {
     Color newColor = new Color(pickAColor.nextValue(), 0, 0);
     if (shape.contains(point)) {
        shape.setColor(newColor);
        shape.moveTo(point);
     } else {
        shape.moveTo(point);
     }
  }

Improvements:

Result:

  public void onMouseClick(Location point) {
     if (shape.contains(point)) {
        Color newColor = new Color(pickAColor.nextValue(), 0, 0);
        shape.setColor(newColor);
     } 
     shape.moveTo(point);
  }

Example 4

Original:

  public void begin() {
    colors = new FramedRect(10,10,50,50,canvas);
    whites = new FramedRect(110,10,50,50,canvas);
    darks = new FramedRect(210,10,50,50,canvas);
  }

Improvements:

Result:

  static final int COLORS_X = 10;
  static final int WHITES_X = COLORS_X + 100;
  static final int DARKS_X = DARKS_X + 100;
  static final int BASKET_Y = 10;
  static final int SIZE = 50;

  public void begin() {
    colors = new FramedRect(COLORS_X,BASKET_Y,SIZE,SIZE,canvas);
    whites = new FramedRect(WHITES_X,BASKET_Y,SIZE,SIZE,canvas);
    darks = new FramedRect(DARKS_X,BASKET_Y,SIZE,SIZE,canvas);
  }

Example 5

Original:

  public boolean contains(Location P) {
  if (magnet.contains(P)) {
  return true;
  } else {
  return false;
  }}

Improvements:

Result:

  public boolean contains(Location point) {
    if (magnet.contains(point)) {
      return true;
    } else {
      return false;
    }
  }

Further Improvements:

Result:

  public boolean contains(Location point) {
    return magnet.contains(point);
  }

Example 6

Original:

  public class TShirt {
    // names for instance variables
    private double startX, startY; 
    private FramedRect sleeveTrim, bodyTrim; 
    private FramedOval neckTrim; 
    private FilledRect body, sleeves; 
    private FilledOval neck; 

Improvements:

Result:

  public class TShirt {
 
    // Initial location of the shirt
    private double startX, startY; 

    // Shapes that form a border around the shirt
    private FramedRect sleeveTrim, bodyTrim; 
    private FramedOval neckTrim; 
    
    // Shapes that form the interior color of the shirt
    private FilledRect body, sleeves; 
    private FilledOval neck;