7 ms·
This is the basic stategy that I try to follow as much as possible: Classes that hold data have no methods (beyond getters/setters). Classes that implement beh
by greydius 10y ago
This is the basic stategy that I try to follow as much as possible:
Classes that hold data have no methods (beyond getters/setters). Classes that implement behavior have no mutable state; they exist as namespaces for the functionality that they provide.
I've been quite successful with this approach. It leads to code that is easier to understand than a big ball of stateful objects.
- unsoundInput 10y agoFunny, I tend to go the opposite way (immutable value types, stateful-as-needed behavior classes). I wonder how you argue that you have less stateful objects considering data objects are usually instantiated more than logic objects?
- mattmanser 10y agoSomeone did this on a project I work on (C#). Utter nightmare. Loads of repeated code as new programmers didn't know methods existed, loads of extraneous DTO objects, lots of nested single line methods, massive code bloat. Hard to find what actually does stuff, hard to use built in editor functionality. It's really not a good tactic imo. I'm undoing it as I go, at one point after having worked on it nearly 4 months according to git I'd still had a net negative on total lines of code, having added a ton of new functionality. Admittedly, it's one of those projects which has had a bunch of freelancers/contractors work on it, but I personally really don't see what it added to move the methods off the classes apart from confusion, code duplication and code bloat. I see the need for something, on the startup + enterprisey projects I work on you always seem to end up with at least one mega class that ends up being a beast, the Order, Person, Customer, Job or Project objects are usual culprits, but most other classes don't need much past a few basic methods. I'm just not convinced this programming tactic is it having now seen it in the wild.
- coldtea 10y ago>Admittedly, it's one of those projects which has had a bunch of freelancers/contractors work on it, but I personally really don't see what it added to move the methods off the classes apart from confusion, code duplication and code bloat. Moving the methods off the classes would actually lead to less code duplication -- as now different data that need the same treatment can be handled with the same methods. "Loads of repeated code as new programmers didn't know methods existed" seems like a bigger issue here...
- jghn 10y agoA bigger issue indeed, and one I've also seen in fully OOP code bases. Entire parallel bits of functionality as developer A has no idea that developer B already made the thing they want
- mattmanser 10y agoas now different data that need the same treatment can be handled with the same methods. That just doesn't happen enough in real life to design around. In the rare instances when you do need to that, it's trivial to handle with interfaces and a wrapped method that calls a shared function.
- coldtea 10y ago>That just doesn't happen enough in real life to design around. On the contrary, I think it does. Especially for web, enterprise and application programming, 90% of the logic is the same tired data transformations.
- mattmanser 10y agoIt'd be nice if you told us what code you're referring to. I've been giving examples and rough technical outlines how I'd handle problems that needed code sharing. So far you've given us zilch but naysaying. Have you got any examples? I've never had to write identical code on an Order class as a Person class. Can you give me an example of 90% of the logic being the same on an Order and a Person class? Like in C#, the entity framework has mainly done away with all the "tired data transformations". The equivalents like Hibernate, ActiveRecord, etc. have done the same in other languages (and were the trailblazers). When I dip into a data transformation these days it's complex, hand-coded SQL, the tricky bit that takes time, with a simple data class to handle strong typing, trivial to write in a minute or two with snippets and auto-completion.
- coldtea 10y ago>It'd be nice if you told us what code you're referring to. I've been giving examples and rough technical outlines how I'd handle problems that needed code sharing. So far you've given us zilch but naysaying. Sorry, why the complaining? We were talking on a more abstract level. Yourself just gave some anecdote about "Someone did this on a project I work on (C#). Utter nightmare. Loads of repeated code as new programmers didn't know methods existed, loads of extraneous DTO objects, lots of nested single line methods, massive code bloat", that's hardly an example either. >I've never had to write identical code on an Order class as a Person class. Can you give me an example of 90% of the logic being the same on an Order and a Person class? Apart from the data contained without, which process would not be the same? Most high level operations would be exactly the same: filtering would be the same (it just needs to accept a predicate), ordering would be the same, serialization would be the same, etc etc. For a very simple example, you don't need: persons.sort("age", sort.DESC) orders.sort("created_at", sort.DESC) etc, you just need: sortedPersons = sort(persons, cur -> cur.age, sort.DESC) sortedOrders = sort(orders, cur -> cur.created_at, sort.DESC) And the same function can work with 100s of other classes and containers.
- dr0verride 10y agoI second this approach. The only state my behavior classes hold are injected dependencies. The only caveat to this approach is that for some cases you will need a state accumulator of some sort. But those state manipulations are isolated and easy to deal with.
- deleted 10y ago[deleted]
- geophile 10y agoA class that holds data and has no methods other than get/set is basically a struct. Why even bother with the get/set methods? You assert that code based on this approach is easier to understand, but you don't give a reason why this is so. This sounds a lot like a pre-OO approach to organizing code and data, and I don't understand the benefits. To use the tried-and-true example: I want my stack state and code to go together. Why would I want to expose the stack state, and keep the code manipulating it separate?