[Software development] Why refactoring works?

Kent Beck

Programs have two kinds of value: what they can do for you today and what they can do for you tomorrow. Most times when we are programming, we are focused on what we want the program to do today. Whether we are fixing a bug or adding a feature, we are making today’s program more valuable by making it more capable.

You can’t program long without realizing that what the system does today is only a part of the story. If you can get today’s work done today, but you do it in such a way that you can’t possibly get tomorrow’s work done tomorrow, then you lose. Notice, though, that you know what you need to do today, but you’re not quite sure about tomorrow. Maybe you’ll do this, maybe that, maybe something you haven’t imagined yet.

I know enough to do today’s work. I don’t know enough to do tomorrow’s. But if I only work for today, I won’t be able to work tomorrow at all.

Refactoring is one way out of the bind. When you find that yesterday’s decision doesn’t make sense today, you change the decision. Now you can do today’s work. Tomorrow, some of your understanding as of today will seem naive, so you’ll change that, too.

What is it that makes programs hard to work with? Four things I can think of as I am typing this are as follows:

  • Programs that are hard to read are hard to modify.

  • Programs that have duplicated logic are hard to modify.

  • Programs that require additional behavior that requires you to change running code are hard to modify.

  • Programs with complex conditional logic are hard to modify.

So, we want programs that are easy to read, that have all logic specified in one and only one place, that do not allow changes to endanger existing behavior, and that allow conditional logic to be expressed as simply as possible.

Refactoring is the process of taking a running program and adding to its value, not by changing its behavior but by giving it more of these qualities that enable us to continue developing at speed.

[Android] Mimic press event of Back button

Must dispatch both ACTION_DOWN and ACTION_UPDATE KeyEvent

// trigger button back programmatically 

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

[Android] Webview on android 2.x does not show content properly with loadData() function


mWebView.loadDataWithBaseURL(null, htmlContent, “text/html”, “UTF8”, null);

[Android] Nice lib to create intuitive demo/instruction message on first app start up


Found from this thread on Stackoverflow

[JUnit] assertEqual vs assertSame

Found a good explanation on Stackoverflow about this 


Actually this is for assertSame, but we can differentiate assertEqual from this easily.

So, for short words, assertSame works on reference objects and assertEqual for equality of values between objects. 


[Android] passing data between service and activity in an active manner

From this question I found on stackoverflow:


This leads to something even more interesting

Some similar threads can be found:



[Android] Remove fading-edge of Listview

How to do it ? 


This attribute is deprecated and will be ignored as of API level 14 (ICE_CREAM_SANDWICH). Useandroid:fadingEdgeLength="0dp" instead.

Reference: http://stackoverflow.com/questions/8878554/how-to-remove-fading-effect-from-listview



[Git setup] setup git on Mac machine

Struggled for hour and get git work now ( just like old time in linux).

It used to be very easy to work with git in linux, just sudo apt-get install git … and things are done.

But it’s a little bit fuzzy with this new Mac machine.

After downloading a version of git from http://git-scm.com/download/mac

The next thing you want to try is to open terminal and type in the most lovely command “git”

But strange thing happens, it saids “bash-git command not found”

WTF, I just installed it seconds ago.

Wow, this happens because we need to tell the system what should it’s supposed to do with that command.

Your git stuffs in now in “/usr/local/git/bin/git

You need to create a symbol link to “/usr/local/bin”

Follow these steps

sudo -s
mkdir -p /usr/local/bin
ln -s /usr/local/git/bin/git /usr/local/bin/git

Done !

[Android] The very first point to get width/height in activity life-cycle

No need to say much, read the code and find out thing.


@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.e(“Height”, ” >> ” + mParentView.getMeasuredHeight()); Log.e(“Width”, ” >> ” + mParentView.getMeasuredWidth()); }

[Android-JSON] Avoid No value exception when service does not return a specific value in json reponse

Normal way to get string value from a json object would be :


well, sometimes this way does not fit. When you don’t know for sure that your service you are requesting is always stable. It may not return some keys for you, and oops you got 

JSONException No value for “missing key”

Here we may need help from other options.

optString(KEY) AND optString(KEY, Fallback_value)