CODING STYLE of MAJIK CLIENT (c) 1998 Jari "Dazzt" Saukkonen $Id$ 1. Which case to use? * Variable names are written in lower case * Structure names and type definitions are in upper case * In function names every word is capitalized, with exception of 'un', 'de', 'dis' (etc) prefixes which are not considered as words, so 'unload' is written as Unload(). Underscores '_' should not be used in function names. All files must be in UNIX (lf only) format. Filenames must be readable both in unixes and win95 (vfat). 2. Indentation Tabs are four (4) characters, use them. Braces are always on their own lines, ie. if (something) { DoSomethingElse(); } Switch statements have an exception, where case statements are indented only two (2) characters. switch(status) { case OK: Great(); break; default: OhNo(); break; } Classes follow the same rule as switch. Class Fisher { private: bool caughtfish; public: bool Caught(); }; Comments are // for single line comments, or /**/ for multi-line comments, see below. They should also be separated from the current indentation level. /* * This is a comment */ Use enough parentheses in if statements. Also, no assignments are allowed in if statements. Calculations should have space in between of each element. In function calls the using of space after comma in argument list is mandatory. a = QueryA(fisha, &arra); b = QueryB(fishb, &arrb); if ((a - 5) > 0) && ((b + 7) < 4)) { ... } 3. Naming convention Code must be clear. Spaghetti-code is not code at all and should be devoured by Sinister (if he likes spaghetti). #define is your friend. Every (numerical) constant should have a corresponding #define. Nothing is more frustrating than trying to search for the meaning of some numerical value. #define OK 1 Different platforms have the following defines defined: Linux: __LINUX__ Win32: __NT__ Win16: __WINDOWS__ Use long enough variable names, int vis_light_counter; makes much more sense than int vlc;. If the variable describes a number of something, it is predeced with 'c', ie. int cfishes; If some class is not fully portable (different code on different platforms), there should be a base class, which has all the portable functions of the class and platform-specific code is made by deriving a new class, ClassName_, which has the non-portable code. corresponds for Lx for linux, W32 for 32bit windows and W16 for 16bit windows. DOS is not supported due to non-existent support for networking. Then, ClassName is defined to ClassName_ to allow same code to compile under different platforms. Class Fisherman_P; Class Fisherman_Lx : public Fisherman_P; Class Fisherman_W32 : public Fisherman_P; #ifdef __LINUX__ #define Fisherman Fisherman_Lx #endif #ifdef __NT__ #define Fisherman Fisherman_W32 #endif This is taken care by a special macro, called MAKEPORTABLE, which performs the #ifdefs above. For the above code, MAKEPORTABLE(Fisherman); would do the thing. 4. Type definitions char Signed character (8 bits) short Signed short (16 bits) int Signed integer (32 bits) BYTE Unsigned character (8 bits) WORD Unsigned short (16 bits) DWORD Unsigned integer (32 bits) float Floating point value (32 bits) There is a global file, that has these types defined. 5. Which english to use? YES: color, organize, ondotypos NO: colour, organise, ondotypoes The meaning of ondotypos goes beyond the word, write english :). 6. File headers All files must comply with the layout below. /* filename.cpp Description of file. * Additional description * * (c) 1998 Dr. No Body * * $Id$ * * $Log$ */ 7. Small example /* fish.c Fisher daemon for lazy people * * (c) 1998 Jari Saukkonen * * $Id$ * * $Log$ */ #include #define FISHNAME_LEN 32 struct FISH { char name[FISHNAME_LEN]; int tail_length; int color; }; Class Fisherman { private: bool caught_fish; int cfishes; FISH *fishes; public: void StartFishing(); void ChangeBait(); void StopFishing(); bool Caught(); } void main() { Fisherman f; f.StartFishing(); while (!f.Caught()) f.ChangeBait(); f.StopFishing(); }