Java Reference
In-Depth Information
6.4
Code duplication
Code duplication is an indicator of bad design. The
Game
class shown in Code 6.1 contains a
case of code duplication. The problem with code duplication is that any change to one version
must also be made to another if we are to avoid inconsistency. This increases the amount of
work a maintenance programmer has to do, and it introduces the danger of bugs. It very easily
happens that a maintenance programmer finds one copy of the code and, having changed it,
assumes that the job is done. There is nothing indicating that a second copy of the code exists,
and it might incorrectly remain unchanged.
Concept:
Code duplication
(having the same
segment of code in
an application more
than once) is a sign
of bad design. It
should be avoided.
Code 6.1
Selected sections of the
(badly designed)
Game
class
public
class
Game
{
//
. . .
some code omitted . . .
private
void
createRooms()
{
Room outside, theater, pub, lab, office;
// create the rooms
outside =
new
Room(
"outside the main entrance of the university"
);
theater =
new
Room(
"in a lecture theater"
);
pub =
new
Room(
"in the campus pub"
);
lab =
new
Room(
"in a computing lab"
);
office =
new
Room(
"in the computing admin office"
);
// initialize room exits
outside.setExits(
null
, theater, lab, pub);
theatre.setExits(
null
,
null
,
null
, outside);
pub.setExits(
null
, outside,
null
,
null
);
lab.setExits(outside, office,
null
,
null
);
office.setExits(
null
,
null
,
null
, lab);
currentRoom = outside;
// start game outside
}
//
. . . some code omitted . . .
/**
* Print out the opening message for the player.
*/
private
void
printWelcome()
{
System.out.println();
System.out.println(
"Welcome to the World of Zuul!"
);
Search WWH ::
Custom Search