1
\$\begingroup\$

I want to avoid using multiple if and else condition here

  if (decode) {
                Imagepath = bucket + "/" + key;
                Result.pages.push({                         
                        Id: Processid,
                        ValidTicket: decode ? true : false
                });
        } else {
                //checking for barcode if qr code is not present
                Quagga.decodeSingle({
                        src: filename,
                        numOfWorkers: 0, // Needs to be 0 when used within node 
                        decoder: {
                                readers: ['code_128_reader',
                                        'ean_reader',
                                        'ean_8_reader',
                                        'code_39_reader',
                                        'code_39_vin_reader',
                                        'codabar_reader',
                                        'upc_reader',
                                        'upc_e_reader',
                                        'i2of5_reader'
                                ] // List of active readers 
                        },
                }, function (result, error) {
                        //if guaggajs returns the result
                        if (result) {
                                console.log("bar code done");
                                //sometimes result is not empty but codeResult is empty
                                if (result.codeResult) {
                                        var barcode = result.codeResult.code;
                                        var Imagepath = '';
                                        Imagepath = bucket + "/" + key;
                                        Result.pages.push({
                                                Id: Processid,
                                                ValidTicket: barcode ? true : false
                                        });
                                        console.log("final array is" + Result);
                                } else {
                                        var Imagepath = '';
                                        Imagepath = bucket + "/" + key;
                                        Result.pages.push({
                                                Id: Processid,
                                                ValidTicket: false
                                        });

                                }
                        } else {
                                var Imagepath = '';
                                Imagepath = bucket + "/" + key;
                                Result.pages.push({
                                        Id: Processid,
                                        ValidTicket: false
                                });

                        }
                });
        }
\$\endgroup\$
  • \$\begingroup\$ Do you want to remove Quagga.decodeSingle() callbacks if...else conditions? \$\endgroup\$ – Tushar Mar 27 '17 at 7:05
  • 5
    \$\begingroup\$ Welcome to StackExchange Code Review! Please review How do I ask a good Question? Specifically, it is best to explain what the code does. This is especially true in the title. \$\endgroup\$ – Stephen Rauch Mar 27 '17 at 7:14
  • \$\begingroup\$ @Tushar exactly \$\endgroup\$ – face turn Mar 27 '17 at 7:25
1
\$\begingroup\$

I would separate them out into functions to make the code more readable. So the first if/else would become:

if (decode)
  doDecode()
else
  doEncodeSingle();

I would also break the doDocdeSingle up:

Quagga.decodeSingle({...},
  handleDecodeResult );

The last if/else is currently structured as:

if (result) {
  if (result.codeResult) {
    // Handle Code
  } else {
    // Handle no-code
  }
} else {
  // No result
}

I often find it cleaner to reverse the condition and structure it as:

if (!result) {
  // No result
} else if (result.codeResult) {
  // Handle Code
} else {
  // Handle no-code
}

Which removes a level of nesting. Again I would try and make each if section a single function call.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

You could remove one else block like this:

var barcode = '';
if (result && result.codeResult) {
    console.log("bar code done");
    //sometimes result is not empty but codeResult is empty
    barcode = result.codeResult.code;
}
var Imagepath = '';
Imagepath = bucket + "/" + key;
Result.pages.push({
    Id: Processid,
    ValidTicket: barcode ? true : false
});
console.log("final array is" + Result);
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ As the barcode actual value result.codeResult.code is never used, why don't turn the variable to a boolean? \$\endgroup\$ – Mario Santini Mar 27 '17 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.