in reply to Class attribute get/set approach flaws?
My take on this is that in general, as a rule of thumb, methods that don't do anything but get or set a variable are a sign of bad design. It would be different if there were some concrete reason for having methods get and set the variables. For instance, the setter methods of some classes might do validation, and that would provide a worthwhile excuse for their existance; if you think you might add validation in the future, you might go ahead and do the interface this way to ease that transition. Another example is Class::DBI, whose methods do rather more than merely get or set a plain old variable (and which, incidentally, implements both the Class::Value() and Class::get(value) interfaces; both are useful, the former for reasons pointed out by others in this thread, and the latter because sometimes it's terribly convenient to run a foreach loop through several values). Nevertheless, these are special cases, cases where the methods either currently do or someday might do something more than just get or set the value.
Object-oriented design is a very useful approach, especially for certain types of problems, but (unless you're taking a class in it and need to make certain grades) don't get too caught up in textbook notions of "clean" OO design, wherein the professor will mark you down if any of your object's variables are public. If the methods are never going to do anything but get or set the object's values, they're probably superfluous and add needless complexity, which is usually a bad thing; in that case, it's probably better to let the calling code get or set the variables via normal assignment. This is especially true in Perl, because normal assignment has some flexibility that you might not think about and might not think to implement in your methods, e.g., it reacts appropriately to contexts, can work with array or hash slicing, and so on. It may also perform better than methods.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Class attribute get/set approach flaws?
by sauoq (Abbot) on Nov 29, 2005 at 01:58 UTC | |
by jonadab (Parson) on Nov 29, 2005 at 05:05 UTC |