I wrote code, but I myself many times don't even understand the code i wrote. I heard people using "refactoring method" to help them to understand others' code.

I just knew a very little about refactoring. That is using the combined key Alt Shift R in eClipse, but it will automatically change all the related code & thus does not make any benefit for understand code.

So, what is "the refactoring method" (for understanding code wrote by someone else) that people talk about?

  • 1
    Eric Lippert provides a nice demonstration of rewriting confusing code in this answer. – Brian Apr 9 '14 at 4:44
  • Shouldn't that be; 'What is “the refactoring method” for understanding code written by someone else?' – Phill Healey Apr 9 '14 at 8:31

Refactoring just means making changes that improve readability but don't change the functionality of code. IDEs like eclipse will help you do it, but it doesn't have to be done automatically.

Where refactoring comes into play in understanding other people's code, is when that code is very poorly written. Say you have a 1000 line function with a lot of repetition that is very difficult to understand. Look for ways to break it up into smaller functions, remove the repetition, and find good names for those smaller functions.

Interestingly, it's usually possible to do this even if you don't understand the big picture at the start. Once you get all the noise out of the way through refactoring, then the big picture becomes easier to see.

| improve this answer | |
  • ok, so Refactoring to break it up into smaller functions, remove the repetition, and find good names for those smaller functions. I got it. Thax u very much. – Kiti Apr 9 '14 at 4:08
  • 3
    @Kiti: that are just three examples for possible refactorings (and probably the most important ones). Martin Fowler wrote a whole book about it, see refactoring.com – Doc Brown Apr 9 '14 at 6:19

Refactoring is not limited to using IDE commands (although they help a lot in refactoring). This is the technical definition: "Code refactoring is the process of restructuring existing computer code – changing the factoring – without changing its external behavior." ([Wikipedia)1

Refactoring to understand code is when and while you are reading code, you try to understand the intent of the programmer and make changes to the code:

  • Renaming variables or methods with better names
  • Extract methods or inlining methods
  • Shortening methods that are too long

and so on. Here is a site that summarizes all of the different refactoring techniques: http://sourcemaking.com/refactoring

The reason this exercise helps you understand code better is that instead of just consuming the code and trying to understand the code for yourself, you are attempting to redesign the code so others can understand your code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.