無心發現的喜感代碼,重構代碼!(記錄將持續更新)

爲了之後方便查看,如今記錄將倒序! 最後更新:2012-03-18html

5.這些API看來還須要修改,須要我幫忙嗎?不過我還不瞭解View.java

來自android-2.3.3/android.view.View類,約3760行處。android

/**
     * @hide
     */
    public void dispatchStartTemporaryDetach() {
        onStartTemporaryDetach();
    }

    /**
     * This is called when a container is going to temporarily detach a child, with
     * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
     * It will either be followed by {@link #onFinishTemporaryDetach()} or
     * {@link #onDetachedFromWindow()} when the container is done.
     */
    public void onStartTemporaryDetach() {
        removeUnsetPressCallback();
        mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
    }

    /**
     * @hide
     */
    public void dispatchFinishTemporaryDetach() {
        onFinishTemporaryDetach();
    }

    /**
     * Called after {@link #onStartTemporaryDetach} when the container is done
     * changing the view.
     */
    public void onFinishTemporaryDetach() {
    }

由於onStartTemporaryDetach()方法纔有方法體。onFinish的就沒有了。

可是仍是寫在哪裏仍是公開了。看來寫代碼的人員很講究對稱美嘛!哈哈,就算空的也要擺出來好看唄!網絡

1.今天想看Java網絡通訊,翻開了HttpURLConnection類。有喜感的東西出現了:它不該該出如今這裏。socket

 (1)來自Java Sun JDK中的源代碼:ide

 

/**
     * HTTP Status-Code 500: Internal Server Error. 
     * @deprecated   it is misplaced and shouldn't have existed.
     */
    @Deprecated
    public static final int HTTP_SERVER_ERROR = 500;

    /** 
     * HTTP Status-Code 500: Internal Server Error. 
     */
    public static final int HTTP_INTERNAL_ERROR = 500;

(2)來自android源代碼中,這兩個常量以下:this

/**
     * Numeric status code, 500: Internal error
     */
    public static final int HTTP_INTERNAL_ERROR = 500;

    /**
     * Numeric status code, 500: Internal error
     *
     * @deprecated Use {@link #HTTP_INTERNAL_ERROR}
     */
    @Deprecated
    public static final int HTTP_SERVER_ERROR = 500;

 經過對比,上面的代碼中,變量,HTTP_INTERNAL_ERROR 是正確的。HTTP_SERVER_ERROR是不當心才寫到這裏的吧,對不對?可是是從Java出生時起這個小錯,就得一直存在了。由於這個是公開的常量。惋惜的是我如今還不明白爲何用HTTP_SERVER_ERROR這樣名字就錯了,誰能告訴我啊?

 

2.重構條件判斷與寫代碼人情感。spa

在android源代碼中SQLiteOpenHelper類中的getWritableDatabase()和getReadableDatabase()方法中.net

都有判斷mDatabase的if語句:2.3.3及以前的版本是這樣的:code

if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {

   return mDatabase;  // The database is already open for business

}

在4.0.3中是下面這樣的:

if (mDatabase != null) {
   if (!mDatabase.isOpen()) {
    // darn! the user closed the database by calling mDatabase.close()
          mDatabase = null;
    } else if (!mDatabase.isReadOnly()) {
      return mDatabase;  // The database is already open for business
    }
}

我以爲把多重條件判斷分開寫確定會可讀性好點,在《重構》一書中,我好像發現有相似的重構。

可是如今沒有書在身邊那。喜感的是,這個重構後的代碼中註釋多了,也有很口語化的文字。darn! Google後知道在這裏應該是,該死的。意思。哈哈。

換成,damn or fuck?

 

3. 喜感的空方法啊!

看java.net.Socket類最後一個方法,JDK_API文檔說了一大堆啊:

可是事實是這樣的:

* @since 1.5
     */
    public void setPerformancePreferences(int connectionTime,
                                          int latency,
                                          int bandwidth)
    {
	/* Not implemented yet */
    }

看看android4.0.3中java.net.Socket中的這個方法:

/**
     * Sets performance preferences for connectionTime, latency and bandwidth.
     *
     * <p>This method does currently nothing.
     *
     * @param connectionTime
     *            the value representing the importance of a short connecting
     *            time.
     * @param latency
     *            the value representing the importance of low latency.
     * @param bandwidth
     *            the value representing the importance of high bandwidth.
     */
    public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
        // Our socket implementation only provide one protocol: TCP/IP, so
        // we do nothing for this method
    }

 哈哈,在android中的代碼註釋中有一些緣由,在javadoc中也說了這個方法是空的。可是

sun的那個沒有說明啊。坑爹啊!我不明白既然這個方法爲空,爲何要公開這麼一個方法呢?

 

4. android自定義的javadoc @hide

我是在java.net.Socket類中首次發現的:

/**
     * @hide internal use only
     */
    public FileDescriptor getFileDescriptor$() {
        return impl.fd;
    }

這是讓我發現變量名方法名還可使用$符號,呵呵,我之前都沒有使用過啊。

關於android中的這個@hide自定義javadoc的其它特色,這個帖子遇到的問題,可能幫助你理解:

http://www.eoeandroid.com/thread-16096-1-1.html  

關於修改frameworks的源碼遇到的一點問題以及解決方法

相關文章
相關標籤/搜索