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

  • SEARCH
  • Home
  • 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 803311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:22+00:00 2026-05-14T23:44:22+00:00

When I took my first programming course in university, we were taught that global

  • 0

When I took my first programming course in university, we were taught that global variables were evil & should be avoided at all cost (since you can quickly develop confusing and unmaintainable code). The following year, we were taught object oriented programming, and how to create modular code using classes.

I find that whenever I work with OOP, I use my classes’ private variables as global variables, i.e., they can be (and are) read and modified by any function within the class. This isn’t really sitting right with me, as it seems to introduce the same problems global variables had in languages like C.

So I guess my question is, how do I stop writing classes with “global” variables? Would it make more sense to pretend I’m writing in a functional language? By this I mean having all functions take parameters & return values instead of directly modifying class variables. If I need to set any fields, I can just take the output of the function and assign it instead of having the function do it directly. This seems like it might make more maintainable code, at least for larger classes. What’s common practice?

  • 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-14T23:44:22+00:00Added an answer on May 14, 2026 at 11:44 pm

    I agree that class member variables can sometimes feel like globals in some ways. I find that the important thing is to keep the classes small, then each variable can only be accessed from a limited number of places. If you have only a few huge classes, the difference between a member variable and a global can sometimes be limited.

    Passing around too many parameters can sometimes be an issue as well, if you’re desperate to avoid all member variables you can easily end up with too many parameters that makes the code difficult to follow.

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

Sidebar

Related Questions

This semester, I took a course in computer graphics at my University. At the
I took a data structures class in C++ last year, and consequently implemented all
I would like to create a stored procedure in MySQL that took a list
I took the plunge this afternoon and began studying LINQ, so far just mucking
It took me forever to reduce the problem to this. I cannot express the
This took me a while to figure out and I found the answer on
I recently took my Db initiating code out of the __construct of my Page
I wish to calculate the time it took for an API to return a
In Windows XP, Microsoft took the decision to limit the number of half open
I called this function once per frame and it took my FPS from >400

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.