Hardware Reference
In-Depth Information
Figure 5.38 ICC12 terminal window after program execution and the “md 1000”
command
5.13 Programming Style
Programming style refers to a set of rules or guidelines used when writing a program. It is
believed that by following a particular programming style the resultant program will be more
readable and easier to debug. The programming style used in a particular program may be de-
rived from the coding standards or code conventions of a company or other computing organiza-
tion, as well as the preferences of the author of the code. Programming styles are often designed
for a specific programming language (or language family), but some rules are commonly applied
to many languages. (Style considered good in C source code may not be appropriate for BASIC
source code, and so on.)
The essence of good programming style is communication . Any program that will be used
must be maintained by somebody and that somebody must be able to understand the code by
reading it. Any program that needs debugging will be easier to debug if the creator carefully
explains what's going on. Within the program text, programmers have three primary tools for
communicating their intentions: comments ; naming of variables, constants, and subroutines;
and program appearance (spacing, alignment, and indentation).
5.13.1 General Guidelines to Comments
The most useful things to know about program documentation are
What to comment
When to comment
In general, the programmer should include comments explaining what every function does,
what every variable does, and an explanation for every tricky expression or section of code.
“When” is easy— comment it before you write it . Whenever you declare a variable, include a
comment. Whenever you start to write a subroutine, first write a comment explaining it. That
will help to make clear in your mind what you are about to do. If you find the explanation for
a subroutine difficult to write, it's a sure sign that you have done a poor job of structuring the
program. Avoid obscure programming language constructs and reliance on language-specific
precedence rules. It is often better to force precedence by use of parentheses since this leaves
no doubt as to meaning. In general, if you had to look up some rule or definition, your readers
most likely will too. Whenever you need to write a difficult expression or some other tricky
business and find it difficult to do, you should expect that it will be difficult to understand. So,
add a comment.
 
Search WWH ::




Custom Search