# Which of these is a better programming practice?

**URL:** <https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201>\
**Category:** In My Humble Opinion\
**Created:** [August 2, 2011, 5:59pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201 "2011-08-02T17:59:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Swords\_to\_Plowshares](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Swords\_to\_Plowshares](https://boards.straightdope.com/u/Swords_to_Plowshares)\
**Post date:** [August 2, 2011, 5:59pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/1 "2011-08-02T17:59:10Z")

</div>

Say you’re writing a small program. It runs a main method, executes a bunch of stuff, and spits out an output. Say it’s in Java for now, since that is what I am doing at the moment.

You have some main data structure that is going to be referenced by the main method a bunch of times and also a bunch of times by some supporting methods. Should the data structure be a global variable in the Main class, or should it be created inside of the main method and then passed to other methods as an argument?

---

<div class="post-metadata">

**Author:** ![bup](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bup](https://boards.straightdope.com/u/bup)\
**Post date:** [August 2, 2011, 6:01pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/2 "2011-08-02T18:01:41Z")

</div>

I’d probably make it a singleton. Just reference it, and get the one instance.

---

<div class="post-metadata">

**Author:** ![Swords\_to\_Plowshares](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Swords\_to\_Plowshares](https://boards.straightdope.com/u/Swords_to_Plowshares)\
**Post date:** [August 2, 2011, 6:06pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/3 "2011-08-02T18:06:56Z")

</div>

So I guess what you’re saying is, if it’s going to be used a lot everywhere, make it global, but if it’s rarely used outside of the main method, just define it in the main method?

---

<div class="post-metadata">

**Author:** ![bup](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bup](https://boards.straightdope.com/u/bup)\
**Post date:** [August 2, 2011, 6:10pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/4 "2011-08-02T18:10:27Z")

</div>

Or, make it its own thing. Self-instantiating upon reference. Doesn’t belong to the main.

On the other hand, testing of many smaller methods might be easier if you did pass in the data structure.

Is your methodology mostly procedural, it sounds like?

---

<div class="post-metadata">

**Author:** ![Swords\_to\_Plowshares](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Swords\_to\_Plowshares](https://boards.straightdope.com/u/Swords_to_Plowshares)\
**Post date:** [August 2, 2011, 6:20pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/5 "2011-08-02T18:20:53Z")

</div>

Yes, it is. This is a very small project. Actually a coding exercise for a job application.

---

<div class="post-metadata">

**Author:** ![HMS\_Irruncible](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/hms_irruncible/32/7394_2.png) [@HMS\_Irruncible](https://boards.straightdope.com/u/HMS_Irruncible)\
**Post date:** [August 2, 2011, 6:38pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/6 "2011-08-02T18:38:30Z")

</div>

There is no such thing as a “global” variable in Java. There are local variables, instance variables and there are class variables. If multiple methods depend on it then storing it as an instance method is the best bet. But typically you want to see the Main class being pretty lean, having just a main method, and delegating the heavy lifting to other objects.

---

<div class="post-metadata">

**Author:** ![Swords\_to\_Plowshares](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Swords\_to\_Plowshares](https://boards.straightdope.com/u/Swords_to_Plowshares)\
**Post date:** [August 2, 2011, 6:43pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/7 "2011-08-02T18:43:32Z")

</div>

Class variable. Sorry. Forgot the term.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 2, 2011, 7:11pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/8 "2011-08-02T19:11:42Z")

</div>

It really depends on the problem. If this is something really fundamental to the problem, such that everything you’d conceivably be using this program with would also use it (or at least not confuse it with anything else), then make it global, or whatever your language’s equivalent of that is. If it’s just for bookkeeping between a few program modules, and you’d be fitting this piece in with other pieces that wouldn’t know nor care what that variable is, then keep it local and pass it through as needed. For anything in between, use your best judgment.

Given that this is just an exercise, you’ll probably never actually have to interface it with anything else, so either way should work just fine. But given that it’s for a job application, I’d recommend the local approach: That’s by far the one you’d want most often, and whoever’s hiring you is probably looking for whether you know how to do it. Plus, of course, if you’re programming professionally, modularity is important, and you’ll probably have to make your code work with code written by others, which will be a lot easier with everything local (or at most, the head programmer on a project will start off by deciding that a small number of variables will be global, and telling everyone which ones).

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [August 2, 2011, 8:22pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/9 "2011-08-02T20:22:14Z")

</div>

> [@bup](#):
>
> I’d probably make it a singleton. Just reference it, and get the one instance.

Agreed - I use the singleton for exactly things like this.

---

<div class="post-metadata">

**Author:** ![CaveMike](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cavemike/32/16379_2.png) [@CaveMike](https://boards.straightdope.com/u/CaveMike)\
**Post date:** [August 2, 2011, 8:36pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/10 "2011-08-02T20:36:55Z")

</div>

My rough rule is: If the supporting functions are going to use most of the fields of the data structure, then pass it around as an argument. If they are each going to access parts of the data structure, then make it a member variable.

If this was just a test app for personal use, then a singleton would be fine. Since it is a job interview, I would avoid a singleton at all costs. Some developers fiercely oppose singletons. And as **Chronos** mentioned, this example would demonstrate the mechanisms of working within a larger codebase.

---

<div class="post-metadata">

**Author:** ![FoundWaldo](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@FoundWaldo](https://boards.straightdope.com/u/FoundWaldo)\
**Post date:** [August 2, 2011, 9:06pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/11 "2011-08-02T21:06:36Z")

</div>

> [@CaveMike](#):
>
> Some developers fiercely oppose singletons.

For example [steve yegge - singleton-considered-stupid](http://sites.google.com/site/steveyegge2/singleton-considered-stupid)

---

<div class="post-metadata">

**Author:** ![CaveMike](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cavemike/32/16379_2.png) [@CaveMike](https://boards.straightdope.com/u/CaveMike)\
**Post date:** [August 2, 2011, 9:38pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/12 "2011-08-02T21:38:54Z")

</div>

> [@FoundWaldo](#):
>
> For example [steve yegge - singleton-considered-stupid](http://sites.google.com/site/steveyegge2/singleton-considered-stupid)

Thanks **FoundWaldo**. That was the article I was thinking of, but I couldn’t remember enough of it to find it.

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [August 2, 2011, 9:59pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/13 "2011-08-02T21:59:54Z")

</div>

If you are going to avoid something in an interview because \*some \*programmers may oppose it, don’t bother going, you won’t be able to say _anything_.

Programming is like that.

I just got a gig, so I have had some interviews recently. In at least two of them they tested my knowledge of patterns by asking me how to implement a singleton in C# (they also wanted to be sure it was thread safe and one programmer wanted to talk about lazy instantiation.)

In fact, the \*patterns \*book seems to be a popular place from which to snag interview questions (singletons and factories were two common ones.)

Having said that: I’m no longer sure I would use a singleton for this problem. As is (too) common for me, I only \*skimmed \*the original question… my apologies to the OP for that.

---

<div class="post-metadata">

**Author:** ![Roadfood](https://avatars.discourse-cdn.com/v4/letter/r/9fc348/32.png) [@Roadfood](https://boards.straightdope.com/u/Roadfood)\
**Post date:** [August 2, 2011, 11:04pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/14 "2011-08-02T23:04:28Z")

</div>

> [@FoundWaldo](#):
>
> For example [steve yegge - singleton-considered-stupid](http://sites.google.com/site/steveyegge2/singleton-considered-stupid)

Yeah, yeah, using singletons is a religious argument, right up there with whether Macs are better than PCs. That article is no more _right_ than my firm belief that Windows just sucks.

There are times when a singleton is perfectly appropriate, and will make everyone’s life easier than having to pass something around _everywhere_. Truly good software engineers do not subscribe to absolute, dogmatic rules like “Never use a singleton!” A good engineer realizes that the right answer to “Should we use a singleton for this?” is _always_: “it depends”.

For every one of his “what if’s” (“what if your Singleton has a handle to some limited resource”), there can easily be a case where it’s n/a (“In this case, our Singleton does not have a handle to anything”). Or “the Singleton design is syntactically noisy”; um, compared to requiring that nearly every single class constructor, and many methods, pass around the thing that otherwise would be a singleton? Or “It’s almost impossible to subclass a Singleton”; ok, but we don’t need to subclass it. Etc.

The bottom line is that good software engineering requires judgment, not dogmatic rules. In this particular case, the OP does not have sufficient information to be able to answer the question with _any_ degree of certainty. So the answer, really, is “it depends.” (A tiny sample of the questions that it depends on: “What is going into this data structure?” “How big is your overall program?” “How many classes/methods will need access to this data structure?” “What are the chances that you will be adding to this data structure?” “What might you add to it?” “Will it reference some limited resource?” etc., etc., etc.)

---

<div class="post-metadata">

**Author:** ![CaveMike](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cavemike/32/16379_2.png) [@CaveMike](https://boards.straightdope.com/u/CaveMike)\
**Post date:** [August 4, 2011, 2:25pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/15 "2011-08-04T14:25:33Z")

</div>

> [@Khadaji](#):
>
> If you are going to avoid something in an interview because \*some \*programmers may oppose it, don’t bother going, you won’t be able to say _anything_.

This isn’t good advice in this case. In an interview, there is a dialog – you can make your assertions and back them up. You have an opportunity to overcome the interviewer’s bias.

But the OP isn’t asking about an interview; he is asking about a pre-interview program that is likely being used as a interview filter. For that, you want to do what you can to get in the door. Part of this includes avoiding obvious traps and pitfalls.

I outlined what I would use instead of a singleton. I mentioned that a singleton might be a pitfall. I am not sure why you exaggerated that to “If you are going to avoid something in an interview because _some_ programmers may oppose it”.

---

<div class="post-metadata">

**Author:** ![Gary\_Robson](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/gary_robson/32/3448_2.png) [@Gary\_Robson](https://boards.straightdope.com/u/Gary_Robson)\
**Post date:** [August 4, 2011, 4:01pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/16 "2011-08-04T16:01:08Z")

</div>

[moderating]  
There isn’t really a GQ answer for this (coding style is subjective), so I’ve moved the thread to IMHO.  
[/moderating]

---

<div class="post-metadata">

**Author:** ![Gary\_Robson](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/gary_robson/32/3448_2.png) [@Gary\_Robson](https://boards.straightdope.com/u/Gary_Robson)\
**Post date:** [August 4, 2011, 4:00pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/17 "2011-08-04T16:00:53Z")

</div>

[moderating]  
There isn’t really a GQ answer for this (coding style is subjective), so I’ve moved the thread to IMHO.  
[/moderating]

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 4, 2011, 4:37pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/18 "2011-08-04T16:37:37Z")

</div>

Y’know, **Gary** , you could have used a singleton post there.

😉

---

<div class="post-metadata">

**Author:** ![Gary\_Robson](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/gary_robson/32/3448_2.png) [@Gary\_Robson](https://boards.straightdope.com/u/Gary_Robson)\
**Post date:** [August 4, 2011, 8:24pm UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/19 "2011-08-04T20:24:17Z")

</div>

I really need to create a [moderating] object, don’t I?

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 6, 2011, 7:46am UTC](https://boards.straightdope.com/t/which-of-these-is-a-better-programming-practice/591201/20 "2011-08-06T07:46:51Z")

</div>

One thing that I’ve noticed from programming (especially in Java) is that **void** methods that alter the state of an argument are often very dangerous, even with proper contracting. Generally with an OO language, you want a method that’s a mutator to act on the object it’s attached to, not the objects its given.

Performing foo.bar(baz) (assuming it’s a mutator, and not to retrieve info) should **use** baz to **alter** foo with bar as a facilitator. For this reason, I wouldn’t pass the data structure into everything. There are, of course, exceptions to this, mostly in the case of static functions to be used as “library” functions, for instance, Collections.sort() isn’t going to modify Collections, that’s ridiculous, since Collections is a class\*, but it is a library function meant to operate on Objects that implement the Collections interface. However, you usually want these to be defined in the super class, or the data structure itself, not in some random place like Main.

For this reason, I’d say use either an encapsulating mutator class with the methods you need, and pass the data structure in the constructor:

Foo bar = new Foo(dataStructure);  
foo.doStuff(args)  
foo.doMoreStuff()  
System.out.println(foo.getStuff());

Or make the data structure itself a Singleton, or register it with a Blackboard\*\*, and have the functions work within those confines. Obviously, like everything, there are situations where violating this may make things cleaner, but for a pre-interview question theory is often more important, you can pontificate on the drawback of a philosophy in an interview where you have a chance to debate.

If I misunderstood, and you’re simply using the data structure to calculate things, without mutating it, then it’s probably fine (and even encouraged) to pass it in.

- Okay, let’s not deal with reflection.  
\*\* Well, okay, Blackboards themselves are usually Singletons, but that’s not the point.
