Let’s assume that [database triggers are evil].1
Does this mean that side effects when setting a property on a java or C# object are also evil?
It seems to me that all the same problems exist.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Going against the grain here…
Properties should NOT trigger side effects. That’s what Method’s are for.
By having properties cause side effects you end up in a situation were code is essentially hidden. People rarely expect properties to kick off some process or cause something else to flip. If this has to be documented, then its not obvious and subject to being ignored.
However, we do expect something to happen when we call a Method.
Taking @astander’s example, he says that the act of changing “Price” should cause a different property “Cost” to change. However, what if we later add a new property called “Discount”? The code around the Price and Amount properties would have to change. Which isn’t very discoverable.
However, if Cost calculated itself.. then everything is better off.