34 Intermediate C Programming
this case, you should ask the purposes of these assignments. In particular, there should
be some learning objectives. Without knowing the purposes, it is impossible to understand
how to evaluate software. This is increasingly important as software becomes more complex.
Complex software has many parts and you need to understand why these parts are needed
and how they affect each other. Developing software requires many steps before, during,
and after coding. The following gives a few principles for you.
3.1.1 Before coding
• Read the specification and understand the requirements.
• Consider possible inputs and expected outputs.
• Identify valid but unexpected inputs and the correct outputs. For example, when
writing a sorting program, what would the program do if the input is already sorted?
What would the program do if the same number occurs multiple times? Identifying
unexpected inputs is a key concern for developing reliable software.
• Identify invalid inputs and the ways to detect them. Consider the sorting program as
an example again. If the specification says valid inputs are positive integers. What
would the program do if the input contains floating-point numbers? What would the
program do if the input contains negative numbers? What would the program do if
the input contains symbols (such as !@#&)? Even when the input is invalid, your
program should never crash (for example, producing a segmentation fault). Besides
being incorrect, software that crashes is a sign of security risks.
• Think about the solution and sketch down an approach on paper.
• Draw block diagrams showing how information is exchanged among different parts of
the program.
• After you have a design, plan the implementation aspect of the program: How many
functions should be created? What does each function do? How many files are needed?
When you design a function, try to keep these two suggestions in mind: Each function
should do only one thing, and should consist of no more than 40 lines of code. This
number is not rigid: 45 lines are all right; 120 lines are too long. A general rule is that
the entire function should fit in your computer screen using a readable font size.
• If you have a detailed design, you will save time on coding and debugging.
3.1.2 During coding
• This may surprise you: If you want to finish the programs correctly and faster, write
more code. Write code that is not needed. Before you put the code for one requirement
into a larger program, write a small program to test your solution. This is called the
unit test. If you cannot make one function work, you will definitely be incapable of
making anything work after putting many functions together. After you have done
some parts of the programs, make sure these parts work before continuing. You need
to write additional code to test these parts, since the other parts are not ready yet. A
rule of thumb is this: For every line of code you have to write, you should write three
additional lines of code. This additional work helps you understand what you must
do and helps you test what you have done.
• Always use a text editor that automatically indents programs. Such an editor can help
you detect braces at wrong places. Why is indentation important? Because it is easier
to visually detect misaligned code. Using the right tools can save you valuable time.
• Read your code line by line before running any test case. If you have not tried this
method, you may be surprised at how effective this method can be. Reading code can
help you find problems that are difficult to find by testing. One example is: