How to avoid “!= null” statements in Java?
来源:互联网 发布:java基础题目编程题 编辑:程序博客网 时间:2024/05/29 04:34
http://stackoverflow.com/questions/271526/how-to-avoid-null-statements-in-java
This to me sounds like a reasonably common problem that junior to intermediate developers tend to face at some point: they either don't know or don't trust the contracts they are participating in and defensively overcheck for nulls. Additionally, when writing their own code, they tend to rely on returning nulls to indicate something thus requiring the caller to check for nulls.
To put this another way, there are two instances where null checking comes up:
Where null is a valid response in terms of the contract; and
Where it isn't a valid response.
(2) is easy. Either use asserts or let it fail. Asserts are a highly underused Java feature that was added in 1.4. The syntax is:
assert *<condition>*
or
assert *<condition>* : *<object>*
where <object>
's toString() output will be included in the error.
Assert throws an Error (AssertionError) is the condition is not true. By default, Java ignores asserts. You can enable the feature by passing the option -ea to the JVM. Its also more sophisticated than that where you can enable and disable asserts for individual classes and packages. This means that you can validate code with the asserts while developing and testing and disable them in a production environment although my testing has shown next to no performance impact from asserts.
Not using asserts in this case is OK because the code will just fail, which is what will happen if you use asserts. The only difference is that with asserts it might happen sooner, in a more meaningful way and possibly with extra information for you to figure out why it happened if you weren't expecting it.
(1) is a little harder. If you have no control over the code you're calling then you're stuck. If null is a valid response, you have to check for it.
If its code you do have control over however (and this is often the case) then its a different story. Avoid using nulls as a response. With methods that return collections, it's easy: return empty collections or arrays over nulls pretty much all the time.
With non-collections it might be harder. Consider this as an example: if you have this interfaces:
public interface Action {
void doSomething();
}
public interface Parser {
Action findAction(String userInput);
}
where Parser takes raw user input and finds something to do, perhaps if you're implementing a command line interface for something. Now you might make the contract that it returns null if theres no appropriate action. That leads the null checking you'er talking about.
An alternative solution is to never return null and instead do something like this:
public class MyParser implements Parser {
private static Action DO_NOTHING = new Action() {
public void doSomething() { /* do nothing */ }
};
public Action findAction(String userInput) {
// ...
if ( /* we can't find any actions */ ) {
return DO_NOTHING;
}
}
}
Compare:
Parser parser = ParserFactory.getParser();
if (parser == null) {
// now what?
// this would be an example of where null isn't (or shouldn't be) a valid response
}
Action action = parser.getAction(someInput);
if (action == null) {
// do nothing
} else {
action.doSomething();
}
to
ParserFactory.getParser().findAction(someInput).doSomething();
which is a much better design because it leads to more concise code.
- How to avoid “!= null” statements in Java?
- How to avoid Unicode pitfalls in Mojolicious
- How to avoid using() mistake in C#?
- How to avoid Conflict
- Memory leaks in C++ and how to avoid them
- How to avoid the "enumeral mismatch" error in eikmenup.h
- How to avoid memory leaks in iPhone applications
- how to avoid css and js cache in UIWebView
- How to avoid "java.security.InvalidKeyException:illegal Key Size" error
- How to avoid "sudo minicom"
- 3 Good Reasons to Avoid Arrays in Java Interfaces
- How to set NULL to a pointer in Managed C++?
- Memory usage in a 32bit Windows environment (how to avoid ORA-4030 /ORA-12500s
- Bind: Address Already in Use Or How to Avoid this Error when Closing TCP Connections
- [iOS开发站在巨人肩膀上]之How to avoid memory leaks in iPhone applications
- How To Avoid ORA-04030/ORA-12500 In 32 Bit Windows Environment [Video] [ID 373602.1]
- How to detect and avoid memory and resources leaks in .NET applications()
- How to detect and avoid memory and resources leaks in .NET applications
- 电阻
- py2exe 打包 pyqt程序的图片(ico等) 不显示的问题
- native2ascii简介
- 数据库中的select 1,select count(1),order by 1
- __declspec
- How to avoid “!= null” statements in Java?
- “太岁头上动土”的解释
- magento----得到当前用户信息!!!!!!!!
- 趣味理解:三层架构与养猪 -李天平
- 经典算法研究系列:九之续、sift算法的编译与实现
- 意图和意图过滤器Intents and Intent Filters
- Observer樣式應用於Android框架設計
- Android2.1消息应用(Messaging)
- Android SDK 中Android.net.wifi简要分析