3

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.

Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
Rob Packwood
  • 3,698
  • 4
  • 32
  • 48

8 Answers8

5

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
  • 22,995
  • 5
  • 52
  • 66
5

Less code is better code.

Achilles
  • 11,165
  • 9
  • 62
  • 113
  • 1
    according to joel ... yes ... but less code doesn't mean unreadable code either .. – aggietech Mar 04 '10 at 21:35
  • 2
    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 Mar 08 '10 at 15:56
  • 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. – JP Silvashy Mar 08 '10 at 16:23
  • How about "Simpler code is better code"? – Richard Ev Mar 08 '10 at 16:50
  • 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 Mar 08 '10 at 17:32
3

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.

Kev
  • 15,899
  • 15
  • 79
  • 112
dsimcha
  • 67,514
  • 53
  • 213
  • 334
3

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.

Michael D. Irizarry
  • 6,186
  • 5
  • 30
  • 35
2

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.

  • 1
    Really? I don't think I can agree with this... – Frank V Mar 04 '10 at 21:59
  • 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 Mar 08 '10 at 15:03
  • 2
    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 Mar 08 '10 at 15:10
  • 2
    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 Mar 08 '10 at 15:26
  • @Patrick Karcher: Do you disagree with my answer or with comments disagreeing with my answer? –  Mar 08 '10 at 15:45
  • 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 Mar 08 '10 at 16:07
2

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
  • 10,190
  • 4
  • 27
  • 32
1

The most underused design pattern is....all of them. As stated earlier, many people who use design patterns don't properly way the pros and cons. I also find that "why focus on the specifics of patterns so much? I don't need to know the names" argument being thrown around by more and more people, but both these are far outweighed by people who don't utilize or think about design patterns at all.

Y-Combinator just posted this link to pages of free university lectures on computer science - http://lecturefox.com/computerscience/ - many advanced topics but not a single one about OOP or design patterns.

Montagist
  • 394
  • 2
  • 8
0

First class functions.

Jared Updike
  • 7,165
  • 8
  • 46
  • 72
  • They are not really a pattern but a language feature... – Dykam Mar 04 '10 at 21:40
  • 1
    A language with design patterns and no first class functions is not really a language. – Jared Updike Mar 04 '10 at 23:00
  • 1
    @Jared inflammatory but not indefensible. Case in point the Strategy pattern in one of the other answers. It achieves the same end as using a first-class function, but with more overhead. – wberry Sep 20 '11 at 21:02
  • Some more fuel on the fire: http://www.norvig.com/design-patterns/ and http://c2.com/cgi/wiki?AreDesignPatternsMissingLanguageFeatures and http://www.paulgraham.com/icad.html (notes at bottom) – Jared Updike Sep 21 '11 at 00:52