What problem are you trying to solve?
Nicer APIs.
Describe the situation before applying the recipe
public class Person {
private int age;
public void setAge(int age) {
this.age = age;
}
}
Describe the situation after applying the recipe
public class Person {
private int age;
public Person setAge(int age) {
this.age = age;
return this; // <--------------
}
}
Any additional context
- One could consider doing the same to other methods returning
void, not only the setters.
- I haven't thought it through though, maybe some heuristics is needed to pick which methods it would be relevant for.