Hello all,
I took a look at this article linked below...
...as I'm trying to determine whether getter/setter accessor methods are good or bad practice. The authors argument makes sense...
...but then that brings me to another question. If this example were true, how would you get X without using an accessor? I guess the goal is not to get X at all, rather, take what I'm trying to do with X and put it in a class method so the class can handle the work internally such that I would no longer need to "get X" outside of the class.
For example, if I have a class called Polynomial (that obviously represents a Polynomial) and I wish to find the derivative of the polynomial, instead of using get accessors to pull out the terms and finding the derivative of each outside of the class, it would be better to call a method such as "GetDerivative()" to return a derivative of the polynomial with the class handling the differentiation internally? I believe the author says this much at the bottom of page 3.
But then there comes some times when I think setters are absolutely necessary/unavoidable. Such as setting the balance on a CreditCard object. I can't see how I could set a balance better than just using a setter and telling the class to set the balance internally doesn't seem like it's any different from using a setter.
I took a look at this article linked below...
http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html?page=1
...as I'm trying to determine whether getter/setter accessor methods are good or bad practice. The authors argument makes sense...
To see why, consider that there might be 1,000 calls to a getX() method in your program, and each call assumes that the return value is of a particular type. You might store getX()'s return value in a local variable, for example, and that variable type must match the return-value type. If you need to change the way the object is implemented in such a way that the type of X changes, you're in deep trouble.
If X was an int, but now must be a long, you'll get 1,000 compile errors. If you incorrectly fix the problem by casting the return value to int, the code will compile cleanly, but it won't work. (The return value might be truncated.) You must modify the code surrounding each of those 1,000 calls to compensate for the change. I certainly don't want to do that much work.
...but then that brings me to another question. If this example were true, how would you get X without using an accessor? I guess the goal is not to get X at all, rather, take what I'm trying to do with X and put it in a class method so the class can handle the work internally such that I would no longer need to "get X" outside of the class.
For example, if I have a class called Polynomial (that obviously represents a Polynomial) and I wish to find the derivative of the polynomial, instead of using get accessors to pull out the terms and finding the derivative of each outside of the class, it would be better to call a method such as "GetDerivative()" to return a derivative of the polynomial with the class handling the differentiation internally? I believe the author says this much at the bottom of page 3.
But then there comes some times when I think setters are absolutely necessary/unavoidable. Such as setting the balance on a CreditCard object. I can't see how I could set a balance better than just using a setter and telling the class to set the balance internally doesn't seem like it's any different from using a setter.
in an ancient installer is quite harmless, I'd rather let it be than getting an exception. The get free space function won't magically nuke the partition.