[Asterisk-cvs] asterisk/doc CODING-GUIDELINES,1.5,1.6
anthm at lists.digium.com
anthm at lists.digium.com
Tue Jan 18 20:31:50 CST 2005
Update of /usr/cvsroot/asterisk/doc
In directory mongoose.digium.com:/tmp/cvs-serv5727/doc
Modified Files:
CODING-GUIDELINES
Log Message:
Modify code example suggestion
Index: CODING-GUIDELINES
===================================================================
RCS file: /usr/cvsroot/asterisk/doc/CODING-GUIDELINES,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- CODING-GUIDELINES 18 Jan 2005 23:35:30 -0000 1.5
+++ CODING-GUIDELINES 19 Jan 2005 02:34:59 -0000 1.6
@@ -124,21 +124,32 @@
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
More information about the svn-commits
mailing list