Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save simpleprogrammer-shared/6d59329cc5954cec201fe9035102af65 to your computer and use it in GitHub Desktop.
Save simpleprogrammer-shared/6d59329cc5954cec201fe9035102af65 to your computer and use it in GitHub Desktop.
Validate User Input, Not Developer Input
var milkShakeMaker = new MilkShakeMaker();
var shake = milkShakeMaker.MakeShake(icecubes, milk, ingredients);
public class MileShakeMaker
{
public Shake MakeShake(IEnumerable iceCubes,
IMilk milk,
IEnumerable ingredients)
{
if(ice == null || milk == null || ingredient == null)
throw new InvalidArgumentException("You passed in a null, duh");
if(ice.Count < 1)
throw new InvalidArgumentException("You need ice.");
if(milk.IsSpoiled)
throw new SpoiledMilkException();
foreach(var ice in iceCubes)
{
if(ice.IsMelted())
throw new MeltedIceException();
}
if(ingredients.Count < 1)
throw new InvalidArgumentException("You need some ingredients.");
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment