Subscribe to tlhIn`toq's Blog        RSS Feed
***** 1 Votes

Planning your project - A movie database program for example

Icon Leave Comment
This post is really an excerpt from a longer conversation. But I thought this might be a good place to keep a copy.
To view the entire conversation check out this thread.

View PostPremier2k, on 19 February 2015 - 02:22 AM, said:

Nope, I have no idea is the simple answer to that. I don't have a plan for what I'm building I'm just trying to learn how to build it right. Maybe I'm going about it the wrong way but how do I learn to build something correctly?

Maybe I do need to understand what I'm building and design it correctly, but I don't know how to design applications using diagrams showing classes, members and interactions. Is that something Visual Studio can help me with or is that done on paper?

Maybe if I knew that then I could do this better. The issue I have is, where do I learn this stuff? What can I type in to a search engine to bring back something that will help me.

There is a saying that is very applicable here, "I don't know what I don't know."

Start with the idea that coding is as much art and style as science. That makes you, the developer, part scientist and part artist. So you have a lot of freedom to do things the way that work for you. Don't worry so much about using the *right* technique or the *right* planning software. Some people use dedicated UML diagram programs. Some use Excel. Some use Visio. Some use bar napkins and markers. Me... I like whiteboards.

Your diagrams don't have to be perfect. They don't even have to be diagrams. They don't have to be of a single style. But what your planning does need to be is... Existent. You have to actually plan before you start typing. It doesn't matter if its a UML diagram or a pages in a 3-ring binder. We're going to do some basic planning right here in a web post (text document of sorts), so clearly it doesn't matter what you use as long as it works for you.

So lets plan this together. Some of this may sound like my classes tutorial. Oh well.
Objects in code should mimic objects in the world. If you are going to build a movie tracking program like this then what are the objects?

Movies... People(actors, directors, etc.)... Genre (horror, sci-fi, mystery...)
These are all objects... Not just strings for their names. They are objects. If the only property they have is a name then so be it. But C# is an object oriented language. If you make classes for each thing you have the ability to upgrade the thing later to have more properties. If you just use a string as the actual movie ("Star Wars") then how much upgradeablity do you leave yourself?

So what are the properties of a movie?

class Movie
string: Title
DateTime: ReleaseDate
TimeSpan: Duration
Language: Primary language it was filmed in
Collection: Locations (where was it filmed)
Collection: Actors
Collection: Writers
Collection: Directors
Collection: Producers
Collection: Production company
Collection: Genre (yes, a collection.  A movie can be a comedy and a drama, or action and sci-fi)

Hmmm.... Actors, writers, directors... They're all people, right?
So I guess we could make a Person class. Then inherit from that for each type.

class Person
string: LastName
string: FirstName
string: MiddleName
DateTime: Date of birth
location: Place of birth
Collection: Milestones (events in the person's timeline/life/history)
Collection: Movies
class Actor : Person
class Director : Person
class Writer : Person

The thing to remember here is that you don't have to plan to the Nth degree at this time. You've stubbed out some objects. If it suddenly dawns on you that you want to add something to a person you can do that 6 months from now.

class Person
string: LastName
string: FirstName
string: MiddleName
DateTime: Date of birth
location: Place of birth
Collection: Milestones (events in the person's timeline/life/history)
Collection: Movies
Collection: People (famous relatives)
Collection: TriviaFact

Don't over think. Don't panic about doing your planning wrong. You're going to plan wrong, just accept it. I do it after a decade and so does everyone else. You'll learn from every mistake and that will help you not repeat the mistake the next time.

Just try to visualize what kinds of things you're working with and what they really are.

Ok... Next comes a new concept most likely: Database
This might get a little thick if you've never dealt with databases. The goal here is not to teach you all about DBs, but just to introduce them enough so you can understand why they are a good thing and how they can make your life easy if you plan them out well.
Just because a movie object has a property of "title" and a list of actors does not mean you are going to store all that data in literal form in every movie.

We are not going to develop your entire database right now. Just recognize that in the real world of development you store data in a database. Databases are tables of data where horizontal rows are records and columns are the fields.

Person table
ID   |NameFirst   |  NameLast | DOB       | PlaceOfBirth       |
001  |Clint       | tlhIntoq  | 05may2330 | Klingon Home World |
002  |Worf        | Mogh      | 01jul2315 | Mining moon        |

Notice that this table does not track if the person is an actor or a director. Well why not? Because its hard to say if this person is just an actor or a brilliant actor, director, writer, producer, music score writer all in one.

Job table
ID   |  Name
001  |  Actor
002  |  Director

PersonToJob table
PersonID | JobID
123 | 001
123 | 002
15 | 043

We have just related actor 001 to being both an actor and a director. Person 15 is only related to being a set carpenter. If 5 years from now that carpenter also becomes a stunt man you just add one row in the PersonToJob table.

You don't want to store all the data of every movie inside every actor, do you? What happens when you have to fix a mistake in the movie? You have to go back and update every actor and crewmember. Not to mention that is a LOT of duplicate data to store and keep syncronized. So what do we do about it? We don't store the entire actor record inside the movie record. We just store a REFERENCE to the actor

Movie table
ID   |  Title
001  | Stripes
002  | Night of the living dead
003  | A few good women

Genre table
ID   |  Name
001  |  Sci-fi
002  |  Fantasy
003  |  Action
004  |  Mystery

MovieToActor table
MovieID    | Actor ID
001        | 125
001        | 003
014        | 016
//You add an actor to the movie by adding a row
001        | 119

MovieToGenre table
MovieID    | Genre ID
001        | 003
001        | 015
//See how you can add a genre to the movie by just adding one row in this table?

So what does this look like when you load a Movie object?
MovieID: 001
ActorCollection: 125, 003
and so on

When you update movie 003 to correct the spelling from "Ex-men" to "X-Men" you don't have to update any of the related records. They don't store the string, they just store the movie ID which isn't changing.

When it comes time to display this record you will pull from the database just the information you need for a particular screen/display/window. If you're just looking at a summary of the movie you may not need to pull all the crew names and trivia records.

The concept to recognize the most in all the DB stuff is the same separation of responsibilities that we talked about with your code. The movie object does not try to store all the details of all the actors. The actor object contains its data but does not references back to the movies because it doesn't need to; The MovieToActor table makes that relationship.

What this really means is that your program is not storing any data until it needs it. There are NO movie objects and NO actor objects until you load some based on a query. That means a PC with only 1gig of free ram can still cope with 2 million movies and 10 million actors if it needs to by just loading the data it needs for one screen at a time (called 'paging'). If you just need a page of movie titles you don't load all the other data.

When you query the database for all the action movies, what happens?
  • First we get the ID of the action genre.
  • Then we get a list of all the movie ID's from the MovieToGenre table.
  • Then we loop through that list to get the details for each matching table: Movie table, MovieToActor, MovieToTrivia
  • Once we have a list of all the actor IDs we can get the details we need for this screen: Actor name

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

May 2022

222324252627 28

Search My Blog

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)