C# Intermediate course Exercise-2 : Complete Solution: Feedback Request

Hello, it’s frustrating that Josh no longer provides solutions, I cannot compare mine to the ideal solution. Therefore, would people mind giving feedback on my solution and also highlighting areas of improvemnt:

namespace ConsoleApp1
{
    public class Post
    {
        private string? _title;
        private string? _description;
        private readonly DateTime? _created;
        private int _vote;

        public Post(string postTitle, string postDescription)
        {
            _created = DateTime.Now;
            CreatePost(postTitle, postDescription);
            _vote = 0;
        }

        private void CreatePost(string postTitle, string postDescription)
        {
            if (string.IsNullOrWhiteSpace(postTitle) || string.IsNullOrWhiteSpace(postDescription))
            {
                throw new Exception(postTitle);
                throw new Exception(postDescription);
            }
            if (postTitle != null && postDescription != null)
            {
                _title = postTitle;
                _description = postDescription;
            }

        }

        public void PostUpVote()
        {
            _vote += 1;
        }

        public void PostDownVote()
        {
            _vote -= 1;
        }

        public string GetPostTitle()
        {
            if (!string.IsNullOrWhiteSpace(_title))
            {
                return _title;
            }
            else
            {
                throw new Exception("Error: No Title Set");
            }
        }

        public string GetPostDescription()
        {
            if (!string.IsNullOrWhiteSpace(_description)) {
                return _description;
            } else
            {
                throw new Exception("Error: No Description Set");
            }
        }

        public int GetPostVotes()
        {
            return _vote;
        }

    }
}



namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var newPost = new Post("This is a title","Hello, this is a new post");
            newPost.PostUpVote();
            newPost.PostUpVote();
            newPost.PostUpVote();
            newPost.PostUpVote();
            newPost.PostUpVote();
            Console.WriteLine("Title: " + newPost.GetPostTitle());
            Console.WriteLine("Description: " + newPost.GetPostDescription());
            Console.WriteLine("Votes: " + newPost.GetPostVotes());
            newPost.PostDownVote();
            Console.WriteLine("Title: " + newPost.GetPostTitle());
            Console.WriteLine("Description: " + newPost.GetPostDescription());
            Console.WriteLine("Votes: " + newPost.GetPostVotes());
        }
    }
}

A few comments:

You check for null and empty title and description in the constructor already so I would remove those checks from the getters.

        public string GetPostTitle()
        {
            return _title;
        }

        public string GetPostDescription()
        {
            return _description;
        }

Also, I would move the content of CreatePost into the constructor itself (maybe except for the validation):

        public Post(string postTitle, string postDescription)
        {
            CheckTitle(post title);
            CheckDescription(postDescription);
            _created = DateTime.Now;
            _title = postTitle;
            _description = postDescription;
            _vote = 0;
        }

Validation should ideally throw better exceptions than Exception and use better error messages. I think ArgumentException is correct here:

private CheckTitle(string postTitle) {
  if (string.IsNullOrWhiteSpace(postTitle)
  {
    throw new ArgumentException("title must be a non-empty string");
  }
}

private CheckDescription(string postDescription) {
  if (string.IsNullOrWhiteSpace(postDescription)
  {
    throw new ArgumentException("description must be a non-empty string");
  }
}

There are probably more things that could be commented on by a more experienced C# developer, but there is no such thing as an “ideal” solution. Mosh could have provided a canonical solution, but he chose not to.

1 Like