Showing posts with label anti-pattern. Show all posts
Showing posts with label anti-pattern. Show all posts

13 August 2018

This is the last time

It is August, it is hot and it is time to do some alternate work:

Bare Brickwork
Quick Fix
Looking at the picture made me think. Every industry seems to have its quick fixes: Engineers use duct tape and WD-40. In software development, we take short cuts by violating existing structure, e.g. adding another conditional resulting in convoluted logic. And in the construction industry it clearly is the usage of polyurethane or spray foam. As every tool it has its use, and like in the picture it is especially useful for temporary work. Unfortunately most professional construction workers I commissioned wanted to use it for everything - like duct tape: A hole in the wall? Why use brick and mortar when it is faster to put some foam into it. A gap in the boards? Why care to work accurately when it is easier to close the gap with foam afterwards. Not enough steam brake to cover the ceiling? Let's fill the remaining area with foam. You get the idea. While some people like the speed such quick fixes provide, e.g. Joel Spolsky praises the Duct Tape Programmer, I do not agree. Anyway, this project must come to an end and I sincerely hope it is the last time. ;-)

8 January 2012

Why Singletons Are Evil

Currently I'm working on enabling automated unit testing in a legacy code base but I am seriously hindered by the singleton design pattern which is very common in the code base in the form of managers. A quick sweep with a static code analysis tool reveals that 1207 out of 4787 classes depend on such (singleton) managers. See the nice graph generated with GSD to get an idea of these dependencies:singletons in current project

Singletons Are Evil
This means that 25% of all classes in the code base are virtually impossible, or at least very hard to unit test. Testing gets difficult as the singletons' global state has to be initialised for each test. This slows down test execution and makes the tests more brittle. That's one of many reasons I just hate the singleton pattern. It's an object oriented anti-pattern. In case you do not believe me, I am sure you will believe well known people like Miško Hevery, Uncle Bob or J.B. Rainsberger to name just a few. At least read Steve Yegge's take on the singleton which is my favourite article on the topic.

Pure Evilness
Some years ago I worked on a code base which was similar both in size and the number of singletons involved. In fact it was not that bad, only 15% of all classes were depending on a singleton, still it was nasty to work with.singletons in a previous projectSo I took the liberty to educate my team mates on the singleton's evilness. The following list was taken from my Design Patterns reading group notes and contains all negative effects of singletons that I'm aware of. Singletons are evil because they ...

... introduce global state/global variables.
  • ... hurt modularity and readability.
  • ... make concurrent programming hard.
  • ... encourage you to forget everything about object oriented design.
... break encapsulation/increase coupling.
  • ... are a throwback to non object oriented programming.
  • ... allow anyone to access them at any time. (They ignore scope.)
  • Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
  • They typically show up like star networks in a coupling diagram.
  • ... make assumptions about the applications that will use them.
  • If nobody is going to use them they are basically just memory leaks.
... cause coding/design problems.
  • Signatures of methods (inputs) don't show their dependencies, because the method could pull a singleton out of thin air.
  • All singletons have to stick in boilerplate code.
  • Everyone who uses it has to stick in boilerplate code, too.
... make code hard to test.
  • When classes are coupled, it is possible only to test a group of classes together, making bugs more difficult to isolate.
  • ... can prevent a developer from testing a class at all.
  • Two tests that actually depend on each other by modifying a shared resource (the singleton) can produce blinking tests.
... prevent you from using other code in place of production implementations.
  • Mocking would make unit tests run more quickly.
  • It is simpler to simulate behaviour than to recreate behaviour.
So always remember that Singletons are Evil!