Skip to content

Instantly share code, notes, and snippets.

@MarcoNicolodi
Last active January 18, 2019 14:10
Show Gist options
  • Save MarcoNicolodi/c163b6fdd03bb6ce2594a9434e6c9819 to your computer and use it in GitHub Desktop.
Save MarcoNicolodi/c163b6fdd03bb6ce2594a9434e6c9819 to your computer and use it in GitHub Desktop.
Chain of responsability code refactoring
public class WebformsComponentCodeBehind
{
public List<TrainingEffectiveness> TrainingEffectiveness { get; private set; }
public List<TrainingEvaluations> TrainingEvaluations { get; private set; }
protected string GetClass(string code, bool approve)
{
// Don't bother these
var evaluation = TrainingEvaluations.Find(x => x.EmployeeCode == code.Split('_')[0] && x.CriteriaCode == code.Split('_')[1]);
var criteriaHasEvaluation = evaluation.Any();
var isNew = code.Split('_')[1] == IEntity.NewCode;
var effectiveness = TrainingEffectiveness.Find(x => x.EmployeeCode == code.Split('_')[0]);
var hasEffectiveness = effectiveness.Any();
// We'll refactor this:
if (hasEvaluation)
{
return evaluation.HasEvaluation
? approve
? evaluation.IsApproved
? "approvedOn"
: "approvedOff"
: !evaluation.IsApproved
? "reprovedOn"
: "reprovedOff"
: approve
? "approvedOff"
: "reprovedOff";
}
if (!hasEffectiveness) return approve ? "approvedOff" : "reprovedOff";
if (isNew) return "approvedOff";
switch (effectiveness.IsEffective)
{
case 0:
return approve ? "approvedOff" : "reprovedOn";
case 1:
return approve ? "approvedOn" : "reprovedOff";
default:
return approve ? "approvedOff" : "reprovedOff";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment