How to design your first programs

Now that you’ve learned some basics about programs, let’s look more closely at how to design a program. When you sit down to write a program, generally you have some sort of problem that you’d like to solve, or situation that you’d like to simulate. New programmers often have trouble figuring out how to convert that idea into actual code. But it turns out, you have many of the problem solving skills you need already, acquired from every day life.

The most important thing to remember (and hardest thing to do) is to design your program before you start coding. In many regards, programming is like architecture. What would happen if you tried to build a house without following an architectural plan? Odds are, unless you were very talented, you’d end up with a house that had a lot of problems: leaky roofs, walls that weren’t straight, etc… Similarly, if you try to program before you have a good gameplan moving forward, you’ll likely find that your code has a lot of problems, and you’ll have to spend a lot of time fixing problems that could have been avoided altogether with a little design.

A little up-front planning will save you both time and frustration in the long run.

Step 1: Define the problem

The first thing you need to figure out is what problem your program is attempting to solve. Ideally, you should be able to state this in a sentence or two. For example:

  • I want to write a phone book application to help me keep track of my friend’s phone numbers.
  • I want to write a random dungeon generator that will produce interesting looking caverns.
  • I want to write a program that will take information about stocks and attempt to predict which ones I should buy.

Although this step seems obvious, it’s also highly important. The worst thing you can do is write a program that doesn’t actually do what you (or your boss) wanted!

Step 2: Define your targets

When you are an experienced programmer, there are many other steps that typically would take place at this point, including:

  • Understanding who your target user is
  • Defining what target architecture and/or OS your program will run on
  • Determining what set of tools you will be using
  • Determining whether you will write your program alone or as part of a team
  • Collecting requirements (a documented list of what the program should do)

However, as a new programmer, the answers to these questions are typically simple: You are writing a program for your own use, alone, on your own system, using an IDE you purchased or downloaded. This makes things easy, so we won’t spend any time on this step.

Step 3: Make a heirarchy of tasks

In real life, we often need to perform tasks that are very complex. Trying to figure out how to do these tasks can be very challenging. In such cases, we often make use of the top down method of problem solving. That is, instead of solving a single complex task, we break that task into multiple subtasks, each of which is individually easier to solve. If those subtasks are still too difficult to solve, they can be broken down further. By continuously splitting complex tasks into simpler ones, you can eventually get to a point where each individual task is manageable, if not trivial.

Let’s take a look at an example of this. Let’s say we want to write a report on carrots. Our task hierarchy currently looks like this:

  • Write report on carrots

Writing a report on carrots is a pretty big task to do in one sitting, so let’s break it into subtasks:

  • Write report on carrots
    • Do research on carrots
    • Write outline
    • Fill in outline with details about carrots

That’s a more managable, as we now have three tasks that we can focus on individually. However, in this case, “Do research on carrots is somewhat vague”, so we can break it down further:

  • Write report on carrots
    • Do research on carrots
      • Go to library and get book on carrots
      • Look for information about carrots on internet
    • Write outline
      • Information about growing
      • Information about processing
      • Information about nutrition
    • Fill in outline with details about carrots

Now we have a hierarchy of tasks, none of them particularly hard. By completing each of these relatively manageable sub-items, we can complete the more difficult overall task of writing a report on carrots.

The other way to create a hierarchy of tasks is to do so from the bottom up. In this method, we’ll start from a list of easy tasks, and construct the hierarchy by grouping them.

As an example, many people have to go to work or school on weekdays, so let’s say we want to solve the problem of “get from bed to work”. If you were asked what tasks you did in the morning to get from bed to work, you might come up with the following list:

  • Pick out clothes
  • Get dressed
  • Eat breakfast
  • Drive to work
  • Brush your teeth
  • Get out of bed
  • Prepare breakfast
  • Get in your car
  • Take a shower

