We recommend the following coding guidelines for ns
We recommend using the BSD Kernel Normal Form coding style located here
Although KNF is specified for C, it also applies reasonably well to C++ and Tcl. Most of ns already follows KNF and it is also extensively used for the BSD and Linux kernels.
The high order bit is 8-space indents. Using 8-space indents avoid confusion about what a "tab" character represents. A downside is it makes deeply nested looping structures hard to fit in 80 columns. (Some people consider this a feature. :-)
Instance variables of a class should all end in an underscore. This helps distinguish instance variables from global and local variables.
C++ and Tcl bound variables should have the same names This helps identify the bound variables quickly and reduces complexity
Don't create two files in the same directory whose names only differ in case. Not all file systems can distinguish between such files (e.g. FAT can not).
Avoid the use of C++ templates. Ns is supported on multiple platforms and templates are not very portable and are often difficult to debug. Exception: This guideline has been relaxed for some imported code, but the core of ns should build and run without templates.
Don't use long long. It's not part of the C++ standard. Some compilers (such as Microsoft Visual C++) do not support it. Use int64_t instead, which is defined if you include config.h.
If you care how many bits are used to store an integer value, then be sure to use int8_t, int16_t, int32_t, or int64_t. For unsigned values, you can use u_int8_t, u_int16_t, etc.
If a function has default arguments, declare them only once.
For example, if your header file has this:
char* foo(int bar, double baz = 0.0);
Then put this in your implementation:
char* foo(int bar, double baz) { // do something }
Do not repeat the "= 0.0" as this is not allowed by the C++ standard. (Note that earlier printings of Stroustrop's The C++ Programming Language are wrong and that this was corrected in the 6th printing.)
It's easy to make this mistake, as gcc 2.95 erroneously accepts this as valid C++. Microsoft Visual C++, on the other hand, follows the C++ standard on this issue and will complain if your code does not.
Do not declare loop variables in a for loop.
for (int i = 0; i < 4; ++i) { // do NOT do this
Although the C++ standard states that the variable i is considered to be defined inside the scope of the loop, some compilers (including Microsoft Visual C++) incorrectly consider the variable to be declared outside the loop. This causes problems if you have two for loops which use the same variable name as a loop index. For example, the following will not work in Visual C++:
for (int i = 0; i < 4; ++i) printf("foo\n"); for (int i = 0; i < 4; ++i) printf("bar\n"); // error in VC++
VC++ has a flag to turn on standards-compliant behavior, but this causes some of Microsoft's standard libraries to break. The easiest thing to do is to declare your loop index variable once outside of the loops:
int i; // this will work in any compiler for (i = 0; i < 4; ++i) printf("foo\n"); for (i = 0; i < 4; ++i) printf("bar\n");
For NsObjects, use the debug_ instance variable to enable debugging functionality. This avoids repetitive definations of debug statements and allows debugging a particular Nsobject without recompilation.
Example: To enable debugging in Queue object include the following statement in your tcl script.
Queue set debug_ true
Debugging statments can be inserted in the classes inheriting from Queue as follows:
debug("This is a debug statement %d",variable_to_debug);
All debugging statements are sent to stdout.
Error messages which cause the program to exit should be sent to stderr. All other messages should be sent to stdout