English (United Kingdom)French (Fr)
Resources Programming rules Programming rules for C

Poll of the month

Have you measured your software projects statistics?
 
Programming rules for C
Resources

This page gives a description of part of the C programming rules verified by the IBM Rational Logiscope tool.

Presentation Rules

CodePres_1_DeclarationPerLineEach line must contain no more than one declaration.
CodePres_2_NumberStatementsThe number of statements shall not exceed 100 in a function and 1000 in a module.
CodePres_3_FileLengthA file shall not exceed 2000 lines.
CodePres_4_StatementSwitchThe number of first level statements in each clause of a switch statement shall not exceed 10.
CodePres_5_StatementSwitchThe total number of statements in each clause of a switch statement shall not exceed 25 (all levels included).
CodePres_6_CommentStatementLineA comment must be on a line without any statement. The exception concerns a comment written on a single line after a statement.
CodePres_7_ExtensionHeaderIncluded files have the extension .h. 
CodePres_8_EnumBooleanSystematically define a Boolean enumerated type containing two values: true and false.
CodePres_9_ParamFunctionThe number of parameters of a function is limited to 7.
CodePres_10_StatementPerLineNo more than one basic statement per line.
CodePres_11_ControlStructureA control structure (do, while, for, if, else, switch, return, break, continue) shall start on a new line.
CodePres_12_BlankLineFunction definition/declaration and function body must be separated by a blank line.
CodePres_13_BraceEach brace (opening and closing) must be placed alone on a line.
CodePres_14_CommentDeclarationEach declaration (type, variable, enumeration item, structure field) is commented.
CodePres_15_PointerDeclarationIn the declaration of a pointer to a data type, the * character shall be stuck to the pointer’s identifier.
CodePres_16_SpacingRefThere shall be no blank before or after the . and -> operators.
CodePres_17_SpacingOperatorOperators ++, -, & (functionAddress), * (functionRef) shall be stuck to their operand.
CodePres_18_SpacingParameterDo not insert a blank after the opening parenthesis or before the closing one.
CodePres_19_LineLengthA line in a source file shall not exceed 80 characters.
CodePres_25_SingleLineCommentComments shall be one line long.
CodePres_26_CommentDefinitionAll the definitions got a comment.
CodePres_28_Definitions A module’s “.c” body file must contain the “in public” definitions of the exported functions, and the “in public” definitions of the exported variables.
CodePres_29_SpacingUnaryOperatorUnary operators ! and ~ must be stuck to their operand to avoid confusion with binary operators.
CodePres_30_DefineThe #define preprocessing directives shall be grouped altogether.

Rules protecting from the dangers of the C language

Complexity_13_SimpleTestStatements like x == y ; or x != y ; shall not be used.
ControlFlow_1_NoDeadCodeThere shall be no dead code, especially after goto and return statements.
ControlFlow_4_ThenElseThe then and else parts of if statements shall not be void.
ControlFlow_8_BreakPathInSwitchBreak is mandatory for case clauses containing statements.

 Règles limitant la complexité du code

Complexity_1_MultipleAssignmentPas d'affectations en cascade
Complexity_2_NoTernaryOpPas d'utilisation de l'opérateur ternaire "?"
Complexity_3_NoUnary+Pas d'utilisation de l'opérateur unaire "+"
Complexity_4_NoAssignmentOpPas d'utilisation des opérateurs d'affectation autres que "=" comme "*=", "/=", "+="...
Complexity_5_CallResultUtiliser la valeur de retour des fonctions
Complexity_6_++--OperatorsN'utiliser "++" et "--" que dans les cas simples.
Complexity_7_NoCastPas de cast.
Complexity_8_NoMultipleInitPas d'initialisation dans des déclarations multiples.
Complexity_9_MacroPas de macros avec plusieurs instructions.
Complexity_10_FieldAddressingPour accéder à un élément de structure pointée, utiliser la notation "Ptr->Field" au lieu de "(*Ptr).Field"
Complexity_11_NoCommaAndTernaryNe pas utiliser les opérateurs ',' et '?'.
Complexity_12_OperatorInConditionUne condition aved plus de 4 opérateurs logiques ne doit pas contenir des opérateurs différents.
Complexity_13_SimpleTestLes instructions du type x == y; et x != y; sont interdites
Complexity_14_InclusionLevelLes inclusions de fichiers sont interdites dans les fichiers header.
Complexity_15_SizeofL'opérande du l'opérateur "sizeof" doit être entre parenthèses.

Règles permettant d'améliorer la structuration du code.

