aboutsummaryrefslogtreecommitdiffstats
path: root/doc/CODING-GUIDELINES
diff options
context:
space:
mode:
authoranthm <anthm@f38db490-d61c-443f-a65b-d21fe96a405b>2005-01-19 02:34:59 +0000
committeranthm <anthm@f38db490-d61c-443f-a65b-d21fe96a405b>2005-01-19 02:34:59 +0000
commitc488473cf0c46af89e51bb8c0542e13b9890a6d8 (patch)
treec0e16c8ad90a4abc7aca59119e9d5ff1af2342d5 /doc/CODING-GUIDELINES
parent23ed5b2332472e5269dc625c0c9102996c182f91 (diff)
Modify code example suggestion
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@4844 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'doc/CODING-GUIDELINES')
-rwxr-xr-xdoc/CODING-GUIDELINES33
1 files changed, 22 insertions, 11 deletions
diff --git a/doc/CODING-GUIDELINES b/doc/CODING-GUIDELINES
index 7239f475c..175fb5380 100755
--- a/doc/CODING-GUIDELINES
+++ b/doc/CODING-GUIDELINES
@@ -124,21 +124,32 @@ 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.
+instead of recomputing it over and over. This can prevent you from
+making a mistake in subsequent computations, make it easier to correct
+if the formula has an error and may or may not help optimization but
+will at least help readability.
-Just an Example:
+Just an example, so don't over analyze it, that'd be a shame:
+
+
+const char *prefix = "pre";
+const char *postfix = "post";
+char *newname = NULL;
+char *name = "data";
+
+if (name && (newname = (char *) alloca(strlen(name) + strlen(prefix) + strlen(postfix) + 3)))
+ snprintf(newname, strlen(name) + strlen(prefix) + strlen(postfix) + 3, "%s/%s/%s", prefix, name, postfix);
- if (strlen(name)) {
- newname = alloca(strlen(name));
- strncpy(newname, name, strlen(name);
- }
-
vs
- if((len = strlen(name))) {
- newname = alloca(len);
- strncpy(newname, name, len);
- }
+const char *prefix = "pre";
+const char *postfix = "post";
+char *newname = NULL;
+char *name = "data";
+int len = 0;
+
+if (name && (len = strlen(name) + strlen(prefix) + strlen(postfix) + 3) && (newname = (char *) alloca(len)))
+ snprintf(newname, len, "%s/%s/%s", prefix, name, postfix);
Use const on pointers which your function will not be modifying, as this