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;
}







-2












$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);









share|improve this question







New contributor




Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$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.























    -2












    $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);









    share|improve this question







    New contributor




    Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $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.



















      -2












      -2








      -2





      $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);









      share|improve this question







      New contributor




      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $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






      share|improve this question







      New contributor




      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      DeveloperDeveloper

      971




      971




      New contributor




      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      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.






















          0






          active

          oldest

          votes

















          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар