I did some C programming, and while OOP is good for simplifying problems by creating class hierarchies, I still think that C's simple approach is the simplest and biggest in many cases. In C, we have structures with only public members, and the programmer passes functions to such structures (in Java, such functions will be, for example, static functions in some class) that control the elements. Data and algorithm are separate because functions are not members of structures. I always had the feeling that VO objects are similar to structures in C.
There are many cases where the C language is not the largest, because, for example, there is no hierarchy, there is no polymorphism, things that worthy OOP programmers find useful. However, overall I like this simple C approach and prefer to use it if I don't know that the OOP method will be really useful. For example. when I need to use the class hierarchy to model something or I need to make sure that the members of one or more classes (in the hierarchy) are always compatible with each other, then I cannot use the C struct approach. But in these cases, I would not only have setters and getters.
I would also call this article on C ++, but I like the way this guy explains things like this: http://www.gotw.ca/gotw/084.htm There are 2 rules in this article about when to make a function class member:
(from the quote below I leave some things, read the original if you want to see everything)
Always make it a member if it should be one: what operations should be members, because C ++ just says that (for example, constructors) or for functional reasons (for example, they should be virtual)? If they should be, then, well, they just have to be; closed.
Prefer to become a member if he needs access to internal elements: for which operations do you need access to internal data that we could provide through friendship? Usually they should be members.
In all other cases, prefer to do this nonmember nonfriend: what operations can work equally well, as nonmember nonfriends? They can and should usually be non-members. This should be the default case you want to strive for.
I have the feeling that if you doubt whether to add functions to these classes, you have no real need. What I wrote here is only part of all the reasons why I would not add methods to such classes, but perhaps this is the father of all my other reasons. But this is all subjective, therefore YMMV. By the way, the approach of static utility functions simplifies unit testing in most cases.
bjdodo Jun 13 '13 at 17:39 2013-06-13 17:39
source share