Solved

C# - Refactor Code

Posted on 2014-09-24
1
112 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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