Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to re-factor
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Best way to re-factor
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01565967
Message ID:
01565967
Views:
85
Hi everybody,

Recently I decided to use one exit point from my methods even though it leads to complicated code (sometimes) with multiple if else statements.

I need to re-factor this code:
SqlCommand sqlCommand = new SqlCommand();

                    sqlCommand.CommandType = CommandType.Text;
                    sqlCommand.CommandText = "select closedrwrs from dbo.prefs_sl;";

                    if (database.ExecuteSqlCommand(sqlCommand, ref messageText, ref statusCode))
                    {
                        Int16 closeDrawers;
                        if (Int16.TryParse(messageText, out closeDrawers))
                        {
                            if (closeDrawers < 2)
                            {
                                statusCode = 610;
                                messageText = "System is not set up to close cash drawers";
                            }
                            // Ok to close a drawer
                            else
                            {
                                sqlCommand.Parameters.Clear();
                                String currentOperator, currentSalespoint, closeOperator, closeSalespoint, cDetails;
                                currentOperator = GetParameterValue(parameters, "tcOperator");
                                currentSalespoint = GetParameterValue(parameters, "tcSalespoint");
                                cDetails = GetParameterValue(parameters, "tcDetails");
                                closeOperator = cDetails.ParseStringFromSqml("CLOSE_OP");
                                if (String.IsNullOrWhiteSpace(closeOperator))
                                    closeOperator = currentOperator;
                                closeSalespoint = cDetails.ParseStringFromSqml("CLOSE_SP");
                                if (String.IsNullOrWhiteSpace(closeSalespoint))
                                    closeSalespoint = currentSalespoint;
                                DateTime startTime, endTime;
                                startTime = cDetails.ParseDateTimeFromSqml("START_TIME");
                                endTime = cDetails.ParseDateTimeFromSqml("END_TIME");
                                String closeType;
There are many lines of code below, but that first part is what is common between two different methods in VFP (where it's repeated as is in both methods).

So, my question is - how can I make the above a separate method, but be able to re-use all these variables such as startTime, etc.
?
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform