Wednesday, October 6, 2010

Understand Source Code by Reading Code and Refactoring for Maintaining Large Projects

This list consists of one-liner tips to be used for easy lookup and reference.

Rewriting Code for Better Understanding (Coding Horror)

  • What I cannot create, I do not understand. [Richard Feynman]
  • "What I do not create, I do not understand."[Chris]
  • "It's easier to understand 600 tables than 100,000 lines of code." [PaulC on comp.databases.theory/2005]
  • Create a new project with blocks of code copied from old code. Refactor to reduce "old code smell"
  • Start refactoring by adding small tests to better understand code and act as regression tests.
  • Just scan through huge amounts of code till it starts making sense - "understand by constant exposure".
  • Create a text file to capture your understanding - briefly describe how the component works, realtions to other components, what it persists, its states, and source browsing/debugging tips you found useful.
  • Manually reformat a copy of the code - white space, and indented as per your personal style.
References:
  1. Working Effectively with Legacy Code
  2. Object Oriented Reengineering Patterns
  3. Refactoring: Improving the Design of Existing Code


 Noble Art of Maintainance Programming (Coding Horror)

Tips for Increasing Code Readability (StackOverFlow)
  • Top-Down: Look for indicative class names and modules
  • Bottom-Up: Navigate to a feature implementation and browse/annotate to build standalone chunks of knowledge.
  • Use whitespace for increasing code-readability
  • Scan the system packages and classes looking for entities you can identify.
References:
  1. Code Reading: The Open Source Perspective
  2. Code Quality: The Open Source Perspective
  3. Implementation Patterns by Kent Beck

Some tips from StackOverflow
  • Debug to understand basic data/control flows in source.
  • Add Logging/print statements to source to understand basic data/control flows in source.
  • Write unit tests for the code
  • Annotate a printout of the source.
  • Do code walk-through with someone knows how the code works.
  • Code review
  • Fixing bugs in source slowly exposes you to small parts of the whole source.
  • Going through bug histories will give you background info esp. on the "smelly parts" of source.
  • Refactor code to improve code quality and testability esp. regression tests.
  • Add annotations to a copy of the code as you push the edges of your ignorance.
  • Reverse engineer source to design document (needs heavy manual editing)
  • Get familiar with language idioms to quickly understand source code.
  • Make a paper Diagram of the application to understand flow and dependencies
This is a list of good tips para-phrased from Coding Horror and StackOverflow (linked) blogs and many user suggestions scattered across some of the useful comments.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.