Reputation: 167
I wrote a code which look somewhat like this
function someName(value){
// some logic here
// the data is coming from this logic part if there is data then
// it will run this part or else it will run next part
if(data){
if(data.type !="pdf"){
anotherFunction(abc,xyz,pqr);
else{
anotherFunction(abc,xyz,pqr);
}
}
else {
if(data.type !="pdf"){
anotherFunction(abc,xyz,pqr);
}
}
}
function anotherFunction(abc,xyz,pqr){
// rest of logic here
}
The point is I am calling anotherFunction(abc,xyz,pqr);
again and again is there any possible way I can reduce my LOC or I have to do it like this only.
Upvotes: 0
Views: 72
Reputation: 13
Fine tuning @infografnet's answer.
function someName(value) {
function anotherFunction(abc, xyz, pqr) {
// rest of logic here
}
var callMe = function () {
anotherFunction(abc, xyz, pqr);
};
if (data) {
if (data.type != "pdf") callMe();
else callMe();
} else {
if (doc.type != "pdf") callMe();
}
}
Upvotes: 1
Reputation: 4005
If you always call anotherFunction with the same set of parameters, you may put it into some other function e.g. callMe() and call only that:
function someName(value) {
// some logic here
function anotherFunction(abc, xyz, pqr) {
// rest of logic here
}
var callMe = function () {
anotherFunction(abc, xyz, pqr);
};
if (data) {
if (data.type != "pdf") {
callMe();
} else {
callMe()
}
} else {
if (doc.type != "pdf") {
callMe();
}
}
}
But your code does not make much sense anyway, the function is being called regardless data.type === "pdf" or not, so I'm only giving you an idea.
Upvotes: 2
Reputation: 6263
You're mainly checking for a pdf type to avoid calling the function, set that check at the begining of the function and return null to avoid further checks.
function someName(value){
if (data && data.type === 'pdf' || doc.type === 'pdf') {
return null;
}
anotherFunction(abc,xyz,pqr);
}
Upvotes: 2