c/c++

Min checkliste for “best practice” og reviewing c/c++ kode.

Det meste piv stjålet fra John T. Baldwin – http://www.literateprogramming.com/Baldwin-inspect.pdf eller “C++ Coding Standards” af Herb Sutter

Arrays

[showhide type=”p0″ more_text=”Undgå at bruge array – Brug vector eller string i stedet” ]

Brugen af vector eller string hjælper med, at lave mere sikker og fleksibel kode.

[/showhide]

[showhide type=”p1″ more_text=”Brug altid en ‘define’ konstant til dimensionering” ]

int intarray[13];

should be

int intarray[TOT_MONTHS+1];

[/showhide]

[showhide type=”p2″ more_text=”Læg altid 1 til ‘last entry’ ved definition af arrayet” ]

char entry[TOTAL_ENTRIES];   should be   char entry[LAST_ENTRY+1];

The first example is extremely error-prone and often gives rise to off-by-one errors in the code. The preferred (second) method permits the writer to use the LAST_ENTRY identifier to refer to the last item in the array. Instances which require a buffer of a certain size are rarely rendered invalid by this practice, which results in the buffer being one element bigger than absolutely necessary.

[/showhide]

Constants

[showhide type=”p3″ more_text=”Does the value of the variable never change?”]

auto months_in_year = 12;

should be

const auto months_in_year = 12;

[/showhide]

[showhide type=”p4″ more_text=”Are constants declared with the preprocessor #define mechanism? “]

#define MAX_FILES 20

should be

const auto MAX_FILES = 20;

[/showhide]

Variables

[showhide type=”p5″ more_text=”Minimer global og delt data” ]

Delt data giver bindinger mellem kode, hvilket hurtigt giver problemer med debuging, og vedligeholdelse. Specielt et problem hvor flere tråde har access til delt data.

[/showhide]