Here is my code:
function function1() {
var ids = GetIds(); // predefined function
if (ids.length === 0) {
alert("Please select at least one item to procees.");
return;
}
...
}
function function2() {
var ids = GetIds(); // predefined function
if (ids.length === 0) {
alert("Please select at least one item to procees.");
return;
}
...
}
function function3() {
var ids = GetIds(); // predefined function
if (ids.length === 0) {
alert("Please select at least one item to procees.");
return;
}
...
}
How to extract the common part out? How to re-factor the code? The return statement is very hard to deal with. Is there any pattern relating to re-factor this code?
Thanks in advance!
if (ids.length === 0) {
alert("Please select at least one item to procees.");
return;
}
You can’t. The only thing you can do is moving the
alert()intoGetIds(). However, the function name would be a bit confusing in this case.Or you could change it like this:
Your
GetIdsfunction would then look e.g. like this: