Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make this code more readable?

I wrote this today and I'm ashamed. What do I need to do to make this chaotic stuff more accurate and readable amongst others?

switch ((RequestReportsCalculatingStoredProcedures.RequestReportStoredProcedureType)Enum.Parse(typeof(RequestReportsCalculatingStoredProcedures.RequestReportStoredProcedureType), ihdType.Value))
    {
            //REF:This can (but should it?) be refactored through strategy pattern
        case RequestReportsCalculatingStoredProcedures.RequestReportStoredProcedureType.ReportPlanWithEffects:
            grvEconomicCriteria.DataSource = RequestReportsCalculatingStoredProcedures.ReportsDataParser(
                            RequestReportsCalculatingStoredProcedures.ReportPlanWithEffects(requestNo, RequestReportsCalculatingStoredProcedures.GetAlgorithmNoByRequestNo(requestNo)));
            break;
        case RequestReportsCalculatingStoredProcedures.RequestReportStoredProcedureType.ReportPlanWithEffectsForFacts:
            DateTime factDate;
            try
            {
                factDate = Convert.ToDateTime(ihdDate.Value);
            }
            catch(FormatException)
            {
                grvEconomicCriteria.DataSource = RequestReportsCalculatingStoredProcedures.ReportsDataParser(
                            RequestReportsCalculatingStoredProcedures.ReportPlanWithEffectsForFacts(requestNo, RequestReportsCalculatingStoredProcedures.GetAlgorithmNoByRequestNo(requestNo), DateTime.MinValue));
                break;
            }
            grvEconomicCriteria.DataSource = RequestReportsCalculatingStoredProcedures.ReportsDataParser(
                            RequestReportsCalculatingStoredProcedures.ReportPlanWithEffectsForFacts(requestNo, RequestReportsCalculatingStoredProcedures.GetAlgorithmNoByRequestNo(requestNo), factDate));
            break;
        default:
            break;
    }
like image 766
Dan Ganiev Avatar asked Oct 21 '10 13:10

Dan Ganiev


People also ask

What does readable code look like?

Readable code is simply code that clearly communicates its intent to the reader. Most likely, the code we write will be read by other developers, who will either want to understand or modify the way our code works. They may need to test the code, fix a problem, or add a new feature.

What is code readable?

Code readability is one of the first factors a developer learns, making it a quality one should always master. It merely means writing and presenting your code in such a manner that it can be easily read and understood. Much easier said than done, but it is as important as solving the problem.

What makes a program readable?

Good variable, method and class names. Variables, classes and methods that have a single purpose. Consistent indentation and formatting style. Reduction of the nesting level in code.


2 Answers

You can always use an alias for the very long type RequestReportsCalculatingStoredProcedures:

using RRCSP = RequestReportsCalculatingStoredProcedures;

Note: You will need to use the fully qualified name (including namespace) in the using directive.

like image 84
Oded Avatar answered Oct 15 '22 06:10

Oded


On top of what the others have said about shortening the names etc, you should think about extracting the code from the case statement into function calls, so you end up with something like

switch (myMassiveVariable)
{
    case RequestReportStoredProcedureType.ReportPlanWithEffects:
        RunReportWithEffects(requestNo);
        break;
    case RequestReportStoredProcedureType.ReportPlanWithEffectsForFacts:
        RunReportWithFacts(requestNo);
        break;
}

That helps to tidy things up a bit.

like image 11
Grant Crofton Avatar answered Oct 15 '22 06:10

Grant Crofton