4 ms·
> that method/logic should be in the object, not elsewhere Only if it's involved in preserving some object invariants, or accessing parts of its state that are
by zozbot234 7y ago
> that method/logic should be in the object, not elsewhere
Only if it's involved in preserving some object invariants, or accessing parts of its state that are abstracted away in the public interface. Otherwise, you're breaking encapsulation by putting some logic in the object that doesn't belong there, and making it hard to change the implementation later.
> The languages were talking about, C#/Java already provide a facility for this called a constructor.
There are sensible reasons to avoid using constructors for this, at least in the general case.
- lazulicurio 7y ago> Only if it's involved in preserving some object invariants, or accessing parts of its state that are abstracted away in the public interface. Otherwise, you're breaking encapsulation by putting some logic in the object that doesn't belong there, and making it hard to change the implementation later. I've been pondering this thread all day, and I think this is the crux of the issue. Ideally, classes would only encapsulate state and you'd use namespaces/modules to encapsulate functionality. But most Java/C# OOP examples use classes to encapsulate both state and functionality, which gets you stuck in the morass that the article discusses.