无意发现的喜感代码,重构代码!(记录将持续更新)

为了以后方便查看,现在记录将倒序! 最后更新:2012-03-18

5.这些API看来还需要修改,需要我帮忙吗?不过我还不了解View.

来自android-2.3.3/android.view.View类,约3760行处。

/**
     * @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类。有喜感的东西出现了:它不应该出现在这里。

 (1)来自Java Sun JDK中的源代码:

 

/**
     * 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源代码中,这两个常量如下:

/**
     * 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.重构条件判断与写代码人情感。

在android源代码中SQLiteOpenHelper类中的getWritableDatabase()和getReadableDatabase()方法中

都有判断mDatabase的if语句:2.3.3及之前的版本是这样的:

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的源码遇到的一点问题以及解决方法

 

 

转载于:https://my.oschina.net/banxi/blog/49210

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值