You are on page 1of 3

Version - 0

Coding Convention
A CSU (Computer Software Unit) is a unit of code that can be compiled and test separately. It is important to decompose your code into independent and well defined CSU. The smallest CSU is ADT, machine or process. A CSU can be translate into a class in OOP. C Language 1. Code must be written in CSU (Computer Software Unit) so that Unit test can be done easily: a. X.h Header file, containing type and prototype b. X.c Body file, code and variable associated with X.h c. mX.c a driver to test X.h and X.C 2. CSU must be well identified as ADT, Machine or Process 3. Guidelines for writing a header file : a. Must not contain variable b. Must have a macro to prevent double inclusion, with #ifndef _X_H #define _X_H #endif Inclusion is only allowed for header file only ( *.h) Dynamic memory must be handled properly : a. Three steps : Declaration, allocation, initialization b. Every allocation must be checked if it is NULL or not c. deletion must be guaranted Write assert for precondition You have to know your role : a. CSU developer (provider) : specification (writing X.h) and implementation (writing X.c) b. CSU user : given X.h, using it as specified withput coding.

4. 5.

6. 7.

IL& BY, IF5021_W1_Coding Convention.docx, Page 1 of 3, Printed on 24-Okt-12

Version - 0 C++ Language 1. A CSU is represented as a class : a. X.h Header file, definition of the class containing attributes and methods b. X.cc Body of the class, code and variable associated with X.h c. mX.c a driver to test X.h and X.C 2. Exception : Body of the templates must be written in header file 3. a Class must be well identified as ADT, Machine or Process 4. Guidelines for writing a header file : see C language a. Must not contain variable b. Must have a macro to prevent double inclusion, with #ifndef _X_H #define _X_H #endif Inclusion is only allowed for header file only ( *.h) A constant that are not defined must be initialized with constructor initialization list. Be careful to define a static data or function member You have to implement Lima Sekawan or at least GoF (Gang Of Four, empat sekawan) correctly especially if you define dynamic type attribute, with following prototype Class XC { Public: /* Prototype of gang of Four */ XC(); XC( ...); XC(const XC&); XC& operator=(const XC&); ~XC(); /* operator + overloading */ friend XC operator+ (XC X1, XC X2); 5. 6. 7. 8. // input output friend stream& operator<<(stream& , const XC*); friend stream& operator >>(stream& , XC&); }; 9. Inheritance a. Define and Test all inheritance tree (base class and all descendant) b. Design the Mode as needed : private, protected, private c. Use polymorhism properly, write virtual when needed. d. Write virtual destructor 10. You have to know your role : a. Class developer (provider) : specification (writing X.h) and implementation (writing X.c) b. Class user : given X.h, using it as specified withput coding.

IL& BY, IF5021_W1_Coding Convention.docx, Page 2 of 3, Printed on 24-Okt-12

Version - 0

Quiz and Exam 1. Please read thoroughly the given specification. You must not change type and prototype definition. 2. Please be careful about function and procedure a. A function must return a value. Call to a function has a value as a result b. A procedure does not return value, but it changes states variables or communicating by parameters. Call a procedure by its name 3. Getter and setter communicated to user shall not be a pointer. 4. Write properly definition of NULL object. 5. Write definition of NULL (Empty) and FULL for a collection of object.

Reference Please read and refer to : http://users.ece.cmu.edu/~eno/coding/CCodingStandard.html http://gcc.gnu.org/wiki/CppConventions http://www.gnu.org/prep/standards/ http://www.oracle.com/technetwork/java/codeconv-138413.html http://code.google.com/p/google-styleguide/ http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml http://msdn.microsoft.com/en-us/library/aa260844%28v=vs.60%29.aspx http://en.wikipedia.org/wiki/Best_Coding_Practices http://en.wikipedia.org/wiki/Code_smell http://www.dcs.gla.ac.uk/~hcp/psd/lectures/badsmells.pdf We will simplify the coding rules in our class, but please notify that there are many standards that you can refer. Please search in the Internet with the following keywords : Coding Standard Coding Standard C Coding Standard C++ Coding Standard JAVA

IL& BY, IF5021_W1_Coding Convention.docx, Page 3 of 3, Printed on 24-Okt-12

You might also like