Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Getters should not include large amounts of logic." True or false?

I tend to assume that getters are little more than an access control wrapper around an otherwise fairly lightweight set of instructions to return a value (or set of values).

As a result, when I find myself writing longer and more CPU-hungry setters, I feel Perhaps this is not the smartest move. In calling a getter in my own code (in particular let's refer to C# where there is a syntactical difference between method vs. getter calls), then I make an implicit assumption that these are lightweight -- when in fact that may well not be the case.

What's the general consensus on this? Use of other people's libraries aside, do you write heavy getters? Or do you tend to treat heavier getters as "full methods"?

PS. Due to language differences, I expect there'll be quite a number of different thoughts on this...

like image 973
Engineer Avatar asked Nov 01 '10 01:11

Engineer


1 Answers

Property getters are intended to retrieve a value. So when developers call them, there is an expectation that the call will return (almost) immediately with a value. If that expectation cannot be met, it is better to use a method instead of a property.

like image 91
Robert Harvey Avatar answered Sep 29 '22 09:09

Robert Harvey