<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 17  文章 - 49  trackbacks - 0
    <2006年9月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    常用鏈接

    留言簿(1)

    隨筆分類(17)

    隨筆檔案(17)

    相冊(cè)

    最新隨筆

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    2006 929 星期五

    檢查方法參數(shù),平凡處的學(xué)問(wèn)

    -effective java item 23


    ????大多數(shù)方法和構(gòu)造器都對(duì)參數(shù)有一定的限制要求,然而有否顯式地檢查參數(shù)就因人而異,回想自己的編碼歷程,也常在興沖沖之際,直接奔向業(yè)務(wù)邏輯。然而這個(gè)看似平凡的小地方,卻應(yīng)該養(yǎng)成一個(gè)堅(jiān)固的習(xí)慣,小小付出帶來(lái)的效果事實(shí)上相當(dāng)可觀。

    若有非法的參數(shù)傳入,加以檢查,則會(huì)立刻引發(fā)意義明確的異常。相反,若無(wú)此檢查,后續(xù)情形大相徑庭。該方法可能在某一行猝死,死因報(bào)告相當(dāng)迷離;更劣者,方法最后壽終正寢,返回的卻是錯(cuò)誤的計(jì)算結(jié)果;最劣者,順利走完并返回正確的結(jié)果,但執(zhí)行的過(guò)程某些牽涉到的對(duì)象被設(shè)置成不正確的狀態(tài),這些隱患最后爆發(fā)于無(wú)法預(yù)計(jì)的時(shí)刻和地點(diǎn)。

    因而,檢查參數(shù)的正確性這一舉動(dòng)帶來(lái)的貢獻(xiàn),更多的是來(lái)源于這一舉動(dòng)若不存在時(shí)可能帶來(lái)的麻煩,這扇大門過(guò)于開放,則帶來(lái)日后清理門戶時(shí)過(guò)多的周折。

    除去檢查需要進(jìn)行計(jì)算的參數(shù),更要注意檢查在本方法中并未使用但存儲(chǔ)起來(lái)以備后用的參數(shù),若不加檢查,等到異常發(fā)生的時(shí)候,來(lái)源難以追蹤。大大增加了debug的痛苦,這一點(diǎn),想必很多coder會(huì)有這樣的郁悶回憶。構(gòu)造方法是這種情形的典型,構(gòu)造器的大多數(shù)參數(shù)是屬于這種“以備后用”的類型。錯(cuò)誤的參數(shù)可能導(dǎo)致類的邏輯規(guī)則被破壞,此后導(dǎo)致的異常可能非常奇怪而且難以理清緣由。

    凡事必有例外。某些情形下,參數(shù)檢查是不應(yīng)該執(zhí)行的,例如這種檢查代價(jià)昂貴或者不切實(shí)際,而且,(注意,是而且)合法性的檢查在計(jì)算的過(guò)程中隱式的進(jìn)行。例如一個(gè)對(duì)list里的object進(jìn)行排序的方法,這些object必須是相互可比較的,否則,比較過(guò)程中就會(huì)拋出ClassCastException,這也正是排序方法應(yīng)該拋出的異常,因而事先進(jìn)行可比性的檢查就無(wú)意義了。但如果不謹(jǐn)慎的使用這種隱式檢查的技巧,就會(huì)損失錯(cuò)誤的原子性,比如這個(gè)方法不僅僅是比較,而且還進(jìn)行寫操作,那么中途拋出異常的時(shí)候,已經(jīng)有某些數(shù)據(jù)被改動(dòng)了,你不得不進(jìn)行roll back,否則就要接受一個(gè)半成品。這一方面在item 46里有更多的描述。另外,有時(shí)候這種隱式的檢查所拋出的異常,也可能并非真正的病因,你還需要做些異常的“翻譯”來(lái)指引真實(shí)的問(wèn)題根源。

    以上所說(shuō)并非鼓勵(lì)你限制參數(shù),事實(shí)上,只要方法本身能應(yīng)付得來(lái),就應(yīng)該盡量放寬參數(shù)的限制,方法才能有通用性。

    總而言之,每當(dāng)你寫一個(gè)方法或構(gòu)造器,就應(yīng)該考慮會(huì)有什么限制需要加之于參數(shù)上,為此配上文檔說(shuō)明,在方法一開始就顯式檢查,養(yǎng)成這樣的習(xí)慣很重要,這一點(diǎn)點(diǎn)謹(jǐn)慎的工作,在這個(gè)方法第一次拋出參數(shù)異常的時(shí)候,就開始了對(duì)你的回報(bào)。


    Friday, September 29, 2006

    Check method parameters for validity, knowledge within a trivial spot

    ???? -effective java item 23

    ?

    Most of methods and constructors have constraints on their parameters, but whether carry out a validity check differs from people to people. Recalling my own programming experience, I did rush to implement the business without making a check. But rigid habit shall be kept here, the little effort really brings back a lot.

    If an invalid parameter is passed into a method with checking, a prompt and clear exception will be thrown, the situation can be quite different without a check. The method may fail somewhere on the way, reporting a confusing exception or error; to be worse, the method executed successfully, but giving back a wrong result, and the worst one, the method end with a correct result, but corrupted some relevant objects during the process, and these latent problems will break out somewhere or sometime you never know.

    Therefore, the benefits of checking for validity actually comes from the fact that possibly lots of problems will appear without the checking. The door was too open and brings too much threat to future situation.

    Besides checking parameters for calculation, more attention shall be put on the checking on parameters that are stored for later use. Without the checking, errors will occur sometime when the original instances are difficult to trace, which brings miserable experience to the debugging process. I supposed quite a lot of coders had tasted that. Constructors are a representative case, most of the parameters of constructors are for later uses, invalid parameters will violate invariants of the class, the exception it caused can be very weird and hard to solve.

    There is exception. In some case which validity checking is expensive or impractical and the check can be perform implicitly in the process of calculation. For instance, a method sorting a list, for such a method, the objects in the list must be mutually comparable, otherwise a ClassCastException will be thrown, and it is exactly the exception a sorting methods should throws, so it will be meaningless to check them ahead of time. However, indiscriminate application of such techniques can lead to the loss of failure atomicity. For example, the method does more than sorting, it also do some writing operation of the objects, in such case, by the time an exception is thrown, some of the objects have already been modified, you have to roll back, or, accept a half complete result. This is discussed in detail in item 46. What’s more, sometimes exception thrown by implicit check can not tell the really problems, and you have to do some “exception translating” for revealing the real cause.

    The above information does not means that arbitrary restrictions on parameters are a good thing. In fact, you shall put fewer restrictions on them as long as the method is capable of dealing the parameters. By this way we get more general methods.

    To summarize, you shall consider what restrictions should exist on the parameters when you are writing a method or constructor, and document for them, apply explicit check in the beginning of the method body, it is important to get into the habit of doing this, this modest work will start pay back at the first time a validity check fails.

    posted on 2006-09-29 13:36 Ye Yiliang 閱讀(1500) 評(píng)論(1)  編輯  收藏 所屬分類: Java

    FeedBack:
    # re: 檢查方法參數(shù),平凡處的學(xué)問(wèn) 2006-09-30 11:27 hsp
    不同場(chǎng)合,對(duì)調(diào)用者的信任程度不同~
    存在一種思想是,由調(diào)用者確保參數(shù)的正確性,當(dāng)然這在某種場(chǎng)合下適用.
    例如inbound  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 亚洲国产精品成人| 亚洲精品网站在线观看你懂的| 国内精品99亚洲免费高清| 99久久精品国产亚洲| 在线精品免费视频| 好男人资源在线WWW免费| 少妇中文字幕乱码亚洲影视| 免费看香港一级毛片| 免费看一区二区三区四区| 亚洲三级中文字幕| 亚洲永久无码3D动漫一区| 99久久这里只精品国产免费| 国产视频精品免费视频| 亚洲综合小说另类图片动图| 久久国产亚洲观看| 日本高清免费中文字幕不卡| 日韩免费在线观看视频| 蜜臀亚洲AV无码精品国产午夜.| 亚洲va久久久噜噜噜久久 | 国产公开免费人成视频| 毛片在线全部免费观看| 春暖花开亚洲性无区一区二区 | 免费人妻av无码专区| 亚欧免费视频一区二区三区| 黄页免费视频播放在线播放| 亚洲网站在线播放| 亚洲精品乱码久久久久久自慰| 午夜免费福利在线| 精品国产免费人成电影在线观看| 免费无遮挡无码视频在线观看| 亚洲国产成人在线视频| 亚洲av永久无码精品漫画 | 久久综合图区亚洲综合图区| 国产成人精品123区免费视频| 天天影院成人免费观看| 国产免费阿v精品视频网址| 成人免费网站久久久| 亚洲乱码日产精品一二三| 亚洲婷婷综合色高清在线| 久久久久亚洲AV成人无码网站 | 亚洲毛片基地4455ww|