Clean Code 读书笔记八

来源:互联网 发布:什么是旋转变换矩阵 编辑:程序博客网 时间:2024/04/28 20:32

简单设计四规则:

• Runs all the tests
• Contains no duplication
• Expresses the intent of the programmer
• Minimizes the number of classes and methods
(作者认为的金钥匙)

运行所有测试

低耦合 高内聚 造成的结果就是会有更多的类与方法。我们就更易编写独立的单元测试。

不可重复

重复不仅仅指重复的代码,当然还有其他形式的存在。比如

int size() {}boolean isEmpty() {}

可以分别实现,也可以:

boolean isEmpty() {return 0 == size();}

再看看这个,不要放过少量的重复:

public void scaleToOneDimension(float desiredDimension, float imageDimension) {    if (Math.abs(desiredDimension - imageDimension) < errorThreshold) return;    float scalingFactor = desiredDimension / imageDimension;    scalingFactor = (float)(Math.floor(scalingFactor * 100) * 0.01f);    RenderedOp newImage = ImageUtilities.getScaledImage(    image, scalingFactor, scalingFactor);    image.dispose();    System.gc();    image = newImage;}public synchronized void rotate(int degrees) {    RenderedOp newImage = ImageUtilities.getRotatedImage(    image, degrees);    image.dispose();    System.gc();    image = newImage;}

重构后:

    public void scaleToOneDimension(float desiredDimension, float imageDimension) {        if (Math.abs(desiredDimension - imageDimension) < errorThreshold)            return;        float scalingFactor = desiredDimension / imageDimension;        scalingFactor = (float) (Math.floor(scalingFactor * 100) * 0.01f);        replaceImage(ImageUtilities.getScaledImage(image, scalingFactor,                scalingFactor));    }    public synchronized void rotate(int degrees) {        replaceImage(ImageUtilities.getRotatedImage(image, degrees));    }    private void replaceImage(RenderedOp newImage) {        image.dispose();        System.gc();        image = newImage;    }

实现大规模复用,必须从理解小规模复用开始。
- 使用模版模式TEMPLATE METHOD pattern 消除重复:

public class VacationPolicy {    public void accrueUSDivisionVacation() {        // code to calculate vacation based on hours worked to date        // ...        // code to ensure vacation meets US minimums        // ...        // code to apply vaction to payroll record        // ...    }    public void accrueEUDivisionVacation() {        // code to calculate vacation based on hours worked to date        // ...        // code to ensure vacation meets EU minimums        // ...        // code to apply vaction to payroll record        // ...    }}

用一个抽象方法去实现公用的代码,并再各自的继承类中实现各自不同的代码:

abstract public class VacationPolicy {    public void accrueVacation() {        calculateBaseVacationHours();        alterForLegalMinimums();        applyToPayroll();    }    private void calculateBaseVacationHours() { /* ... */    };    abstract protected void alterForLegalMinimums();    private void applyToPayroll() { /* ... */    };}public class USVacationPolicy extends VacationPolicy {    @Override    protected void alterForLegalMinimums() {        // US specific logic    }}public class EUVacationPolicy extends VacationPolicy {    @Override    protected void alterForLegalMinimums() {        // EU specific logic    }}
0 0