views:

338

answers:

7

I have been reading a lot on design patterns lately and some of them can make our lives much easier and some of them seem to just complicate things (at least to me they do). I am curious to know what design patterns everyone sees as underunsed or underappreciated. Some patterns are simple and many people do not even realize they are using a pattern (decorator probably being the most used, without realized). My goal from this is to give us pattern-newbies some appreciation for some of the more complex or unknown patterns and why we should use them.

+4  A: 

Most important "patterns" for a newbie is to

  • keep it simple.
  • Learn why supposedly good practices are good, so you can tell if they are good for the particular situation.
  • You have to balance competing concerns for a situation, not just think about one. (decoupling v. cohesion, testing soundness v. completeness, good practices v. time)

As far as formal patterns with cool names go, I think reviewing patterns that focus on interfaces are helpful, because help get you thinking in terms of dependency and contracts, rather than just objects. Or as Uncle Bob (Martin) says, Depend on abstractions, not on concretions. So my answer is:

See Dependency Inversion Principal, one of the SOLID Principals, (which I'm sure you've already analyzed). Looked at narrowly, DI is very simple though it's often made over-complicated. I believe it's important to also look at it in a wider context, a way of thinking about every level and part of your design, not just code arranged in a certain way.

A small word of warning. I think some pattern-newbies become over-zealous with patterns and don't think code is good unless it's implementing a pattern, and that a pattern makes a solution good. They sometimes focus on patterns to the detriment of other good OOP/D practices. This is a mistake. Mr. Solid himself explains this here.

Patrick Karcher
+5  A: 

Less code is better code.

Achilles
according to joel ... yes ... but less code doesn't mean unreadable code either ..
aggietech
It's not always better. Simpler is (almost) always better, but less code is not necessarily more simple. Sometime good encapsulation and separation of concerns can cause there to be more lines of code, even though this increases overall simplicity.
Patrick Karcher
I don't agree with this entirely, I think shorter methods is better, but less code is not better if cram everything on one line when it would have been easier to read as a block.
Joseph Silvashy
How about "Simpler code is better code"?
Richard Ev
Less code is better code. The hardest part of coding is reading a developer's code. The less code you have to read the easier that code is to understand. There is a reason i = i + 1; is not as good as i+=1; or i++;
Achilles
+1  A: 

Common sense and efficiency can replace all design patterns.

EDIT: Seeing that commenters didn't get my point...

What I was trying to say is that instead of memorizing all design patterns and thinking in these terms, you can simply reflect about the problem for a while and solve it in an efficient and elegant way, based in common sense and your experience. With good probability, what you will come up with will be one or more of those design patterns. But you don't need to know them in advance in order to use them. Anyway, I could never master memorizing the names of those 20+ patterns and holding mental associations of what each name was standing for.

Developer Art
Really? I don't think I can agree with this...
Frank V
The idea behind using 'patterns' whatsoever is to reduce the amount of re-invention. Common sense _is_ needed to detect when to use or not use a pattern, though.
xtofl
Technically correct but entirely misleading. You could say the same about algorithms or say that as long as you have base logic principles then you don't need to learn math because you can just work it all out as you need it. Design patterns may be misused, but the concept of abstracting out commonalities and not reinventing the wheel is extremely important in any field.
Dinah
I sympathize with this, but disagree. I'm tired of everything being a pattern, of people thinking of particular patterns **instead of** really thinking about the situation. I'm annoyed that newbies want to skip painstaking OOP/D thinking and head straight to patterns. But, they do have a great place as light reading for Beginners, and for an Intermediate trying to move to Advanced. But if a beginner was going to read Code Complete or a Patterns book, Code Complete is the easy choice.
Patrick Karcher
@Patrick Karcher: Do you disagree with my answer or with comments disagreeing with my answer?
Developer Art
I disagree with the answer, the conclusion that design patterns are not helpful. You Edit is good, and I certainly agree that way too many people concentrate on design patterns too much. But they can be good for communication, and great study for people who already have good OOP/D learning/experience. I don't believe common sense is an adequate replacement for Patters.
Patrick Karcher
+2  A: 

Balance

I mean, there are all sorts of patterns and guidelines, but all can be overapplied, resulting in an anti-pattern effect.

Experience about what and how to apply known practices comes with each line you write, each discussion you have with fellow coders and each reading write-ups from people who think different.

Dykam
+1. Not really a pattern, but it's more important in my opinion. Someone is not ready for patterns if they don't weigh and evaluate.
Patrick Karcher
Yeh. It would prevent yet another question about e.g. Singleton...
Dykam
A: 

First class functions.

Jared Updike
They are not really a pattern but a language feature...
Dykam
A language with design patterns and no first class functions is not really a language.
Jared Updike
+3  A: 

I'm a big fan of monte carlo unit testing. A lot of very complex, efficient algorithms are extremely hard to test because there are so many different code paths and edge conditions. If there exists a naive algorithm that does the same thing as your complex algorithm, or even one that simply makes different tradeoffs (like a hash table vs. a balanced tree), you can just generate tons of random input, feed it to both the naive and efficient algorithm, and make sure the results match. I've found tons of weird edge case bugs this way that would otherwise have been nearly impossible to find.

dsimcha
+3  A: 

I implemented the other day the Strategy Pattern, it is used to encapsulate methods than you can swap at runtime. Sort of like the Command Pattern but more decoupled.

This is how I used it. I had to send files to third parties and I had a few options to choose from I had FTP, SFTP, FTPS and SMB.

Interface:

interface ITransferStrategry
{
    void TransferFiles(System.Collections.Generic.IList<string> files);
}

Implementation:

public class FTPTransfer : ITransferStrategry
{

    public void TransferFiles(IList<string> files)
    {
        // Transfer to FTP Code Here
    }

}

Context:

public class TransferContext
{

    private ITransferStrategry _strategy;

    public TransferContext(ITransferStrategry strategy)
    {
        this._strategy = strategy;
    }

    public void Send(IList<string> files)
    {
        this._strategy.TransferFiles(files);
    }

}

Client:

public void Client()
{
   TransferContext context = new TransferContext(new FTPTransfer());
   context.Send(files);
}

Its pretty easy and easy to maintain. Hope this helps anyone.

JeremySpouken
I can't tell the difference with the Abstract Factory.
Lluis Martinez
I love this pattern. +1 For something I can use.
Achilles
This pattern is more straight forward. With the abstract factory you provide an interface for creating hierarchies or families a.k.a various methods that are related or not independent without specifying their concrete class.
JeremySpouken