日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

无效的Java

發布時間:2023/12/3 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 无效的Java 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

也許我可以被機器人代替進行代碼審查。 有一些反饋我發現自己一遍又一遍。 這是我最不喜歡的一些:

通用代碼結構

放棄其他

if return的else就是多余的,并造成不必要的縮進。

if (foo) { return bar; } else { return baz; } // should be replaced by if (foo) { return bar; } return baz;

數組->列表->流

List< ... > list = Arrays.asList(someArray); list.stream(...) // should be replaced by Arrays.stream(someArray)

測試代碼

之前是一個重型初始化器

我們使用@Before方法來設置復雜的對象,通常我們需要進行處理以計算類實例成員需要包含的對象。 另一方面,它是過大的:

// this is part 1 of two private MyService myService; @Before public void before() { // now initialize myService = new MyService().init( 123 ); } // the above code can be expressed in the initializer // and is simple to read there... // if it threw exceptions or needed some more complex // set up, it wouldn't be // it's in one clear place where we know where to // find it private MyService myService = new MyService() .init( 123 );

測試投擲

@Test public void someTest() throws IOException, JsonException { } // never bother with multiple or specific exception // throws in tests nobody cares and it's just noise // the test runner will catch anything! @Test public void someTest() throws Exception { }

斷言大小

// long-winded assertThat(list.size()).isEqualTo(2); // should be assertThat(list).hasSize(2);

AssertJ的一切

內置的JUnit斷言不如AssertJ提供的斷言豐富。 至少,我建議使用某種形式的assertThat ,這樣您就不會最終使用對情況有點弱的斷言。

您的assertEquals是錯誤的方法

60%的時間,當我使用assertEquals查看代碼時,順序是錯誤的。 提示:使用AssertJ !!! JUnit在這一點上是錯誤的! 我們應該從左到右閱讀。

// wrong: assertEquals(something.getFoo(), 123 ); // it's expected IS actual assertEquals( 123 , something.getFoo());

Mockito靜態導入

// this is not normal Mockito.verify(mock).called(); // static import all mockito methods verify(mock).called();

Mockito時報(1)

// this is a tautology verify(mock, times( 1 )).called(); // look at what verify(mock) does internally // replace with verify(mock).called();

翻譯自: https://www.javacodegeeks.com/2019/10/ineffective-java.html

總結

以上是生活随笔為你收集整理的无效的Java的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。