Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should come first -- the design pattern or the code?

I'm starting on a fresh new project--should I look at my spec and decide which design patterns to apply, or just come up with a general idea of organization and allow the patterns to emerge organically through refactoring?

In your experience, which technique will be most productive and have a greater chance of leading to clean elegant code?

I'm also wondering if there are design patterns out there that aren't defined by the GoF, but could be just as valuable? If so, what are some useful resources for informing myself about these?

like image 457
alchemical Avatar asked May 21 '09 16:05

alchemical


2 Answers

You should grow your code organically, applying patterns as they fit. Matching up with patterns too early can lead to a lot of inappropriate code and so many layers of abstraction that the design becomes obfuscated. Witness any code you've seen that was written right after someone had discovered patterns for the first time ;-)

like image 175
dwc Avatar answered Oct 02 '22 15:10

dwc


Unless a pattern seems to obviously jump out of the spec, I wouldn't necessarily try to pick something from GoF and hammer the problem until it fits the pattern.

It's better to conceptually understand the different levels of abstraction in your head and come up with a plan (not necessarily a popular design pattern) for how you will implement it. This is something you will get better at with experience. While knowing the GoF patterns will help you improve your ability to think about problems in terms of code design, they are not meant to be solutions to every problem, and artificially forcing your problem to fit into a design pattern can mean unnecessary complication and obfuscation.

like image 26
mandaleeka Avatar answered Oct 02 '22 15:10

mandaleeka