1

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?

MrMAG
  • 1,194
  • 1
  • 11
  • 34
  • I've never seen a coding tutorial in video form before. It just seems wrong. How do you copy and paste from it when writing production code? – Resource Dec 30 '14 at 11:23
  • @user910683 Not sure what you mean. Every author who teach code via video will upload the code somewhere. From the above link, author links to the [code](http://www.newthinktank.com/2013/01/code-refactoring/) – Sriram Sakthivel Dec 30 '14 at 11:31
  • @user910683 Usually I do not use tutorial in video form either. But this tutorial seemed valuable for me, that's why I started watching it. You simply can't copy and paste from videos, you need to write it by yourself or if you are lucky there's a link in the video description. But I always learn by typing it by myself, with all the typos that might occur while coding! – MrMAG Dec 30 '14 at 11:33
  • I was just joking. The very idea of coding video tutorials makes me feel old - in my day, you bought a book and hoped the information you needed was covered somewhere inside. Otherwise, you would have to buy another book. – Resource Dec 30 '14 at 11:57

0 Answers0