consider this method:
public ActionResult DoSomeAction(ViewModel viewModel)
{
try
{
if (!CheckCondition1(viewModel))
return Json(new {result = "Can not process"});
if (CheckCondition2(viewModel))
{
return Json(new { result = false, moreInfo = "Some info" });
}
var studentObject = _helper.GetStudent(viewModel, false);
if (viewModel.ViewType == UpdateType.AllowAll)
{
studentObject = _helper.ReturnstudentObject(viewModel, false);
}
else
{
studentObject.CourseType = ALLOW_ALL;
studentObject.StartDate = DateTime.UtcNow.ToShortDateString();
}
if (studentObject.CourseType == ALLOW_UPDATES)
{
var schedule = GetSchedules();
if (schedule == null || !schedule.Any())
{
return Json(new { result = NO_SCHEDULES });
}
_manager.AddSchedule(schedule);
}
else
{
_manager.AllowAllCourses(studentObject.Id);
}
_manager.Upsert(studentObject);
return Json(new { result = true });
}
catch (Exception e)
{
// logging code
}
}
This method has many exit points and has a Cyclomatic Complexity of 8. Our best practices say that it should not be greater than 5.
Is it because of multiple IFs?
What can I do to refactor this so that it has less exit points?
Thanks in advance.