views:

495

answers:

9

Hi, I am Studying about Patterns and Anti-patterns . I have a clear idea about Patterns. but I am not getting Anti-Patterns. Web Definitions and Wikipedia is confusing me a lot. can anybody explain me in simple words that what is anti-pattern ? what is the purpose . what they do ? .. is it a bad thing or good thing ?

+5  A: 

A pattern is an idea of how to solve a problem of some class. An anti-pattern is an idea of how not to solve it because implementing that idea would result in bad design.

An example: a "pattern" would be to use a function for code reuse, an "anti-pattern" would be to use copy-paste for the same. Both solve the same problem, but using a function usually leads to more readable and maintainable code than copy-paste.

sharptooth
i.e a bad thing :P
annakata
+12  A: 

Anti-patterns are certain patterns in software development that is considered a bad programming practice.

As opposed to design patterns which are common approaches to common problems which have been formalized, and are generally considered a good development practice, anti-patterns are the opposite and are undesirable.

For example, in object-oriented programming, the idea is to separate the software into small pieces called objects. An anti-pattern in object-oriented programming is a God object which performs a lot of functions which would be better separated into different objects.

For example:

class GodObject {
    function PerformInitialization() {}
    function ReadFromFile() {}
    function WriteToFile() {}
    function DisplayToScreen() {}
    function PerformCalculation() {}
    function ValidateInput() {}
    // and so on... //
}

The example above has an object that does everything. In object-oriented programming, it would be preferable to have well-defined responsibilities for different objects to keep the code less coupled and ultimately more maintainable:

class FileInputOutput {
    function ReadFromFile() {}
    function WriteToFile() {}
}

class UserInputOutput {
    function DisplayToScreen() {}
    function ValidateInput() {}
}

class Logic {
    function PerformInitialization() {}
    function PerformCalculation() {}
}

Bottom line is, there are good ways to develop software, and commonly used patterns (design patterns) but there are also ways software is developed and implemented which can lead to problems. Patterns that are considered bad software development practices are anti-patterns.

For some ideas on additional anti-patterns, here is another question from Stack Overflow: What is your "favorite" anti pattern?

coobird
+3  A: 

An common way to make a mess. Like the god/kitchensink(does everything) class for example

Robert Gould
+4  A: 

As sharptooth put it, an anti-pattern is a way of not solving a problem. But there is more to it: it is also a way that can frequently be seen in attempts to solve the problem.

Ralph Rickenbach
good point - it's the ubiquity which makes it a pattern
annakata
+1  A: 

Just like with "design pattern" anti pattern is also a template and repeatable ways of solving problems but in a non-optimal and ineffective ways.

Darnell
+1  A: 

If you really wish to study AntiPatterns, get the book - Anti Patterns, ISBN-13: 978-0471197133 .

In it, they define "An AntiPattern is a literary form that describes a commonly occurring solution to a problem that generates decidedly negative consequences.

So, if it's a bad programming practice but not a common one- limited to one application, one company or one programmer, it does not meet the "Pattern" part of the AntiPattern definition.

kmarsh
+1  A: 

Interestingly a given way of solving a problem can be both a pattern and an anti-pattern. Singleton is the prime example of this. It will appear in both sets of literature.

Ed Sykes
A: 

Anti-patterns are common ways people tend to program the wrong way, or at least the not so good way.

Roman A. Taycher
A: 

Probably off topic, but you may also like:

http://en.wikipedia.org/wiki/Antiobjects

They're really cool and instead of being "bad design" is an ingenious way to solve certain problems.

anon