I am a self-educated software engineer.
My first job developing websites was a spin off of a marketing job. I bought a book, read it over the weekend, and failed miserably at my first several attempts to build a website.
I taught myself Visual Basic and C# entirely through books and online tutorials.
I taught myself PHP by reading WordPress core code and fumbling to put together patches for the open source project.
I spent quite a few years copying and pasting really really bad code into my applications because I didn’t understand what I was doing and needed to ship to keep my job. I made mistakes. I shipped bugs. My bugs crashed entire servers and cost people money.
But I learned from these mistakes – and I want to make the world a better place for new developers.
Every language and paradigm with which I’ve worked has used its own version of the Singleton. Developers at every company I’ve been a part of have used Singletons. Tutorials in every environment I’ve touched have advocated the use of the Singleton and, rather than explaining what it is and how it works, suggested just reusing the pattern for everything.
Just about every WordPress plugin I come across these days – even those by some of the most accomplished developers in the community – has a Singleton lurking somewhere inside.
When I ask developers, “why’d you use a Singleton here,” more often than not they reference a tutorial, another developer, or “that’s how I’ve always done it.” No one ever takes the time to learn about the patterns (or anti-patterns) they’re using so they can make informed decisions about software design.
The Singleton has a (incredibly rare) real use in software development. As such, it’s not something that’s ever going to go away. To many new developers, the benefits of a Singleton are more visible than the drawbacks – so they’re quick to jump on and implement this dangerous pattern in their own code. Why? Because super-awesome-developer-X used it that one time in some code she posted to GitHub.
I don’t want everyone using Singletons.
I want anyone using a Singleton to understand what they’re doing and why.
The Cost of Education
Unfortunately, talking about anti-patterns in any sort of a positive light is frowned upon pretty universally in the community. I’m insulted and derided for documenting a Singleton pattern. Made fun of for demonstrating possible ways to work around the limitations of the pattern.
I actually received death threats the first time I advocated the anti-pattern on this site.
But the fact remains: Singletons exist, are documented in coding manuals, and are used in tutorials. New developers can and will come across these patterns during their career and will end up using them without truly understanding the implications.
I would much rather these developers find a handful of articles explaining how to safely use the pattern – and still urging them not to – when they search the Internet than find the kind of vitriol I’ve seen posted in the aftermath of my articles.
Does it mean other developers will continue to hurl insults my way? Probably.
Will it stop me? Absolutely not.