Solved

C# - Refactor Code

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

14 Experts available now in Live!

Get 1:1 Help Now