Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Created August 31, 2011 22:55
Show Gist options
  • Save kevinswiber/1184975 to your computer and use it in GitHub Desktop.
Save kevinswiber/1184975 to your computer and use it in GitHub Desktop.
Example of Nullable type hurting innocent puppies.
public class FoodRating
{
public int? Score { get; set; } // YAY, let's make it Nullable! Is this even valid?
}
public class ChickenReview
{
private readonly FoodRating _rating;
public ChickenReview(FoodRating rating)
{
_rating = rating;
}
public bool HasAboveAverageScore()
{
if (!_rating.Score.HasValue)
{
return false; // WTF???
}
return _rating.Score.Value >= 5;
}
}
public class FoodRating
{
private int _score;
public FoodRating(int score) // class can't be in an invalid state.
{
_score = score;
}
public bool IsAboveAverage()
{
return _score >= 5;
}
}
public class ChickenReview
{
private readonly FoodRating _rating;
public ChickenReview(FoodRating rating)
{
_rating = rating;
}
public bool IsPhenomenal() // encapsulate what matters inside, expose what matters outside.
{
return _rating.IsAboveAverage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment