19226
Developer's Corner / Re: Accessor methods vs public variables
« Last post by mouser on June 13, 2009, 04:51 AM »Modern programming practices tell you to always use accessors when manipulating or retrieving variables from outside of the class itself.
In general, a class should not allow anyone to mess with its insides directly.
This makes good common sense as well -- an object/class should be able to implement whatever gatekeeper or background functions that it wants to, with complete confidence that it knows when people are reading its values or changing them, and has the opportunity to intercept such requests if it needs to.
The only exception to this would be if you were working with code that absolutely had to be as fast as possible and you couldn't be sure the compiler was going to optimize the accessor access code.
In general, a class should not allow anyone to mess with its insides directly.
This makes good common sense as well -- an object/class should be able to implement whatever gatekeeper or background functions that it wants to, with complete confidence that it knows when people are reading its values or changing them, and has the opportunity to intercept such requests if it needs to.
The only exception to this would be if you were working with code that absolutely had to be as fast as possible and you couldn't be sure the compiler was going to optimize the accessor access code.

Recent Posts

