Yos*_*ale 2 java validation parameters function
当我第一次从c转移到Java时,我认为我已经完成了所有烦人的参数检查,在每个函数的开头.(有福的例外)
最近我意识到我正在慢慢地再次回到那种做法,而我开始对所有这些都感到非常恼火
if (null == a || null == b || null == a.getValue() || ...)
{
return null;
}
Run Code Online (Sandbox Code Playgroud)
例如,我有一个实用程序类,用于分析网页并从中提取特定元素.任何使用null元素调用dom对象函数通常都会导致异常 - 所以在我在这个类中编写的几乎所有函数中都有无数的null检查:
private URL extractUrl(Element element) throws Exception {
if (null == element) {
return null;
} ...
public List<Object> getConcreteElements(String xpath) throws Exception {
if (null == xpath) {
return Collections.emptyList();
}...
public String getElementsAsXML(String xpath) throws Exception {
if (null == xpath) {
return null;
}...
Run Code Online (Sandbox Code Playgroud)
在每个功能的开头.这是我应该习惯的东西,还是有一些我不知道的编码习惯可以简化我的生活?