码迷,mamicode.com
首页 > 其他好文 > 详细

Clean Code 读书笔记五

时间:2015-06-20 09:12:51      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:exception   handle   

Use Exception Rather than Return Code(使用异常而不是错误处理)

public class DeviceController {...
    public void sendShutDown() {
        DeviceHandle handle = getHandle(DEV1);
        // Check the state of the device
        if (handle != DeviceHandle.INVALID) {
            // Save the device status to the record field
            retrieveDeviceRecord(handle);
            // If not suspended, shut down
            if (record.getStatus() != DEVICE_SUSPENDED) {
                pauseDevice(handle);
                clearDeviceWorkQueue(handle);
                closeDevice(handle);
            } else {
                logger.log("Device suspended. Unable to shut down");
            }
        } else {
            logger.log("Invalid handle for: " + DEV1.toString());
        }
    }...
}

重构后:

public class DeviceController {...
    public void sendShutDown() {
        try {
            tryToShutDown();
        } catch (DeviceShutDownError e) {
            logger.log(e);
        }
    }
    private void tryToShutDown() throws DeviceShutDownError {
        DeviceHandle handle = getHandle(DEV1);
        DeviceRecord record = retrieveDeviceRecord(handle);
        pauseDevice(handle);
        clearDeviceWorkQueue(handle);
        closeDevice(handle);
    }
    private DeviceHandle getHandle(DeviceID id) {...
        throw new DeviceShutDownError("Invalid handle for: " + id.toString());...
    }...
}

使用unchecked exception? checked exception?

什么是unchecked exception :unchecked exceptions (RuntimeException, Error, and their subclasses),这种异常不用手动去捕获或在方法声明抛出(不会有编译错误)。
RuntimeException 以及它的子类都是unchecked exception ,下面看看 oracle对RuntimeException 的定义,有助于了解unchecked exception:

Runtime exceptions represent problems that are the result of a
programming problem, and as such, the API client code cannot
reasonably be expected to recover from them or to handle them in any
way. Such problems include arithmetic exceptions, such as dividing by
zero; pointer exceptions, such as trying to access an object through a
null reference; and indexing exceptions, such as attempting to access
an array element through an index that is too large or too small.

…………………..

Here’s the bottom line guideline:
If a client can reasonably be
expected to recover from an exception, make it a checked exception. If
a client cannot do anything to recover from the exception, make it an
unchecked exception.

https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

unchecked exception 不用强制去捕获 或者在方法处声明,如果系统底层代码中要抛出一个checked exception,编译器会强制要求每个调用了该方法的方法去捕获或者声明抛出。这违法了OCP原则(对修改关闭,对扩展开放),也就是会耦合过重。所以作者建议用Unchecked exception,不会强制我们去处理。

但是,关于使用checked exception 还是unchecked exception 网上看来没有定论!oracle 官网说,unchecked exception 用于不可恢复或者无法预期的错误、异常。 checked exception则相反。

其实我也很疑惑:
1.我倾向于 没有特殊情况使用 checked exception .因为oracle定义 “unchecked exception 用于不可恢复或者无法预期的错误、异常”
2.然而 checked exception 确实违法了 OCP原则,(如果系统底层代码中要抛出一个checked exception,编译器会强制要求每个调用了该方法的方法去捕获或者声明抛出。)

我还要在想想………….

Don’t Return Null

Don’t Pass Null

代码中与其返回Null,接着去if判断.不如 不要返回Null,这样减少了NPE的可能
作者建议的替换方案:

1.用特例模式处理。可以返回一个空对象(比如 返回类型为List ,可以用Collections.emptyList()代替)
2.抛出异常

Clean Code 读书笔记五

标签:exception   handle   

原文地址:http://blog.csdn.net/lemon89/article/details/46566057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!