Solved

C# - Refactor Code

Posted on 2014-09-24
1
106 Views
Last Modified: 2014-09-25
Can the below code be refactored?

    private void PayrollDeduction(DataSet myDataSet)
    {
        DataTable dtPreTax = new DataTable();
        DataTable dtPostTax = new DataTable();
        double dblPreTaxTotal = 0;
        double dbloPreTaxTotal = 0;
        double dblPostTaxTotal = 0;

        //Get PreTax Data
        dtPreTax = myDataSet.Tables["Deduction"].Copy();
        dtPreTax.TableName = "dtPreTax";
        DataRow[] drPreTax = dtPreTax.Select("postTax=true");
        for (int i = 0; i < drPreTax.Length; i++)
            drPreTax[i].Delete();
        dtPreTax.AcceptChanges();

        DataColumn col = new DataColumn("Sort", typeof(System.Int16));
        col.DefaultValue = 0;
        dtPreTax.Columns.Add(col);

        if (dtPreTax != null)
        {
            //Get Total
            dblPreTaxTotal = Convert.ToDouble(dtPreTax.Compute("Sum(amount)", ""));
            if (dblPreTaxTotal >= 0)
                dbloPreTaxTotal = dblPreTaxTotal;

            //Decide Description
            if (dblPreTaxTotal > 0)
                this.lblPreTaxDesc.Text = "Total Deduction (PreTax)";
            else
                this.lblPreTaxDesc.Text = "Total Credit";
            double dblPreTxTotal = 0;
            //Do not display Negative sign
            if (dblPreTaxTotal <= 0)
                dblPreTxTotal = Math.Abs(dblPreTaxTotal); 
            //Display PreTax Total
            if (dblPreTaxTotal < 0)
                this.lblPreTaxAmt.Text = dblPreTxTotal.ToString("c");
            else
                this.lblPreTaxAmt.Text = dbloPreTaxTotal.ToString("c");
        }

        //Do not display negative values on GUI
        foreach (DataRow row in dtPreTax.Rows)
        {
            if (row[0].ToString().Contains("Budgeted"))
            {
                //Set Sort
                row["Sort"] = 1;
            }
            if (Convert.ToDouble(row[1].ToString()) < 0)
            {
                //Convert negative to positive
                row["amount"] = Convert.ToDouble(row[1].ToString()) * -1;
            }
        }
        dtPreTax.AcceptChanges();

        //Sort
        dtPreTax.DefaultView.Sort = "Sort ASC";

        //Bind PreTax Data
        this.rptPreTax.DataSource = dtPreTax;
        this.rptPreTax.DataBind();

        //Get PostTax Data
        dtPostTax = myDataSet.Tables["Deduction"].Copy();
        dtPostTax.TableName = "dtPostTax";
        DataRow[] drPostTax = dtPostTax.Select("postTax=false");
        for (int i = 0; i < drPostTax.Length; i++)
            drPostTax[i].Delete();
        dtPostTax.AcceptChanges();

        //Does PostTax Data Exist?
        if (dtPostTax.Rows.Count == 0 || dtPostTax == null)
        {
            this.pnlPostTax.Visible = false;
        }
        else
        {
            dblPostTaxTotal = Convert.ToDouble(dtPostTax.Compute("Sum(amount)", "")); ;
            this.lblPostTaxAmt.Text = dblPostTaxTotal.ToString("c");
            this.lblPostTaxDesc.Text = "Total Payroll Deduction (PostTax)";

            this.pnlPostTax.Visible = true;
            this.rptPostTax.DataSource = dtPostTax;
            this.rptPostTax.DataBind();
        }

        this.lblGrandTotal.Text = "Deduction Grand Total";

        double dblGrandTotal = dbloPreTaxTotal + dblPostTaxTotal;
        if (dblGrandTotal < 0)
            dblGrandTotal = 0;
        this.lblGrandTotalAmt.Text = dblGrandTotal.ToString("c");
    }

Open in new window

0
Comment
Question by:CipherIS
1 Comment
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40342182
yes.

There's no need for extra variables.


//just display the absolute value regardless
  this.lblPreTaxAmt.Text = math.Abs(dbloPreTaxTotal).ToString("c");

replaces
            double dblPreTxTotal = 0;
            //Do not display Negative sign
            if (dblPreTaxTotal <= 0)
                dblPreTxTotal = Math.Abs(dblPreTaxTotal); 
            //Display PreTax Total
            if (dblPreTaxTotal < 0)
                this.lblPreTaxAmt.Text = dblPreTxTotal.ToString("c");
            else
                this.lblPreTaxAmt.Text = dbloPreTaxTotal.ToString("c");

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short film showing how OnPage and Connectwise integration works.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now