230 likes | 343 Views
Conventions to be used in Modgen models. by Claude Charette Claude.Charette@statcan.ca Workshop for Modgen users May 27th, 2008 www.statcan.ca/english/spsd/Modgen.htm. Goals of usage of conventions. Better understanding of code Team work Cooperation between different teams
E N D
Conventions to be used in Modgen models by Claude Charette Claude.Charette@statcan.ca Workshop for Modgen users May 27th, 2008 www.statcan.ca/english/spsd/Modgen.htm
Goals of usage of conventions • Better understanding of code • Team work • Cooperation between different teams • Bugs waiting to happen
Categories of conventions • Names of Modgen symbols • Names of variables • Code layout • Other layout conventions specific to Modgen • Bugs waiting to happen
Names of Modgen symbols • Principles to follow when choosing conventions: • Must help avoid collisions between names of different symbols • Must help in recognizing kind of symbol • Must not pollute the model’s interface
Names of variables • Local variables, global variables, function arguments
Code layout • Only one instruction per line • Indentation • Using curly brackets • Meaningful names
Code layout (continued) • Only one instruction per line Bad example: voidSimulation(){int nCase;for ( nCase = 0; nCase <CASES() &&!gbInterrupted &&!gbCancelled &&!gbErrors;nCase++ ){StartCase(); CaseSimulation(); SignalCase(); }} Good example: voidSimulation(){ int nCase; for ( nCase = 0; nCase <CASES() &&!gbInterrupted &&!gbCancelled &&!gbErrors;nCase++ ){ StartCase(); CaseSimulation(); SignalCase(); }}
Code layout (continued) • Indentation • Blocs left aligned • Subblocks shifted to the rigth Bad example: classification LANGUE2 {//EN francophoneL2_FRANCO,//EN non francophoneL2_NON_FRANCO}; Good exemple: classification LANGUE2 { //EN francophone L2_FRANCO, //EN non francophone L2_NON_FRANCO};
Code layout (continued) • Using curly brackets • Opening bracket on separate line • Brackets left-aligned with parent block Bad example: classification LANGUE2 {//EN francophoneL2_FRANCO,//EN non francophoneL2_NON_FRANCO}; Good exemple: classification LANGUE2 { //EN francophone L2_FRANCO, //EN non francophone L2_NON_FRANCO};
Code layout (continued) • Using curly brackets Bad example: voidSimulation(){ int nCase;for ( nCase = 0; nCase <CASES() &&!gbInterrupted && !gbCancelled &&!gbErrors;nCase++ ){StartCase(); CaseSimulation(); SignalCase(); }} Good example: voidSimulation() { int nCase;for ( nCase = 0; nCase <CASES() &&!gbInterrupted && !gbCancelled &&!gbErrors;nCase++ ) {StartCase(); CaseSimulation(); SignalCase(); }}
Code layout (continued) • Meaningful names Bad examples: Variables: int nJ; Symbols: classification TYPE2 { UL_VS_MARIE, HU_VS_MARIE };
Layout conventions specific to Modgen • Abbreviations • Choosing a single working language • Two-letter language codes • Initializing variables and states using curly brackets • Meaningful numbering
Layout conventions specific to Modgen (continued) • Abbreviations • Hard to understand for the non-initiated • To be avoided as much as possible • Documentation mandatory Example: ULHU
Layout conventions specific to Modgen (continued) • Choosing a single working language • Easier to translate • Used also for abbreviations Example: vismin • Two-letter language codes • Industry standard • EN for English • FR for French • Meaningful numbering
Bugs waiting to happen • Initializing all variables • Avoiding global variables • Using curly brackets in conditional blocks • Using constants • Using variables for temporary state values in events
Bugs waiting to happen (continued) • Initializing all variables • Value of a non-initialized variable depends on memory state of the system • Can cause bugs that are not reproducible • Always initialize using curly brackets • Not necessary to initialize symbols
Bugs waiting to happen (continued) • Using global variables • Should be avoided • Will cause problem if more than one simulation thread • Using model-generated parameters is prefered
Bugs waiting to happen (continued) • Using curly brackets in conditional blocks • Prevents errors when code added Bad example: if (dAleatoireAnniversaire <= dProbul) etat_mat = EM_UNION_LIBRE; else etat_mat = EM_MARIE; Good example: if (dAleatoireAnniversaire <= dProbul) { etat_mat = EM_UNION_LIBRE; } else { etat_mat = EM_MARIE; }
Bugs waiting to happen (continued) • Using constants • Mostly used for: • Initializing variables or states • Comparisons Example: if (nAge != 111) { nAge = nAge + 1;} • Other options: • Declared constants Example: constint nAgeMax = {111}; • MIN and MAX macros Example: if (nAge !=MAX(AGE_MORTALITE)) { nAge = nAge + 1;}
Bugs waiting to happen (continued) • Temporary state values in events • Weird results in BioBrowser • Using a local variable is preferred