Skip to content

Instantly share code, notes, and snippets.

@samfieldscc
Created September 21, 2020 16:45
Example of deferred exception throw
//CLEAN CODING WARNING: It's not good practice to have a method that does two completly different tasks
//This is just to make a point, good coding practices state that you would have a method
//that is responsable for performing any filtering operations and another one to print your data.
//If because of some restriction you have to, at least make sure the name of the method
//states what is happening clearly.
public void FilterAndPrint()
{
List<int> dataSource = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<int> valuesToRemove = null;
IEnumerable<int> filteredData = new List<int>();
try
{
filteredData = dataSource.Where(t => !valuesToRemove.All(x => x == t));
}
catch (ArgumentNullException e)
{
Console.WriteLine(e);
}
foreach (var i in filteredData)
{
Console.WriteLine(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment