13

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!

Darren Young
  • 2,175

5 Answers5

33

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.

9000
  • 24,342
8

I can't comment your specific case, but using global variables is sometimes a good solution. Java's System class is full of static, global variables.

But in general, if you think you're right and your boss is wrong, then why don't you ask your boss to explain why he thinks a global variable is a better solution? A plain "I don't like it" is as vague as "global variables are evil". You must demand more from your boss!

If he can't give reasons for his stance, and you can give reasons to your stance, or vice versa, then it's a good learning opportunity. If both of you can justify your stances, then it's probably a matter of taste - or matter of experience: your boss must have been programming professionally more than 4 months, I think?

5

As someone in your position, the most you can do is learn. If the object is essentially a constant (i.e. it can't change and it doesn't maintain state), then your boss may be right. There is no harm in having a static constant object. After all, how many definitions of PI are there?

Some people defend the "no global objects" mindset to the point of religious ferver. That's for one of two reasons: they've been bit by difficult to track bugs or system fragility due to overuse of global variables/objects, or they've heard that it's bad and they can't think for themselves. I personally fall into the group of people that have been hit hard by fragility and hard to track bugs--but I've also learned some balance here.

If I were in your shoes, I'd go ahead and do what the boss says--it's his butt on the line after all. Then I'd watch and see the effects of that choice over time. This is an effective learning tool.

2

Depending on the specific situation, a global variable might be the best solution. In embedded programming, globals are not occupying stack space and for that reason a good choice.

Globals or not, I have found it a good praxis to put a prefix on globals. This will make other programmers googling your code aware of the impact if the mess with it.

1

Maybe what your boss wanted was you to use the Singleton Pattern. Its a very good practice when you need to access a certain object from multiple instances.

Here is a link to the wikipedias explanation

guiman
  • 2,088
  • 13
  • 17