Skip to content

Instantly share code, notes, and snippets.

@thaianhduc
Last active December 8, 2018 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thaianhduc/ca2a238c764091e567faeafa7ce28eab to your computer and use it in GitHub Desktop.
Save thaianhduc/ca2a238c764091e567faeafa7ce28eab to your computer and use it in GitHub Desktop.
A simple piece of code to demonstrate the code readability by looking at method's parameters.
using System;
using System.Collections.Generic;
using System.Linq;
public class VerificationContext
{
public string SpecialNotes { get; set; }
public ApplicantCv ApplicantCv { get; set; }
}
public class ApplicantCv
{
public string ApplicantName { get; set; }
}
public class SeniorPosition : IVerifyingByPosition
{
public bool Accept(VerificationContext context)
{
return !string.IsNullOrWhiteSpace(context.SpecialNotes);
}
public void Verifying(VerificationContext context)
{
// In a real project, there might be many lines in this method.
Console.WriteLine($"Special notes: {context.SpecialNotes}");
Console.WriteLine($"You are passed when {context.ApplicantCv.ApplicantName} is Batman");
}
}
public class JuniorPosition : IVerifyingByPosition
{
public bool Accept(VerificationContext context)
{
return string.IsNullOrWhiteSpace(context.SpecialNotes);
}
public void Verifying(VerificationContext context)
{
Verifying(context.ApplicantCv);
}
private void Verifying(ApplicantCv applicantCv)
{
// The method might be long
Console.WriteLine($"Welcome junior: {applicantCv.ApplicantName}");
}
}
public interface IVerifyingByPosition
{
void Verifying(VerificationContext context);
bool Accept(VerificationContext context);
}
public class HrDepartment
{
public void VerifyApplicants(IEnumerable<VerificationContext> applicants)
{
var byPositions = new List<IVerifyingByPosition>
{
new JuniorPosition(),
new SeniorPosition()
};
foreach (var app in applicants)
{
byPositions.First(x => x.Accept(app))
.Verifying(app);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment