I have a block of code that I'm looking to clean up and streamline for better efficiency.
My main goal is to remove the multiple return statements within the method.
Any suggestions on how I might refactor this code? Are there any design patterns that could be helpful in this situation? Your advice is greatly appreciated. Thank you!
class Test{
private client;
private concreteMixer;
constructor(client, concreteMixer){
this.client = client;
this.concreteMixer = concreteMixer;
}
public method(){
let form = new Form();
if(form.isSubmitted()){
if(form.isValid()){
let field = form.getField();
let infoField = this.client.testField(field);
if(!infoField){
form.setError('This is not a valid field');
return form;
}
let coffee = this.concreteMixer.makeСoffee();
//two days have passed
if(!coffee){
form.setError('I am craving coffee');
return form;
}
this.concreteMixer.pourInThermosBottle();
//two days have passed
return coffee;
}
}
return form;
}
}