What to say to your boss if they want you to use a global variable

coding-standardssingletonteamwork

I am currently 4 months into an internship, and when reviewing my code, my boss didn't like that I had kept a specific object local to a number of methods across a few separate classes within one assembly. He didn't like that I was created a new object each time, and instead told me to create a single object that can be accessed from anywhere. I have therefore had to create it as a static object within a static class, and simply reference it from here I want to use it!

How would you deal with this, as I have only been programming professionally for 4 months!

Best Answer

If one object is enough, creating an object every time is a waste, and here your boss may be right.

The problem is proper access to that object. A factory-like method with proper visibility which always returns that static object is the first solution that springs to mind. Others certainly exist.

Related Topic