C# add validation on a setter method

If you want to validate when the property is set, you need to use non-auto properties (i.e., manually defined get and set methods). But another way to validate is to have the validation logic separate from the domain object. class Customer { public string FirstName { get; set; } public string LastName { get; set; … Read more

Python overriding getter without setter

Use just the .getter decorator of the original property: class superhuman(human): @human.name.getter def name(self): return ‘super ‘ + self._name Note that you have to use the full name to reach the original property descriptor on the parent class. Demonstration: >>> class superhuman(human): … @human.name.getter … def name(self): … return ‘super ‘ + self._name … >>> … Read more

Allen Holub wrote “You should never use get/set functions”, is he correct? [duplicate]

I don’t have a problem with Holub telling you that you should generally avoid altering the state of an object but instead resort to integrated methods (execution of behaviors) to achieve this end. As Corletk points out, there is wisdom in thinking long and hard about the highest level of abstraction and not just programming … Read more

Setter methods or constructors

You should use the constructor approach, when you want to create a new instance of the object, with the values already populated(a ready to use object with value populated). This way you need not explicitly call the setter methods for each field in the object to populate them. You set the value using a setter … Read more

In Objective-C on iOS, what is the (style) difference between “self.foo” and “foo” when using synthesized getters?

I have personally settled on using an underscore prefix for ivars, and this kind of synthesize @synthesize name = _name; That way I don’t mix them up. The major issue with not using self is that this code _name = … is very different from self.name = … When the @property uses the retain option. … Read more