Reputation: 131
For a 'class' in modern programming languages, e.g. Java/C#/C++/Python..., it contains both data and logic in one place. However, according to 'separation of concern', it seems that we shouldn't mix data with logic. I know both are right, but how to explain the two seemingly conflicting ideas?
Upvotes: 0
Views: 215
Reputation: 67802
... according to 'separation of concern', it seems that we shouldn't mix data with logic ...
That's just false. We don't consider "data" and "logic" to be concerns in the first place. Nothing in the linked article suggests that anyone does or ever has considered this.
Consider the first example given in your own link: the Internet protocol stack.
The TCP layer is responsible for both the TCP stream state data and the TCP window management, retrans logic, etc.
The concern is reliable stream-oriented communication, and that requires both data and logic.
Admittedly the article does a very poor job of describing what it does mean by "concern", except by listing of examples - perhaps because it's difficult to give a single definition. The original Dijkstra quote is exclusively in terms of cross-cutting concerns, like program-wide correctness or efficiency. OO is better at representing non-cross-cutting concerns, such as a reliable stream or some kind of logger or an associative container.
NB. There is one context in which we might consider "data" and "logic" to be concerns - when we write a program that operates on data and logic, such as a compiler. However we should distinguish the logic & data used in the program's own implementation from the Logic and Data objects it manipulates at runtime. Unless, perhaps, it's written in LISP.
Upvotes: 3
Reputation: 993
Separating data and logic to different classes should be evaluated on case-by-case basis. For example, a data transfer object usually only carries data while application services uses the data transfer objects to perform tasks. It all depends on the pattern and architecture you are using for the situation.
Upvotes: 0
Reputation: 3676
A class should, in very basic terms, contain data and the logic that manipulates that data.
The idea of separation of concerns is that unrelated areas of functionality should be kept separate, e.g. data about a person's name address and phone number should be separate from data about their business role within a company.
But the data and logic for address etc. should be kept together in one class, and the data and logic for the business role should be kept together in another class.
This is often described as the Single Responsibility Principle (SRP). See here: https://youtu.be/IrcMr0Xqz8Q
Upvotes: 1