According to Origin of "a method should return a value or have side-effects, but not both", a method should either return a value or have side-effect, but not both. However, I often see some situations that I need both the result of modify objects and the process of modifying it, for example, a function that modify an array, and the steps that involved during modifying:
function customSort(myArray){
let stepCount=0;
//some sort steps to modify myArray
return stepCount;
}
const stepCount=customSort(this.myArray);
this.showAnimation(stepCount);
So, should I separate it into 2 methods:
function customSortReallyModifyArray(myArray){
//some sort steps to modify myArray
}
function customSortCountSortStepOnly(myArray){
const copy_myArray=JSON.parse(JSON.stringify(myArray));
let stepCount=0;
//some sort steps to modify copy_myArray
return stepCount;
}
this.customSortReallyModifyArray(this.myArray);
const stepCount=customSortCountSortStepOnly(this.myArray);
this.showAnimation(stepCount);
even if it would cause the "sort step" run twice?