Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Accessing variables

Accessing variables

Scheduled Pinned Locked Moved The Lounge
question
14 Posts 10 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BrockVnm
    wrote on last edited by
    #1

    I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


    We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

    C M L N B 9 Replies Last reply
    0
    • B BrockVnm

      I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


      We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

      C Offline
      C Offline
      Craster
      wrote on last edited by
      #2

      He's right. For example, if you need to change something within your library, such as a datatype, you can make the necessary updates to handle the changes within the get/set functions, meaning that you never have to touch the code that's calling the library. If you access the variables directly, then if you make a change to that variable type in your library, you then have to change every single app that references that library.

      P 1 Reply Last reply
      0
      • B BrockVnm

        I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


        We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        BrockVnm wrote:

        He says that you should always use get and set functions.

        I totally agree, here's a subset of reasons: allows the class to change functionality without the application knowing/caring necessary for databinding to work in C# necessary for events to work on value changes allows internal implementation, such as data type, to vary independently of the application. allows for specialization (well, if the base class property setters/getters were virtual) interfaces support get/set (again C#), so you can use the class in a highly decoupled manner Marc Pensieve Functional Entanglement vs. Code Entanglement Static Classes Make For Rigid Architectures

        M 1 Reply Last reply
        0
        • B BrockVnm

          I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


          We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I agree - "data hiding" as some call it is well worth the effort, as you can change the implementation later without breaking other code that relies on your classes. Plus you can choose to make some member variables read-only by only implementing a "Get" function, etc. Back when I was first dabbling with C++, I couldn't be bothered to spend time adding Get/Set functions and I came to regret it. You can also expand this some more - should, for example, code within the class use Get/Set methods to access the variables? I know someone doing a Java course at University and they insist that "getters/setters" are always used, and the only place a variable is referenced directly is when it is initialised in the constructor! Sure, there would be a performance hit, but if this isn't an issue, I know one or two people that would argue that this is a good thing. Also, what about derived classes? Do you make the variables protected, or leave them private and make the derived classes use Get/Set methods too? (I prefer the latter approach myself, but it is often a matter of taste).


          The Rob Blog
          Google Talk: robert.caldecott

          1 Reply Last reply
          0
          • B BrockVnm

            I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


            We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            I am not sure, but here's Bjarne Stroustrup's opinion on the matter[^]: If every data can have any value, then it doesn't make much sense to have a class. Take a single data structure that has a name and an address. Any string is a good name, and any string is a good address. If that's what it is, it's a structure. Just call it a struct. Don't have anything private. Don't do anything silly like having a hidden name and address field with get_name and set_address and get_name and set_name functions. Or even worse, make a virtual base class with virtual get_name and set_name functions and so on, and override it with the one and only representation. That's just elaboration. It's not necessary.


            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

            1 Reply Last reply
            0
            • B BrockVnm

              I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


              We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

              B Offline
              B Offline
              BrockVnm
              wrote on last edited by
              #6

              I feel alittle bit better now. Thanks everyone!


              We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

              1 Reply Last reply
              0
              • B BrockVnm

                I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


                We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

                A Offline
                A Offline
                Alvaro Mendez
                wrote on last edited by
                #7

                Unless they're declared const, you should never make member variables publicly accessible. If you're working with a language that supports properties (like the .NET ones), it's not an issue because you can create properties for your fields and syntactically it will look as if you're accessing the variables directly. The compiler may even optimize the call to a direct access to the variable. Regards, Alvaro


                ... since we've descended to name calling, I'm thinking you're about twenty pounds of troll droppings in a ten pound bag. - Vincent Reynolds

                1 Reply Last reply
                0
                • M Marc Clifton

                  BrockVnm wrote:

                  He says that you should always use get and set functions.

                  I totally agree, here's a subset of reasons: allows the class to change functionality without the application knowing/caring necessary for databinding to work in C# necessary for events to work on value changes allows internal implementation, such as data type, to vary independently of the application. allows for specialization (well, if the base class property setters/getters were virtual) interfaces support get/set (again C#), so you can use the class in a highly decoupled manner Marc Pensieve Functional Entanglement vs. Code Entanglement Static Classes Make For Rigid Architectures

                  M Offline
                  M Offline
                  Member 96
                  wrote on last edited by
                  #8

                  Yeah, your subset reasons are more realistic than the generally given vague reason of hiding private member variables. To be honest though 99% of the time it's a waste to bother with the accessors for most stuff. I always do it that way, but for most code I always feel like "I'm never going to touch this again, why bother?" To me it's sort of a flaw in the language, you have to do it so often it's a lot of typing for little real world gain in most cases, there should be some other way to do the same thing by thinking outside the box in the language design itself.

                  M 1 Reply Last reply
                  0
                  • B BrockVnm

                    I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


                    We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I once had to fix program with the following global veriables: a, b, c, d, e, f, g, h, i :doh: The tigress is here :-D

                    M 1 Reply Last reply
                    0
                    • B BrockVnm

                      I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


                      We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #10

                      Only on exported types IMO, and that serves as a protective/notification measure. For internal (.NET) types, public fields are acceptable if you dont need measure above.

                      xacc.ide-0.1.3.2

                      1 Reply Last reply
                      0
                      • M Member 96

                        Yeah, your subset reasons are more realistic than the generally given vague reason of hiding private member variables. To be honest though 99% of the time it's a waste to bother with the accessors for most stuff. I always do it that way, but for most code I always feel like "I'm never going to touch this again, why bother?" To me it's sort of a flaw in the language, you have to do it so often it's a lot of typing for little real world gain in most cases, there should be some other way to do the same thing by thinking outside the box in the language design itself.

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #11

                        John Cardinal wrote:

                        To be honest though 99% of the time it's a waste to bother with the accessors for most stuff.

                        True. I'm slowly getting into the habit of providing OnxxxChanged event capability to a lot of properties, because the stuff I do often involves a lot of data binding, even when it's not visual properties.

                        John Cardinal wrote:

                        you have to do it so often it's a lot of typing

                        Indeed. Snippets are sort of useless for that, so I use QuickCode, which is adequate.

                        John Cardinal wrote:

                        to do the same thing by thinking outside the box in the language design itself.

                        Some of the AOP concepts come to mind. But yes, I imagine people 50 years from now will look at coding and roll their eyes the way we flinch looking at how programming was done by toggling switches 50 years ago. Marc Pensieve Functional Entanglement vs. Code Entanglement Static Classes Make For Rigid Architectures

                        1 Reply Last reply
                        0
                        • B BrockVnm

                          I have a question for everyone here. I am not sure if their is a proper way that this should be done or if it is more of a style thing. I have a senior developer that I work with that told me that you should never access any type of global/instance variables directly. He says that you should always use get and set functions. I was just wondering what every bodies take on this is. Thanks!! :-D


                          We shouldn't assume something's debugged just because everyone in the whole world has access to the source code.

                          C Offline
                          C Offline
                          cmk
                          wrote on last edited by
                          #12

                          First off, you should try to eliminate all global variables. Writing get/set functions for them shouldn't even be an issue. Second, to dogmatically make every class variable private/protected and provide a get/set for them is not the 'right way' (it is however a very pure OO way), there is no universal 'right way'. As a developer you are expected to think, understand, and implement the best solution for the situation. A given methodology is just another tool; you don't always have to use the same one. As a general rule i only hide a class variable and/or provide get/set methods for them if their value is intimately tied to other state (i.e. changing their value requires validation of other state), or a change in their value requires other code to be executed. For example,

                          class DbRecEmp {
                          ...
                          string NameFirst;
                          ...
                          };

                          In almost every situation where this type of case has come up it has made no sense to make NameFirst private/protected and provide GetNameFirst()/SetNameFirst() methods. ...cmk Save the whales - collect the whole set -- modified at 13:25 Thursday 9th March, 2006

                          1 Reply Last reply
                          0
                          • L Lost User

                            I once had to fix program with the following global veriables: a, b, c, d, e, f, g, h, i :doh: The tigress is here :-D

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #13

                            Jewish programmers get two more letters than English ones. I'm jealous. :rolleyes: Marc Pensieve Functional Entanglement vs. Code Entanglement Static Classes Make For Rigid Architectures

                            1 Reply Last reply
                            0
                            • C Craster

                              He's right. For example, if you need to change something within your library, such as a datatype, you can make the necessary updates to handle the changes within the get/set functions, meaning that you never have to touch the code that's calling the library. If you access the variables directly, then if you make a change to that variable type in your library, you then have to change every single app that references that library.

                              P Offline
                              P Offline
                              ProffK
                              wrote on last edited by
                              #14

                              So why don't you have to change the type of the accessor functions if you change the type of the variable? I used to get high on life until I realized that life was cut with morons - Unknown

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups