Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Multiple return points - is it OK practice?
Message
 
 
To
01/02/2013 16:19:45
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01564936
Message ID:
01564965
Views:
53
>Checking parameters should not be done in the main code. That's the job of unit tests. If you're checking parameters in the code, you're saying, "Programmer, we can't count on you to do your job, so we're going to check parameters every time this code is called".
>While it was somewhat necessary in VFP, it really isn't necessary in C# because of static data typing.
>
>If you're passing ints, strings, bools as flags for how to do things in the method, then checking that the right value is passed, you're again doing it wrong. Create a struct and pass one of those values.

The parameters are passed as XML string. This is the typical method and I am following the pattern which was developed before me:

This is part of the code of the method I wrote today and unit testing right now
internal static String GetGuestActivity(Dictionary<String, String> parameters)
        {
            Logging.Log(1, "Entering GetGuestActivity");

            List<String> requiredParameters = new List<String>() { "tnGuestNo", "ttStartDate", "ttEndDate" };
            //always required parameters (but not used)          { "tcSalesPoint", "tcOperator" };
            //optional parameters                                { "tcFilter"};
            
            //TODO: get rid of not used tcFilter parameter in the code and SP

            HashSet<String> availableReturnTypes = new HashSet<String>(StringComparer.OrdinalIgnoreCase) { "STR", "ADO", "XML", "XM2" };

            String messageText = "";
            Int32 errorValue = 0;
            Int32 statusCode = 0;

            // If all necessary parameters are present and valid, proceed...
            if (Functions.CheckRequiredParameters(parameters, requiredParameters, availableReturnTypes, out statusCode, out messageText))
            {
                // Parse out the parameters
                String tcGuestNo = Functions.GetParameterValue(parameters, "tnGuestNo");
                String tcStartDate = Functions.GetParameterValue(parameters, "ttStartDate");
                String tcEndDate = Functions.GetParameterValue(parameters, "ttEndDate");
                String tcFilter = Functions.GetParameterValue(parameters, "tcFilter");

                Decimal guestNo;

                if (!Decimal.TryParse(tcGuestNo, out guestNo) || 0 == guestNo)
                {
                    statusCode = 999;
                    messageText = "tnGuestNo expression can not be zero";
                    return Base.LogAndReturnResult(errorValue, statusCode, messageText);
                }
UPDATE. I changed this method to use only 1 exit point as it has only 2 exit points, so it was relatively easy to convert.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform