• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help me refactor this smelly method.

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds like a winner to me. Thanks for the help one more time Stan.
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic