views:

512

answers:

7

So ....

There are obviously many questions that all have been asked about Singletons, Global State Variables, and all that great stuff. My question is,

If Singletons and Globals are So Bad, Why are they used so often?

The following examples are simply ones off the top of my head that I believe are used by a good chunk of people.

I give you a function from CodeIgniter that uses a psuedo-singleton function:

(system\codeigniter\Common.php Line 89)

/**
* Class registry
*
* This function acts as a singleton.  If the requested class does not
* exist it is instantiated and set to a static variable.  If it has
* previously been instantiated the variable is returned.
*
* ......
*/
function &load_class($class, $instantiate = TRUE)
{
    static $objects = array();

    // Does the class exist?  If so, we're done...
    if (isset($objects[$class]))
    {
        return $objects[$class];
    }
  .......
}

By placing every object into a single registry, you can't use their load_class function to create multiple instances of anything. This is especially inconvenient when you want to use classes as data structures.

Also, because there is only one instance of all those classes, it leads into the argument against Global State. Which leads me to .....

The entire Wordpress System, which runs mainly on global variables. All of the data for looping through posts is strewn about in various globals.

(wp-includes\query.php Line 2644)

/**
 * Setup global post data.
 *
 *....
 */
function setup_postdata($post) {
    global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;

    $id = (int) $post->ID;

    $authordata = get_userdata($post->post_author);
    ....
}

These are just two main examples of Frameworks that use Singleton/Globals as the basis for their entire system!

So .. is it just because these systems haven't caught up to OOP methodology? It just doesn't make sense when you have so many people telling you not to use Global Variables or Singletons, to make your entire system based on said practices.

There of course is the argument about backwards-compatibility with PHP4. I still think that there were ways to do OOP programming in PHP4, as classes were still available.

+2  A: 

People discourage the use of global variables because it increases the likeliness of bugs. It's so much easier to make a mistake somewhere if every function in your program accesses the same global variables, and it's much harder to debug. It's also much harder to test for.

I imagine they're still used so often because programmers are lazy. We don't want to spend time up-front making the code all organized and pretty, we just want to get the job done. It's so much easier to just write a global function/variable/whatever than to modularize it, and once you've started down that path it's too much of a pain to go back and refactor. Maybe that's the reason: They started out that way and simply never turned back.

musicfreak
I get the concept of Global Variables. My question is, why is there so many of them used in popular systems.
Chacha102
I don't think he was asking why singletons are bad, but why they keep getting used by high-profile projects.
Matt Olenik
I think I mentioned that in my second paragraph, did I not?
musicfreak
Now you did. Before there was only part of the first paragraph.
Chacha102
Oh sorry, I tend to make incremental edits. :)
musicfreak
Same Here :) My Original Answers are almost never close to the end product.
Chacha102
+2  A: 

Probably because of the Design Patterns book by the GoF. It's become too pervasive and people treat it as infallible.

Matt Olenik
+3  A: 

Reasons in PHP4-based apps like WP or CI are partially due to PHP4's worse support of OOP constructs.

Globals and singletons are also simple: It requires much less thinking to slap something in a global than building it with proper OOP practices. It's simpler to access them too, just point the code at the name and that's it instead of needing to pass the object in from somewhere else.

One negative side effect of global state (global variables, singletons, etc.) is that it makes things much more difficult to unit test.

ps: In my experience wordpress in general has a pretty poor quality of code. I would not use it as a metric of anything...

Jani Hartikainen
In some cases it is pragmatic to use a global variable or singleton to make it easier to re-use resources to increase efficiency. For example, keeping a single global database connection throughout the lifetime of a script. However, having looked at some of WordPress' code, in places it uses global variables liberally, making broad assumptions about the context in which functions will be called, and that's just clueless design.
Ben James
I was just pointing out Wordpress as it is used by many, many people.
Chacha102
+4  A: 

Because it's relatively easy to work with singletons, and working without takes much more detailed planning of your application's structure. I asked a question about alternatives some time ago, and got interesting answers.

Pekka
+2  A: 

If Singletons and Globals are So Bad, Why are they used so often?

I think the gist of it is that Singletons make things easy. At least at first sight. I don't have enough experience with the matter to say anything more useful, however I found the following a good read:

Singleton Considered Stupid (Steve Yegge)

mkrause
A: 

In the process of cleaning a Singleton would represent hiding the dirt in the dark corner instead of cleaning.

It's so widely used because it does it's job of hiding problems very well.

It's a good thing for those who are to clean, but don't want to.

It's a bad thing for those who are up to actually clean something.

Consider Dependency Injection http://martinfowler.com/articles/injection.html if a Singleton is causing a problem you can't hide anymore.

George Polevoy
A: 
  1. Global variables and singletons are popular because they are simple and convenient.
  2. Dependency injection, the only reasonably convenient replacement for global things, is still rather unknown in the PHP-community.
  3. The PHP-community often prefers simple hacks over proper solutions.
  4. Many PHP-developers are quite clueless about programming and know barely enough to make their programs work.
Waquo