DEV Community

Andrei Kuzmich
Andrei Kuzmich

Posted on

Reinventing anti-patterns

A wolf in a perfect sheep's hide

Beware of false prophets, who come to you in sheep's clothing but inwardly are ravenous wolves. You will recognise them by their fruits.

If you work with a language which lacks strong type system you must be familiar with duck typing - if it walks like a duck and it quacks like a duck, then it must be a duck. But apparently if someone looks and behaves like a "he", may easily identify himself as a "she".
One of the most popular ways to detect if something is a "Promise" in JavaScript is to check if it has "then" function property. The example of an exploit for this scenario may be an object with "then" function which, instead of resolving once, also creates a never ending subscription of some observable to produce only God knows what side effects (WARNING: do not implement this at home).

An omniscient chatterbox

Are you tired of having hundreds of subscriptions/event listeners for every small peace of data/event in your program? Enter "omniscient chatterbox". As you can imply from his name, he knows everything and is willing very much to share. You just need to connect him to your global state of data and he will immediately start its transmission to everyone who shows some interest.

A tolerant racist

This is a variant of a guard for monomorphic data structures that can operate on only one type of data, let's say on strings. By attaching a "tolerant racist" to a method which adds new values to a data structure you are guaranteed that he will accept every single value and won't throw any exceptions, but will explicitly convert every item, in our case, into a string. Keep in mind that success rate of such conversions is not necessarily high.

An abandoned child with father issues

This anti-pattern may come handy when you do not want to reinvent the wheel and predict or have been told that needed implementations are already somewhere in your program, you just do not know their exact names and locations (imagine finding yourself working with a huge new codebase).
The idea is quite simple: you specify all methods you need in a "father" object, but do not bother implementing any of them. The only implementation you need is for a method to spawn a child. When a "child" is born it immediately starts to search in a global scope for objects with similar method names its "father" possessed and once they are found - desperately copies them. I'm not gonna lie to you though, there is always a good chance of finding nothing.

Top comments (0)