Created
August 31, 2011 22:55
-
-
Save kevinswiber/1184975 to your computer and use it in GitHub Desktop.
Example of Nullable type hurting innocent puppies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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