Home > Back-end >  Is there a better way to do this (may be using methods)?
Is there a better way to do this (may be using methods)?

Time:03-14

C#
Here orders is a JSON object. I have 3 string which are probably int and I am validating that (using extension methods), however I seem to be repeating the same code for each of the variables. I am looking a way to optimise this as somehow it seems to verbose. Also can I consume date or the double check too in this?

            int itemNumber;
            int customer;
            DateTime date = DateTime.Now;
            string dateFormatted = date.ToString("dd-MM-yyyy");
            int itemQuantity;
            double itemCost;

            bool result;
            int orderNum = int.Parse(orders.OrderNumber);//convert string to int

            int validInt;
            double validDbl;

            //loop through each of the orders
            foreach (var order in orders.OrderDetails)
            {
                string item = order.ItemNumber;
                string cusNumber = order.CustomerNumber;
                string orderDate = order.OrderDate;
                string quantity = order.Quantity;
                string cost = order.Cost;

                //string field;

                result = item.isNumber();

                // Validate item number
                validInt = validate(item);


                //}
                int counter = 0;
                if (validInt != 0)
                {
                    itemNumber = validInt;
                    counter  ;
                }
                else
                {
                    errMessage = $"{item} is not a valid integer value.";
                    errors.Add(errMessage);

                }

                // Validate customer number
                validInt = validate(cusNumber);
              
                if (validInt != 0)
                {
                    customer = validInt;
                    counter  ;
                }
                else
                {
                    errMessage = $"{cusNumber} is not a valid integer value.";
                    errors.Add(errMessage);
                }

                

                validInt = validate(quantity);
                if (validInt != 0)
                {
                    itemQuantity = validInt;
                    counter  ;
                }
                else
                {
                    errMessage = $"{quantity} is not a valid integer value.";
                    errors.Add(errMessage);
                }

                validDbl = isDouble(cost);
                if (validDbl != 0)
                {
                    itemCost= validDbl;
                    counter  ;
                }
                else
                {
                    errMessage = $"{quantity} is not a valid integer value.";
                    errors.Add(errMessage);
                }



            }

I shall then be using the counter and the errMessage to display correct Orders if the counter = total count of items in the JSON object(array).

CodePudding user response:

A smarter validate

 static bool validate(string input, out int field, ref int counter, List<string> errors){
       bool ok = Int32.TryParse(input, out field);
       if(ok){
            counter  ;
       }else{
           errors.Add($"{input} is not a valid integer value.");
       }
       return ok; // just in case we need it
   }

now

    validate(item, out itemNumber, ref counter, errors);
    validate(cusNumber, out customer, ref counter, errors);
    validate(quantity, out itemQuantity, ref counter, errors);
    .....
       

or you could make validate a member function and have counter and errors as member variables, in that case they dont need to be passed as args

  • Related