The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Refactoring!

Hey

I am doing a project for univeristy that requires us to refactor someone elses implementation of the genetic algorithm.

Im trying to refactor it to the MVC design pattern, im currently working through the main class - GACanvas.java to split the logic from presentation.

So here is an example method

private void  PutNewEater (int loc, boolean brandNew) { // loc = location in eater array
      int row, col;
      int[] location;
      switch (eaterBirth.getSelectedIndex()) {
        case 0:  {
              row = centerRow + ((int)(Math.random()*12) - 7);
              col = centerCol + ((int)(Math.random()*12) - 7);
              location = FindSpaceNear(row, col);
              break;
            }
        case 1:  {
              row = (int)(Math.random()*12) + 1;
              col = (int)(Math.random()*12) + 1;
              location = FindSpaceNear(row, col);
              break;
            }
        case 2: {
              location = FindSpace();
              break;
            }
        default: {
            if (brandNew)
              location = FindSpace();
            else {
                  row = eater[loc].row;
                  col = eater[loc].col;
                  location = FindSpaceNear(row, col);
              }
        }
      }
      cellContents[location[0]][location[1]] = loc;
      eater[loc].row = location[0];
      eater[loc].col = location[1];
      eater[loc].facing = (int)(Math.random()*4);
      if (draw) {
        OSG.setColor(Color.red);
        DrawEater(eater[loc].facing, location[0], location[1]);
      }
  }

From what I can tell most of this method deals with the model logic and the last piece of code draws the eater(presentation?).

I think that I should extract that last piece of code into a new method as about 4 methods do the same thing at the end of them!

Am I heading in the right direction here? Don't want to take a huge leap of course! Any help is greatly appreicated

regards
Dan
Dan
Wednesday, December 07, 2005
 
 
If it were me, I'd look at doing even smaller refactorings first.

Rows and columns obviously go together so I'd invent something like a Location class.  Then, I'd convert everything from:

row = eater[loc].row;
col = eater[loc].col;
location = FindSpaceNear(row, col);
cellContents[location[0]][location[1]] = loc;
eater[loc].row = location[0];
eater[loc].col = location[1];

To something like:

Location eaterLoc = eater[loc];
location = FindSpaceNear(eaterLoc);
cellContents[location.getRow()][location.getCol()] = loc;
eater[loc] = location;

After doing that, the larger, multi-method refactorings would hopefully become more obvious.

The key in these small refactorings is to substitute objects rather than encoding data into int arrays.
Daniel Howard Send private email
Wednesday, December 07, 2005
 
 
Thanks for that!

yeah i'm just trying to do some smaller refactorings first, this is my first attempt at anyting like this!

Thanks again
Dan
Wednesday, December 07, 2005
 
 
Another great candidate for refactoring is the switch statement. As the number of possibilities grow, you'd end up with a zillion cases in your switch. You might want to take each switch case and make it into its own method.
BenjiSmith Send private email
Wednesday, December 07, 2005
 
 
Doesn't moving code to a separate method introduce a performance penalty?
roman from poland
Thursday, December 08, 2005
 
 
To the OT: have you got a copy of Martin Fowler's "Refactoring" book?
John Topley Send private email
Thursday, December 08, 2005
 
 
Dan,

Have you written tests for this code?  If you're not using tests to verify that your changed code works the same as the original, you're not really refactoring...and you're very likely to introduce bugs into the code.  (I speak from painful experience.)
Kyralessa Send private email
Thursday, December 08, 2005
 
 
First thing to go is that nearly duplicate code in the switch statement.  Screams out a parameterized call to another method.
flash91
Friday, December 09, 2005
 
 
+1 for Kyralessa,

The first thing to do if one want to refactor the code is to have it test.  This makes sure that after being refectored, system still behaves as same as it used to.
Daniel Chein Send private email
Sunday, December 11, 2005
 
 
Cheers for all the input :)
Dan
Monday, December 12, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz