Project

General

Profile

Coding Standards » History » Version 1

Kishan Parekh (TechPartner), 01/03/2023 04:47 PM
WIP: added intro section, formatting yet to be done

1 1 Kishan Parekh (TechPartner)
# Coding Standards
2
3
## Why coding standards?
4
5
To be able to understand where to start from and where to head when designing coding standards, one must re-visit some basic ideas about programming itself.
6
7
Novice programmers feel that as long as source is correct and bug-free, nothing else about it matters. With a little more experience, they learn about indenting.
8
9
With a lot more experience, they learn about writing good comments. Most mediocre programmers write either too much comments or too little. This is because they are often unskilled in communicating, poor as writers and teachers.
10
11
After insight into commenting comes insight into identifier design. One needs a good grasp over the English language to select sensible identifiers, which makes this a difficult job for unskilled communicators. Then, on a separate track, they learn about issues of software architecture, modularisation, and other aspects of good design. All this adds up to a large and difficult subject, and many books have been written about various corners of this territory.
12
13
What makes the software development process more confusing is that the large and thriving software development industry has attempted to term software design an engineering activity. It is an engineering activity only to the extent that a finished, tested software program is in many respects like a machine, and we know that it requires engineering to design and build machines. Because of this relationship with engineering, many assume that the process of building software is akin to that of building physical machines in a factory, where lines of skilled workers labour over their tasks in a long assembly line.
14
15
The software development industry talks of developing software using the
16
"software factory" approach, where roles are differentiated, and design,
17
assembly, testing, and measurement are all assigned to separate teams.
18
This ``factory'' approach implicitly assumes that designing needs
19
experienced designers, but code can be written by semi-skilled
20
assembly-line workers.
21
22
This extrapolation is rubbish. At best, this approach can product only
23
mediocre software to perform simple tasks like maintaining account
24
balances for financial transactions.
25
26
We believe that good code cannot be written this way. A company engaged
27
in executing one-shot software projects which address relatively simple
28
business applications may sometimes succeed by using the ``software
29
factory'' approach. But a software product company, which must live
30
for many years with the goods and evils of the code it puts into its
31
own products, cannot afford to leave coding to semi-skilled assembly-line
32
workers. Developing difficult and long-lived software is a process
33
very different from manufacturing machines in a factory. It is more like
34
writing literature. You must know what you want to write, and why. Then
35
you must have the skills to translate your realisations and insights
36
into beautiful, simple and elegant code. This is a skilled, individual,
37
cerebral activity. It is not a manufacturing process.
38
39
It is clear, by extrapolation, what we feel about industrial quality
40
standards like the ISO~9000-2001 and SEI~CMM quality standards. We
41
feel that these standards, when applied to software processes, do
42
not necessarily help in the creation of beautiful, simple and elegant
43
code. These standards are not without merit, though. They may bring
44
standardisation and order into an otherwise unmanaged and unstructured
45
software development process, specially when large developer teams are
46
involved. The documentation that these standards impose helps to enforce
47
a software *process*. But these standards cannot elevate the
48
output of the team from structured, documented code to beautiful, simple
49
and elegant code. If one keeps in mind the similarity between writing
50
literature and writing code, the reasons for this failure are obvious.
51
The goodness that we strive for in code, the goodness that we refer to
52
with words like ``beautiful, simple and elegant,'' is not an abstract or
53
personal aesthetic that one aims for purely for emotional satisfaction. We
54
firmly believe that good code makes good business sense. Good code
55
costs less to write and maintain. It translates to happier customers and
56
software users, and earns the software developing company more money in
57
the long run. This may not always be true of one-shot software project
58
companies, but is *always* true about software product companies.
59
As a software products company, we have a business reason to take our
60
software development process and the *goodness* of our code very
61
seriously.
62
63
Once we understand this, we realise that coding standards are hard
64
to get right. The belief that the output of a group of workers can
65
be standardised is itself questionable in the context of software.
66
This belief probably comes from the industrial-production mindset, and we
67
have just said that good code is rarely born in that environment. However,
68
any code which expects to live long needs to be maintained; it needs
69
care and feeding. This means that successive generations of workers need
70
to tend to a body of code as it grows and morphs. It is important that
71
some degree of uniformity be maintained between original code and new
72
additions and modifications, however superficial this uniformity may
73
be, purely to make this maintenance easier. Coding standards help to
74
achieve this uniformity, provided they do not stifle the freedom a good
75
programmer needs to write good code. Self-documenting identifiers is a
76
good example of standards which stifle good code. A sensitively designed
77
coding standard should avoid such pitfalls.
78
79
This document attempts to specify coding standards to be used at \merce,
80
keeping in mind the challenges that a coding standard designer must
81
face. The quality of code is not determined by how closely it conforms
82
to a coding standard -- we must never let ourselves forget this. The
83
yardsticks which determine excellence in code are far more difficult to
84
pin down than a mere coding standard. We hope we will hold this in our
85
hearts and minds as we approach the task of defining our coding standards
86
and the larger task of developing software at Merce.
87
88
## Basic principles
89
90
Code has only a five-decade history. It is doubtful whether there were large
91
software projects many years before the IBM OS/360 project. Therefore,
92
coding standards have no centuries-old tradition to fall back on, no
93
stories which can claim "This is how code was written in Middle England
94
from the days of King Richard the Lionheart."
95
96
In the absence of its own tradition and philosophy, we have to borrow
97
from other, older disciplines to create a set of basic principles on
98
which we can then do the detailing. What are these basic principles? The
99
following three come to mind:
100
101
* The basis of any coding standard should be the rules of good
102
    typesetting which are applied to English. These rules are quite
