Solved

Duplicate Code Driving me Crazy

Posted on 2013-01-03
1
378 Views
Last Modified: 2013-01-07
Hello,

I am trying to improve my coding practices, and have an area I could use help getting some advice on.  This is the code in a static Class in a WCF project.  It checks to see if an item is in the database already and active, in the database and inactive, or not in the database. del in the database is a bit type so will return true or false.
 public static bool? CheckForComplaintCode(IndComplaint ind)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM comcodes where comcode = @ComCode";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@ComCode", ind.ComCode));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }

        public static bool? CheckForDepartmentCode(IndDepartment ind)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM deptcode where deptcode = @DeptCode";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@DeptCode", ind.DeptCode));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }//end def 'CheckForDepartmentCode'

Open in new window


I have 15 of these, each taking a differnt object.  I understand duplicated code is not ideal, and want to know another way of doing this, that uses some aspect of C# I am not fully educated in.

 I thought about using inheritance and making all the objects Derive from a Parent calss, then do the following:
 if (object is IndDepartment) { do stuff.. }
 but that seems like it will be the same amount of code, but only in one function.

I would like to use Delegates, Func<>  or Action<> to solve this, but while I understande the syntax, I cant wrap my head around how to apply them (of if they even apply in this situation).  

Any help would be appreciated.  Thank you.
0
Comment
Question by:paulpp
1 Comment
 
LVL 1

Accepted Solution

by:
ogm earned 500 total points
ID: 38742564
A simple solution would have a single CheckForCode function with 2 parameters: the code name, and the value to check for.

Something like this:

public static bool? CheckForCode(string codeName, int value)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM " + codeName + " where " + codeName +" = @Value";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@Value", value));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }//end def 'CheckForCode'

Open in new window


You can do a sanity check for the codeName argument if it's necessary, so the function only accepts valid ones.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

16 Experts available now in Live!

Get 1:1 Help Now