This was my Initial Method
public String GetAllDocuments(string url,int pager =0)
{
if (SessionInfo.IsAdmin)
{
ReportHandler dal = new ReportHandler();
var documents = dal.FetchDocumentsList(SessionInfo.ClientID, pager);
string documentsDataJSON = JsonConvert.SerializeObject(documents);
return documentsDataJSON;
}
else
{
return "Sorry!! You are not authorized to perform this action";
}
}
Visual Studio shows the following Code Metrics:-
Member: GetAllDocuments(string, int) : string
Maintainability Index: 67
Cyclomatic Complexity: 2
Class Coupling: 7
Lines of Code: 7
So in order to improve it, I modified my method as below:-
public String GetAllDocuments(string url,int pager =0)
{
ReportHandler dal = new ReportHandler();
var documents = dal.FetchDocumentsList(SessionInfo.ClientID, pager);
//moved the JSON Conversion to Separate class
string documentsDataJSON = JsonHandler<T>.ConvertToJSON(documents);
return documentsDataJSON;
}
But still it shows the Code Metrics as
Member: GetAllDocuments(string, int) : string
Maintainability Index: 72
Cyclomatic Complexity: 1
Class Coupling: 5
Lines of Code: 5
I can't submit this, unless the Maintainability Index is 90+.
What else can I do to improve Code Metrics.
Also, I am thinking for such small things creating separate method/class isn't it overhead
If you interested in just make Maintainability Index better read how this is calculated: http://www.projectcodemeter.com/cost_estimation/help/GL_maintainability.htm
You can see that it depends mostly on number of lines of code.
If want good design, stop using concrete implementations, use one of SOLID principles - Dependency Inversion. For example, instead of using:
You shoud use something like:
Or even better is to inject such things using DI container.