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

When I first became interested in programming, I took a class that introduced me
First of all I have to admit that my programming skills are pretty limited
I took my first 'fundamentals of programming' lab session at uni today. One thing
First of all a statement: I'm a newbie when it comes to programming for
Okay first of all, I admit I took elements from this question here: How
everyone, first of all, i have googled yet for this question, and took a
I took a look in the developer's console and saw for the first time
it took me some time to find out that both Eclipse and Aptana get
I took over an old HTML based site with all hard coded links, no
Last year I dabbed in a bit of perl programming. The first thing I

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.