2.3.8. Refactoring

What is refactoring? In simple words, it is just renaming of identifiers [1]. It is about changing the source code such that that there is no functional change in program’s behaviour. More can be read at http://en.wikipedia.org/wiki/Code_refactoring

2.3.8.1. Rename

The most basic application of refactoring is to rename a C/C++ identifier [1]. This renaming is context sensitive. Eclipse knows whether you are renaming something globally or locally. Just select the identifier and press Ctrl + Shift + R. The advantage over renaming? See an example. The file example.c has c_stream in almost every function. If we refactor c_stream to cmprStream in test_deflate, the rename would happen only in test_deflate. c_stream would remain as it is in other function.

Here is refactoring/renaming in action. Select c_stream and press Ctrl + Shift + R (Or Refactor → Rename). A dialog would pop up. Enter the new name and press Preview. Eclipse would show Diff of the proposed changes. Here is a preview of c_stream replaced with cmprStream

../../../_images/e-refactor-c-stream-to-cmpr-stream.png

Rename c_stream to cmprStream

../../../_images/e-diff-before-replacing.png

Diff of the proposed changes

../../../_images/e-c-stream-to-cmpr-stream.png

c_stream replaced with cmprStream

(The best use of refactoring this way is to rename local variables foo, bar and the omnipresent int i=0 in loops)

2.3.8.2. Extract Local Variable

Complex operations are difficult to read. If they are reduced with intermediate local variables, code becomes more readable. Eclipse can help to do that.

  1. Select the expression

    Select the expression to be refactored in Eclipse Editor
  2. Press Ctrl + Shift + L (Or Refactor → Extract Local Variable ). Key in the local variable name (in this case i_plus_j )

    Insert the new name of the refactored variable.
  3. Eclipse would replace the variable name.

    Eclipse has replaced the the value in the refactored variable.

2.3.8.3. Extract Constants / Magic Numbers

Magic numbers are easy to write, and difficult to interpret later. Better replace with better meaning. Eclipse can help to do this, too.

  1. Select the constant you want to replace. (In this case 10)

    Select the expression to extract constant.
  2. Press Alt + C (Or Refactor → Extract Constant). Key in the new constant name (in this case TEN)

    ../../../_images/e-refactor-sel-5.png
  3. Eclipse would replace all the 10/ s with TEN/ s

    ../../../_images/e-refactor-sel-6.png

2.3.8.4. Extract Functions

We can reduce a set of source lines to a meaningful function. It is not just copy/pasting code from one place to another. In some cases necessary local variables have to be passed in. Either by reference in CPP, or using pointers in C. Eclipse is intelligent enough to figure out the necessary local variables to be passed in. And also can figure out if they have to be passed using reference, pointers or just by value.

  1. Select the source lines you want to extract as function.

    ../../../_images/e-refactor-sel-7.png
  2. Press Alt + Shift + M (Or Refactor → Extract Function). Key in the function name.

    ../../../_images/e-refactor-sel-8.png
  3. Eclipse would replace the source lines with a function call.

    ../../../_images/e-refactor-sel-9.png

But, every selected source lines cannot be extracted to a function. We can not extract lines with return or break.

[1](1, 2) Functions, variables, typedefs, enumerators, defines, etc.