lundi 28 novembre 2016

Simplifying conditional statement android

I have a method with few conditional statements. I have added the if conditions so I can avoid my NPE crash. Is there any way I can simplify the if conditions where I have added the non-null check?

 private void addComplementaryProductToBasket(String productId, String comboName) {
        Product product = ProductComboUtils.getProductById(productId);
        if (null == product) {
            LOG.info("Product does not exist in ProductComboUtils, fetching from DB");
            product = getProduct(productId);
        }
        if (product != null){
            product.setPrice(BigDecimal.ZERO);
            product.setCurrencyCode(currentBasket.getCurrencyCode());
            // Ensure to add comboName with purchase, this
            currentBasket.addToPurchase(product, comboName);
            int numberOfComplementaryProducts =
                    currentBasket.getPurchaseLineItemForProduct(product.getProductId()).getNumberOfComplementaryProducts();
            currentBasket.getPurchaseLineItemForProduct(product.getProductId()).setNumberOfComplementaryProducts(
                    ++numberOfComplementaryProducts);
        }else
        {
            CrashUtil.logNonFatalException("Product instance is null for productId: " + productId);
        }
    }

Aucun commentaire:

Enregistrer un commentaire