Reputation: 165
Just wondering what is best practice where creating to methods with the same signature
case 1
public void transfer(Department department){
this.department = department;
}
public void transfer(Department department,String postion){
this.department = department;
this.postion = position;
}
Case 2
public void transfer(Department department){
this.department = department;
}
public void transfer(Department department,String postion){
transfer(department);
this.postion = position;
}
Upvotes: 2
Views: 1852
Reputation: 140427
You avoid code duplication.
From that point of view, option 2 is slightly better, as it prevents the repetition of that assignment. Typically, you do that with constructors, and then you call it constructor telescoping. To get to real telescoping here, you should follow the advice from Berger and rather have the one argument method call the two-argument method. That also communicates to the reader what setting just the Department actually leads to.
Beyond that: the real problem I see here is naming. transfer()
implies that something gets transferred. In reality, your method is nothing but a setter. It should be named accordingly, like setTransferDetails()
.
Upvotes: 8