Meaningless Drivel is fun!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Help me refactor this smelly method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Help me refactor this smelly method." Watch "Help me refactor this smelly method." New topic
Author

Help me refactor this smelly method.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
 
subject: Help me refactor this smelly method.
 
Similar Threads
Enum Problem
Struts 2 and auto population of HTML tables
July Newsletter Puzzle (Maze Solver)
Java Recursion method
Please critique!