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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如果–否则为编码风格最佳实践

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如果–否则为编码风格最佳实践 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面的帖子將是一個高級花括號討論,沒有對與錯的答案,只是更多的“品味”。 它是關于是否將“ else”(以及其他關鍵字,例如“ catch”,“ finally”)放在換行符上。



有些人可能會寫

if (something) {doIt(); } else {dontDoIt(); }

但是,我更喜歡

if (something) {doIt(); } else {dontDoIt(); }

這看起來很愚蠢。 但是評論呢? 他們去哪里? 這在我看來有點不對勁:

// This is the case when something happens and blah // blah blah, and then, etc... if (something) {doIt(); } else {// This happens only 10% of the time, and then you// better think twice about not doing itdontDoIt(); }

以下不是更好嗎?

// This is the case when something happens and blah // blah blah, and then, etc... if (something) {doIt(); }// This happens only 10% of the time, and then you // better think twice about not doing it else {dontDoIt(); }

在第二種情況下,我實際上是分別記錄“ if”和“ else”情況。 我沒有記錄對“ dontDoIt()”的調用。 這可以進一步:

// This is the case when something happens and blah // blah blah, and then, etc... if (something) {doIt(); }// Just in case else if (somethingElse) {doSomethingElse(); }// This happens only 10% of the time, and then you // better think twice about not doing it else {dontDoIt(); }

或使用try-catch-finally:

// Let's try doing some business try {doIt(); }// IOExceptions don't really occur catch (IOException ignore) {}// SQLExceptions need to be propagated catch (SQLException e) {throw new RuntimeException(e); }// Clean up some resources finally {cleanup(); }

看起來很整潔,不是嗎? 與此相反:

// Let's try doing some business try {doIt(); } catch (IOException ignore) {// IOExceptions don't really occur } catch (SQLException e) {// SQLExceptions need to be propagatedthrow new RuntimeException(e); } finally {// Clean up some resourcescleanup(); }

我很好奇您的想法...

參考: if – else,來自JAVA,SQL和ANDJOOQ博客的JCG合作伙伴 Lukas Eder 編碼風格最佳實踐 。


翻譯自: https://www.javacodegeeks.com/2012/01/if-else-coding-style-best-practices.html

總結

以上是生活随笔為你收集整理的如果–否则为编码风格最佳实践的全部內容,希望文章能夠幫你解決所遇到的問題。

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