diff options
author | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-01-18 23:35:30 +0000 |
---|---|---|
committer | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2005-01-18 23:35:30 +0000 |
commit | 23ed5b2332472e5269dc625c0c9102996c182f91 (patch) | |
tree | 8b3186197ac52cb27eecbd99519035158f40c7b2 /doc | |
parent | 0fa7671c5dd76337701b95bd91ddba1c97d52096 (diff) |
Additional coding guidelines (bug #3374)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@4840 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/CODING-GUIDELINES | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/doc/CODING-GUIDELINES b/doc/CODING-GUIDELINES index 643e5444c..7239f475c 100755 --- a/doc/CODING-GUIDELINES +++ b/doc/CODING-GUIDELINES @@ -76,3 +76,70 @@ for (x=0;x<5;x++) { baz(); } } + + + +Make sure you never use an uninitialized variable. The compiler will +usually warn you if you do so. + +Name global variables (or local variables when you have a lot of them or +are in a long function) something that will make sense to aliens who +find your code in 100 years. All variable names should be in lower +case. + +Make some indication in the name of global variables which represent +options that they are in fact intended to be global. + e.g.: static char global_something[80] + +When making applications, always ast_strdupa(data) to a local pointer if +you intend to parse it. + if(data) + mydata = ast_strdupa(data); + +Always derefrence or localize pointers to things that are not yours like +channel members in a channel that is not associated with the current +thread and for which you do not have a lock. + channame = ast_strdupa(otherchan->name); + +If you do the same or a similar operation more than 1 time, make it a +function or macro. + +Make sure you are not duplicating any functionality already found in an +API call somewhere. If you are duplicating functionality found in +another static function, consider the value of creating a new API call +which can be shared. + +When you achieve your desired functionalty, make another few refactor +passes over the code to optimize it. + +Before submitting a patch, *read* the actual patch file to be sure that +all the changes you expect to be there are, and that there are no +surprising changes you did not expect. + +If you are asked to make changes to your patch, there is a good chance +the changes will introduce bugs, check it even more at this stage. + +Avoid needless malloc(),strdup() calls. If you only need the value in +the scope of your function try ast_strdupa() or declare struts static +and pass them as a pointer with &. + +If you are going to reuse a computable value, save it in a variable +instead of recomputing it over and over. + +Just an Example: + + if (strlen(name)) { + newname = alloca(strlen(name)); + strncpy(newname, name, strlen(name); + } + +vs + + if((len = strlen(name))) { + newname = alloca(len); + strncpy(newname, name, len); + } + + +Use const on pointers which your function will not be modifying, as this +allows the compiler to make certain optimizations. |