I'm trying to upgrade my general class design skills in C#, and want you guys to reveal code-smells I might have. (hope general discussions are allowed at Stackoverflow.com)
Regarding to This Tutorial, I wrapped this up to C# and tried to chain constructors, avoid same attribute signatures and solve it the right way, without duplicating code.
The problem in this tutorial: The Running Back and Wide Receiver would use the same attribute signatures. Multiple constructions for each player role would cause errors. Instead you might use object creation.
Below, how I solved it.
Football Player class:
public class FootballPlayer
{
public double PasserRating { get; set; } // Specific to QBs
public int RushingYards { get; set; } // Specific to RBs & QBs
public int ReceivingYards { get; set; } // Specific to RBs & WRs
public int TotalTackles { get; set; } // Specific to DEF
public int Interceptions { get; set; } // Specific to DEF
public int FieldGoals { get; set; } // Specific to Kickers
public double AvgPunt { get; set; } // Specific to Punters
public double AvgKickoffReturn { get; set; }// Specific to Special Teams
public double AvgPuntreturn { get; set; } // Specific to Special Teams
private FootballPlayer() : this(0.0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0) { }
private FootballPlayer(double passerRating, int rushingYards, int receivingYards, int totalTackles, int interceptions, int fieldGoals, double avgPunt, double avgKickoffReturn, double avgPuntreturn)
{
PasserRating = passerRating;
RushingYards = rushingYards;
ReceivingYards = receivingYards;
TotalTackles = totalTackles;
Interceptions = interceptions;
FieldGoals = fieldGoals;
AvgPunt = avgPunt;
AvgKickoffReturn = avgKickoffReturn;
AvgPuntreturn = avgPuntreturn;
}
public static FootballPlayer CreateQb(double passerRating, int rushingYards)
{
return new FootballPlayer
{
PasserRating = passerRating,
RushingYards = rushingYards
};
}
public static FootballPlayer CreateRb(int rushingYards)
{
return new FootballPlayer
{
RushingYards = rushingYards
};
}
public static FootballPlayer CreateWr(int receivingYards)
{
return new FootballPlayer
{
ReceivingYards = receivingYards
};
}
}
Program:
class Program
{
static void Main()
{
var aaronRodgers = FootballPlayer.CreateQb(132.0, 259);
var drewBrees = FootballPlayer.CreateQb(129.6, 244);
Console.WriteLine("Aaron Rodgers passer Rating: {0}", aaronRodgers.PasserRating);
// OUTPUT: Aaron Rodgers passer Rating: 132
Console.WriteLine("Drew Brees passer Rating: {0}", drewBrees.PasserRating);
// OUTPUT: Drew Brees passer Rating: 129.6
Console.ReadLine();
}
}
Would you guys say that's the way to go?
Or what should I take into account?