Practice: The Need for Methods
Plan a program that prints college football scores in pseudo-code, extend it by copying and pasting its code, and see the problems copying causes.
Novice · C# · Updated
After this lesson you can
- Plan a program in pseudo-code, the steps in plain words and in order, then turn each step into C#
Key takeaways
- Pseudo-code is a plan written in plain words, the program's steps in order, before any C#. Nobody runs it, so it follows no C# rules.
- Each pseudo-code step becomes one or a few lines of C#, and the steps can stay in the code as comments.
- Copying a block for every team works, but every copy is another place for a mistake to hide.
- When the same plan repeats for each team with only the values changing, a method would fit.
This program prints the points scored in each quarter, and the final score, of the top three college football teams in last week's games. (The scores are made up; change them if you like.)
| Rank | Team | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|---|
| 1 | Georgia | 12 | 14 | 6 | 7 |
| 2 | Alabama | 14 | 17 | 10 | 14 |
| 3 | Ohio State | 21 | 10 | 14 | 7 |
| 4 | Michigan | 10 | 7 | 0 | 17 |
| 5 | Clemson | 14 | 6 | 8 | 10 |
Try it
In Git Bash, make a folder for this chapter and open a new file:
mkdir -p ~/bootcamp/methods
cd ~/bootcamp/methods
code scores.cs
Type in this program, save it, and run it with dotnet run scores.cs:
string teamName;
int rank, q1, q2, q3, q4, finalScore;
// Georgia
teamName = "Georgia";
rank = 1;
q1 = 12;
q2 = 14;
q3 = 6;
q4 = 7;
finalScore = q1 + q2 + q3 + q4;
Console.WriteLine("(" + rank + ") " + teamName);
Console.WriteLine("Q1: " + q1 + " Q2: " + q2 + " Q3: " + q3 + " Q4: " + q4);
Console.WriteLine("Final: " + finalScore);
Console.WriteLine();
// Alabama
teamName = "Alabama";
rank = 2;
q1 = 14;
q2 = 17;
q3 = 10;
q4 = 14;
finalScore = q1 + q2 + q3 + q4;
Console.WriteLine("(" + rank + ") " + teamName);
Console.WriteLine("Q1: " + q1 + " Q2: " + q2 + " Q3: " + q3 + " Q4: " + q4);
Console.WriteLine("Final: " + finalScore);
Console.WriteLine();
// Ohio State
teamName = "Ohio State";
rank = 3;
q1 = 21;
q2 = 10;
q3 = 14;
q4 = 7;
finalScore = q1 + q2 + q3 + q4;
Console.WriteLine("(" + rank + ") " + teamName);
Console.WriteLine("Q1: " + q1 + " Q2: " + q2 + " Q3: " + q3 + " Q4: " + q4);
Console.WriteLine("Final: " + finalScore);
Console.WriteLine();
(1) Georgia
Q1: 12 Q2: 14 Q3: 6 Q4: 7
Final: 39
(2) Alabama
Q1: 14 Q2: 17 Q3: 10 Q4: 14
Final: 55
(3) Ohio State
Q1: 21 Q2: 10 Q3: 14 Q4: 7
Final: 52
int rank, q1, q2, q3, q4, finalScore; declares six int variables in one line. Each team's block then gives them new values.
Plan it first: pseudo-code
Before you add two more teams, look at what one team's block does. Someone wrote this program from a plan, even if the plan only lived in their head. Writing the plan down first is how professionals start anything bigger than a few lines, the way a cook reads the recipe card before turning on the stove.
That plan is called pseudo-code ("SOO-doh code"; pseudo means pretend). It's the program's steps in plain words, in the order they happen. Nobody runs it and the compiler never sees it, so there are no C# rules: no semicolons, no types, no exact spelling. Some textbooks write it as one word, pseudocode. Here is the plan for one team's block:
For one team:
1. Set the team's name and rank.
2. Set its four quarter scores.
3. Add the four quarters to get the final score.
4. Print the rank and the name.
5. Print the four quarter scores.
6. Print the final score, then an empty line.
Each step then becomes one or a few lines of C#:
| Step | The C# for Georgia |
|---|---|
| 1. Set the team's name and rank. | teamName = "Georgia"; and rank = 1; |
| 2. Set its four quarter scores. | q1 = 12;, q2 = 14;, q3 = 6; and q4 = 7; |
| 3. Add the four quarters to get the final score. | finalScore = q1 + q2 + q3 + q4; |
| 4. Print the rank and the name. | Console.WriteLine("(" + rank + ") " + teamName); |
| 5. Print the four quarter scores. | Console.WriteLine("Q1: " + q1 + " Q2: " + q2 + " Q3: " + q3 + " Q4: " + q4); |
| 6. Print the final score, then an empty line. | Console.WriteLine("Final: " + finalScore); and Console.WriteLine(); |
A good habit: type the plan into your file as comments first, one // line per step, then write the code under each one. The comments stay behind and tell the next reader what each part is for. Here is Georgia's block written that way, as a program of its own:
// 1. Set the team's name and rank.
string teamName = "Georgia";
int rank = 1;
// 2. Set its four quarter scores.
int q1 = 12;
int q2 = 14;
int q3 = 6;
int q4 = 7;
// 3. Add the four quarters to get the final score.
int finalScore = q1 + q2 + q3 + q4;
// 4. Print the rank and the name.
Console.WriteLine("(" + rank + ") " + teamName);
// 5. Print the four quarter scores.
Console.WriteLine("Q1: " + q1 + " Q2: " + q2 + " Q3: " + q3 + " Q4: " + q4);
// 6. Print the final score, then an empty line.
Console.WriteLine("Final: " + finalScore);
Console.WriteLine();
(1) Georgia
Q1: 12 Q2: 14 Q3: 6 Q4: 7
Final: 39
If a step is hard to turn into code, the plan isn't detailed enough yet: split that step into smaller ones. If a step is missing, you'll see it in the plan before you've spent time on the code.
Your task
Add Michigan and Clemson to the program by copying a team's block and changing its values. Don't try to write a method yet: do it the copy-and-paste way.
Run it again and check that all five teams are printed. Michigan's final score is 34, and Clemson's is 38.
Think about it
Before you read on, look at what you've built and answer these for yourself:
- How many times does the program now contain the four lines that print a team?
- Suppose the scoreboard should print
Final score:instead ofFinal:. How many lines would you change? What if you missed one? - What if you copied a block and forgot to change one of the quarter scores?
- Look at your plan: is it different for Michigan than for Georgia, or only the values?
Every copy is another place for a mistake to hide. And the plan gives away the fix: its six steps are the same for all five teams, and only the values change. Steps that repeat with different values are the sign that a method would fit. The next lesson shows how a method removes the copies.
Do it: turn the plan into code for one team
A sixth team, Texas, ranked 6th, gets a scoreboard of its own. The plan is already in the box as comments, one for each step, but no code is under them yet.
Texas's scores:
| Rank | Team | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|---|
| 6 | Texas | 7 | 10 | 14 | 3 |
The final score is 7 + 10 + 14 + 3 = 34, so the program prints exactly these three lines:
(6) Texas
Q1: 7 Q2: 10 Q3: 14 Q4: 3
Final: 34
Your job: under each comment, write the C# for that step.
- Step 3 stores the final score in an
intvariable namedfinalScore, worked out from the four quarter scores. - Print the lines exactly as shown, with two spaces between the quarters, the way the program above prints them.
- This plan has no empty line at the end: print only the three lines.
Practiced live on the lesson page: write the C# right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
A plan in plain words comes before the code: it catches a missing step early, turns into code one step at a time, and here it already shows that the same steps for every team belong in a method.