AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

別再用 “! = null” 做判空了!

來源: 責編: 時間:2023-09-18 21:41:59 320觀看
導讀問題為了避免空指針調用,我們經常會看到這樣的語句:...if (someobject != null) { someobject.doCalc();}...最終,項目中會存在大量判空代碼,丑陋繁雜。。。如何避免這種情況?是否濫用了判空?精華回答這是初、中級程序猿

問題

為了避免空指針調用,我們經常會看到這樣的語句:JV728資訊網——每日最新資訊28at.com

...if (someobject != null) {    someobject.doCalc();}...

JV728資訊網——每日最新資訊28at.com

最終,項目中會存在大量判空代碼,丑陋繁雜。。。如何避免這種情況?是否濫用了判空?JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

精華回答

JV728資訊網——每日最新資訊28at.com

這是初、中級程序猿經常會遇到的問題。他們總喜歡在方法中返回null,因此,在調用這些方法時,也不得不去判空。另外,也許受此習慣影響,他們總潛意識地認為,所有的返回都是不可信任的,為了保護自己程序,就加了大量的判空。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

吐槽完畢,回到這個題目本身:JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

進行判空前,請區分以下兩種情況:JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

  1. null 是一個有效有意義的返回值(Where null is a valid response in terms of the contract; and)
  2. null是無效有誤的(Where it isn't a valid response.)

JV728資訊網——每日最新資訊28at.com

你可能還不明白這兩句話的意思,不急,繼續往下看,接下來將詳細討論這兩種情況JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

先說第2種情況

JV728資訊網——每日最新資訊28at.com

null就是一個不合理的參數,就應該明確地中斷程序,往外拋錯誤。這種情況常見于api方法。例如你開發了一個接口,id是一個必選的參數,如果調用方沒傳這個參數給你,當然不行。你要感知到這個情況,告訴調用方“嘿,哥們,你傳個null給我做甚"。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

相對于判空語句,更好的檢查方式有兩個:JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

  • assert語句,你可以把錯誤原因放到assert的參數中,這樣不僅能保護你的程序不往下走,而且還能把錯誤原因返回給調用方,豈不是一舉兩得。(原文介紹了assert的使用,這里省略)
  • 也可以直接拋出空指針異常。上面說了,此時null是個不合理的參數,有問題就是有問題,就應該大大方方往外拋。

JV728資訊網——每日最新資訊28at.com

第1種情況會更復雜一些。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

這種情況下,null是個”看上去“合理的值,例如,我查詢數據庫,某個查詢條件下,就是沒有對應值,此時null算是表達了“空”的概念。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

這里給一些實踐建議:

JV728資訊網——每日最新資訊28at.com

假如方法的返回類型是collections,當返回結果是空時,你可以返回一個空的collections(empty list),而不要返回null,這樣調用側就能大膽地處理這個返回,例如調用側拿到返回后,可以直接print list.size(),又無需擔心空指針問題。(什么?想調用這個方法時,不記得之前實現該方法有沒按照這個原則?所以說,代碼習慣很重要!如果你養成習慣,都是這樣寫代碼(返回空collections而不返回null),你調用自己寫的方法時,就能大膽地忽略判空)JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

返回類型不是collections,又怎么辦呢?JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

那就返回一個空對象(而非null對象),下面舉個“栗子”,假設有如下代碼:JV728資訊網——每日最新資訊28at.com

public interface Action {  void doSomething();} public interface Parser {  Action findAction(String userInput);}

JV728資訊網——每日最新資訊28at.com

其中,Parse有一個接口FindAction,這個接口會依據用戶的輸入,找到并執行對應的動作。假如用戶輸入不對,可能就找不到對應的動作(Action),因此findAction就會返回null,接下來action調用doSomething方法時,就會出現空指針。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

解決這個問題的一個方式,就是使用Null Object pattern(空對象模式)。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

改造后

JV728資訊網——每日最新資訊28at.com

類定義如下,這樣定義findAction方法后,確保無論用戶輸入什么,都不會返回null對象:JV728資訊網——每日最新資訊28at.com

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;    }  }}

JV728資訊網——每日最新資訊28at.com

對比下面兩份調用實例:JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

  • 冗余:每獲取一個對象,就判一次空
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.findAction(someInput);if (action == null) {  // do nothing} else {
  • 精簡
ParserFactory.getParser().findAction(someInput).doSomething();

無論什么情況,都不會返回空對象,因此通過findAction拿到action后,可以放心地調用action的方法。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

其他回答精選:

JV728資訊網——每日最新資訊28at.com

(1) 如果要用equal方法,請用object<不可能為空>.equal(object<可能為空>)),例如使用:JV728資訊網——每日最新資訊28at.com

"bar".equals(foo)

而不是:JV728資訊網——每日最新資訊28at.com

foo.equals("bar")

JV728資訊網——每日最新資訊28at.com

(2) Java8或者guava lib中,提供了Optional類,這是一個元素容器,通過它來封裝對象,可以減少判空。不過代碼量還是不少。不爽。JV728資訊網——每日最新資訊28at.com

JV728資訊網——每日最新資訊28at.com

(3) 如果你想返回null,請停下來想一想,這個地方是否更應該拋出一個異常。JV728資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-10476-0.html別再用 “! = null” 做判空了!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 優化自動化接口測試:利用鉤子函數增強HTTP請求處理

下一篇: HashMap 的基礎結構,必須掌握!

標簽:
  • 熱門焦點
Top