refactoring to make it simple without many if [on hold]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I have this piece of code in my repository of nodejs.
Criteria is huge but I have cut it short to keep the question simple.
there are lot of if conditions, which I don't really like... wondering what is best way to refactor it to more simple and to the point...
const {
a,
b,
} = criteria;
const bindings = { a: `${a}` };
if b!== '') {
bindings.b = `${b}%`;
}
.... if conditions keep going depending on the long criteria, I just skips those part to cut the question short
..
...
const optionalWhere = `
${(b !== '') ? ' AND b LIKE :b' : ''}
`;
const result = await this.knex.raw(`SELECT blah blah
WHERE a = :a
${optionalWhere}
`, bindings);
javascript node.js
New contributor
$endgroup$
put on hold as off-topic by 200_success, Jamal♦ 1 hour ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
I have this piece of code in my repository of nodejs.
Criteria is huge but I have cut it short to keep the question simple.
there are lot of if conditions, which I don't really like... wondering what is best way to refactor it to more simple and to the point...
const {
a,
b,
} = criteria;
const bindings = { a: `${a}` };
if b!== '') {
bindings.b = `${b}%`;
}
.... if conditions keep going depending on the long criteria, I just skips those part to cut the question short
..
...
const optionalWhere = `
${(b !== '') ? ' AND b LIKE :b' : ''}
`;
const result = await this.knex.raw(`SELECT blah blah
WHERE a = :a
${optionalWhere}
`, bindings);
javascript node.js
New contributor
$endgroup$
put on hold as off-topic by 200_success, Jamal♦ 1 hour ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
I have this piece of code in my repository of nodejs.
Criteria is huge but I have cut it short to keep the question simple.
there are lot of if conditions, which I don't really like... wondering what is best way to refactor it to more simple and to the point...
const {
a,
b,
} = criteria;
const bindings = { a: `${a}` };
if b!== '') {
bindings.b = `${b}%`;
}
.... if conditions keep going depending on the long criteria, I just skips those part to cut the question short
..
...
const optionalWhere = `
${(b !== '') ? ' AND b LIKE :b' : ''}
`;
const result = await this.knex.raw(`SELECT blah blah
WHERE a = :a
${optionalWhere}
`, bindings);
javascript node.js
New contributor
$endgroup$
I have this piece of code in my repository of nodejs.
Criteria is huge but I have cut it short to keep the question simple.
there are lot of if conditions, which I don't really like... wondering what is best way to refactor it to more simple and to the point...
const {
a,
b,
} = criteria;
const bindings = { a: `${a}` };
if b!== '') {
bindings.b = `${b}%`;
}
.... if conditions keep going depending on the long criteria, I just skips those part to cut the question short
..
...
const optionalWhere = `
${(b !== '') ? ' AND b LIKE :b' : ''}
`;
const result = await this.knex.raw(`SELECT blah blah
WHERE a = :a
${optionalWhere}
`, bindings);
javascript node.js
javascript node.js
New contributor
New contributor
New contributor
asked 2 hours ago
DeveloperDeveloper
971
971
New contributor
New contributor
put on hold as off-topic by 200_success, Jamal♦ 1 hour ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by 200_success, Jamal♦ 1 hour ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes