What is an anti-pattern?

前端 未结 14 1118
日久生厌
日久生厌 2020-12-04 04:52

I am studying patterns and anti-patterns. I have a clear idea about patterns, but I don\'t get anti-patterns. Definitions from the web and Wikipedia confuse me a lot.

<
相关标签:
14条回答
  • 2020-12-04 05:15

    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.

    0 讨论(0)
  • 2020-12-04 05:16

    Just like with a design pattern, an anti-pattern is also a template and a repeatable way of solving a certain problem, but in a non-optimal and ineffective way.

    0 讨论(0)
  • 2020-12-04 05:17

    Anti-patterns are certain patterns in software development that are considered bad programming practices.

    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() {}
    }
    

    The bottom line is there are good ways to develop software with 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.

    0 讨论(0)
  • 2020-12-04 05:21

    it is sometimes used when you misuse design patterns in an illegal way, or you don't know the actual usage of it. for example, having builder pattern for simple classes, or obsessively defining one singleton instance for each Active class you use in your code. also it might be beyond design patterns. for example, defining local variables in Java as final, or using try / catch for NullPointerException when you could simply check input against being null, or nulling objects after they are used (like what you do in some other languages) that you don't notice about garbage collection mechanism, or calling system.gc() to make memory empty, and many other misunderstandings, which are quite likely to be considered as a Cargo-Cult phenomena.

    0 讨论(0)
  • 2020-12-04 05:22

    Like in Algorithm you can achieve the solution using Brute force , but you have to pay lot if situation become complex.

    0 讨论(0)
  • 2020-12-04 05:24

    Today, software engineering researchers and practitioners often use the terms “anti-pattern” and “smell” interchangeably. However, they are conceptually not the same. The Wikipedia entry of anti-pattern states that an anti-pattern is different from a bad practice or a bad idea by at least two factors. An anti-pattern is

    "A commonly used process, structure or pattern of action that despite initially appearing to be an appropriate and effective response to a problem, typically has more bad consequences than beneficial results.”

    It clearly indicates that an anti-pattern is chosen in the belief that it is a good solution (as a pattern) to the presented problem; however, it brings more liabilities than benefits. On the other hand, a smell is simply a bad practice that negatively affects the quality of a software system. For example, Singleton is an anti-pattern and God class (or Insufficient Modularization) is a design smell.

    0 讨论(0)
提交回复
热议问题