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#

Daniel Hildreth
Daniel Hildreth
16,170 Points

How do I know what to put into a class/object and what methods/properties it should have?

So in the C# Basics and C# Objects courses, you make a game called TreehouseDefense. Jeremy seemed to know right away what classes to right, how to write them, what methods and properties each should have, and etc. Obviously he has been coding a lot longer than me. However, is there a way to conceptualize and write out code like that? I mean I'll look at a class, and know what needs to be done and whatnot. However, I'll get stumped on how to go about writing it and such. Is there a tutorial or something on this, or is it something that comes in time? I've never programmed in C# before till I started doing the C# courses on Treehouse, so it's all brand new to me. Thanks in advance!

3 Answers

Well, you have to think at the purpose of that class.I don't know C# but OOP fundamentals are the same for all programming languages.You should follow more about C# and OOP to get an idea of how to structure your classes.I've been there, where you are, with time you will know, just keep coding.

Remember, think at the purpose of that class and you'll get some ideas, try to model things from real life.As an example, how you would model a phonebook into a class, something pretty simple.What properties do you need, what methods do you need? You know that a phonebook stores people's names and phone numbers alphabetically and you also know that if you search for a letter, you will get all the names/phone numbers for that letter.Try to make something pretty simple.

Remember, properties are for storing data and all this data represents the state of that object.The methods represent the behavior of that object, they act on properties.

Daniel Hildreth
Daniel Hildreth
16,170 Points

Pirvan, could you please provide a property, object, and a method for the phone book class you mentioned earlier. Would it be something like the people and phone number's are an object with each individual person and phone number a property of that object respectively? Would the method of that be the search query to find a particular person/phone number?

As Pirvan mentioned, most OOP languages follow a similar construct in that, the purpose of the class should be thought of and what the intended goal is. Jeremy knew exactly what classes were needed, aside from a well thought course, because he conceptualized what the TreehouseDefense game should accomplish. In most development, it is best to look at the project you are faced with and begin to segment it out into manageable parts that are aimed at one specific goal.

This isn't always easy but does become easier to infer in time and practice. Also, be aware that there is no such thing really as perfect code. Most all code can be refactored over time as better practices are learned. More often than not we incrementally build functionality as we see fit and were it makes sense.

In the example of the TreehouseDefense game, the immediate questions to answer are, what does it do or what do I want it to do? Well, I want to place towers that attack enemies, who move from one side of a map to the other. All bolded items are concrete items (nouns) in the game, which often should be created as some sort of object. While all underlined items of the sentence are actions (verbs), which most often align themselves with methods. Placing a tower however doesn't seem like something the object itself would do though right? So it may be best as a property, since it is something the player will do, initialized in the constructor.

Hope this helps and that it doesn't just sound like rambling nonsense.

Something more I forgot to say, think at what that class/object it represents too, in my example, it represents a phonebook.You can model anything from real life, it's up to you.

Well, you need just one property, the one who stores everything, like an array.Then you need a method to get all the names and their related phone numbers for a particular letter, that's all, something pretty simple.

You see, properties are for storing data and methods are for interacting with properties, with the stored data.

All the data the object stores at a moment of time it represents the state of that object.