File: code_style.txt

package info (click to toggle)
nethack 3.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: ansic: 266,495; cpp: 13,652; yacc: 2,903; perl: 1,426; lex: 581; sh: 535; xml: 372; awk: 98; makefile: 68; fortran: 51; sed: 11
file content (198 lines) | stat: -rw-r--r-- 5,913 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# NetHack 3.6  code_style.txt       $NHDT-Date: 1524689669 2018/04/25 20:54:29 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.2 $
# Copyright (c) 2015 by Derek S. Ray
# NetHack may be freely redistributed.  See license for details.

NetHack DevTeam Coding Style
============================

NetHack is written in C, with a little bit of C++ to access platform
libraries. This coding style document is concerned primarily with the style
used for C source files. We do not have a defined style for C++ files, but C++
should be styled in keeping with C.

The code base in C files was, close to the 3.6 release, reformatted using a
version of the clang-format tool patched to support K&R-style argument
declarations. Due to some incompatibilities, the patch is not publicly
available and clang-format is not expected to be regularly used.

Developers should do their best to adhere to the coding style to promote
legible, easy-to-edit code. Legibility is paramount, so in some cases, it may
be better not to fully adhere to the style guidelines.

Recipes for common text editors can be found at the end of this file.

Indentation and Spacing
-----------------------

The basic indentation is 4 spaces wide. All indentation is done using space
characters, not tabs.

Lines should be at most 78 characters wide. If a line would be longer than the
limit, the line should be wrapped and the wrapped portion should be aligned
with the parentheses or brackets containing the wrap. If there is no set of
parentheses or brackets, the line should be indented four spaces. Wrapping
should normally occur after a comma or before a binary operator, when
possible:

    int index
        = SomeExcessivelyLongExpression;

    fcall(arg1,
          arg2, (cond13
                 && cond2));

Single blank lines should be used wherever convenient to improve readability.

Functions and Control Statements
-------------------------------

For a function definition, the return type, declarator, and opening brace
should each appear on a line of their own. Arguments are never declared in the
function declarator, but are declared, unintended, K&R-style before the
opening brace:

    void
    foo(i, c)
    int i;
    char c;
    {
        /* function body */
    }

Opening braces of control statements appear on the same line as the control
statement:

    if (condition) {
        /* body */
    }

Else statements and the while statements of do-while blocks appear on the same
line as the closing brace of the if or do statement. Otherwise, closing braces
always get a line of their own.

    if (condition) {
        /* body */
    } else if (condition) {
        do {
            /* body */
        } while (condition);
    } else {
        /* body */
    }

If a control block has only a single statement, it can appear on a line of its
own, with an indent. If the statement is a null statement, then it should be
expressed as an empty set block, not with a semicolon, because many compilers
will warn if a null statement is used:

    if (condition)
        fcall();

    if (condition) {
    } else
        fcall();

If multiple control blocks are being used in a row, it may be more readable to
use braces even for single statements, and they should be used if they improve
readability. The following is an example of poor usage:

    if (condition) {
        /* long body */
    } else if (condition)
        statement;
    else {
        /* very long body */
    }

Switch statements should have the case labels unindented, and the statements
should be indented normally. The default case should occur last unless there's
a compelling reason not to, and fallthroughs should be explicitly marked as
such with a comment, to avoid Yeenoghu getting the touch of death again:

    switch (condition) {
    case FOO:
    case BAR:
        fcall();
        /* fall-through */
    case BAZ:
        fcall();
        break;

    default:
        statement;
    }

Variables should never be declared in a condition or a for loop
initialization, and if an assignment is used as a condition, it should be
wrapped in an additional set of parentheses for clarity:

    int *p;
    if ((p = fcall())) {
        /* body */
    }

    int i;
    for (i = 1; i < 10; ++i) {
        /* body */
    }

Spaces in Expressions
---------------------

Spaces should appear around binary operators, after commas, after a C-style
cast, and after the keyword in a control statement. They should not appear
between a function name and the opening parenthesis of a function call, nor
immediately inside a pair of parentheses:

    foo(i, j, l);
    if ((boolean) condition) {
        /* body */
    }

Vim Configuration
=================

For vim, the following settings are encouraged when editing NetHack code, to
ensure that indentation is done correctly:

    set shiftwidth=4
    set softtabstop=4
    set expandtab
    set tabstop=4
    set shiftround
    set textwidth=78
    set cindent
    set filetype=c


Visual Studio Configuration
===========================

In Visual Studio under Tools->Options->Text Editor->C/C++, you can set the 
following options to obtain desired behavior:

[Tabs]
Indenting: Smart
Tab size: 4
Indent size: 4
Insert Spaces

There are a number of other options under [Formatting] that should be
checked (Indentation, New Lines, Spacing, and Wrapping), but there are so
many entries that reproducing them here is impractical. Fortunately, the 
options are in plain English, so walking through them with a copy of 
this Guide handy and making changes as required will suffice.

Emacs Configuration
===================

There are no doubt umpteen different ways to handle this in Emacs.
Putting the following in ~/.emacs.el is one

(defun hook-c ()
  (setq c-set-style "k&r")
  (setq c-basic-offset 4)
  (setq indent-tabs-mode nil)
  (c-set-offset 'knr-argdecl-intro 0))
(add-hook 'c-mode-common-hook 'hook-c)