Is it a good OOP practice to have static variables to store global, changing information needed by different classes?
as opposed to passing parameters around so that it can be accessed by the called classes.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s not generally a good idea, no… it can definitely simplify some things, but it makes testing harder (and means you can’t run tests in parallel, for example).
Some aspects such as logging are typically implemented like this, but I would tend to try not to do it. Dependency injection makes life much simpler in terms of testing. (It can become painful when you need to pass a dependency to class Foo just for that to pass it to Bar, which then passes it to Baz etc. I think we’re still not quite “there” in terms of dependency injection. I think something more advanced around scoping/lifecycle would be useful as part of the language, but we’ll see… I can’t see it happening in C# itself, mind you.)