|
|
||||||||||||||||||||
Programmers Guide v0.53OverviewThe purpose of this document is to establish common programming methods for those developing code compatable with Cosm protocols. Development CycleSince we use CVS the development cycle is continuous. Coders should make sure they are familiar with CVS and cosm-cvs.html. Coordination_Any_ questions at all, ask. Before implementing any additional low level functions inform me, so we can make sure there isn't any overlap. I intend to be familiar with every line of code, so if you need any "major" functionality let me know, someone else may already be working on it. Also, if you're going to do anything "neat" be sure and inform me first, just so more than one person knows what you're doing. Use of Other Code and LibrariesAll code must be original and your own writing, you can't cut-n-paste from _any_ other projects or code libraries due to license problems (and other code won't follow the Cosm standards anyway). I've made sure there are no license problems with the couple of libraries that are eventually needed, and even then they will be completely separated from the main code. SimplicitySimplicity and clarity in the code are more important than weird optimization tricks. Especially since all the real work is done in the cores. All code interfaces are designed to be as simple as possible. Thread Safe CodeAll Cosm routines must be fully thread safe. This is not optional. If you don't know how to use locks correctly please go read up, there are many sources on the net on using locks. Keep in mind also that 64 and 128bit data types may not be atomic. Portability
Straight ANSI C + GNU Assembly. Period. TestingThe v3_TestModule() functions must test all the functions in a file, causing all cases to be taken within the functions and all outcomes and errors to be generated. Cosm will also use the "well tested, infrequent release" philosophy, not "rapid release, let the user debug it" one. All code should always be kept in a state where it can be compiled without errors/warnings. Never submit code that you can't compile. The v3_TestModule() code will also serve as the example code, so it must be complete and correct. CommentsComments, comments, comments. In general anything more complex then a for( ... ) loop should have a comment in the code every few lines. Function definitions should also be documented (in the header file) with the parameters, return values, and error conditions. Any { } block longer then ~30 (1/2 page) lines should have a comment at the closing } Header FilesHeader files for a given C file should contain all defines, structs, and function prototypes for the code. Functions should appear in the .c file in the same order they appear in the .h file, the 'helper' functions (v3_*) after main interface functions (v3*). Defines should be used liberally, and constants directly in the code where the meaning isn't obvious (read: everywhere) should not be used. Any other non-Cosm header files that are needed for the .c file should be included at the top of the .c, not in the .h which should only include it's own information. Only if a .h is needed for the .h to parse should it be included from the .h. Headers also contain the programmer documentation for a function, what it does, parameters, and expected return values or errors. No one should ever have to find and then dig around .c files to be able to use a function, and so any documentation should be in the header. All things will eventually be documented in non-programmer language in other formats. Version ControlVersion numbers for any code or files must be incremented when any change is made. Problems caused by a non-incremented version number can be quite difficult to track. Luckily, CVS does a wonderful job of this. CVS also does a great job of tracking changes and history. Make sure your messages on commited code are descriptive. Do no use the top of a .c file for the history, it quickly takes over the entire file. #ifdef UsageSystem specific #ifdef for OS/CPU is only allowed in utility level libraries. The only #ifdefs in the main level of code should be for a 32/64 bit constant number definition. When doing any test for CPU/OS type, use globally defined OS_TYPE and CPU_TYPE to switch off of e.g. #if ( OS_TYPE == OS_MACOSX ) - this is more readable to people not familiar with each compilers/os/cpu, and so it is less likely to lead to confusion. Naming ConventionsAll names should make sense and be long enough for the code to be self documenting
v3
Source File Names
Functions
Library Functions
Local Variables
Global Variables
Structures
Defines
Macros Code Style
Liberal Spacing
Indenting
Width Example:-- blah.h -- #ifndef V3_BLAH_H #define V3_BLAH_H /* include everything we need */ #include "cputypes.h" #include "cosmio.h" #define V3_FOO_ERROR -3 s32 v3_TestBlah( void ); #endif -- blah.c -- #include "blah.h" s32 v3_TestBlah( void ) { s32 error; /* run all tests this is a multi-line comment */ if ( /* test fails */ ) { error = V3_FOO_ERROR; } else /* this is the else format */ { error = V3_PASS; } /* foo failed - this is single line comment */ return( error ); } int v3Blah( void ) { u64 apple; s32 e; /* use the macro to set apple */ _V3_SET64( apple, 01234567, 89ABCDEF ) v3PrintA( (ascii *) "A is for Apple = 0x%016Y\n", apple ); if ( foo = v3_TestBlah() ); { v3PrintA( (ascii *) "Test Blah Failed = %i\n", foo ); return( V3_FAIL ); } return( V3_PASS ); } General Conventions
Return Values if ( ( error = test() ) != V3_PASS ) { /* deal with the problem and recover */ } When pointers are returned, NULL is failure, anything else should be a considered valid. if ( ( pointer = test() ) == NULL ) { /* deal with the problem and recover */ } When return( ... ) is used, always use the ()'s.
int, char, short
Parameter order
Additional keywords (for syntax highlighting):
Credit Where Credit is Due Common Pitfalls
Error Code Checking
Network Buffers and Memory Overruns
Use of Uncleared Structs
Pointer Math
NULL Pointers
Typecasting
Memory Leaks by Adam L. Beberg
© Copyright Adam L. Beberg 1999. All rights reserved.
Cosm (tm) is a trademark of Adam L. Beberg.
Any use of the information presented here is subject to
the terms in the Cosm License.
|