Using the bottom up method, we can organize these into a hierarchy of items by looking for ways to group items with similarities together:

  • Get from bed to work
    • Bedroom things
      • Get out of bed
      • Pick out clothes
    • Bathroom things
      • Take a shower
      • Brush your teeth
    • Breakfast things
      • Prepare breakfast
      • Eat breakfast
    • Transportation things
      • Get in your car
      • Drive to work

As it turns out, these task hierarchies are extremely useful in programming, because once you have a task hierarchy, you have essentially defined the structure of your overall program. The top level task (in this case, “Write a report on carrots” or “Get from bed to work”) becomes main() (because it is the main item you are trying to solve). The subitems become functions in the program.

If it turns out that one of the items (functions) is too difficult to implement, simply split that item into multiple subitems, and have that function call multiple subfunctions that implement those new tasks. Eventually you should reach a point where each function in your program is trivial to implement.

Step 4: Figure out the sequence of events

Now that your program has a structure, it’s time to determine how to link all the tasks together. The first step is to determine the sequence of events that will be performed. For example, when you get up in the morning, what order do you do the above tasks? It might look like this:

  • Get out of bed
  • Pick out clothes
  • Take a shower
  • Get dressed
  • Prepare breakfast
  • Eat breakfast
  • Brush your teeth
  • Get in your car
  • Drive to work

If we were writing a calculator, we might do things in this order:

  • Get first number from user
  • Get mathematical operation from user
  • Get second number from user
  • Calculate result
  • Print result

This list essentially defines what will go into your main() function:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
     GetOutOfBed();
     PickOutClothes();
     TakeAShower();
     GetDressed();
     PrepareBreakfast();
     EatBreakfast();
     BrushTeeth();
     GetInCar();
     DriveToWork();
}

Or in the case of the calculator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
     // Get first number from user
     GetUserInput();
  
     // Get mathematical operation from user
     GetMathematicalOperation();
  
     // Get second number from user
     GetUserInput();
  
     // Calculate result
     CalculateResult();
  
     // Print result
     PrintResult();
}

Step 5: Figure out the data inputs and outputs for each task

Once you have a hierarchy and a sequence of events, the next thing to do is figure out what input data each task needs to operate, and what data it produces (if any). If you already have the input data from a previous step, that input data will become a parameter. If you are calculating output for use by some other function, that output will generally become a return value.

When we are done, we should have prototypes for each function. In case you’ve forgotten, a function prototype is a declaration of a function that includes the function’s name, parameters, and return type, but does not implement the function.

Let’s do a couple examples. GetUserInput() is pretty straightforward. We’re going to get a number from the user and return it back to the caller. Thus, the function prototype would look like this:

1
int GetUserInput()

In the calculator example, the CalculateResult() function will need to take 3 pieces of input: Two numbers and a mathematical operator. We should already have all three of these by the time we get to the point where this function is called, so these three pieces of data will be function parameters. The CalculateResult() function will calculate the result value, but it does not display the result itself. Consequently, we need to return that result as a return value so that other functions can use it.

Given that, we could write the function prototype like this:

1
int CalculateResult( int nInput1, char chOperator, int nInput2);

Step 6: Write the task details

In this step, for each task, you will write it’s actual implementation. If you have broken the tasks down into small enough pieces, each task should be fairly simple and straightforward. If a given task still seems overly-complex, perhaps it needs to be broken down into subtasks that can be more easily implemented.

For example:

1
2
3
4
5
6
7
8
9
10
11
char GetMathematicalOperation()
{
     cout << "Please enter an operator (+,-,*,or /): " ;
  
     char chOperation;
     cin >> chOperation;
  
     // What if the user enters an invalid character?
     // We'll ignore this possibility for now
     return chOperation;
}

Step 7: Connect the data inputs and outputs

Finally, the last step is to connect up the inputs and outputs of each task in whatever way is appropriate. For example, you might send the output of CalculateResult() into an input of PrintResult(), so it can print the calculated answer. This will often involve the use of intermediary variables to temporary store the result so it can be passed between functions. For example:

