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. 














    Amazon has a significantly larger inventory to track than the convenience store as well as Amazon must have significantly more money to invest in computer infrastructure and maintenance. Both things impact which algorithms and processes would be a better choice to use. The larger a database is, the longer it takes to search the database, however, that may also take a more experienced or time-consuming program to maintain. On a smaller database, like a convenience store inventory list, a less efficient and less time-consuming algorithm may be sufficient, but Amazon has such a large inventory that a much more efficient algorithm and process are necessary to be effective.

     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.

     Armed with this knowledge, go forth and code your lists till you have filled your heart's desires!

Comments