Things That Matter Most

Things That Matter Most

  • Home
  • Business
  • Faith
  • Technology
  • Politics
  • Creative Writing
  • Email
  • GitHub
  • RSS
  • Twitter

Powered by Genesis

Why I Advocate “Anti-Patterns”

March 7, 2014 by Eric

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 taught myself JavaScript by printing out every visual quickstart guide I could find for YUI and jQuery and forcing myself to memorize them.

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.

The Singleton

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.

Filed Under: Technology Tagged With: anti-pattern, singleton

Like what you’re reading?

Consider pitching in to help cover the costs of maintaining this site and keeping my work 100% ad-free.

Close×

Comments

  1. Mike Bijon says

    March 7, 2014 at 10:17 am

    +1 for everyone learning not only what, but why.

    It’s an huge part of moving from being a developer who just writes code to one who solves problems. Ultimately there are loads of problems that could be solved in 10 lines of code vs. 1,000 if we take enough time to talk to the users/clients and understand their usage patterns as well as we understand our own dev patterns.

    I’d highly recommend this ‘Apprecticeship Patterns’ book on meta patterns, http://www.amazon.com/Apprenticeship-Patterns-Guidance-Aspiring-Craftsman/dp/0596518382/. It pulls back from learning ever more-complicated code patterns and helps break habit many devs develop as their day-to-day community becomes more & more technical & Sr.

    • Eric says

      March 7, 2014 at 10:57 am

      I frequently recommend that book, too 🙂

      I’m a huge fan of some more complicated code patterns (I really like the repository pattern for data factories), but getting people to understand a complicated pattern when they’re used to just copy-pasting code to ship on schedule is difficult. Just look at all of the resistance the development community has had to refactoring code for testability. Unit testing is not a new concept, but is often treated as such because, as a community, we’re lazy.

      “If it ain’t broke, don’t fix it” is very much what I hear when someone asks, “well it’s worked so far, why take the time to rebuild it?”

      • Mike Bijon says

        March 7, 2014 at 11:03 am

        Well, you are a C# dev, so those code patterns… 😉

        As for testability, I always respond about prevention. Something along the lines of it works now, but what if you could prevent it from breaking, or help some other dev to get an alert if they do break it … “You do debug your own code right? Why not help someone else to debug theirs?” Good interview question too. There’s not really a right answer, but their attitude reveals just how much they want to make things better vs. check a box & move on.

About Me

Eric is a storyteller, web developer, and outdoorsman living in the Pacific Northwest. If he's not working with new tech in web development, you can probably find him out running, climbing, or hiking in the woods.

Get the newsletter