第五章 重新组织你的函数

第五章 重新组织你的函数

提炼函数

void printOwing(double amount) {
  printBanner();
  //print details
  System.out.println ("name:" + _name);
  System.out.println ("amount" + amount);
}

=>

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

函数内联化

int getRating() {
  return (moreThanFiveLateDeliveries()) ? 2 : 1;
}
boolean moreThanFiveLateDeliveries() {
  return _numberOfLateDeliveries > 5;
}

=>

int getRating() {
  return (_numberOfLateDeliveries > 5) ? 2 : 1;
}

临时变量内联化

如果有一个临时变量只被表达式赋值一次,应该替换掉。

double basePrice = anOrder.basePrice();
return (basePrice > 1000)

=>

return (anOrder.basePrice() > 1000)

以查询取代临时变量

局部变量会使代码难以被提取,尽可能替换成查询式

double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
  return basePrice * 0.95;
else
  return basePrice * 0.98;

=>

if (basePrice() > 1000)
  return basePrice() * 0.95;
else
  return basePrice() * 0.98;
...
double basePrice() {
  return _quantity * _itemPrice;
}

引入解释性变量

if ( (platform.toUpperCase().indexOf("MAC") > -1) && 
    (browser.toUpperCase().indexOf("IE") > -1) &&
    wasInitialized() && resize > 0 )
{
  // do something
}

=>

final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
final boolean wasResized = resize > 0;
if (isMacOs && isIEBrowser && wasInitialized() && wasResized) 
{
  // do something
}

分解临时变量

每次赋值创建一个独立的对应的临时变量

double temp = 2 * (_height + _width);
System.out.println (temp);
temp = _height * _width;
System.out.println (temp);

=>

final double perimeter = 2 * (_height + _width);
System.out.println (perimeter);
final double area = _height * _width;
System.out.println (area);

移除对参数的赋值动作

int discount (int inputVal, int quantity, int yearToDate) {
  if (inputVal > 50) inputVal -= 2;

=>

int discount (int inputVal, int quantity, int yearToDate) {
  int result = inputVal;
  if (inputVal > 50) result -= 2;

以函数对象取代函数

class Order...
double price() {
  double primaryBasePrice;
  double secondaryBasePrice;
  double tertiaryBasePrice;
  // long computation;
  ...
}

替换你的算法

String foundPerson(String[] people){
  for (int i = 0; i < people.length; i++) {
    if (people[i].equals ("Don")){
      return "Don";
    }
    if (people[i].equals ("John")){
      return "John";
    }
    if (people[i].equals ("Kent")){
      return "Kent";
    }
  }
  return "";
}

=>

String foundPerson(String[] people){
  List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});
  for (int i=0; i<people.length; i++)
    if (candidates.contains(people[i]))
    return people[i];
  return "";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值