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;
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With