Difference between revisions of "C programming/House of Technology C coding policy"
From Teknologisk videncenter
m (→Indents) |
m |
||
Line 1: | Line 1: | ||
{{In progress}} | {{In progress}} | ||
− | =Variable names= | + | =Naming= |
+ | ==Variable names== | ||
*All variable names should be in lower case. | *All variable names should be in lower case. | ||
*All variable names should describe purpose and content. | *All variable names should describe purpose and content. | ||
Line 7: | Line 8: | ||
***If you abbreviate and use '''tim0_inst_cnt''' remember to list the abbreviations in the Abbreviation Table. | ***If you abbreviate and use '''tim0_inst_cnt''' remember to list the abbreviations in the Abbreviation Table. | ||
− | =Function names= | + | ==Function names== |
*All function names should be in lower case. | *All function names should be in lower case. | ||
*All function names should describe purpose and function. | *All function names should describe purpose and function. | ||
Line 31: | Line 32: | ||
− | =Abbreviations= | + | ==Abbreviations== |
If abbreviations are used - use logical abbreviations and make a abbreviation table | If abbreviations are used - use logical abbreviations and make a abbreviation table | ||
<source lang=text> | <source lang=text> | ||
Line 44: | Line 45: | ||
*/ | */ | ||
</source> | </source> | ||
− | =Indents= | + | =Programming style= |
+ | ==Indents== | ||
Use K&R or Allman indents. But do it consistently. | Use K&R or Allman indents. But do it consistently. | ||
===K&R example=== | ===K&R example=== | ||
Line 68: | Line 70: | ||
bar(); | bar(); | ||
} | } | ||
+ | </source> | ||
+ | =Documenting= | ||
+ | ==Module headers== | ||
+ | ===.c files=== | ||
+ | <source lang=c> | ||
+ | /************************************************************************** | ||
+ | # # | ||
+ | ## ## ###### ##### #### ## # # ##### ###### #### | ||
+ | # # # # # # # # # # # ## # # # # # | ||
+ | # # # ##### # # # # # # # # # ##### # | ||
+ | # # # ##### # ###### # # # # # # | ||
+ | # # # # # # # # # # ## # # # # | ||
+ | # # ###### # # #### # # # # # ###### #### | ||
+ | |||
+ | *************************************************************************** | ||
+ | Author..: name <mail@address> | ||
+ | Company.: House of Technology at Mercantec ( http://www.mercantec.dk ) | ||
+ | date....: 2012 Nov. 13 | ||
+ | *************************************************************************** | ||
+ | Abstract: Brief description | ||
+ | Purpose.: To be used for fun to challenge our students making multithreaded | ||
+ | solutions to break the codes. | ||
+ | *************************************************************************** | ||
+ | Cavets..: Real lousy solution generating random numbers with rand() | ||
+ | Real lousy solution solving missing hits in sub rr. missing | ||
+ | randomness. | ||
+ | This program was written in C after years of no C-coding, so | ||
+ | the generel structure is quite messy. | ||
+ | It's purpose however is to challenge students so perhaps they | ||
+ | will improve it. | ||
+ | *************************************************************************** | ||
+ | Modification log: | ||
+ | *************************************************************************** | ||
+ | License: Free open software but WITHOUT ANY WARRANTY. | ||
+ | Terms..: see http://www.gnu.org/licenses | ||
+ | ***************************************************************************/ | ||
</source> | </source> | ||
[[Category:C]] | [[Category:C]] |
Revision as of 06:58, 7 June 2012
Naming
Variable names
- All variable names should be in lower case.
- All variable names should describe purpose and content.
- Use _ (underscore) as space.
- Use int timer0_instance_counter instead of int timer0InstanceCounter
- If you abbreviate and use tim0_inst_cnt remember to list the abbreviations in the Abbreviation Table.
- Use int timer0_instance_counter instead of int timer0InstanceCounter
Function names
- All function names should be in lower case.
- All function names should describe purpose and function.
- Use _ (underscore) as space.
- Use galaxy_soarsystem_planet approach instead of planet_solarsystem_galaxy
best approach | dont use |
---|---|
timer0_start() | start_timer0() |
timer0_stop() | stop_timer0() |
timer0_init() | init_timer0() |
timer1_start() | start_timer1() |
timer1_stop() | stop_timer1() |
timer1_init() | init_timer1() |
Abbreviations
If abbreviations are used - use logical abbreviations and make a abbreviation table
/*Abbreviation Table
disp == display
dsp == digital signal processor
tim == timer
inst == instance
cnt == counter
...
*/
Programming style
Indents
Use K&R or Allman indents. But do it consistently.
K&R example
if (x == y) {
x++;
foo();
} else {
x--;
bar();
}
Allman
if (x == y)
{
x++;
foo();
}
else
{
x--;
bar();
}
Documenting
Module headers
.c files
/**************************************************************************
# #
## ## ###### ##### #### ## # # ##### ###### ####
# # # # # # # # # # # ## # # # # #
# # # ##### # # # # # # # # # ##### #
# # # ##### # ###### # # # # # #
# # # # # # # # # # ## # # # #
# # ###### # # #### # # # # # ###### ####
***************************************************************************
Author..: name <mail@address>
Company.: House of Technology at Mercantec ( http://www.mercantec.dk )
date....: 2012 Nov. 13
***************************************************************************
Abstract: Brief description
Purpose.: To be used for fun to challenge our students making multithreaded
solutions to break the codes.
***************************************************************************
Cavets..: Real lousy solution generating random numbers with rand()
Real lousy solution solving missing hits in sub rr. missing
randomness.
This program was written in C after years of no C-coding, so
the generel structure is quite messy.
It's purpose however is to challenge students so perhaps they
will improve it.
***************************************************************************
Modification log:
***************************************************************************
License: Free open software but WITHOUT ANY WARRANTY.
Terms..: see http://www.gnu.org/licenses
***************************************************************************/