Advantages
- The internal storage format of the data is hidden; in the example, an expectation of the use of restricted character sets could allow data compression through recoding (e.g., of eight bit characters to a six bit code). An attempt to encode characters out of the range of the expected data could then be handled by casting an error in the set routine.
- In general, the get and set methods may be produced in two versions - an efficient method that assumes that the caller is delivering appropriate data and that the data has been stored properly, and a debugging version that while slower, performs validity checks on data received and delivered. Such detection is useful when routines (calling or called) or internal storage formats are newly created or modified.
- The location of the stored data within larger structures may be hidden and so enabling changes to be made to this storage without the necessity of changing the code that references the data. This also reduces the likelihood of unexpected side effects from such changes. This is especially advantageous when the accessors are part of an operating system (OS), a case where the calling (application) code may not be available to the developers of the OS.
Read more about this topic: Field Encapsulation
Famous quotes containing the word advantages:
“The advantages found in history seem to be of three kinds, as it amuses the fancy, as it improves the understanding, and as it strengthens virtue.”
—David Hume (17111776)
“Men hear gladly of the power of blood or race. Every body likes to know that his advantages cannot be attributed to air, soil, sea, or to local wealth, as mines and quarries, nor to laws and traditions, nor to fortune, but to superior brain, as it makes the praise more personal to him.”
—Ralph Waldo Emerson (18031882)
“There is no one thoroughly despicable. We cannot descend much lower than an idiot; and an idiot has some advantages over a wise man.”
—William Hazlitt (17781830)