1
2
3
4
// nResult is a temporary value used to transfer the output of CalculateResult()
// into an input of PrintResult()
int nResult = CalculateResult(nInput1, chOperator, nInput2);
PrintResult(nResult);

This tends to be much more readable than the alternative condensed version that doesn’t use a temporary variable:

1
PrintResult( CalculateResult(nInput1, chOperator, nInput2) );

This is often the hardest step for new programmers to get the hang of.

Note: To fully complete this example, you’ll need to utilize if statements, which we won’t cover for a while, but you’re welcome to take a sneak-peak at now.

A fully completed version of the above calculator sample follows (hidden in case you want to take a stab at it yourself first):

Show Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
  
int GetUserInput()
{
     cout << "Please enter an integer: " ;
     int nValue;
     cin >> nValue;
     return nValue;
}
  
char GetMathematicalOperation()
{
     cout << "Please enter an operator (+,-,*,or /): " ;
  
     char chOperation;
     cin >> chOperation;
     // What if the user enters an invalid character?
     // We'll ignore this possibility for now
     return chOperation;
}
  
int CalculateResult( int nX, char chOperation, int nY)
{
     if (chOperation== '+' )
         return nX + nY;
     if (chOperation== '-' )
         return nX - nY;
     if (chOperation== '*' )
         return nX * nY;
     if (chOperation== '/' )
         return nX / nY;
  
     return 0;
}
  
void PrintResult( int nResult)
{
     cout << "Your result is: " << nResult << endl;
}
  
int main()
{
     // Get first number from user
     int nInput1 = GetUserInput();
  
     // Get mathematical operation from user
     char chOperator = GetMathematicalOperation();
  
     // Get second number from user
     int nInput2 = GetUserInput();
  
     // Calculate result
     int nResult = CalculateResult(nInput1, chOperator, nInput2);
  
     // Print result
     PrintResult(nResult);
}

Words of advice when writing programs

Keep your program simple to start. Often new programmers have a grand vision for all the things they want their program to do. “I want to write a role-playing game with graphics and sound and random monsters and dungeons, with a town you can visit to sell the items that you find in the dungeon” If you try to write something too complex to start, you will become overwhelmed and discouraged at your lack of progress. Instead, make your first goal as simple as possible, something that is definitely within your reach. For example, “I want to be able to display a 2d representation of the world on the screen”.

Add features over time. Once you have your simple program working and working well, then you can add features to it. For example, once you can display your 2d world, add a character who can walk around. Once you can walk around, add walls that can impede your progress. Once you have walls, build a simple town out of them. Once you have a town, add merchants. By adding each feature incrementally your program will get progressively more complex without overwhelming you in the process.

Focus on one area at a time. Don’t try to code everything at once, and don’t divide your attention across multiple tasks. Focus on one task at a time, and see it through to completion as much as is possible. It is much better to have one fully working task and five that haven’t been started yet than six partially-working tasks. If you split your attention, you are more likely to make mistakes and forget important details.

Test each piece of code as you go. New programmers will often write the entire program in one pass. Then when they compile it for the first time, the compiler reports hundreds of errors. This can not only be intimidating, if your code doesn’t work, it may be hard to figure out why. Instead, write a piece of code, and then compile and test it immediately. If it doesn’t work, you’ll know exactly where the problem is, and it will be easy to fix. Once you are sure that the code works, move to the next piece and repeat. It may take longer to finish writing your code, but when you are done the whole thing should work, and you won’t have to spend twice as long trying to figure out why it doesn’t.

Most new programmers will shortcut many of these steps and suggestions (because it seems like a lot of work and/or it’s not as much fun as writing the code). However, for any non-trivial project, following these steps will definitely save you a lot of time in the long run. A little planning up front saves a lot of debugging at the end.

The good news is that once you become comfortable with all of these concepts, they will start coming naturally to you without even thinking about it. Eventually you will get to the point where you can write entire functions without any pre-planning at all.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值