Structuring your First Program
The first, and sometimes hardest, part of writing a program is figuring out where to start. Here is the best piece of advice given to me on my journey to figuring out how to program: “Outline the steps you need to take in comments before you start.” Now, these steps don’t need to be incredibly detailed on how each step should operate, you just need an outline. Let’s take a real-world example of what I mean.
Example:
Making a bowl of cereal
Steps to
create a bowl of cereal in comments:
// get bowl
// get spoon
// get cereal
// get milk
// add cereal to bowl
// put cereal away
// add milk to bowl
// put milk away
// add spoon to bowl
Now,
some of these steps can be in a different order and still work. For example, do
you put the milk or the cereal in the bowl first? In this case, it doesn’t
really matter, but starting out with a plan helps you know what you need to
create and gives you a basic structure for your program. The bowl, spoon,
cereal, and milk are all objects you need to create in the program and the
actions of adding the cereal, milk, and spoon and putting the cereal and milk
away become actions you need those objects to make. Knowing this, you can
choose a step and start making decisions. What kind of object does the bowl
need to be? What about the cereal? How do you choose what cereal to get? Are
you going to the store? Do you have it in the cupboard? Are there options you
need to decide between?
Deciding what to use can be daunting. Some algorithms and structure types are generally better to use, however, it depends on the problem being solved and the resources available to solve it. For example, both Amazon Inc and the local corner convenience store need to track and be able to search their inventory to maintain it.
One of the
main ways to track the efficiency of an algorithm is called Big O notation. This type of notation is a mathematical
formula for showing how efficient an algorithm is and determining how long it
would take for the algorithm to run a specific data set size. This allows for
the comparison of the efficiency of two or more different algorithms without
having to run comparison tests.
Comments
Post a Comment