When planning out my programs, I often start with a chain of thought like so:
A football team is just a list of football players. Therefore, I should
What if the FootballTeam
has a reserves team along with the main team?
class FootballTeam
{
List Players { get; set; }
List ReservePlayers { get; set; }
}
How would you model that with?
class FootballTeam : List
{
public string TeamName;
public int RunningTotal
}
The relationship is clearly has a and not is a.
or RetiredPlayers
?
class FootballTeam
{
List Players { get; set; }
List ReservePlayers { get; set; }
List RetiredPlayers { get; set; }
}
As a rule of thumb, if you ever want to inherit from a collection, name the class SomethingCollection
.
Does your SomethingCollection
semantically make sense? Only do this if your type is a collection of Something
.
In the case of FootballTeam
it doesn't sound right. A Team
is more than a Collection
. A Team
can have coaches, trainers, etc as the other answers have pointed out.
FootballCollection
sounds like a collection of footballs or maybe a collection of football paraphernalia. TeamCollection
, a collection of teams.
FootballPlayerCollection
sounds like a collection of players which would be a valid name for a class that inherits from List
if you really wanted to do that.
Really List
is a perfectly good type to deal with. Maybe IList
if you are returning it from a method.
In summary
Ask yourself
Is X
a Y
? or Has X
a Y
?
Do my class names mean what they are?