How and why to practice programming

Borrow code and make it your own

Borrow a short program from your professor, a book, or the Internet.

Make a small change to that program. Test the program. Confirm that your change produced the result that you expected or, through this experiment, learn more about what kind of change a modification to that part of the program will produce.

Repeat this process. Make more small changes. After each small change, run the program to catch errors you might have introduced into the program and to increase your understanding of the part of the program you have modified.

After many small changes the program will be yours. Be sure to include in your program a comment that acknowledges your original source. Let readers of your code know that you have created your software by adapting someone else’s software.

This is my rifle

“Every Marine is, first and foremost, a rifleman. All other conditions are secondary.”

A Marine must know how to use a rifle. Students of computer science must learn how to use computers and software to solve problems. Let’s find a few small programs that will be for us what the rifle is to the Marine.

You might have seen it in movies: Recruits sit in a barracks inspecting their rifles, cleaning their rifles, disassembling their rifles, and putting their rifles back together.

Inspect your program. Pick it apart. Rebuild it. Know the purpose of every part. Learn how each part fits together.

What kinds of programs?

The College Board’s Computer Science A course asks students to learn five algorithms:

  • sequential search
  • binary search
  • selection sort
  • insertion sort
  • merge sort

Choose a short program that just executes one of these algorithms and does nothing more. If you are more ambitious, you could choose a program that measures an algorithm’s performance. You might prefer a simpler program that executes just part of an algorithm. For example, the merge sort repeatedly merges two sorted lists. If you wish, write a program that merges just two sorted lists and leaves out the part of the merge sort algorithm that constructs successively larger sorted lists and repeats the merging step for each new pair of sorted lists.

Have you begun a study of object-oriented design? Then you might choose to write a program that models a number with two parts. Your program will define a class that contains a constructor, instance variables, and methods to add and print these special kinds of numbers.

What is a number with two parts? A fraction has a numerator and denominator. A length (measured as we do in the United States) has feet and inches. A weight has pounds and ounces.

There is a different rule for addition with each of these kinds of numbers. Note that if you have 1 pound, 10 ounces of grapes and 2 pounds, 14 ounces of cherries, the scale does not say that you have 3 pounds, 24 ounces of fruit.

What kinds of changes?

Here are some ideas:

Add comments. Edit comments. Elaborate comments. By writing what you think each part of the program is doing, you will increase your understanding of the program. Attach more comments to the code in your practice exercises than you would to software that goes to a client. In this case, your goal is not to help a client or teammate understand the code. Your goal is to explore the code and improve your own understanding. Comments in practice serve a different purpose than do comments in work-for-hire.

Change the names of variables and functions. Choose new names that more clearly indicate the purpose of the variable or function.

Add statements that print intermediate results. Make the program tell you what it is doing at each step.

Add statements that confirm that inputs and values that the program produces during its execution lie within expected ranges.

Divide a lengthy or complicated expression into smaller, easier to comprehend expressions. Alternatively, you might combine two short expressions, statements, or functions in a single longer one. You want to write code that other readers can understand easily. Sometimes you can help readers by writing more verbosely and sometimes by writing concisely. Find your own balance.

Test the program with more or different data.

If you have a program that draws a circle on the screen, you might try moving the circle, making it bigger or smaller, or changing its color. If you have learned one way to specify a color, try to learn other ways. (The Java API gives programmers the option of specifying a color with 3 integer values, 3 floating point values, 1 integer value, or with a named constant like Color.RED.) If you have a program that draws one circle, maybe you can write a program that draws two, three, or more circles? Code that draws a rectangle differs only a little from code that draws a circle.

A bond with other members of your profession

Because all Marines learn how to use a rifle, all Marines share a common experience. Their shared experience and knowledge unites them. Many kinds of careers are possible within the United States Marine Corps. However, all Marines, no matter what specialized training they choose later and no matter how they choose to support the mission, have the same foundational training.

All students of computer science should learn some of the same algorithms. Shared experience and knowledge can unite members of our profession, too. Many kinds of careers are open to people who have studied computer science. You can contribute to the software engineering profession without writing code. However, just as skills with a rifle remind every Marine that the mission is to win battles, so your knowledge of a few basic algorithms will remind you that our mission is to deliver working software that solves our clients’ problems.

Non-martial analogies

To readers who do not like a comparison of software engineering to soldiering: choose another field of endeavor and think about the most fundamental skills and tools in that field. Athletes talk about “fundamentals.” There are drills that all basketball players do, drills that all baseball and softball players do, and so on. Similarly, pianists practice scales. Can you think of other examples that we could also use to argue for the importance of practice?

A challenge to you

Could you, on your own, persuade newcomers to our field that they can gain more than a knowledge of if statements and for loops through practice with the selection sort? Do you believe that this kind of practice can help us develop a common vocabulary that will it easier for each of us to work on teams later in our careers? Have I persuaded you that students of computer science can develop a sense of belonging to our profession by learning the same lessons through repeated practice with the same algorithms (our “rifles”)? If you do not already feel it, can you imagine someday feeling a commitment to a shared mission?

Want to know more?

Read “‘Every Marine a rifleman’ begins at Recruit Training” and “The Creed of a United States Marine.”