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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
Coding Style and Conventions for Shlomi Fish’s Projects
=======================================================
Shlomi Fish <shlomif@cpan.org>
:Date: 2012-05-14
:Revision: $Id$
Perl Style Guidelines
---------------------
Use Test::More for test scripts while using Test::Count annotations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
One should use Test::More for new test scripts, while using Test::Count
( http://beta.metacpan.org/module/Test::Count ) "# TEST" annotations. Some
of the old test scripts under +t/*.t+ are still using Test.pm, but it should
not be used for new code.
Any bug fixes or feature addition patches should be accompanied with
a test script to test the code.
Avoid trailing statement modifiers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
One should not use trailing "if"s "while"s "until"s, etc.
Bad:
----------------
print "Hello\n" if $cond;
----------------
Good:
----------------
if ($cond)
{
print "Hello\n";
}
----------------
Avoid until and unless
~~~~~~~~~~~~~~~~~~~~~~
"until" and "unless" should be spelled using "if !" or "while !" or
alternatively "if not" or "while not".
Make sure you update the "MANIFEST" file with any new source files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All the new source files should be places in the "MANIFEST" file in the core
distribution. Note that I am considering to make use of "MANIFEST.SKIP"
instead, which would not necessitate that in general.
Make sure to update the "Changes" (or equivalently named) file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A patch should also patch the "Changes" file (whose name may vary) with the
explanation of the change. A Changes file should not be automatically
generated. Note that due to historical reasons, the exact format of the Changes
varies between different projects of mine and you should try to emulate the
style and format of the one of the CPAN distribution in question.
Test programs should not connect to Internet resources
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As a general rule, test programs should not connect to Internet resources
(such as global web-sites) using LWP or WWW::Mechanize or whatever, and
should rely only on local resources. The reasons for that are that relying
on such Internet resources:
* May fail if the machine does not have a fully open Internet connection.
* Will add load to the hosts in question.
* Such Internet resources can fluctuate in their content and behaviour,
which may break the tests.
Other elements to avoid
~~~~~~~~~~~~~~~~~~~~~~~
See http://perl-begin.org/tutorials/bad-elements/ .
C Style Guidelines
------------------
Here are some style guidelines for new code to be accepted into XML-LibXML:
4 Spaces for Indentation
~~~~~~~~~~~~~~~~~~~~~~~~
The source code should be kept free of horizontal
tabs (\t, HT, \x09) and use spaces alone. Furthermore, there should be
a 4 wide space indentation inside blocks:
----------------
if (COND())
{
int i;
printf("%s\n", "COND() is successful!");
for (i=0 ; i < 10 ; i++)
{
...
}
}
----------------
Curly Braces Alignment
~~~~~~~~~~~~~~~~~~~~~~
The opening curly brace of an if-statement or a for-statement should be
placed below the statement on the same level as the other line, and the
inner block indented by 4 spaces. A good example can be found in the previous
section. Here are some bad examples:
----------------
if ( COND() ) {
/* Bad because the opening brace is on the same line.
}
----------------
----------------
if ( COND() )
{
/* Bad because the left and right braces are indented along with
the block. */
printf(....)
}
----------------
----------------
/* GNU Style - fear and loathing. */
if ( COND() )
{
printf(....)
}
----------------
Comments should precede the lines performing the action
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Comments should come one line before the line that they explain:
----------------
/* Check if it can be moved to something on the same stack */
for(dc=0;dc<c-1;dc++)
{
.
.
.
}
----------------
+TODO: Fill in+
One line clauses should be avoided
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
One should avoid one-line clauses inside the clauses of +if+, +else+,
+elsif+, +while+, etc. Instead one should wrap the single statements inside
blocks. This is to avoid common errors with extraneous semicolons:
----------------
/* Bad: */
if (COND())
printf ("%s\n", "Success!");
/* Good: */
if (COND())
{
printf ("%s\n", "Success!");
}
/* Bad: */
while (COND())
printf("%s\n", "I'm still running.");
/* Good: */
while (COND())
{
printf("%s\n", "I'm still running.");
}
----------------
Identifier Naming Conventions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here are some naming conventions for identifiers:
1. Please do not use capital letters (including not +CamelCase+) - use
all lowercase letters with words separated by underscores. Remember, C is
case sensitive.
2. Note, however, that comments should be phrased in proper English, with
proper Capitalization and distinction between uppercase and lowercase
letters. So should the rest of the internal and external documentation.
3. Some commonly used abbreviations:
----------------
max - maximum
num - numbers
dest - destination
src - source
ptr - pointer
val - value
iter - iterator
idx - index
i, j - indexes
----------------
Don't comment-out - use #if 0 to temporarily remove code
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code should not be commented-out using gigantic +/* ... */+ comments. Instead,
it should be out-blocked using +#if 0...#endif+.
In Perl code, one can use the following POD paradigm to remove a block of
code:
----------------
=begin Removed
Removed code here.
=end Removed
=cut
----------------
No declarations after statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
One should make sure there are no declarations after statements in the ANSI
C code. If you're using gcc, you can make sure this is the case by adding
the flags "-Wdeclaration-after-statement -Werror" to "CCFLAGS" in the makefile.
Bad:
----------------
int my_func(int arg)
{
int var;
printf("%s\n", "Foo");
/* Declaration after statement. */
int other_var = var;
return;
}
----------------
Better:
----------------
int my_func(int arg)
{
int var;
int other_var;
printf("%s\n", "Foo");
other_var = var;
return;
}
----------------
Comments should have an empty space after the comment leader
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Comments in Perl, C, Python, Ruby, and other languages should have an
empty space after the comment leader.
Bad:
----------------
#Print a value.
print "Hello\n";
/*Print a value.*/
printf("%s\n", "Hello");
----------------
Better:
----------------
# Print a value.
print "Hello\n";
/* Print a value. */
printf("%s\n", "Hello");
----------------
sizeof(var) is preferable to sizeof(mytype_t)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Given the choice between +sizeof(var)+ as well as +sizeof(*var)+
and +sizeof(mytype_t)+ where +mytype_t+ is a type, the former should be
prfereable. This way, if the type of the variable changes, one does not
need to fix the +sizeof(…)+.
sizeof() must always be enclosed in parentheses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not write +sizeof int+, +sizeof mystruct_t+ etc. Instead write
+sizeof(int)+, +sizeof(mystruct_t)+ .
Types should end in “_t” ; Raw struct definitions in “_struct”
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
New typedefs should call their types in names that end with a “_t”:
----------------
typedef int myint_t;
typedef struct
{
.
.
.
} mystruct_t
----------------
Prefer doing +typedef struct { ... } mystruct_t+ to declaring a struct
separately. If a struct declartion is still needed (e.g: if it contains
a pointer to itself) it should:
1. Have an identifier that ends with “_struct”.
2. Be typedefed into a type (that ends with “_t”):
+typedef struct my_struct_struct my_struct_t;+.
|