Design Patterns: Pattern or Anti-Pattern, That is the Question
Join the DZone community and get the full member experience.Join For Free
i have recently encountered the wiki page anti-pattern that has an exhaustive list of anti patterns. some of them were obvious for me. some of them made me think a bit, other a bit more. then i started to look for the anti-pattern “singleton” on the page and i could not find it. (text search stops at singlet…)
is singleton a pattern or an anti-pattern?
do not worry. i will not talk about singletons. there are more than enough discussions about that on the net. for now let me just use singleton as an example. in case of singleton there is no clean line between pattern and anti-pattern.
as an example, singleton can easily be used, it is easy to understand and serves the purpose being a good example. singletons were very popular until a few years ago until they started to be considered as anti-pattern. what has changed? do computer scientists became cleverer and discover that something they thought as a good pattern was actually anti-pattern? was singleton an anti-pattern from the very start, we just did not know it? or did singleton itself change from being a good design pattern to be an anti-pattern?
in my opinion, the answer, strangely: both of the cases. it is very similar to physical theory. newtons theorem says that the force is proportional with the acceleration and the mass of the body accelerating. einstein said that this is not true, and the larger the speed the larger the error is. was newton wrong? yes, in some sense. can we still use newton’s laws? yes we can, and actually we do in case of low speed (as compared to the speed of light).
were singletons bad from the very start? yes, just as much as they are now. can we still use singletons? yes we can, and actually we do in case of low spee… ops in case of simple problems that do not need considerations that make singleton to be an anti-pattern.
it seems that anti-pattern is environment dependent. and in this case environment is much more complex than in case of physics. quantum theory, relativity or irreversible thermodynamics (that my father is professor of) are simple, because they do not consider humans. they are only about the matter.
programming is about people.
this is something that we tend to forget many times. when it all started it was all about bits and bytes, registers, memory and hexa code. a program was either running and executing well or not. but later it became more and more complex. computer science became information technology. more and more people use it and more and more people write programs. they are programmers who do this for living. even a relatively simple project, if it is commercial needs six to eight programmers. they, we are also part of the environment that the “pattern or anti-pattern”-ness depends on.
the environment also contains simple things, like frameworks, programming languages, operating systems and other technical details, but none of them individually or together has a complexity comparable that of the humans.
when making a decision on the pattern versus anti-pattern debate many asks the question: what can the pattern be used for? what is this good for? what good can it do? these are important questions. if there is no good: no reason to consider the pattern. but it does not make it anti-pattern. if there is no good then the approach is simply not a pattern. the important question what bad it can do? what way average joe can f@#k up the structure. how likely is it that someone applying the pattern will shoot his own leg? this is not new thing. game theory also states that this is more important to minimize the maximum loss than to maximize the possible gain.
looking at a pattern, can you tell that? probably not. in some cases, yes. congrats, you found an anti-pattern. in other cases you can not find any wrong use. it does not mean there is none. time will show. average joe will come and will use the pattern in ways you could not imagine in your wildest dream (or night mare for that matter).
thus anti-pattern is not something inherently bad, just as guns do not kill people. the use of patterns in a wrong way is bad. when people tend to use a pattern more the wrong way, then it is an anti-pattern. this is the case with singletons.
similar thing can be captured with code commenting. when i started to learn programming pascal our teacher required that 50% of the code has to be meaningful comment. how does it come today? clean code says that comment is evil. sort of. too many comments smell. at times i find myself in projects where comments are totally evicted. not even javadoc is allowed. weird? this is just the way they apply the clean code pattern.
so the question that starts to bug me these days is whether using design patterns in programming is a good pattern or is that itself an anti-pattern?
Published at DZone with permission of Peter Verhas, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.