Even people who normally have no problem with coding can find code that reverses Getter and Setter. If you use Getter as the return value of the data to be acquired, and if you use Setter to change internal variables or update arguments, you can use Setter, but it may not be so simple. Let's take a look at the sample code.
All of the sample code simplifies the code you've seen in a real project. It should be noted that the processing from NG1 to NG3 is not related and is not a series of processing.
private void Main()
{
//NG1 Get method even though there is no return value and the argument is updated
var dtoes = new List<Dto>();
GetData(dtoes);
//NG2 Set method even though there is a return value
dtoes = SetData();
//NG3 Difficult to read due to a mixture of correct Setters
var dto2 = SetDto2();
dto2.SetDtoes(SetData());
}
private void GetData(List<Dto> dtoes)
{
//Assumed to be acquired from DB.
dtoes.Add(new Dto());
}
private List<Dto> SetData()
{
//Assumed to be acquired from DB.
var dtoes = new List<Dto>();
return new List<Dto>();
}
private Dto2 SetDto2()
{
var dto2 = new Dto2();
return dto2;
}
/**Table DTO**/
private class Dto
{
}
/**Parent DTO with Dto as a member**/
private class Dto2
{
public List<Dto> _dtoes;
public void SetDtoes(List<Dto> dtoes)
{
_dtoes = dtoes;
}
}
public void main() {
//NG1 Get method even though there is no return value and the argument is updated
List<Dto> dtoes = new ArrayList<Dto>();
getData(dtoes);
//NG2 Set method even though there is a return value
dtoes = setData();
//NG3 Difficult to read due to a mixture of correct Setters
Dto2 dto2 = setDto2();
dto2.setDtoes(setData());
}
private void getData(List<Dto> dtoes) {
//Assumed to be acquired from DB.
dtoes.add(new Dto());
}
private List<Dto> setData() {
//Assumed to be acquired from DB.
List<Dto> dtoes = new ArrayList<Dto>();
return new ArrayList<Dto>();
}
private Dto2 setDto2() {
Dto2 dto2 = new Dto2();
return dto2;
}
/**Table DTO**/
private class Dto {
}
/**Parent DTO with Dto as a member**/
private class Dto2 {
public List<Dto> _dtoes;
public void setDtoes(List<Dto> dtoes) {
_dtoes = dtoes;
}
}
Consider why it looks like the sample code above. GetData: Getter because we are getting data from DB SetData: Since the data acquired from DB is set and returned, Setter SetDto2: Since the instance is set and returned, Setter SetDtoes: Since Dto is set, Setter Is that the place? There is no sense of unity and the code is difficult to read. If the data to be acquired is used as the return value, I rewrote it with a rule called Get, updating internal variables and updating arguments with a rule called Set.
private void Main()
{
//OK Argument update
var dtoes = new List<Dto>();
SetData(dtoes);
//OK The return value is the acquired data.
dtoes = GetData();
//OK Getter and Setter have been unified to make them easier to read.
var dto2 = GetDto2();
dto2.SetDtoes(GetData());
}
private void SetData(List<Dto> dtoes)
{
//Assumed to be acquired from DB.
dtoes.Add(new Dto());
}
private List<Dto> GetData()
{
//Assumed to be acquired from DB.
var dtoes = new List<Dto>();
return new List<Dto>();
}
private Dto2 GetDto2()
{
var dto2 = new Dto2();
return new Dto2();
}
/**Table DTO**/
private class Dto
{
}
/**Parent DTO with Dto as a member**/
private class Dto2
{
public List<Dto> _dtoes;
public void SetDtoes(List<Dto> dtoes)
{
_dtoes = dtoes;
}
}
public void main() {
//OK Argument update
List<Dto> dtoes = new ArrayList<Dto>();
setData(dtoes);
//OK The return value is the acquired data.
dtoes = getData();
//OK Getter and Setter have been unified to make them easier to read.
Dto2 dto2 = getDto2();
dto2.setDtoes(getData());
}
private void setData(List<Dto> dtoes) {
//Assumed to be acquired from DB.
dtoes.add(new Dto());
}
private List<Dto> getData() {
//Assumed to be acquired from DB.
List<Dto> dtoes = new ArrayList<Dto>();
return new ArrayList<Dto>();
}
private Dto2 getDto2() {
Dto2 dto2 = new Dto2();
return dto2;
}
/**Table DTO**/
private class Dto {
}
/**Parent DTO with Dto as a member**/
private class Dto2 {
public List<Dto> _dtoes;
public void setDtoes(List<Dto> dtoes) {
_dtoes = dtoes;
}
}
Previous article (Can I separate function calls and conditionals?)