January 31st, 2006, 2:57 pm
Obviously there are lots of tradeoffs to consider each time in designing any interface. Usually these considerations are directly opposed to efficiency (e.g. custom, purpose-written code with hard-coded values is almost always faster, easier to maintain and smaller than generic, general-purpose code). But the best way I have found to make the evaluation decision is through capricious rules. Eg. If a function needs more than seven parameters, it is doing too much, no functions of more than 37 lines, etc. -- and anything that fits within these rules (yes, they are arbitrary) in my head is "okay". One other rule is to do things at as low a level as possible while writing initially, because code gets re-used a lot less than one thinks it will, but do it at as high a level as possible when doing maintenance (because documentation is never as good as you thing it is, and the law of unintended consequnces will get you every time).So, when it comes to your function, I would definitely make it the responsibility of the class to be efficient. That's the whole purpose of setter/getter functions -- to check inputs, to insert efficiency short cuts, etc.Finally, elegance is good for physics. But not for code. The number of times I have seen folks who write one line functions just because they were always taught to use setter/getter functions without using their brains... oi! don't get me started.