ControlFlow_10_SwitchBetterThanIfUtiliser un switch plutôt que des tests en cascade.
ControlFlow_11_OneBreakContinueUn seul "break" ou "continue" autorisé dans le corps d'une  boucle do, while ou for.
ControlFlow_1_NoDeadCodePas de code après goto", "return", "break" ou "continue".
ControlFlow_2_FunctionReturnUn seul return autorisé par fonction.
ControlFlow_3_NoGotoPas de goto.
ControlFlow_4_ThenElseLes parties "then" et "else" d'une structure "if" ne doivent pas être vides.
ControlFlow_5_NoBreakContinue"break" et "continue" interdits dans le corps d'une  boucle do, while ou for.
ControlFlow_6_DefaultInSwitchLa clause "default" est obligatoire dans les switchs.
ControlFlow_7_BreakInSwitchLe "break" est obligatoire comme dernière instruction de chaque clause d'un switch.
ControlFlow_8_BreakPathInSwitchToutes les branches des clauses d'un switch doivent se terminer par un break.
ControlFlow_9_ControlStructureNestingPas plus de 6 structures de contrôles imbriquées.

Portability Rules

Portability_1_C++KeywordsKeywords from C++ language (class, new, friend...) shall not be
used.
Portability_2_NoDollarThe ‘$’ character shall not be used in an identifier.
Portability_4_CharIdentifierUse only letters, numbers and the underscore character in identifier names.
Portability_5_NoSignedRightShiftThe right shift operator >> shall not be used on signed integer.
Portability_6_MainNamingOnly the exit function shall be used to go out from main.
Portability_7_NoRecursiveHeaderHeader files shall not include themselves recursively.
Portability_9U_AbsolutePathIncludeFile names in #include directives must be in the same case than the file name and shall not contain any absolute path.
Portability_12U_FilenameLengthFile names shall be lower-case and shall not exceed 8 characters for the name and 3 characters for the extension.
Portability_13_NoTabTabulations shall not be used in source files.

 Rules making maintenance easier

Resource_1_AccessArrayA pointer shall be used to run through successive elements of an
array rather than an index.
Resource_2_ForCounterLoop counters and control variables shall not be modified within the body of a for statement.
Resource_3_DeclarationInitSeparateDeclaration and initialisation of a variable shall be separate.
Resource_4_DeclarationInitCombineDeclaration and initialisation of a variable shall be done at the same time, if possible.
Resource_5_LocalDeclarationDeclaration of local variables in an instruction block shall not be
used.
Resource_6_GlobalDeclarationGlobal objects shall be declared in an inclusion file.
Resource_7_VariableUseDeclared variables shall be used.
Resource_8_FunctionUseDeclared functions shall be used.
Resource_9_ParameterUseFunction parameters shall be used.
Resource_10_NoGlobalParameterA global variable shall not be used as a parameter.
Resource_11_InputParameterA function’s input parameter shall be either a pointer to const, or
passed by value.
Resource_12_NoExternBodyThe keyword extern shall not be used in a.c file.
Resource_13_NoStaticInFuncThe keyword static shall not be used in the body of a function.
Resource_14_ExternHeaderDeclarations of variables in an header file shall be preceded by
extern.
Resource_15_NoFunctionHeaderFunctions (other than macros) shall not be defined in a header file.
Resource_16_FileExtensionThe header file shall have the extension .h and the body file the
extension .c.
Resource_18_NoBodyInclusionA .c file shall not be included in another .c file.
Resource_19_NoBitfieldBitfields shall not be used.
Resource_20_NoAutoDeclaration of variables local to a function shall never be made with "auto".
Resource_21_ArrayInitInitialization of an array shall conform to its structure.
Resource_22_PointerInitA pointer shall always be initialized. If it points to no known variable, it shall be initialized to NULL.
Resource_23_WhileInitThe initial value of a parameter of a while loop shall be known before entering the loop.
Resource_24_ConstVolatileInitOnly const and volatile variables to a function shall be initialized
when they are defined.
Resource_26_TypedefUnionStructA typedef shall not be used to mask structures or unions.
Resource_30_EnumInitThe initialization of enumeration fields shall not be explicit.
Resource_31_StructUnionUsing the union type shall be limited to declaring partially variable
types.
Resource_32_ForSpecificationAll parts of a for statement shall be filled.

 

 

Kalimetrix IBM Business Partner

In june, 2009, Kalimetrix became ISV (Independant Software Vendor), approved by IBM to sell the Rational product line for software development.

Tax Credit for Research

Kalimetrix has just received a government agreement concerning the French Tax Credit for Research disposition. This enables French companies to benefit from fiscal reductions when they entrust Kalimetrix with research and development related work.

 Please note that this applies only to French based companies.