91

I'm having something of a hard time with designing classes in an oo way. I've read that objects expose their behavior, not their data; therefore, rather than using getter/setters to modify data, the methods of a given class should be "verbs" or actions operating on the object. For example, in an 'Account' object, we would have the methods Withdraw() and Deposit() rather than setAmount() etc. See: Why getter and setter methods are evil.

So for example, given a Customer class that keeps alot of information about the customer, e.g. Name, DOB, Tel, Address etc., how would one avoid getter/setters for getting and setting all those attributes? What 'Behavior' type method can one write to populate all that data?

  • 3
  • 3
  • 9
    I would point out that if you have to deal with Java Beans specification, you will be having getters and setters. Lots of things use Java Beans (Expression Language in jsps) and trying to avoid such will likely be... challenging. –  May 18 '15 at 16:32
  • 5
    ...and, from the opposite perspective from MichaelT: if you're not using the JavaBeans spec (and I personally think you should avoid it unless you're using your object in a context where it's necessary), then there's no need for the "get" wart on getters, especially for properties that don't have any corresponding setter. I think a method called name() on Customer is as clear, or clearer, than a method called getName(). – Daniel Pryden May 18 '15 at 21:05
  • 2
    @Daniel Pryden: name() can mean both to set or to get?... – IntelliData May 19 '15 at 16:51
  • 1
    Why do you assume that article is nothing but 100% BS? – Andy May 19 '15 at 16:58
  • 1
    @IntelliData: As a method with no arguments, it cannot be a setter. The pattern of using methods named after the properties exists throughout the Java standard library (Collection.size() for example) and is quite common in other languages as well (I see it a lot in C++ code, for example). I've seen people make name() the getter and name(new_name) the setter, although I'd shy away from that in favor of having an explicit setName(), or better yet omitting setters entirely. IMO, value objects should always be immutable unless there's a strong need for them to be otherwise. – Daniel Pryden May 19 '15 at 18:16
  • I wonder how much of this entire conversation would be obsolete if Java had a hash literal syntax with dot property access for plain jane data structures. – Jared Smith May 20 '15 at 12:57
  • @DanielPryden: Methods are typically named using verbs. – Robert Harvey May 20 '15 at 13:03
  • @DanielPryden: How can a Customer be an immutable object? Don't peoples addresses & phone numbers change? – IntelliData May 20 '15 at 16:33
  • @RobertHarvey: They are indeed, and that's a good way to articulate why I don't like the name(new_name) form. I don't believe that naming methods after verbs is a hard rule though, so I have no objection to naming a (non-state-changing, obvious-from-context) method after a noun. I realize that not everyone concurs with this opinion, though. – Daniel Pryden May 20 '15 at 18:11
  • @DanielPryden: getSomething() is unambiguous, and enterprise Java is already exceptionally wordy anyway. – Robert Harvey May 20 '15 at 18:14
  • @IntelliData: It depends on what you're modeling Customer to mean. If you model Customer to mean "the knowledge I have about a customer at a point in time", then it can freely be immutable; when you have more knowledge you just create a different instance. In fact that can be a much easier way to model data if you have both local (in-memory) and remote (database) structures: the Customer object in memory will by necessity not always be identical to the Customer record in the database, so being able to represent them differently (and being to compare between them) can be very useful. – Daniel Pryden May 20 '15 at 18:15
  • @RobertHarvey: Are we talking exclusively about enterprise Java here, though? Like I said in my comment above, I personally think you should avoid specs like JavaBeans if you don't need them -- but if you do need them, then yeah, ignore my advice. :-) – Daniel Pryden May 20 '15 at 18:17
  • @DanielPryden the conventions that are idiomatic to a language are useful even if you are not doing "enterprise development"... – Roland Tepp May 21 '15 at 13:20
  • @RolandTepp: You're saying that naming a method size() or ordinal() rather than getSize() or getOrdinal() is not idiomatic? Better change all collection types in the standard library and the definition of enums in the language spec then, because those don't use the "get" prefix. There are plenty of other examples I can find. Basically: I reject the premise that the "get" prefix is an idiom of the language, only of some libraries of the language. – Daniel Pryden May 21 '15 at 15:27
  • 1
    Note that the article you mention was written in 2003 and contains (among more useful stuff) this pearl: " a metadata feature will be incorporated into Java 1.5 (due in mid 2004). [...] You'll be able to use something like: private @property int property;" which clearly didn't happen. There's project Lombok but it does bad things to your bytecode so I wouldn't recommend it. Better use your IDE's code generation feature. – Michał Kosmulski May 23 '15 at 08:07
  • @DanielPryden I am not saying any such thing. What I was trying to say is that labeling certain idiomatic patterns as not useful just because you don't agree with them is pure hubris. Code isn't only meant for machines to execute. It's also useful to remember the people (including you five years from now) who read it later and need to understand it. Human brain is great with recognizing patterns. Naming your getters and setters in idiomatic ways helps people to recognize them for what they are. – Roland Tepp May 23 '15 at 13:49
  • @RolandTepp: I don't really want to derail the comments on this question. But my point was that omitting the "get" prefix is also a valid idiom in Java. Since there are multiple possible idioms which are both in wide use (see my previous comment, which you say you do not disagree with), I don't see how adopting one idiom over another -- in contexts where you have the choice -- is "pure hubris" or results in reduced code readability. You are free to disagree, and I don't begrudge you your opinion. Perhaps you should open a separate question about the naming convention? – Daniel Pryden May 23 '15 at 23:02
  • @DanielPryden, true, omitting get prefix is a valid pattern in Java, but if you look at how or when this is used, it usually is reserved for either computed, derived or transformed values, not so much for object attributes/properties of an object. In any case, bringing it back to the original topic, it does not matter if you put a get prefix in front of it or not, if it's only function is to get a value of a private field, it is a getter. Disguising it by omitting the get prefix is only going to confuse people. – Roland Tepp May 26 '15 at 06:51
  • 1
    @IntelliData Is there anything specific that you think the existing answers are lacking? Are you going to be satisfied with answers of the form "You probably want DTOs/property bags in x and y situation, but otherwise you can avoid getters and setters by...", or do you really want to be puritanical about avoiding them altogether? – Ben Aaronson May 26 '15 at 15:22
  • @Ben Aaronson: Well, the question was asked assuming the OO puritan point of view, which says that public getters/setters are almost as bad as accessing field variables, and do not implement encapsulation. According to my understanding of OOP, DTO's are discouraged as well, at least as regular entities. Most answers were attempting to justify getters/setters fir this situation, or why we might need a DTO, which is not OO pure at all... – IntelliData May 27 '15 at 16:21
  • @IntelliData DTOs are discouraged in many situations (like as domain objects), but they're very useful for transferring data across boundaries, (esp. using serialization) and this is a widely accepted practice. I don't think you're going to get any good answer that says "there are no situations where DTOs are appropriate!" simply because that isn't true. What you might get is an answer that defines a very narrow category of situations where DTOs are appropriate, and gives advice on how to more or less eliminate getters/setters elsewhere. Would that suffice? – Ben Aaronson May 27 '15 at 16:26
  • That is exactly what I meant.. Primarily with domain objects, how would I eliminate getters/setters, as in the Customer example which i gave. Whew! Somebody finally understood my question... – IntelliData May 27 '15 at 21:12
  • The easiest way to get around getters and setters is to access the data member directly. QED. – Thomas Eding May 28 '15 at 14:55
  • As the question is tagged as a Java question, please note that Java conventions require you to start your methods' names with a lower case. – mgoeminne May 30 '15 at 08:04

