Solved

C# - Refactor Code

Posted on 2014-09-24
1
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

739 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