So, You Need to Write a Program but Don't Know How to Start
Patricia Shanahan
One problem faced by all programmers, but possibly most difficult for some
beginners, is getting started on a program.
You could get someone else
to guide you on the specific program, but that is not a long term
solution. The design skills needed to start writing a program are at least
as essential a part of programming as knowledge of any programming
language.
Most people who get stuck getting started on a program are students
taking a programming course, so this advice is framed in terms of doing
a coursework assignment. Adapt as needed to other circumstances.
I wrote this document in response to questions in the newsgroups
comp.lang.java.programmer
and comp.lang.java.help
. See the Java FAQ for general
Java questions and useful links.
-
Read the assignment several times. Make sure you really understand
it. Any confusion on the programmer's part about what a program is intended
to do gets reflected in the program. If you are muddled, the program will
be muddled. If you are trying to build something that is not quite what
was asked for, the program is more likely to do that than to do the right
thing. If you cannot work out what the assignment means discuss it with
someone who knows, such as the instructor or a teaching assistant. Some
assignments may be quite detailed. Other just tell you what the overall
program is required to do, and leave all internal design to the sutudent.
-
Are you allowed to collaborate with other students? This is more likely
to be permitted than asking outside programmers to help you. Even two people
who are separately stuck on a problem may be able to make progress if they
discuss it.
-
Do all earlier assignments, whether or not they are required to be handed
in and graded. Some instructors use a sequence of assignments where ideas
or even code from the early ones can be reused in later ones. In any case,
the sequence will build skills - the first graded assignment may not be
simple enough for a first program. Even if you are not taking a formal
course, if you are stuck on the program you are trying to write do some
simpler ones first.
-
Work through the task the program is going to have to do using paper and
pencil. If necessary, reduce the problem size.
-
Construct an overall plan for how to solve the problem, including what
classes to use. Think about which objects need to have what information,
and how they are going to get it. Work through the task again thinking
about what your objects would do and how they would interact.
-
Design, code, and test simple classes that appear likely to be useful in
solving the problem.
-
Try to write good, clear, accurate interface comments on each class and
all non private variables, constructors, and methods. This is useful even
if nobody ever reads the comments because it is an important test
of whether you have a clear concept of the interface the class offers.
It takes very little time to describe a well thought out interface and
yet it is impossible to write good simple comments describing a muddled
interface. If an interface is very difficult to describe it is probably
not a good idea.
-
Design, write, and test programs that do some part of the job. For example,
if you need to read some input and then do something with the values write
one of the following programs first:
-
Read the input into an array or other data structure.
-
Do the main action of the program based on data in an array or other data
structure.
-
If you think you have all the data for a method to do some task but don't
know how to code it, try writing detailed implementation comments first.
This separates thinking about algorithms from the mechanics of coding.