10 Answers10

77

The simplest way to avoid setters is to hand the values to the constructor method when you new up the object. This is also the usual pattern when you want to make an object immutable. That said, things are not always that clear in the real world.

It is true that methods should be about behavior. However, some objects, like Customer, exist primarily to hold information. Those are the kinds of objects that benefit the most from getters and setters; were there no need at all for such methods, we would simply eliminate them altogether.

Further Reading
When are Getters and Setters Justified

Robert Harvey
  • 199,517
  • 3
    so why all the hype about 'Getter/Setters' are evil? – IntelliData May 18 '15 at 15:03
  • 47
    Just the usual pontification by software developers who should know better. For the article you linked, the author is using the phrase "getters and setters are evil" to get your attention, but that doesn't mean the statement is categorically true. – Robert Harvey May 18 '15 at 15:04
  • 13
    @IntelliData some people will tell you that java itself is evil. – null May 18 '15 at 15:09
  • I've heard null is evil. – MetaFight May 18 '15 at 16:14
  • 19
    @MetaFight setEvil(null); –  May 18 '15 at 16:26
  • 4
    Certainly eval is Evil Incarnate. Or a near cousin. – bishop May 18 '15 at 16:47
  • If Customer primarily holds information, it can be interesting to have a Property type and a single method Customer.get : Property -> Value, solving the equation algorithms = data + code by putting more on the data side and less on the code side. – Michaël Le Barbier May 18 '15 at 19:09
  • 1
    @IntelliData It's people who spend all their time behind ivory towers and textbooks, instead of putting good, clean code together in the real world. The rest of us understand the need to group related variables together into meaningful, yet simple, aggregates from time to time. It makes code much more maintainable than letting too many random variables float around as members in a much bigger class, just to avoid getters and setters. It also helps clean up method signatures. – Panzercrisis May 18 '15 at 20:38
  • 3
    @IntelliData It used to be the case that direct access to variables was considered evil and that all access/mutation operations should go through get/set methods. Now it's swinging the other way. I think calling getters and setters "evil" is a tongue-in-cheek reference to former conventional wisdom. – Rag May 18 '15 at 21:25
  • 1
    ... or just public final fields. Right? – user253751 May 19 '15 at 03:24
  • Data Objects are evil in a devout OOP sense, in that they separate data from behavior. But, alas, sometimes they a necessary evil. Use them when needed, but don't turn all your behavior objects into Data Objects or JavaBeans by automatically adding getters and setters for everything. – user949300 May 19 '15 at 17:16
  • 1
    @bishop eval can be misused, but it can be very useful for e.g. writing a REPL – Max Nanasy May 19 '15 at 17:36
  • @BrianGordon I understood the issue is that blindly following a "all variables must be accessed via a method" means people replace myobj.var with myobj.getvar() - which is fundamentally no different and why brain-dead getters and setters should be considered evil. Getting rid of these and replacing them with get/set methods that make sense in context of the object and not the internal variables is how accessors should be considered. – gbjbaanb May 20 '15 at 09:08
  • @IntelliData That would make an interesting question of its own (if it doesn't already exist) – Ben Aaronson May 20 '15 at 16:48
  • And then to allow you to make "modifications" while maintaining the immutability, you could have "setters" that actually return a new object that the desired property. For example, Scala's case classes have a special copy method that allows any number of fields to be set in this manner. So you could do something like employee.copy(phone = "555-555-5555", email = "[email protected]") to get a new object with the desired values. – Kat May 21 '15 at 15:24
  • @Panzercrisis: When we say 'Avoid getters/setters' we don't mean access the field variables directly; we mean access them some other way. – IntelliData May 21 '15 at 16:06
  • @Mike:The builder pattern in java is probably the closest thing to smalltalk's copy. – IntelliData May 21 '15 at 16:06
  • @IntelliData I'm not talking about exposing the variables. – Panzercrisis May 22 '15 at 03:17
62

It is perfectly fine to have an object which exposes data rather than behavior. We just call it a "data object". The pattern exists under names like Data Transfer Object or Value Object. If the purpose of the object is to hold data, then getters and setters are valid to access the data.

So why would someone say "getter and setter methods are evil"? You will see this a lot - someone takes a guideline which is perfectly valid in a specific context and then remove the context in order to get a more hard-hitting headline. For example "favor composition over inheritance" is a fine principle, but soon enough someone is going to remove the context and write "Why extends is evil" (hey, same author, what a coincidence!) or "inheritance is evil and must be destroyed".

If you look at the content of the article it actually have some valid points, it just stretches the point to make a click-baity headline. For example, the article states that implementation details should not be exposed. This is the principles of encapsulation and data hiding which are fundamental in OO. However, a getter method does not by definition expose implementation details. In the case of a Customer data object, the properties of Name, Address etc. are not implementation details but rather the whole purpose of the object and should be part of the public interface.

Read the continuation of the article you link to, to see how he suggest actually setting properties like 'name' and 'salary' on a 'Employee'-object without the use of the evil setters. Turns out he uses a pattern with an 'Exporter' which is populated with methods called addName, addSalary which in turn sets fields of the same name... So in the end he ends up using exactly the setter pattern, just with a different naming convention.

This is like thinking you avoid the pitfalls of singletons by renaming them therecanbeonlyonethings while keeping the same implementation.

Robert Harvey
  • 199,517
JacquesB
  • 59,334
  • In oop, the so called experts seem to say that this is not the case. – IntelliData May 18 '15 at 15:26
  • @IntelliData: I have amended the answer to explain why so-called experts would say such a thing. – JacquesB May 18 '15 at 16:39
  • As a matter of fact, some have explicitly said 'Never use Getter/Setters'! – IntelliData May 18 '15 at 18:54
  • 8
    Then again, some people have said 'Never use OOP': http://harmful.cat-v.org/software/OO_programming/ – JacquesB May 18 '15 at 18:57
  • True, but the question is being asked in the context of OOP... – IntelliData May 18 '15 at 19:09
  • 7
    FTR, I think more “experts” still teach to meaninglessly create getters/setters for everything, than to never create any at all. IMO, the latter is less misguiding advice. – leftaroundabout May 19 '15 at 07:44
  • 5
    @leftaroundabout: OK, but I'm suggesting a middle ground between 'always' and 'never' which is 'use when appropriate'. – JacquesB May 19 '15 at 09:29
  • @JacquesB A Value Object can have behavior and be more than a dumb data structure. It's an orthogonal concept. – guillaume31 May 19 '15 at 13:09
  • 4
    I think the issue is that many programmers turn every object (or too many objects) into a DTO. Sometimes they are necessary, but avoid them as much as possible since they separate data from behavior. (Assuming you are a devout OOPer) – user949300 May 19 '15 at 17:19
  • @guillaume31 Do you mean it in the DDD sense? It has historically also been used to mean the same as DTO or "property bag" (which is more C#-ish). The double-meaning is confusing which is why I think people should avoid using it for this – Ben Aaronson May 20 '15 at 16:51
  • @BenAaronson Yes, in the DDD sense which I think is the most common now. But you're right, according to Martin Fowler, "Early J2EE literature used the term value object to describe a different notion, what I call a Data Transfer Object. They have since changed their usage and use the term Transfer Object instead." – guillaume31 May 22 '15 at 20:24
  • I'd say the evilness is object behavior dependent on the client manually setting state first (i.e. not using constructor or other method parameters). However, exposed in context, i.e. properly encapsulated, exposing data is not per se evil. – radarbob Jun 03 '15 at 16:04
62

As stated in quite a few answers and comments, DTOs are appropriate and useful in some situations, especially in transferring data across boundaries (e.g. serializing to JSON to send through a web service). For the rest of this answer, I'll more or less ignore that and talk about domain classes, and how they can be designed to minimize (if not eliminate) getters and setters, and still be useful in a large project. I also won't talk about why remove getters or setters, or when to do so, because those are questions of their own.

As an example, imagine that your project is a board game like Chess or Battleship. You might have various ways of representing this in a presentation layer (console app, web service, GUI,etc.), but you also have a core domain. One class you might have is Coordinate, representing a position on the board. The "evil" way to write it would be:

public class Coordinate
{
    public int X {get; set;}
    public int Y {get; set;}
}

(I'm going to be writing code examples in C# rather than Java, for brevity and because I'm more familiar with it. Hopefully that's not a problem. The concepts are the same and the translation should be simple.)

Removing Setters: Immutability

While public getters and setters are both potentially problematic, setters are the far more "evil" of the two. They're also usually the easier to eliminate. The process is a simple one- set the value from within the constructor. Any methods which previously mutated the object should instead return a new result. So:

public class Coordinate
{
    public int X {get; private set;}
    public int Y {get; private set;}
public Coordinate(int x, int y)
{
    X = x;
    Y = y;
}

}

Note that this doesn't protect against other methods in the class mutating X and Y. To be more strictly immutable, you could use readonly (final in Java). But either way- whether you make your properties truly immutable or just prevent direct public mutation through setters- it does the trick of removing your public setters. In the vast majority of situations, this works just fine.

Removing Getters, Part 1: Designing for Behavior

The above is all well and good for setters, but in terms of getters, we actually shot ourselves in the foot before even starting. Our process was to think of what a coordinate is- the data it represents- and create a class around that. Instead, we should have started with what behavior we need from a coordinate. This process, by the way, is aided by TDD, where we only extract classes like this once we have a need for them, so we start with the desired behavior and work from there.

So let's say that the first place you found yourself needing a Coordinate was for collision detection: you wanted to check if two pieces occupy the same space on the board. Here's the "evil" way (constructors omitted for brevity):

public class Piece
{
    public Coordinate Position {get; private set;}
}

public class Coordinate { public int X {get; private set;} public int Y {get; private set;} }

//...And then, inside some class
public bool DoPiecesCollide(Piece one, Piece two)
{
    return one.X == two.X && one.Y == two.Y;
}

And here's the good way:

public class Piece
{
    private Coordinate _position;
    public bool CollidesWith(Piece other)
    {
        return _position.Equals(other._position);
    }
}

public class Coordinate { private readonly int _x; private readonly int _y; public bool Equals(Coordinate other) { return _x == other._x && _y == other._y; } }

(IEquatable implementation abbreviated for simplicity). By designing for behavior rather than modelling data, we've managed to remove our getters.

Note this is also relevant to your example. You may be using an ORM, or display customer information on a website or something, in which case some kind of Customer DTO would probably make sense. But just because your system includes customers and they are represented in the data model does not automatically mean you should have a Customer class in your domain. Maybe as you design for behavior, one will emerge, but if you want to avoid getters, don't create one pre-emptively.

Removing Getters, Part 2: External Behaviour

So the above is a good start, but sooner or later you will probably run into a situation where you have behavior which is associated with a class, which in some way depends on the class's state, but which doesn't belong on the class. This sort of behavior is what typically lives in the service layer of your application.

Taking our Coordinate example, eventually you'll want to represent your game to the user, and that might mean drawing to the screen. You might, for example, have a UI project which uses Vector2 to represent a point on the screen. But it would be inappropriate for the Coordinate class to take charge of converting from a coordinate to a point on the screen- that would be bringing all sorts of presentation concerns into your core domain. Unfortunately this type of situation is inherent in OO design.

The first option, which is very commonly chosen, is just expose the damn getters and say to hell with it. This has the advantage of simplicity. But since we're talking about avoiding getters, let's say for argument's sake we reject this one and see what other options there are.

A second option is to add some kind of .ToDTO() method on your class. This- or similar- may well be needed anyway, for example when you want to save the game you need to capture pretty much all of your state. But the difference between doing this for your services and just accessing the getter directly is more or less aesthetic. It still has just as much "evil" to it.

A third option- which I've seen advocated by Zoran Horvat in a couple of Pluralsight videos- is to use a modified version of the visitor pattern. This is a pretty unusual use and variation of the pattern and I think people's mileage will vary massively on whether it's adding complexity for no real gain or whether it's a nice compromise for the situation. The idea is essentially to use the standard visitor pattern, but have the Visit methods take the state they need as parameters, instead of the class they're visiting. Examples can be found here.

For our problem, a solution using this pattern would be:

public class Coordinate
{
    private readonly int _x;
    private readonly int _y;
public T Transform<T>(IPositionTransformer<T> transformer)
{
    return transformer.Transform(_x,_y);
}

}

public interface IPositionTransformer<T> { T Transform(int x, int y); }

//This one lives in the presentation layer public class CoordinateToVectorTransformer : IPositionTransformer<Vector2> { private readonly float _tileWidth; private readonly float _tileHeight; private readonly Vector2 _topLeft;

Vector2 Transform(int x, int y)
{
    return _topLeft + new Vector2(_tileWidth*x + _tileHeight*y);
}

}

As you can probably tell, _x and _y aren't really encapsulated any more. We could extract them by creating an IPositionTransformer<Tuple<int,int>> which just returns them directly. Depending on taste, you may feel this makes the entire exercise pointless.

However, with public getters, it's very easy to do things the wrong way, just pulling data out directly and using it in violation of Tell, Don't Ask. Whereas using this pattern it's actually simpler to do it the right way: when you want to create behaviour, you'll automatically start by creating a type associated with it. Violations of TDA will be very obviously smelly and probably require working around a simpler, better solution. In practice, these points make it much easier to do it the right, OO, way than the "evil" way that getters encourage.

Finally, even if it isn't initially obvious, there may in fact be ways to expose enough of what you need as behavior to avoid needing to expose state. For example, using our previous version of Coordinate whose only public member is Equals() (in practice it would need a full IEquatable implementation), you could write the following class in your presentation layer:

public class CoordinateToVectorTransformer
{
    private Dictionary<Coordinate,Vector2> _coordinatePositions;
public CoordinateToVectorTransformer(int boardWidth, int boardHeight)
{
    for(int x=0; x&lt;boardWidth; x++)
    {
        for(int y=0; y&lt;boardWidth; y++)
        {
            _coordinatePositions[new Coordinate(x,y)] = GetPosition(x,y);
        }
    }
}

private static Vector2 GetPosition(int x, int y)
{
    //Some implementation goes here...
}

public Vector2 Transform(Coordinate coordinate)
{
    return _coordinatePositions[coordinate];
}

}

It turns out, perhaps surprisingly, that all the behavior we really needed from a coordinate to achieve our goal was equality checking! Of course, this solution is tailored to this problem, and makes assumptions about acceptable memory usage/performance. It's just an example that fits this particular problem domain, rather than a blueprint for a general solution.

And again, opinions will vary on whether in practice this is needless complexity. In some cases, no such solution like this might exist, or it might be prohibitively weird or complex, in which case you can revert to the above three.

Ben Aaronson
  • 6,913
  • 1
  • 31
  • 30
  • Beautifully answered! I would like to accept, but first some comments:1. I do think the toDTO() is great, bcuz ur not accessing the get/set, which allows u to change the fields given to DTO w/o breaking existing code. 2. Say Customer has enough behaviour to justify making it an entity, how would u access props to modify them, e.g. address/tel change etc. – IntelliData May 28 '15 at 17:02
  • @IntelliData 1. When you say "change the fields", you mean change the class definition or mutate the data? The latter can just be avoided by removing public setters but leaving the getters, so the dto aspect is irrelevant. The former isn't really the (whole) reason that public getters are "evil". See http://programmers.stackexchange.com/questions/157526/explanation-on-how-tell-dont-ask-is-considered-good-oo for example. – Ben Aaronson May 28 '15 at 17:58
  • @IntelliData 2. This is difficult to answer without knowing the behaviour. But probably, the answer is: you wouldn't. What behaviour could a Customer class have that requires being able to mutate its telephone number? Perhaps the customer's telephone number changes and I need to persist that change in the database, but none of that is the responsibility of a behaviour-providing domain object. That's a data-access concern, and would probably be handled with a DTO and, say, a repository. – Ben Aaronson May 28 '15 at 18:03
  • @IntelliData Keeping the Customer domain object's data relatively fresh (in sync with the db) is a matter of managing its lifecycle, which is also not its own responsibility, and would again probably end up living in a repository or a factory or an IOC container or whatever instantiates Customers. – Ben Aaronson May 28 '15 at 18:03
  • I mean change class definition; even exposing getters only can be a problem: say I have several functions calling getDTO() on the object, I can always change the fields being exposed via getDTO() w/o breaking the code; however, if I do getProperty(), then if I remove the property later on I will be breaking the code in many places. – IntelliData May 28 '15 at 18:20
  • @IntelliData Yeah I understand. I agree toDTO it may mitigate that, but as I mentioned in the comment, there are other problems (violation of "Tell, Don't Ask"), which it does not. – Ben Aaronson May 28 '15 at 18:25
  • 'Tell, Don't Ask', good point; however, if I say provideDTO(), is it in effect 'Tell' even though it returns something? (Yeah, I know it's only a semantical difference.)
  • If a behaviour providing Customer also has data, whose responsibility is it to change the data if necessary?
  • – IntelliData May 28 '15 at 18:37
  • In general, would u say that 'Tell Don't Ask' means never return data? How is that possible? – IntelliData May 28 '15 at 18:40
  • 2
    I really like the Design for Behavior concept. This informs the underlying "data structure" and helps avoid the all too common anemic, hard-to-use classes. plus one. – radarbob Jun 03 '15 at 15:38
  • 1_ Where does that "IEquatable" (which you say you've abbreviated in your answer) belong to? May I know where you wanted to use this interface? 2_ Also about this line: "return _position.Equals(other._position);", would this be possible at all? It's accessing a private field of another object(although it's of the same class, it doesn't make a difference, private field cannot be read from an object). – aderchox Sep 06 '21 at 04:02
  • I somehow like the toDTO() - I'd probably call it transfer() - approach for use cases where you need to access a lot of fields of domain entities in order to make them cross boundaries e.g. in a HTTP API response. It's much better than accessing all the individual fields through getters and putting the data that way into the response, because a transfer() method is expressing the purpose very specifically and prevents careless misuse of the countless would-be-added getters on the domain entities. It's easy to spot if some yokel is abusing that method. – Eugene Nov 28 '21 at 02:12
  • ^-- After thinking some more about it I tend to belive CQRS is the correct solution to the problem mentioned in my prev comment – Eugene Nov 28 '21 at 23:05