Thread: Getters/Setters - Good or Bad

Page 1 of 5 123 ... LastLast
Results 1 to 10 of 43
  1. #1 Getters/Setters - Good or Bad 
    Registered Member

    Join Date
    Sep 2011
    Posts
    1,140
    Thanks given
    385
    Thanks received
    397
    Rep Power
    474
    A lot of people say they are good, but some people say they should never be used. I haven't decided which side I am on, but what's all of your opinions on the subject?
    Reply With Quote  
     

  2. #2  
    Banned

    Join Date
    May 2011
    Posts
    915
    Thanks given
    20
    Thanks received
    178
    Rep Power
    0
    'never'

    everything has its purpose and ofc it should be used but only when needed to.
    Reply With Quote  
     

  3. #3  
    Registered Member
    Whired's Avatar
    Join Date
    Aug 2007
    Posts
    2,126
    Thanks given
    238
    Thanks received
    501
    Rep Power
    822
    I thought about this for a while too. It never made sense to me until I went to write my own library..


    Here's how I see it:
    Use a mutator/setter if
    • There is a limit on the value (IE > x, < x, == x)
    • There is some event to be raised when the value changes

    Use an accessor/getter if
    • Access is restricted (IE private only, protected only)
    • The value is read-only (outside code can use this variable, but not change* it)
      * "Change" used loosely. Doing this does not make the variable immutable, only stops an explicit assignment (IE: getInt() = 7; won't work)




    Most times it's good practice to just always use them because a lot of time at least one of the above requirements are met.
    [Only registered and activated users can see links. ]
    Reply With Quote  
     


  4. #4  
    Banned
    Join Date
    Aug 2011
    Posts
    370
    Thanks given
    50
    Thanks received
    57
    Rep Power
    0
    I've never heard of someone who says that they are bad.
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Sep 2011
    Posts
    1,140
    Thanks given
    385
    Thanks received
    397
    Rep Power
    474
    Quote Originally Posted by Omer View Post
    I've never heard of someone who says that they are bad.
    [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  6. Thankful user:


  7. #6  
    Registered Member Drain's Avatar
    Join Date
    Mar 2011
    Posts
    35
    Thanks given
    0
    Thanks received
    0
    Rep Power
    1
    i think there useful distinguishing variables upon variables. its always good programming practice to promote encapulation.
    Reply With Quote  
     

  8. #7  
    ̿ ̿|̿ ̿ |̶ ̶ ̶ ̶| |̶͇̿ ̶͇̿ ͇̿ Haskelle

    Join Date
    Nov 2007
    Age
    22
    Posts
    1,228
    Thanks given
    329
    Thanks received
    517
    Rep Power
    1133
    you should only encapsulate fields when you do checks on them before returning. but because then some fields will be accessed through accessors and mutators, and some not the API isn't clear so we usually choose to encapsulate all fields to have a clear interface to the programmer.
    Monads are just Monoids in the category of Endofunctors. What is the problem?
    Costate Comonad Coalgebra is equivalent of Java's member variable update technology for haskell. Problem?
    Reply With Quote  
     

  9. Thankful user:


  10. #8  
    Registered Member
    Teemuzz's Avatar
    Join Date
    Oct 2009
    Posts
    2,661
    Thanks given
    1,087
    Thanks received
    384
    Rep Power
    550
    Concurrency reasons are the best reason to use them tbh.
    Iirc the word for it was mutability
    I'm back.
    ScapeEmulator #592 Convert [Only registered and activated users can see links. ]/[Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  11. #9  
    Registered Member

    Join Date
    Jan 2008
    Posts
    2,663
    Thanks given
    492
    Thanks received
    626
    Rep Power
    955
    The people that say they are good are talking about using getters/setters as opposed to accessing the variable directly. Whired listed the problems. The people that say they are bad are talking about (from what I can gather from the links you provided) exposing implementation details.
    In an ideal object oriented system, you shouldn't have to get the data from an object and then perform some operation with that data. The right thing to do would be to ask the object who owns the data to perform the operation itself.
    A general rule of thumb, but don't blindly follow it. The idea really is that an elegant interface doesn't expose implementation details.
    ~iKilem
    Reply With Quote  
     

  12. Thankful users:


  13. #10  
    Valar Morghulis

    Laxika's Avatar
    Join Date
    Sep 2006
    Age
    26
    Posts
    2,814
    Thanks given
    1,808
    Thanks received
    276
    Rep Power
    2128
    Getters and setters are usually good. If Jages used the getters and setters instead direct accest to the username, then they shouldn't replace the username variable in 20000 place when name changing came out... That's it...

    [Only registered and activated users can see links. ]

    Direct acces should be used very rarely, especially if you program a package what other programers will use. (Like Netty for example.)

    This will tell you why encapsualisation (getters and setters) is a must:
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

Page 1 of 5 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 209
    Last Post: 09-20-2011, 05:34 AM
  2. [S] Good Main - Good Cash - Trustfull - Memb 29Days!
    By - TheSadx - in forum Accounts
    Replies: 19
    Last Post: 11-11-2010, 05:08 PM
  3. Replies: 13
    Last Post: 03-08-2010, 06:54 PM
  4. Getters & Setters - Why you should use them
    By PSNB in forum Tutorials
    Replies: 11
    Last Post: 12-15-2009, 01:51 PM
  5. Help with getters.
    By Jordie in forum Help
    Replies: 0
    Last Post: 07-03-2009, 09:18 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •