04 restructuring code - lab solutions

3

Click here to load reader

Upload: poojacambs

Post on 18-Feb-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 04 Restructuring Code - Lab Solutions

7/23/2019 04 Restructuring Code - Lab Solutions

http://slidepdf.com/reader/full/04-restructuring-code-lab-solutions 1/3

Last changed: 08 January 2014 1

4CCS1PRAProgramming Applications  Lab Solutions

Lab 4: Code Structure

Exercise 1Here is the new main method:

publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { Maze aMaze = new Maze( ) ;Room r 1 = new Room ( 1) ;Room r 2 = new Room ( 2) ;Room r 3 = new Room ( 3) ;Room r 4 = new Room ( 4) ;Door Wal l d = new Door Wal l ( r 1, r 2) ;Door Wal l d2 = new Door Wal l ( r 3, r 1) ;Door Wal l d3 = new Door Wal l ( r 4, r 3) ;Door Wal l d4 = new Door Wal l ( r 4, r 2) ;

aMaze. addRoom( r 1) ;aMaze. addRoom( r 2) ;aMaze. addRoom( r 3) ;aMaze. addRoom( r 4) ;

r 1. set Si de( Room. NORTH, new Wal l ( ) ) ;r 1. setSi de( Room. EAST, d) ;r 1. set Si de( Room. SOUTH, d2) ;r 1. setSi de( Room. WEST, new Wal l ( ) ) ;

r 2. set Si de( Room. NORTH, new Wal l ( ) ) ;

r 2. setSi de( Room. EAST, new Wal l ( ) ) ;r 2. set Si de( Room. SOUTH, d4) ;r 2. set Si de( Room. WEST, d) ;

r 3. set Si de( Room. NORTH, d2) ;r 3. setSi de( Room. EAST, d3) ;r 3. set Si de( Room. SOUTH, new Wal l ( ) ) ;r 3. setSi de( Room. WEST, new Wal l ( ) ) ;

r 4. set Si de( Room. NORTH, d4) ;r 4. setSi de( Room. EAST, new Wal l ( ) ) ;r 4. set Si de( Room. SOUTH, new Wal l ( ) ) ;r 4. set Si de( Room. WEST, d3) ;

aMaze. pr i nt ( ) ;}The original lines have been given a grey background, so all lines with a white background are

either new or modified. All told, we have touched 17 lines of code for this change. An easy

mistake to make is to forget to update the walls of rooms 1 and 2 in correspondence to the

new doors introduced by rooms 3 and 4.

Exercise 2This version of the code has reduced code replication by encapsulating the repeated code into

a parameterised method createWallsFor(). We would expect this to mean we need to write

fewer lines of code to make our change. We will still need to watch out to update the walls ofexisting rooms appropriately.

Page 2: 04 Restructuring Code - Lab Solutions

7/23/2019 04 Restructuring Code - Lab Solutions

http://slidepdf.com/reader/full/04-restructuring-code-lab-solutions 2/3

Last changed: 08 January 2014 2

Here is the new main method:

publ i c stati c voi d mai n( St r i ng[ ] ar gs) {Maze aMaze = new Maze( ) ;Room r 1 = new Room( 1) ;Room r 2 = new Room( 2) ;

Room r 3 = new Room( 3) ;Room r 4 = new Room( 4) ;Door Wal l d = new Door Wal l ( r 1, r 2) ;Door Wal l d2 = new Door Wal l ( r 3, r 1) ;Door Wal l d3 = new Door Wal l ( r 4, r 3) ;Door Wal l d4 = new Door Wal l ( r 4, r 2) ;

aMaze. addRoom( r 1) ;aMaze. addRoom( r 2) ;aMaze. addRoom( r 3) ;aMaze. addRoom( r 4) ;

set Wal l sFor ( r 1, new Wal l ( ) , d, d2, new Wal l ( ) ) ;

set Wal l sFor ( r 2, new Wal l ( ) , new Wal l ( ) , d4, d) ;set Wal l sFor ( r 3, d2, d3, new Wal l ( ) , new Wal l ( ) ) ;set Wal l sFor ( r 4, d4, new Wal l ( ) , new Wal l ( ) , d3) ;

aMaze. pr i nt ( ) ;}Notice how it is shorter overall. Also, there are fewer changed or added lines (highlighted as

before): 11 lines had to be touched rather than the 17 from Exercise 1. Note that we still need

to make sure to remember to update the walls of existing rooms.

Exercise 3This is even shorter still! Here is the new main method:

publ i c stati c voi d mai n( St r i ng[ ] ar gs) {Maze aMaze = new Maze( ) ;Room r 1 = new Room ( 1) ;Room r 2 = new Room ( 2) ;Room r 3 = new Room ( 3) ;Room r 4 = new Room ( 4) ;addDoor ( r 1, r 2, Room. EAST) ;addDoor ( r 3, r 1, Room. NORTH) ;addDoor ( r 4, r 3, Room. WEST) ;addDoor ( r 4, r 2, Room. NORTH) ;

aMaze. addRoom( r 1) ;

aMaze. addRoom( r 2) ;aMaze. addRoom( r 3) ;aMaze. addRoom( r 4) ;

aMaze. pr i nt ( ) ;}This time, we only had to add 7 lines of code. We did not have to change any existing code,

because updating corresponding walls is automatically taken care of by the addDoor method.

This should reduce the potential for making errors, especially as our maze grows larger.

Can you encapsulate even more repetition or implicit constraints into explicit methods?

Page 3: 04 Restructuring Code - Lab Solutions

7/23/2019 04 Restructuring Code - Lab Solutions

http://slidepdf.com/reader/full/04-restructuring-code-lab-solutions 3/3

Last changed: 08 January 2014 3

Exercise 4Compare your notes from the previous three exercises: Which version was most easy to change?

Why?

We have largely discussed this above already. Each version is easier to modify and maintain

than the previous one. Two things have contributed to this:

1. The second version reduced code repetition by encapsulating it in a separate method,

using method parameters to allow for slight variations between the repeated code

snippets.

2. The third version further improved comprehensibility and maintainability by reorganis- 

ing the code according to the concerns it addressed, ensuring clear responsibilities for

each method. This also resulted in explicit support for the previously hidden con- 

straint, which further improved comprehensibility and maintainability of the code.