欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何寫出更好的Java代碼-創(chuàng)新互聯(lián)

編碼風格

武安網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、自適應網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)建站于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)建站

傳統(tǒng)的Java編碼方式是非常啰嗦的企業(yè)級JavaBean的風格。新的風格更簡潔準確,對眼睛也更好。

結構體

我們這些碼農(nóng)干的最簡單的事情就是傳遞數(shù)據(jù)了。傳統(tǒng)的方式就是定義一個JavaBean:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">public class DataHolder {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsppublic final String data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"><br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsppublic DataHolder(String data) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbspthis.data = data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp}<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">} nbsp </code>

這不僅拖沓而且浪費。盡管你的IDE可以自動地生成這個,但這還是浪費。因此,不要這么寫。

相反的,我更喜歡C的結構體的風格,寫出來的類只是包裝數(shù)據(jù):

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">public class DataHolder {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public final String data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public DataHolder(String data) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp this.data = data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">}</code>

這樣寫減少了一半的代碼。不僅如此,除非你繼承它,不然這個類是不可變的,由于它是不可變的,因此推斷它的值就簡單多了。

如果你存儲的是Map或者List這些可以容易被修改的數(shù)據(jù),你可以使用ImmutableMap或者ImmutableList,這個在不可變性這節(jié)中會有討論。

Builder模式

如果你有一個相對復雜的對象,可以考慮下Builder模式。

你在對象里邊創(chuàng)建一個子類,用來構造你的這個對象。它使用的是可修改的狀態(tài),但一旦你調(diào)用了build方法,它會生成一個不可變對象。

想象一下我們有一個非常復雜的對象DataHolder。它的構造器看起來應該是這樣的:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">public class ComplicatedDataHolder {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public final String data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public final int num;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp // lots more fields and a constructor<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public class Builder {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp private String data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp private int num;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public Builder data(String data) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp this.data = data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp return this;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public Builder num(int num) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp this.num = num;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp return this;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public ComplicatedDataHolder build() {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp return new ComplicatedDataHolder(data, num);<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp // etc<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp } <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">}</code>

現(xiàn)在你可以使用它了:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">final ComplicatedDataHolder <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">cdh = new ComplicatedDataHolder.Builder()<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp .data("set this")<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp .num(523)<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp .build();</code>

關于Builder的使用這里還有些更好的例子,我這里舉的例子只是想讓你大概感受一下。當然這會產(chǎn)生許多我們希望避免的樣板代碼,不過好處就是你有了一個不可變對象以及一個連貫接口。

依賴注入

這更像是一個軟件工程的章節(jié)而不是Java的,寫出可測的軟件的一個最佳方式就是使用依賴注入(Dependency injection,DI)。由于Java強烈鼓勵使用面向?qū)ο笤O計 ,因此想寫出可測性強的軟件,你需要使用DI。

在Java中,這個通常都是用Spring框架來完成的。它有一個基于XML配置的綁定方式,并且仍然相當流行。

重要的一點是你不要因為它的基于XML的配置格式而過度使用它了。在XML中應該沒有任何的邏輯和控制結構。它只應該是依賴注入。

還有一個不錯的方式是使用Dagger庫以及Google的Guice。它們并沒有使用Spring的XML配置文件的格式,而是將注入的邏輯放到了注解和代碼里。

避免null值

如果有可能的話盡量避免使用null值。你可以返回一個空的集合,但不要返回null集合。如果你準備使用null的話,考慮一下@Nullable注解。IntelliJ IDEA對于@Nullable注解有內(nèi)建的支持。

如果你使用的是Java 8的話,可以考慮下新的Optional類型。如果一個值可能存在也可能不存在,把它封裝到Optional類里面,就像這樣:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">public class FooWidget {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp private final String data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp private final OptionalltBargt bar;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp public FooWidget(String data) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsp this(data, Optional.empty());<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp public FooWidget(String data, OptionalltBargt bar) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsp this.data = data;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsp this.bar = bar;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp public OptionalltBargt getBar() {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsp return bar;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">}</code>

現(xiàn)在問題就清楚了,data是不會為null的,而bar可能為空。Optional類有一些像isPresent這樣的方法,這讓它感覺跟檢查null沒什么區(qū)別。

不過有了它你可以寫出這樣的語句:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">final OptionalltFooWidgetgt <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">fooWidget = maybeGetFooWidget(); <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">final Baz baz = fooWidget.flatMap(FooWidget::getBar)<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp.flatMap(BarWidget::getBaz)<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp.orElse(defaultBaz); nbsp </code>

這比使用if來檢查null好多了。唯一的缺點就是標準類庫中對Optional的支持并不是很好,因此你還是需要對null進行檢查的。

不可變

變量,類,集合,這些都應該是不可變的,除非你有更好的理由它們的確需要進行修改。

變量可以通過final來設置成不可變的:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">final FooWidget fooWidget;<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbspif (condition()) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbspfooWidget = getWidget();<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp} else {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsptry {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbspfooWidget = cachedFooWidget.get();<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp} catch (CachingException e) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbsplog.error("Couldn't get cached value", e);<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp nbsp nbspthrow e; nbsp<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp} <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp } // fooWidget is guaranteed to be set here nbsp </code>

現(xiàn)在你可以確認fooWidget不會不小心被重新賦值了。final關鍵字可以和if/else塊以及try/catch塊配合使用。當然了,如果fooWidget對象不是不可變的,你也可以很容易地對它進行修改。

有可能的話,集合都應該盡量使用Guava的ImmutableMap, ImmutableList, or ImmutableSet類。這些類都有自己的構造器,你可以動態(tài)的創(chuàng)建它們,然后將它們設置成不可變的。

要使一個類不可變,你可以將它的字段聲明成不可變的(設置成final)。你也可以把類自身也設置成final的這樣它就不能被擴展并且修改了,當然這是可選的。

避免大量的工具類

如果你發(fā)現(xiàn)自己添加了許多方法到一個Util類里,你要注意了。

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">public class MiscUtil {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public static String frobnicateString(String base, int times) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp // ... etc<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public static void throwIfCondition(boolean condition, String msg) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp // ... etc<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">}</code>

這些類乍一看挺吸引人的,因為它們里面的這些方法不屬于任何一個地方。因此你以代碼重用之名將它們?nèi)既拥竭@里了。

這么解決問題結果更糟。把它們放回它們原本屬于的地方吧,如果你確實有一些類似的常用方法,考慮下Java 8里接口的默認方法。并且由于它們是接口,你可以實現(xiàn)多個方法。

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">public interface Thrower {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public void throwIfCondition(boolean condition, String msg) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp // ... nbsp nbsp<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp } nbsp nbsp<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp public void throwAorB(Throwable a, Throwable b, boolean throwA) { nbsp <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp // ... nbsp <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> }</code>

這樣需要使用它的類只需簡單的實現(xiàn)下這個接口就可以了。

格式

格式遠比許多程序員相像的要重要的多。一致的格式說明你關注自己的代碼或者對別人有所幫助?

是的。不過你先不要著急為了讓代碼整齊點而浪費一整天的時間在那給if塊加空格了。

如果你確實需要一份代碼格式規(guī)范,我強烈推薦Google的Java風格指南。這份指南最精彩的部分就是編程實踐這節(jié)了。非常值得一讀。

文檔

面向用戶的代碼編寫下文檔還是很重要的。這意味著你需要提供一些使用的示例,同時你的變量方法和類名都應該有適當?shù)拿枋鲂畔ⅰ?/p>

結論就是不要給不需要文檔的地方添加文檔。如果對于某個參數(shù)你沒什么可說的,或者它已經(jīng)非常明顯了,別寫文檔了。模板化的文檔比沒有文檔更糟糕,因為它欺騙了你的用戶,讓他覺得這里有文檔。

Java 8有一個漂亮的流和lambda表達式的語法。你的代碼可以這么寫:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">final ListltStringgt <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">filtered = list.stream()<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp .filter(s -gt s.startsWith("s")) nbsp nbsp<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp .map(s -gt s.toUpperCase()); </code>

而不是這樣:

<code style="margin: 0px; padding: 0px; max-width: 100%; font-family: Fixedsys; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); box-sizing: border-box !important; overflow-wrap: break-word !important;">final ListltStringgt <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">filtered = Lists.newArrayList(); <br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">for (String str : list) {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp if (str.startsWith("s") {<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp nbsp nbsp filtered.add(str.toUpperCase());<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nbsp nbsp }<br data-filtered="filtered" style="line-height: normal; margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">}</code>

這樣你能寫出更連貫的代碼,可讀性也更強。

部署

正確地部署Java程序還是需要點技巧的。現(xiàn)在部署Java代碼的主流方式有兩種 :使用框架或者使用自家摸索出來的解決方案,當然那樣更靈活。

框架

由于部署Java程序并不容易,因此才有了各種框架來用于部署。最好的兩個是Dropwizard以及Spring Boot。Play Framework也可以算是一個部署框架。

這些框架都試圖降低部署程序的門檻。如果你是一個Java的新手或者你需要快速把事情搞定的話,那么框架就派上用場了。單個jar的部署當然會比復雜的WAR或者EAR部署要更容易一些。

然而,這些框架的靈活性不夠,并且相當頑固,因此如果這些框架的開發(fā)人員給出的方式不太適合你的項目的話,你只能自己進行配置了。

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。

文章標題:如何寫出更好的Java代碼-創(chuàng)新互聯(lián)
分享鏈接:http://www.chinadenli.net/article4/deeeie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)網(wǎng)站制作網(wǎng)站設計搜索引擎優(yōu)化用戶體驗做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設