103
    robust, having seen more than a millenium of use and having evolved
104
    well with time. The low-level rules which govern how to place commas,
105
    parentheses, \etc are all directly applicable for source code.
106
    Since code is not organised into printed pages, page composition
107
    rules do not help much here. But source is written in units of
108
    source files. Therefore, a source file can be loosely treated as
109
    a chapter. Only loosely.
110
    After all, good source is read in a manner very similar to reading
111
    good English.
112
* Make the source file readable without the artefacts of typeface
113
    changes. All source files are laid out in monospaced font of uniform
114
    size. Therefore, normal typesetting artefacts like section headers
115
    and typeface changing are not available.
116
    The intelligent use of spaces and blank lines therefore becomes
117
    almost an art form of its own. Use too little and you crowd the
118
    lines. Use too much, and you break the flow and continuity.
119
    Just like blank lines control spacing in the vertical direction, the
120
    block structure of nested constructs are highlighted by indentation
121
    in the horizontal axis. Modern source is unreadable without strict
122
    indentation.
123
* Make the source file readable to a good programmer who is
124
    unfamiliar with the code.
125
126
When designing a coding standard, your choices can make things easier
127
or harder for different types of readers -- no one size fits all. And
128
the different types of readers we encounter can be described thusly:
129
130
* the bureaucratic template-oriented code layout, meant more for
131
    conforming to a standard than to assist readability. Such code is,
132
    or at least appears to be, more suited to machine analysis than
133
    human reading.\footnote{See Section~\ref{selfdocumentingcodesection}
134
    on page~\pageref{selfdocumentingcodesection} for some examples and
135
    an explanation of the philosophy which drives such coding standards.}
136
* the incompetent programmer, who, it is believed, needs copious
137
    amounts of comments and super-simple, widely-spaced tokens in
138
    his source. Usually such code is also written \textit{by}
139
    incompetent programmers.
140
* the good programmer who is intimately familiar with his code
141
    -- no comments needed, obfuscated code is not obfuscated, and
142
    coding standards are at some level unnecessary
143
* the good programmer who is unfamiliar with the code. For such
144
    programmers, some comments are a big help, and clear use of
145
    space to delineate blocks make for easy reading.
146
147
You must choose the type of reader you want your coding standards to cater
148
to. Many large software divisions or companies choose the bureaucratic
149
approach. Such companies also find it harder to retain really talented
150
programmers --- probably there is a connection.
151
This document will assume that this coding standard is for the good
152
programmer who is unfamiliar with the code.
153
154
## Language independent standard
155
156
This document aims to be independent of programming language. It applies
157
to C, C++, Perl, shellscripts, and Java. Wherever there are
158
language-specific issues, they have been highlighted with explicit
159
observations.
160
161
Therefore, it is implicit that this coding standard will not get into
162
the deeper issues of language-specific source formatting. We feel that
163
such detailing can be left to the programmer. After all, we expect this
164
document to be used by good programmers.