4
\$\begingroup\$

I'm quite new to object-oriented C#. I wanted to test my knowledge by creating a Dog/Animal classes. I also tried to implement an IDangerous interface in my code.

Is there anything I could've done better/differently? Again, this is my first time on this Stack Exchange so if I could've restructured this post differently please tell me.

public class Animal
{
    public int Age { get; }
    public string Color { get; }
}
public class Dog : Animal
{
    public bool IsVaccinated { get; set; }
    public string Name { get; set; }
}
public interface IDangerous
{
    void TightenLeash(string Reason, int LeashTightness);
}
public class GoldenRetriever : Dog { }
public class Beagle : Dog { }
public class PitBull : Dog, IDangerous
{
    public void TightenLeash(string Reason, int LeashTightness) 
    {
        Console.WriteLine("Leash tightened...");
    }
}
static class Program
{
    static void Main(string[] args)
    {
        Dog obj1 = new GoldenRetriever();

        obj1.Name = "Fluffy";
        obj1.IsVaccinated = true;
        Console.WriteLine(obj1.Name + obj1.IsVaccinated + obj1.Color);

        List<Dog> DogList = new List<Dog>()
        {
            new Beagle()
            {
                IsVaccinated = false,
                Name = "Korg"
            },
            new GoldenRetriever()
            {
                IsVaccinated = true,
                Name = "Juan"
            },
            new Beagle()
            {
                IsVaccinated = false,
                Name = "Toby"
            },
        };

        List<IDangerous> AngryPitBulls = new List<IDangerous>()
        {
            new PitBull()
            {
                IsVaccinated = false,
                Name = "Angry"
            },
            new PitBull()
            {
                IsVaccinated = false,
                Name = "Ryder"
            }
        };
        AngryPitBulls[0].TightenLeash("For biting another dog", 3);
        AngryPitBulls[1].TightenLeash("For biting the cat", 2);

        Console.ReadLine();
    }


}
\$\endgroup\$
3
  • \$\begingroup\$ Code Review requires concrete code from a project, with enough code and / or context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site. \$\endgroup\$
    – slepic
    Sep 20, 2020 at 6:36
  • 5
    \$\begingroup\$ The biggest problem with these animal examples is, that they are very far from real world problems and hardly useful to learn anything. You may understand the syntax and semantics but you get no idea about the purpose. \$\endgroup\$
    – slepic
    Sep 20, 2020 at 6:39
  • \$\begingroup\$ @slepic They're famously used as programming exercises, just like FizzBuzz. \$\endgroup\$
    – Mast
    Sep 20, 2020 at 10:34

2 Answers 2

3
\$\begingroup\$
Dog obj1 = new GoldenRetriever();

    obj1.Name = "Fluffy";
    obj1.IsVaccinated = true;
    Console.WriteLine(obj1.Name + obj1.IsVaccinated + obj1.Color);

Variable names: avoid name s like obj1 why not Dog fluffy=new GoldenRetriever(); You have derived GoldenRetriever and Beagle from Dog. Obviously this is an incomplete example and you may have wanted to add further methods according to breed. In this case however it is often best to modify the implmentation at the time you are introducing the extra methods. Unless there are different implementations best not to create new classes. So you could have had a member property Breed in the Dog class.

The IDangerous interface looks like it should be called IDangerousDog If you had a tiger in data tightening the leash is not a likely scenario except if tigers in the data can only be circus tigers. Even then I am not sure I would want to pull an angry tiger closer to me.

When you construct your Dogs it may be better to use a constructor to instantiate the Name and colour properties. This allows you then to have read only members. This is debateable in your example as you could argue that the name of a dog may change if the owner changes. It is best to keep properties immutable where they should be considered so.

If you stored the date of birth rather than the age then this could be immutable too. It is much easier then to be confident that the age has been corrupted by a bug. Consider what would happen if a bug where to cause the Update age to be fired more than once or at the wrong time. It would be difficult to go back and identify what the correct age of a dog is. However if the Date of birth is stored you can always calculate the age and if there is a bug in the calculation you have not lost the value on which the age calculation is based.

\$\endgroup\$
2
\$\begingroup\$

This would be long comment.

These general exercises are meant for giving a clearer visual to the code design and syntax in simple terms. It would be hard to criticize, and you will get very different feedback perspectives, where some of them would conflict each other and confuses you. It would give you a small percentage improvements, but not the improvement that you desire.

If you need to develop your coding skills and experience, you need to find a real-world examples that are already running and try to recreate them in your own terms with similar functionalities, then you can add your improvements, missing functionalities ..etc. to it.

School, and Book Store are examples of a real-world examples or you can even try to mimic existing applications like the Calculator for example. There are many others examples out there, just pick whatever you think it's near your desired field of expertise that you need to improve.

\$\endgroup\$
1
  • \$\begingroup\$ Makes sense. Thanks for your reply. \$\endgroup\$
    – tommy99
    Sep 20, 2020 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.