The journey from global variables to managed dependency injection When writing object-oriented code, we inevitably end up with some objects that are either unique (we only want one instance of them), long-lived (they stick around for the whole time the program runs), or referenced from many different places. In these cases it's tempting to use single global instances for these objects. The problems with global instances Unfortunately, global instances contribute to making code painful to understand and difficult to change. When we use global instances, we hide our object interactions deep in the details of the code: any instance might be using any other instance, and the only way to tell is to read through every single line of code that is called. Using global instances also does nothing to encourage us to separate discrete behaviour into discrete places. When we refer to global instances we don't need to think about which classes should depend on and use which other classes, we just grab whichever global we want at a moment's notice here and there.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to

Coding