Skip to content

Instantly share code, notes, and snippets.

@tamizhvendan
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save tamizhvendan/4fd1f5ea2b4e28658e81 to your computer and use it in GitHub Desktop.

Select an option

Save tamizhvendan/4fd1f5ea2b4e28658e81 to your computer and use it in GitHub Desktop.
Verbs - Buried abstraction in OO World
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
// Populate Students List here
Student[] studentsSortedByName = students.ToArray();
Array.Sort(studentsSortedByName, new StudentNameComparer());
}
}
class Program1
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
// Populate Students List here
Student[] studentsSortedByAge = students.ToArray();
Array.Sort(studentsSortedByAge, new StudentAgeComparer());
}
}
class Program2
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
// Populate Students List here
var studentsSortedByName = Enumerable.OrderBy(students, s => s.Name);
var studentsSortedByAge = Enumerable.OrderBy(students, s => s.Age);
}
}
public class Student
{
public int Age { get; set; }
public string Name { get; set; }
}
public class StudentAgeComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
if (x.Age == y.Age)
{
return 0;
}
if (x.Age < y.Age)
{
return -1;
}
return 1;
}
}
public class StudentNameComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return string.Compare(x.Name, y.Name, StringComparison.CurrentCulture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment