第九章 简化函数调用

第九章 简化函数调用

重新命名函数

函数的名称未能解释函数的用途

添加参数

某个函数需要从调用端得到更多的信息

移除参数

函数本体不再需要某个参数

将查询函数和修改函数分离

某个函数几返回对象状态值,又修改对象状态

建立两个不同的函数,一个负责查询,另一个负责修改

将查询所得到的结果高速缓存于某个值域中,这样后续的重复查询就可以大大加快速度

令函数携带参数

若干个函数做了类似的工作,但函数本体却包含了不同的值

建立单一函数,以参数表达那些不同的值

以明确的函数取代参数

你有一个函数,其内完全取决于参数值而采取不同的反应

针对该参数的每一个可能值,建立一个函数

void setValue (String name, int value) {
  if (name.equals("height"))
    _height = value;
  if (name.equals("width"))
    _width = value;
  Assert.shouldNeverReachHere();
}

=>

void setHeight(int arg) {
  _height = arg;
}
void setWidth (int arg) {
  _width = arg;
}

保持对象完整

你从某个对象中取出若干值,将他们作为某一次函数调用的参数

改为使用传递整个对象

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

=>

withinPlan = plan.withinRange(daysTempRange());

以函数取代参数

缩减参数列

int basePrice = _quantity * _itemPrice;
discountLevel = getDiscountLevel();
double finalPrice = discountedPrice (basePrice, discountLevel);

=>

int basePrice = _quantity * _itemPrice;
double finalPrice = discountedPrice (basePrice);

引入参数对象

某些参数总是很自然的同时出现

以一个对象取代这些参数

Image.png

移除设置函数

你的class中的某个值域,应该在对象初创时被设值,然后就不再改变

去掉该值域的所有设值函数

class Account {
  private String _id;
  Account (String id) {
    setId(id);
  }
  void setId (String arg) {
    _id = arg;
  }

=>

class Account {
  private final String _id;
  Account (String id) {
    _id = id;
  }

隐藏某个函数

有一个函数从来没被其他class调用

将其修改为private

以「工厂函数」取代「构造函数」

class Employee {
  private int _type;
  static final int ENGINEER = 0;
  static final int SALESMAN = 1;
  static final int MANAGER = 2;
  Employee (int type) {
    _type = type;
}
static Employee create(int type) {
  switch (type) {
    case ENGINEER:
      return new Engineer();
    case SALESMAN:
      return new Salesman();
    case MANAGER:
      return new Manager();
    default:
      throw new IllegalArgumentException("Incorrect type code value");
  }
}

封装「向下转型」动作

某个函数返回的对象,需要由调用者执行「向下转型」的动作

将「向下转型」动作移动到函数中

Object lastReading() {
  return readings.lastElement();
}

=>

Reading lastReading() {
  return (Reading) readings.lastElement();
}

用异常取代错误码

int withdraw(int amount) {
  if (amount > _balance)
    return -1;
  else {
    _balance -= amount;
    return 0;
  }
}

=>

void withdraw(int amount) throws BalanceException {
  if (amount > _balance) throw new BalanceException();
  _balance -= amount;
}

以测试取代异常

面对一个「调用者可以预先加以检查」的条件,你抛出一个异常。

修改调用者,使他们在调用函数之前就检查

double getValueForPeriod (int periodNumber) {
  try {
    return _values[periodNumber];
  } catch (ArrayIndexOutOfBoundsException e) {
    return 0;
  }
}

=>

double getValueForPeriod (int periodNumber) {
  if (periodNumber >= _values.length) return 0;
  return _values[periodNumber];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值