The Essence of Good Programming Practices
The Essence of Good Programming Practices
By Nathan Delson
Like an essay, a computer program can be written well or poorly. Developing good
programming skills will serve one well throughout ones career in engineering as well as
open doors to many other career opportunities. As with writing essays, there are many
opinions on how to write good code. This document presents a few points that I believe
highlight good programming approaches. Many of the points are relevant to all
programming languages, but the specific examples relate to Matlab, and are geared
towards MAE150, a course for Aerospace, Bioengineering, Mechanical, and Structural
Engineering students. There are other more in-depth books on the subject
(see Examples and Resources end of document).
Modularity
A module of a program is a function or separate file that is called from the main program.
In a well-written modular program, one module can be updated without requiring
changes in the other parts of the program.
Consider the print dialog box that appears from most Windows applications, as shown
below:
When a new printer is added to the computer, the programs that use the printer do not
have to be re-written. Instead the features of the new printer are automatically available
to all programs that access the dialog box. This ease of updating the program
functionality is due to the modularity of the program. The print drivers are written
separately than the calling program. To achieve this modularity care must be taken to
ensure that there is proper information flow between the calling program and the module
(e.g. printer features and data to be printed).
When should one write a separate module (Matlab function or script file)?
• When there are well defined separate functions of the program, one should
separate the modules of the program accordingly. For example, in a dynamic
simulation, one module should could calculate the dynamics of the system,
another module could perform the plotting for an animation, and another module
could define the parameters of the model.
• Whenever one finds repeated code, it probably makes sense to write this code in
a separate function. Otherwise, when a change is required in the code (and it
always is), one needs to make the exact change in numerous locations that,
which is in itself an error prone process.
• An added advantage of writing repeated code in a function is that the overall size
of the program is shorter, which can be important is running the code on a low
powered microprocessor with limited memory.
In general Matlab functions are considered better code than a script file. With a function,
the input and output arguments are clearly defined, so it can easily be discerned what
variables are modified within the function. Moreover, the same function could be reused
to operate on variables of different names.
Generalization
A generalizable piece of code is preferred over a code that can only be used for a
unique application. For example, code which fixes the number of links in a robot arm is
less useful than code where that number is variable and can be used for a wide range of
robot arms. Below are some examples:
• Never hardcode values into program. Instead define variables for such
parameters. For example, by defining the number of links on a robot as a
variable, it can easily be modified in a single location. As a link is added or
removed from the robot. It is best to locate the definition of all parameters in a
single location, so one can view all parameters at once (instead of having
surprises hiding deep within the code)
• Use arrays instead of sequentially numbered variables. If you find yourself
defining variables such as: link_1 and link_2, replace this with an array link(1)
and link(2). There are many advantages to using arrays:
o The number of elements can be easily adjusted
o One can write loops to perform repeated operations, rather than
rewriting code with different variable names
o Matlab has many matrix operations that take advantage of arrays.
% Define number of links as variable % The following code may be easy to start
% and use arrays when possible writing,
% but does not allow easy changes to
number of links or use of loops
nlink = 4; % number of links
disp(link_length_1);
% use loop to easily display link lengths
disp(link_length_2);
for ilink=1:nlink
disp(link_length_3);
disp(link_length(ilink));
disp(link_length_4);
end
% Calc new angle using Euler method % (bad: no short overview of code)
alpha_new = alpha + dalpha*time_step; a = a + da*time; % (bad: reuse of variable
name is confusing)