Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:42+00:00 2026-05-26T08:24:42+00:00

When reading questions on Stack Overflow or Stack Programmers, I often see answers which

  • 0

When reading questions on Stack Overflow or Stack Programmers, I often see answers which state that you should avoid (or at least have very little) global state in your programs. I’m writing a little program at the minute and I was concerned that I was introducing a lot of global state. Allow me to show you an example piece of code.

public class ContactUpdater
{
    //this is used in a few method and requires a web request so it's fairly
    //expensive to populate - therefore we only want to do it once.
    private Group _group;

    public ContactUpdater
    {
        _group = GetGroup();
    }
}

This approach meant that my code was littered with _group which felt ugly and wrong so I decided to use a property like so:

public class ContactUpdater
{
    private Group _group;
    private Group Group
    {
        get
        {
            if (_group == null)
            {
                _group = GetGroup();
            }
            return _group;
        }
    }

    public ContactUpdater()
    {
    }
}

Now I have an empty constructor and wherever I need to use the group, I now just call Group rather than _group.

I have a couple of questions:

  1. Am I using global state in both of these code snippets?
  2. Does the second example avoid global state?
  3. Is one of the code examples the preferred way of coding?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:24:42+00:00Added an answer on May 26, 2026 at 8:24 am

    As @Conrad Frix pointed out, it depends on what GetGroup() does. I would normally decouple the group from the updater with an interface and use constructor injection:

    public class ContactUpdater
    {
        public ContactUpdater(IGroup group)
        {
            _group = group;
        }
    
        private readonly IGroup _group;
    }
    

    This way ContactUpdater can be tested independently of Group and GetGroup() by passing in a fake or mock implementation of the IGroup interface.

    You can create ContactUpdater manually, passing in the result of GetGroup(), or use a dependency injection framework.

    I personally don’t use private properties for encapsulation often (I do sometimes), and I use the same underscore convention you do. I don’t have a strong opinion on this. Try both and see what’s easier to read and maintain.

    Update: Since GetGroup() calls an external service, I would absolutely decouple that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading other questions on stack overflow but nothing simply explains what
I've seen a lot of questions on that on StackOverflow, but reading the answers
I've been reading questions on Stack Overflow for a few weeks now... this'll be
I've been reading Stack Overflow questions for about 15 minutes now and every single
I have been reading up on File's Owner here on Stack Overflow and a
After reading several questions regarding problems with compilation (particularly C++) and noticing that in
I've been reading similar questions, but many of the answers are outdated or not
I've been reading the other questions dealing with master pages, but I didn't see
Reading through some of the questions here, the general concensus seems to be that
After reading a couple of answers and comments on some SQL questions here, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.