File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me refactor this smelly method.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to implement an algorithm that creates a Maze using a Depth - First Search algorithm (like this one). I have a two - dimensional array of Cell objects in a CellMatrix class. Each Cell holds a reference to a List<Cell> of its neighbors. Each cell also holds a reference to a Map which maps a direction (NORTH, SOUTH, EAST, WEST) to any of three conditions that can occur at a edge (WALL, PASSAGE, BOUNDARY). My current method is much longer than I usually like, and contains four if / else statements with what at first glance seems like a lot of code duplication (each if / else does basically the same thing). I know that many times these conditions can be alleviated through creative use of a Strategy (or Command) pattern. but I'm at a loss to see how it could be implemented here.
Any help here would be greatly appreciated.
[ October 06, 2006: Message edited by: Garrett Rowe ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are thinking, I'd look to collapse those four repetitive sections into one method called four times. The method would need to know whether we're at an edge, which direction we're facing and the neighbor cell.

Would something like that make sense?
[ October 07, 2006: Message edited by: Stan James ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a winner to me. Thanks for the help one more time Stan.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic