answers 1597423 2

Question #3 – Refactoring Programs

Refactor the following code; i.e. improve its quality without modifying its behavior;

  • Use meaningful names for variables, parameters & functions

  • Provide proper documentation as required in the PAs

  • Provide proper indentation & programming style similar to the examples from the textbook, videos & PAs

  • Remove useless code

  • Simplify program

  • Improve performance

You will provide your version in the “Your Modified Version” subsection which is already pre-filled with the original. Then, for each improvement you applied, use the table in the “Summary” subsection to explain what you did & why you did it.

Program to Refactor

  1. int mystery(int v1, int v2){

  2. if( (v1 < 1) || (v2 < 1) ) return 0;

  3. if( v1 < v2 ) return mystery(v1, v2-1)+1;

  4. if( v2 < v1 ) return mystery(v1-1, v2)+1;

  5. return mystery(v1-1, v2-1);

  6. }

Your Improved Version

  1. int mystery(int v1, int v2){

  2. if( (v1 < 1) || (v2 < 1) ) return 0;

  3. if( v1 < v2 ) return mystery(v1, v2-1)+1;

  4. if( v2 < v1 ) return mystery(v1-1, v2)+1;

  5. return mystery(v1-1, v2-1);

  6. }

Summary of Improvements

 

Feel free to add rows to, or remove rows from, this table as needed;

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *