Standards-General.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Coding Standards: General
  4. %
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. \chapter{General}
  7. \label{c:CS:General}
  8. \section{Introduction}
  9. \label{s:Introduction}
  10. \emph{Why should I follow a coding standard ?} The answer is
  11. simple: I everybody in a project does, it becomes easy to read
  12. and maintain other people's code. Besides, this documents also
  13. provides lots of hints on how to avoid errors.
  14. No coding standards can be universal, so there will be certain
  15. exceptions in many of the topics below. However, These
  16. exceptions are however much rarer than it seems. \emph{Think
  17. twice before you break the standard}. If you are in doubt,
  18. follow the coding standard. Exceptions should be what the name
  19. suggests: rare situation where the standard cannot be applied.
  20. And now for the real thing \ldots{}
  21. %
  22. % Documentation
  23. %
  24. \section{Documentation}
  25. \subsection{Document Your Source Code}
  26. In-program documentation should provide the following
  27. information:
  28. \begin{itemize}
  29. \item A short description of the module.
  30. \item Document all \emph{non obvious code}. Any peculiarities
  31. (e.g.\ system dependencies) should be noted in the header.
  32. \end{itemize}
  33. \noindent Long and descriptive names are even more important
  34. than comments. In many cases comments can be omitted if the name
  35. says it all.
  36. \subsection{Use English}
  37. All documentation \emph{must} be in English. Also,
  38. all names used in the programs \emph{must} be in English. This
  39. is necessary because your code might be used by many people from
  40. all over the world, and over 90\% of them do not speak german,
  41. but all of them are capable of understanding english. Image you
  42. try to understand a program that is documented in elvish. Do
  43. also consider \emph{spel checking} your documentation.
  44. \subsection{Document Header}
  45. \label{ss:CS:Headers}
  46. All files in the Graphlet distribution should
  47. contain a header which describes the file. A sample header for
  48. C++ files is shown in example \ref{e:C++Header}. Other file
  49. formats should copy and adapt this header.
  50. \begin{itemize}
  51. \item The header should contain a short description of the
  52. file.
  53. \item The fields enclosed in
  54. \mbox{\texttt{\$\Param{Tag}:\textnormal{\ldots}\$}} are used by
  55. the CVS version control system. \emph{Do not enter information
  56. in these fields, they will be overwritten as soon as the file
  57. is checked in}. See also Section \ref{s:CS:VersionControl}.
  58. \item Header templates for most common file formats can be
  59. found in the directory \texttt{lib/graphlet/etc} in the
  60. graphlet distribution.
  61. \end{itemize}
  62. \begin{example}{e:C++Header}{A sample header for C++ Programs.}
  63. \begin{small}
  64. \begin{verbatim}
  65. //
  66. // filename
  67. //
  68. // The description of filename goes HERE.
  69. //
  70. //------------------------------------------
  71. //
  72. // $Source: /home/br/CVS/graphlet/doc/Standards-General.tex,v $
  73. // $Author: himsolt $
  74. // $Revision: 1.1.1.1 $
  75. // $Date: 1998/08/27 17:19:01 $
  76. // $Locker: $
  77. // $State: Exp $
  78. //
  79. //------------------------------------------
  80. //
  81. // (C) University of Passau 1995-1996, graphlet Project
  82. //
  83. \end{verbatim}
  84. \end{small}
  85. \end{example}
  86. %
  87. % Editor
  88. %
  89. \section{Editor}
  90. On UNIX machines, we recommend to use the \texttt{emacs} or
  91. \texttt{xemacs} editor. A customization for the \emph{cc mode}
  92. of \texttt{emacs}/\texttt{xemacs} is available in the Graphlet
  93. distribution in \texttt{lib/graphlet/etc/cc-mode.el}. This mode
  94. helps you to proper format your C and C++ code. To use this
  95. customization, insert
  96. \begin{quote}
  97. \texttt{(load "\Param{graphlet-dir}/lib/graphlet/etc/cc-mode")}
  98. \end{quote}
  99. \noindent in your emacs startup file (usually \texttt{.emacs} or
  100. \texttt{.emacslocal}), where \emph{graphlet-dir} is the directory
  101. where your copy of Graphlet resides, or the directory where
  102. Graphlet is installed.
  103. \begin{note}
  104. Dont copy the file \texttt{cc-mode.el} to your directory, since
  105. you will loose updates then.
  106. \end{note}
  107. %
  108. % External Tools
  109. %
  110. \section{External Tools}
  111. We recommend to use the following external tools:
  112. \begin{description}
  113. \item[GNU make]
  114. Use GNU make for makefiles. GNU make's extended features
  115. are admittedly incompatible with other make systems, but what counts
  116. is the fact that GNU make is the same on all platforms.
  117. We decided not to \emph{GNU autoconfigure} or other
  118. preprocessor based tools, since automatically generated
  119. generated files are usually hard to debug. Instead, use GNU
  120. make's \texttt{if}\ldots\texttt{then}\ldots\texttt{else}
  121. construct.
  122. \item[tcl, perl] Use tcl and perl for scripts. They are
  123. external tools, but Tcl is ok since Graphlet needs Tcl anyway.
  124. Perl is installed on most systems and provides a highly
  125. portable alternative to \texttt{sh}, \texttt{csh},
  126. \texttt{bash}, \texttt{awk}, \texttt{sed} and other scripts. It
  127. is also available on most non-UNIX platforms.
  128. \item[Others] If you have to use additional external tools,
  129. make sure that they are \emph{available} for all major
  130. platforms, easy to install and free if possible. If this is
  131. not the case, the tool should only be used for optional
  132. features.
  133. In any case, check it with Graphlet maintainers if you want to
  134. include your code in the distribution.
  135. \end{description}
  136. %
  137. % Filenames
  138. %
  139. \section{Filenames}
  140. File names \texttt{must} adhere the following conventions:
  141. \begin{itemize}
  142. \item
  143. File names should be descriptive. However, names \emph{must} be
  144. unique in the first \texttt{27} characters (this is a limit imposed
  145. by the Macintosh file system).
  146. \item Use the following endings in your program:
  147. \begin{itemize}
  148. \item \texttt{.c} for C code,
  149. \item \texttt{.cpp} for C++ code,
  150. \item \texttt{.h} for C and C++ header files,
  151. \item \texttt{.tcl} for Tcl code,
  152. \item \texttt{.perl} for Perl scripts,
  153. \item \texttt{.html} for html files.
  154. \item \texttt{.tex} and \texttt{.sty} for LaTeX files.
  155. \end{itemize}
  156. \noindent Graphlet's \texttt{GNUMakefiles} provide default suffix
  157. rules which rely on proper use of the above endings.
  158. \begin{note}
  159. \item The ending \texttt{.C} for C++ code will not work well
  160. with file systems that make no distinction between upper and
  161. lower case names (such as the Macintosh's file system), and
  162. is therefore discouraged.
  163. \item The ending \texttt{.cpp} for C++ files was chosen
  164. because most compilers accept it without problems. Older
  165. versions used \texttt{.cc}, which proved to be problematic
  166. with MS Windows compilers.
  167. \end{note}
  168. \item Use the name \texttt{GNUmakefile} for makefiles. This
  169. makes sure that other make systems (which are incompatible with
  170. GNU make) use do not recognize the file and issue an error
  171. message\footnote{They would issue an error message even if the
  172. name is \texttt{Makefile}, but we found that this error
  173. message is not helpful because it implies a syntax error in
  174. the makefile.}.
  175. \end{itemize}
  176. %
  177. % Line Width
  178. %
  179. \section{Line Width}
  180. The maximum line with of program code must not
  181. exceed \texttt{79} characters so that emacs and other editors can
  182. be used without adjusting the window width. This ensures that
  183. code can
  184. \begin{itemize}
  185. \item easily be printed,
  186. \item edited in a standard size window (even on an ASCII terminal), and
  187. \item easily be inserted into a text document.
  188. \end{itemize}
  189. \noindent Documentation should not use more than 65 characters per line.
  190. \begin{notes}
  191. \item In emacs, you can help that text (not program code!)
  192. does not exceed a width of \emph{n} lines by inserting
  193. \begin{quote}
  194. \texttt{(setq-default fill-column} \emph{n}\texttt{)}
  195. \end{quote}
  196. in your emacs startup file (usually \texttt{.emacs} or
  197. \texttt{.emacslocal}). To use this, type your text and use the
  198. fill command (default shortcut \texttt{M-q}) to format the text.
  199. \item Hint for non-emacs-novices: \texttt{auto-fill-mode} may
  200. be useful. Type \texttt{C-h f RET auto-fill-mode} in emacs for
  201. more information.
  202. \end{notes}
  203. The only exception from the above width restrictions are files in
  204. special formats where the syntax demands lines longer than the
  205. above limit, or where long lines are highly common, for example
  206. X11 resource files.
  207. %
  208. % Portability
  209. %
  210. \section{Portability}
  211. The following guidelines must be followed to help
  212. portability:
  213. \begin{description}
  214. \item[Minimize the Amount of Non Portable Code.]
  215. Almost all programs can be written in using \emph{only portable
  216. code}.
  217. \item[Identify Non Portable Code] Put non portable code into a
  218. separate module \emph{and mention this in the documentation.}
  219. \item[No Compiler Specific Features] Avoid \emph{compiler
  220. specific} or other non standard features at all costs. If
  221. you must do\footnote{The only reasonable exception is a
  222. compiler incompatibility or bug.}, use appropriate
  223. \texttt{\#ifdef} statements. \emph{Always provide a solution
  224. that runs with all compilers.}
  225. \textbf{Beware:} Your supervisor might ask you to prove that
  226. you really need this feature, and might ask you to remove it if
  227. your arguments are not convincing.
  228. \item[Test with Different Compilers and on Different Platforms]
  229. Test your code on several compilers and on different operating
  230. systems. This will help you to find bugs easier, and make sure
  231. that it is more likely that your code works with the next
  232. compiler generation.
  233. \end{description}
  234. %
  235. %
  236. %
  237. \section{Readability}
  238. Your code must be easy to read. Here are some hints how to
  239. achieve that:
  240. \begin{description}
  241. \item[Consistency] Your code should have a consistent
  242. ``look-and-feel'', which should ideally be the same as used in
  243. the Graphlet core\footnote{The Graphlet core is the graphlet
  244. system without algorithms, that is \texttt{src/gt\_base} and
  245. \texttt{src/gt\_tcl}. All examples in the Graphlet
  246. documentation use this style.}.
  247. \item[Indentation] Use indentation to outline the structure of
  248. your program. Graphlet generally uses \textbf{4} spaces for
  249. indentation levels.
  250. \begin{note}
  251. Do \emph{not} to redefine the tabulator width to achieve
  252. indentation.
  253. \end{note}
  254. \item[Long Names] Use long and descriptive names. This can
  255. replace lots of documentation. Use short names \emph{only if
  256. the meaning is obvious}. For example, it is often ok to use
  257. \texttt{x} and \texttt{y} for coordinates or \texttt{i},
  258. \texttt{j}, \texttt{k} for running indices. However, it is
  259. \emph{not a good idea} to use the name \texttt{h} or
  260. \texttt{help} or \texttt{a1}, \texttt{a2}, \ldots{} for
  261. auxiliary variables.
  262. \item[Use separators] Use blank lines as separators.
  263. Especially, add a blank line before and after an \texttt{if},
  264. \texttt{while} or \texttt{for} block, and add at least
  265. \textbf{2} blank lines between functions. Add more blank
  266. lines, or even comments if suitable to show the large scale
  267. structure of your program.
  268. \item[Write simple code] Generally, avoid overly complex
  269. constructions. Split them into smaller units. The exception is
  270. code which is critical for optimization and you know (e.g. from
  271. profiling) the compiler cannot handle this.
  272. \textbf{Beware:} Your supervisor might ask you to prove that you
  273. really need too complex code, and might ask you to remove it if your
  274. arguments are not convincing.
  275. \end{description}
  276. %
  277. % Standards
  278. %
  279. \section{Standards}
  280. Make your code conform to standards (keywords: ANSI, Posix). If
  281. you use de facto standards, make sure that they are really as
  282. common as you think. Here are some hints on how to use library
  283. functions:
  284. \begin{itemize}
  285. \item SUN's SunOS and Solaris operating systems have somewhat
  286. different standard libraries. This because SunOS is BSD based,
  287. Solaris is SYSV based. For example, SunOS has both
  288. \texttt{/usr/include/string.h} and
  289. \texttt{/usr/include/strings.h}, whereas Solaris has only
  290. \texttt{/usr/include/string.h}.
  291. \item Things get worse when it comes to other operating
  292. systems, such as Linux or even MS Windows.
  293. \item On SunOs and Solaris machines, conformance to standards
  294. like POSIX is usually listed in the manual. Be careful with
  295. functions which are not marked as standard.
  296. \end{itemize}
  297. %
  298. % Version Control
  299. %
  300. \section{Version Control}
  301. \label{s:CS:VersionControl}
  302. We \emph{strongly} recommend to use a version control tool like
  303. RCS or CVS. CVS is \textbf{mandatory} for development in Passau.
  304. Graphlet's standard headers (see Section \ref{ss:CS:Headers} do
  305. already provide entries for CVS headers.
  306. \subsection{CVS and emacs}
  307. Recent versions of \texttt{emacs} and \texttt{xemacs}
  308. automatically recognize CVS and show the current versions in the
  309. modeline.
  310. Emacs wont make backup files if version control is active. Put
  311. the following line in your emacs startup file (\texttt{.emacs} or
  312. \texttt{.emacslocal}) file to change this behavior.:
  313. \begin{quote}
  314. \verb|(setq vc-make-backup-files t)|
  315. \end{quote}
  316. \noindent We recommend to use this configuration as the
  317. backup copy will almost always be more recent than your latest
  318. committed version.
  319. \subsection{Local Configuration (Passau)}
  320. The local configuration at the University of Passau\footnote{The
  321. following steps are specific to the computers at the chair of
  322. theoretical science and are not guaranteed to work elsewhere.
  323. Ask your system administrator for details.} is as follows:
  324. \begin{itemize}
  325. \item The CVS root is at \texttt{/home/br/CVS}. You need to set the
  326. environment variable \emph{CVSROOT} to that location. To do that,
  327. add the line
  328. \begin{quote}
  329. \texttt{export CVSROOT=/home/br/CVS}
  330. \end{quote}
  331. to your \texttt{.bashlocal} file.
  332. \item The CVS module for Graphlet is called \emph{graphlet}. To
  333. check the source code out, type
  334. \begin{quote}
  335. \texttt{cvs checkout graphlet}
  336. \end{quote}
  337. \item Documentation for CVS is available as emacs info files. To
  338. read the documentation, start emacs and type
  339. \begin{quote}
  340. \texttt{C-h i m cvs RET}
  341. \end{quote}
  342. to get to the CVS documentation.
  343. % If you cannot type this, you are an emacs illiterate.
  344. \end{itemize}
  345. %%% Local Variables:
  346. %%% mode: latex
  347. %%% TeX-master: "CodingStandards"
  348. %%% End: