Code Bloaters-代码肿胀

Code Bloaters

1. Long Method

1.1 method lines cant longer then ten should make you start asking questions.

# Extract Method

Problem

//You have a code fragment that can be grouped together.
void printOwing() {
  printBanner();

  // Print details.
  System.out.println("name: " + name);
  System.out.println("amount: " + getOutstanding());
}

Solution

//Move this code to a separate new method (or function) and replace the old code with a call to the method.
void printOwing() {
  printBanner();
  printDetails(getOutstanding());
}

void printDetails(double outstanding) {
  System.out.println("name: " + name);
  System.out.println("amount: " + outstanding);
}

# Replace Temp with Query

Problem

//You place the result of an expression in a local variable for later use in your code.
double calculateTotal() {
  double basePrice = quantity * itemPrice;
  if (basePrice > 1000) {
    return basePrice * 0.95;
  }
  else {
    return basePrice * 0.98;
  }
}

Solution

//Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary.
double calculateTotal() {
  if (basePrice() > 1000) {
    return basePrice() * 0.95;
  }
  else {
    return basePrice() * 0.98;
  }
}
double basePrice() {
  return quantity * itemPrice;
}

# Introduce Parameter Object

在这里插入图片描述

# PreServe Whole Object

Problem

//You get several values from an object and then pass them as parameters to a method.
int low = daysTempRange.getLow();
int high = daysTempRange.getHigh();
boolean withinPlan = plan.withinRange(low, high);

Solution

//Instead, try passing the whole object.
boolean withinPlan = plan.withinRange(daysTempRange);

# Replace Method with Method Object

Problem

//You have a long method in which the local variables are so intertwined that you cannot apply Extract Method.
class Order {
  // ...
  public double price() {
    double primaryBasePrice;
    double secondaryBasePrice;
    double tertiaryBasePrice;
    // Perform long computation.
  }
}

Solution

//Transform the method into a separate class so that the local variables become fields of the class. Then you can split the method into several methods within the same class.
class Order {
  // ...
  public double price() {
    return new PriceCalculator(this).compute();
  }
}

class PriceCalculator {
  private double primaryBasePrice;
  private double secondaryBasePrice;
  private double tertiaryBasePrice;
  
  public PriceCalculator(Order order) {
    // Copy relevant information from the
    // order object.
  }
  
  public double compute() {
    // Perform long computation.
  }
}

# Decompose Conditional

Problem

//You have a complex conditional (if-then/else or switch).
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {
  charge = quantity * winterRate + winterServiceCharge;
}
else {
  charge = quantity * summerRate;
}

Solution

//Decompose the complicated parts of the conditional into separate methods: the condition, then and else.
if (isSummer(date)) {
  charge = summerCharge(quantity);
}
else {
  charge = winterCharge(quantity);
}

1.2 write code is easier than read code, so you must let other know what method do

//Using Descriptive method name
public void removeUserInfo()
public void checkUserInfoEligible()

2. Large Class

A class contains many fields/methods/lines of code.

# Extract Class
在这里插入图片描述
*** # Extract Subclass***

Problem
A class has features that are used only in certain cases.
在这里插入图片描述
Solution
Create a subclass and use it in these cases.
在这里插入图片描述

# Extract Interface

Problem
Multiple clients are using the same part of a class interface. Another case: part of the interface in two classes is the same.
在这里插入图片描述

Solution
在这里插入图片描述

# Duplicate Observed Data

Problem
Is domain data stored in classes responsible for the GUI?
在这里插入图片描述

Solution
Then it is a good idea to separate the data into separate classes, ensuring connection and synchronization between the domain class and the GUI.
在这里插入图片描述

3. Primitive Obsession

# Replace type code with class

Problem
A class has a field that contains type code. The values of this type are not used in operator conditions and do not affect the behavior of the program.
在这里插入图片描述

Solution
Create a new class and use its objects instead of the type code values.
在这里插入图片描述

# Replace type code with subclass

Problem
You have a coded type that directly affects program behavior (values of this field trigger various code in conditionals).
在这里插入图片描述
Solution
Create subclasses for each value of the coded type. Then extract the relevant behaviors from the original class to these subclasses. Replace the control flow code with polymorphism.
在这里插入图片描述
# Replace Type Code with State/Strategy

Problem
You have a coded type that affects behavior but you cannot use subclasses to get rid of it.
在这里插入图片描述
Solution
Replace type code with a state object. If it is necessary to replace a field value with type code, another state object is “plugged in”.
在这里插入图片描述

# Replace Array with Object

Problem
You have an array that contains various types of data.

String[] row = new String[2];
row[0] = "Liverpool";
row[1] = "15";

Solution
Replace the array with an object that will have separate fields for each element.

Performance row = new Performance();
row.setName("Liverpool");
row.setWins("15");

4. Long Parameter List

# Replace Parameter with method call

Problem
Before a method call, a second method is run and its result is sent back to the first method as an argument. But the parameter value could have been obtained inside the method being called.

int basePrice = quantity * itemPrice;
double seasonDiscount = this.getSeasonalDiscount();
double fees = this.getFees();
double finalPrice = discountedPrice(basePrice, seasonDiscount, fees);

Solution
Instead of passing the value through a parameter, place the value-getting code inside the method.

int basePrice = quantity * itemPrice;
double finalPrice = discountedPrice(basePrice);

# Preserve Whole Object

Problem
You get several values from an object and then pass them as parameters to a method.

int low = daysTempRange.getLow();
int high = daysTempRange.getHigh();
boolean withinPlan = plan.withinRange(low, high);

Solution
Instead, try passing the whole object.

boolean withinPlan = plan.withinRange(daysTempRange);

# Introduce Parameter Object

Problem
Your methods contain a repeating group of parameters.
在这里插入图片描述

Solution
Replace these parameters with an object.
在这里插入图片描述

5. Data Clumps

pls refer to Extract Class, Introduce Parameter Object, Preserve Whole Object

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值