0

I am creating a global static object which will be used throughout the application. I will use the member functions inside delegates/threads. Is there any potential pitfalls in this approach?

logeeks
  • 4,268
  • 11
  • 53
  • 84
  • 4
    a potential locking bottleneck? – Mitch Wheat May 30 '11 at 09:14
  • of course there are potential pitfalls, every approach has potential pitfalls, the idea is to navigate around them whilst making your application do what you want. You didn't really tell us what your application is going to do, so we cannot tell you whether the potential pitfalls are relevant. – Bazzz May 30 '11 at 09:15
  • if you are talking about Singletons this link could help http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Arseny May 30 '11 at 09:15
  • Thanks Mitch. Can you please explain more. Any link to help will suffice – logeeks May 30 '11 at 09:20
  • What @Mitch meant was that if you have global mutable state in a multithreaded application you most likely need to lock it to access it. If many threads do this often, performance suffers. – CodesInChaos May 30 '11 at 19:49

1 Answers1

5

If the object contains no mutable state it can be fine. I like using static functions that are side effect free. (Mockists might disagree with that since you can't mock static functions as easily as interface functions.)

But global mutable state is evil. You can find plenty of articles on the issues with global variables (and their other forms like singletons, static fields,...).

If you really need some kind of global state, don't write your program with that assumption in mind. Instead have DependencyInjection inject an instance of than global state object it into your other objects. That way you can switch to multiple instances easily once the need arises. For example when running tests it's really useful.

CodesInChaos
  • 100,017
  • 20
  • 197
  • 251