From the Begining – Object Oriented Design

Hey all.  Life has gotten really crazy as I’ve increased my workout schedule and been plagued with DLC for Fallout 4 so my blogging is suffering, but I’m not going to just let it end as I have more to type…and now I may even have an interested audience…someone who’s specifically asking me advice about how to get into coding.

He specifically said he just wanted to learn C#…which is something I used to say as well way back when…however what I’ve learned since then that C# more or less the adhesive that bind things together.  It’s really .NET and what it can do that is the real meat of C#.  And once you realize that…you find that you can use a whole bunch of different languages to do the same base .NET things and that it’s not only just C#.

What I also learned is that C# is a comfort zone…but one a person seriously wanting to learn how to code needs to break out of.  Sure if you become a C# wiz, you’ll be able to slap together some cool slick object models…make some nifty libraries out of them…etc…however if you want to display anything to a user…these days you pretty much will have to know at least some minimal level of HTML / XAML.  Then if you go the HTML route you’ll probably also need a little javascript / jquery.  You can’t paint a modern UI picture with just C# anymore…WinForms are dying and WPF / MVC is here to stay…which is a good thing.  Then if you want to store that data…sure you can write a file out to the local storage…but someone serious will want to work with a database so there will inevitably be some SQL showing up in your future…anyway, a long paragraph short, don’t be too locked down to C#…it’s a good jumping off point, but eventually you’ll need to branch out.  It’s just how it is so get it in your head now.

So first let put this out there right off…I learned everything I know about C# and .NET from reading books and the internet.  One of the first books I read that was extremely useful was called “Beginning C# Game Programming.  Even though it’s very game centric…it does an amazing job at explaining key elements of C# / .NET and how they apply to object design in a very easy to understand way.  I often look back to the chapter on polymorphism to this day.

So trying to not just plaguarize the hell out of that book…I’ll just be giving pointers as a brain dump sort of style in these “From the Begining” blog posts.

The big strength of C# is its ability to define an object.  Everything in the universe boils down to being something that’s definable…C# gives you the structure to put that definition into something tangable.  Very basically and simply put…these tools for structure are constructors, properties, and methods.

One of the tricks I learned early on was to take the object you’re trying to define and write a sentence out about what you’re trying to do with it.

I’m trying to count all the stars in the solar system that have planets.

Nouns are your object names and properties, verbs are your methods.

So we have “Solar System” which would be the object name.  Then we have “Stars” which would be a property hanging off Solar System…which in turn has “Planets” which hangs as a property for a Star.  Already we’re starting to see some structure in our definition.  The tricky part is that we’re trying to count the stars that have planets…which means that the Stars object will need a “Count” method.  It just so happens when you use a generic collection from .NET you get a Count for free (it’s a little misleading in this case because it’s a property and not a method…Count should really be a method in my way of thinking but that’s a discussion for another time).  So here’s what the code would look like from the short little sentence above.

Planet Object:

public class Planet
{
    public string Name { get; set; }
}

Star Object:

public class Star
{
    public List<Planet> Planets { get; set; }
}

SolarSystem Object:

public class SolarSystem
{
    public List<Star> Stars { get; set; }
}

Really that’s all you “have” to do to get the info you need to satisfy the definition sentence.  You spin through the collection of “Stars” and check the Count property of each Star object’s “Planets” property.  It would look something like this…

SolarSystem milkyWay = new SolarSystem();
int starsWithPlanetsCount = 0;

foreach (Star star in milkyWay.Stars)
{
    if(star.Planets.Count > 0)
    {
        starsWithPlanetsCount++;
    }
}

…it works but lets add a couple other properties to make things a little easier…so now our SolarSystem object code looks like the following…

public class SolarSystem
{
    public List<Star> Stars { get; set; }

    public List<Star> StarsWithPlanets
    {
        get
        {
            List<Star> returnList = new List<Star>();

            foreach(Star star in Stars)
            {
                if(star.Planets.Count > 0)
                {
                    returnList.Add(star);
                }
            }

            return returnList;
        }
    }

    public List<Star> StarsWithoutPlanets
    {
        get
        {
            List<Star> returnList = new List<Star>();

            foreach (Star star in Stars)
            {
                if (star.Planets.Count <= 0)
                {
                    returnList.Add(star);
                }
            }

            return returnList;
        }
    }
}

…and the calling code would look something like this…

SolarSystem milkyWay = new SolarSystem();
int starsWithPlanetsCount = milkyWay.StarsWithPlanets.Count;

So much easier and clearner. Personally I always like to keep the logic and work being done on a specific object within a method or property in that object instead of having it in calling code.  It’s just a way to logically group code and make it more organized…but it’s more a personal preference thing I think.

Anyway, that’s my first lesson about C#. Use it for what it’s good for…which is defining objects. Hope this helps someone. Enjoy! 🙂

Advertisements
From the Begining – Object Oriented Design

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s