Docsity

Refactoring Techniques, Slides of Computer Science

Partial preview of the text

Download Refactoring Techniques and more Slides Computer Science in PDF only on Docsity!

Refactoring

2

Refactoring

  • (^) Refactoring is:
    • (^) restructuring (rearranging) code...
    • ...in a series of small, semantics-preserving transformations (i.e. the code keeps working)...
    • (^) ...in order to make the code easier to maintain and modify
  • (^) Refactoring is not just any old restructuring
    • (^) You need to keep the code working
    • (^) You need small steps that preserve semantics
    • (^) You need to have unit tests to prove the code works

Why Refactor?

  • (^) To find the bugs It helps in finding the Bugs present in the program.
  • (^) To program faster It helps us to do the coding/programming faster as we have better understanding of the situation.

When to Refractor?

  • (^) When you add a function Helps you to understand the code you are modifying. Sometimes the existing design does not allow you to easily add the feature.
  • (^) When you need to fix a bug If you get a bug report its a sign the code needs refactoring because the code was not clear enough for you to see the bug in the first place.

Properties of Refactoring

  • (^) Preserve Correctness
  • (^) One step at a time
  • (^) Frequent Testing

Reasons to Refactor

  • (^) Sometimes code degenerates under maintenance, and sometimes the code just wasn’t very good in the first place.
  • (^) In either case, there are some warning signs —sometimes called “smells” —that indicate where refactorings are needed.

Reasons to Refactor

A routine is too long

  • (^) In object-oriented programming, routines longer than a screen are rarely needed. A loop is too long or too deeply nested
  • (^) Loop innards tend to be good candidates for being converted into routines, which helps to better factor the code and to reduce the complexity of the loop.

Reasons to Refactor

A class has poor cohesion

  • (^) If you find a class that takes ownership for a hodge-podge of unrelated responsibilities, that class should be broken up into multiple classes, each of which has responsibility for a cohesive set of responsibilities. A parameter list has too many parameters
  • (^) Well-factored programs tend to have many small, well-defined routines that don’t need large parameter lists.
  • (^) A long parameter list is a warning that the abstraction of the routine interface has not been well thought out.

Reasons to Refactor

Changes require parallel modifications to multiple classes

  • (^) A project had a checklist of about 15 classes that had to be modified whenever a new kind of output was added.
  • (^) When you find yourself routinely making changes to the same set of classes, that suggests the code in those classes could be rearranged so that changes affect only one class.

Reasons to Refactor

Inheritance hierarchies have to be modified in parallel

  • (^) Finding yourself making a subclass of one class every time you make a subclass of another class is a special kind of parallel modification. Case statements have to be modified in parallel
  • (^) Case statements are not inherently bad, but if you find yourself making parallel modifications to similar case statements in multiple parts of the program, you should ask whether inheritance might be a better approach.

Reasons to Refactor

A class doesn’t do very much

  • (^) Sometimes the result of refactoring code is that an old class doesn’t have much to do.
  • (^) If a class doesn’t seem to be carrying its weight, ask if you should assign all of that class’s responsibilities to other classes and eliminate the class altogether

Reasons to Refactor

A chain of routines passes tramp data

  • (^) Finding yourself passing data to one routine just so that routine can pass it to another routine is called “tramp data”

Reasons to Refactor

A subclass uses only a small percentage of its parents’ routines

  • (^) Typically this indicates that that subclass has been created because a parent class happened to contain the routines it needed, not because the subclass is logically a descendent of the superclass. Comments are used to explain difficult code
  • (^) Comments have an important role to play, but they should not be used as a crutch to explain bad code.

Reasons to Refactor

A program contains code that seems like it might be needed someday

  • (^) Programmers are notoriously bad at guessing what functionality might be needed someday.