Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Kevin Huang
Kevin Huang
5,806 Points

Address book challange, please review my code , any bad practice I have done in my code and suggestions?

https://github.com/yizhouh/addressbook Please let me know the bad practice I have done in my code here. īŧŠ havent add any exception handle in this program, since I couldnt define any exception could be done.

1 Answer

Allan Clark
Allan Clark
10,810 Points

The main thing here that could be considered bad practice is having so much of your code in the Main() method of the program. Generally we want the main method to be as small and simple as possible. To accomplish this I would suggest adding 2 classes. AddressBook (to manage all interactions with the list of AddressBookEntries) and UserInterface (to handle all the calls to Console.ReadLine/WriteLine).

Addressbook:

  • List<AddressBookEntry> (this shouldn't be static in this context since anytime you new up an Addressbook you would want a new list as well)
  • Search() and Add() methods should be here

UserInterface:

  • Greeting() prints greeting and options
  • ProcessUserInput() the case switch to call other functions
  • possibly have the Addressbook you are working on reside here as a property

Bonus Points if you can override the ToString method on AddressBookEntry to keep from having to repeat this line:

Console.WriteLine("Name: {0},  Phone number: {1},  Email: {2}", item.FirstName + " " + item.LastName, item.PhoneNumber, item.EmailAddress);

These are just my initial thoughts with giving the code a once over. Not sure how far along you are or even what course so some of this you may not have gone over yet. If you have any questions feel free to ask :)

Hope this helps, Happy Coding!!