The document discusses several C programming standards and best practices including:
1. C17 is the latest C programming language standard published in 2018 that specifies the representation, syntax, semantics, and restrictions/limits of C programs.
2. Guidelines for naming conventions recommend using verbs for function names, nouns for structs, and common suffixes/prefixes to indicate meaning.
3. Formatting recommendations include brace placement, spacing around parentheses with keywords/functions, limiting line length, and commenting closing braces and switch statements.
2. C17
โข C17 is the informal name for ISO/IEC 9899:2018, the most recent
standard for the C programming language, prepared in 2017 and
published in June 2018.
โข It specifies the following:
1. the representation of C programs;
2. the syntax and constraints of the C language;
3. the semantic rules for interpreting C programs;
4. the representation of input data to be processed by C programs;
5. the representation of output data produced by C programs;
6. the restrictions and limits imposed by a conforming implementation of C.
3. NAMES
โข Names are the heart of programming.
โข Only a programmer who understands the system as a whole can create
a name that "fits" with the system.
โข If the name is appropriate everything fits together naturally,
relationships are clear, meaning is derivable, and reasoning from
common human expectations works as expected.
4. โข Usually every function performs an action, so the name should make
clear what it does.
โข Structs are often nouns. By making function names verbs and following
other naming conventions programs can be read more naturally.
โข Suffixes are sometimes useful:
โข max - to mean the maximum value something can have.
โข cnt - the current count of a running count variable.
โข key - key value.
โข Prefixes are sometimes useful:
โข is - to ask a question about something. Whenever someone sees Is they will know
it's a question.
โข get - get a value.
โข set - set a value.
5. FORMATTING
โข BRACES : 1. A recommended brace placement stratergy:
if (condition) { while (condition) {
... ...
} }
2. Add Comments to Closing Braces.
3. Consider Screen Size Limits.
โข PARENS () WITH KEY WORDS AND FUNCTIONS POLICY :
1. Do not put parens next to keywords. Put a space between.
2. Do put parens next to function names.
3. Do not use parens in return statements when it's not necessary.
6. โข A LINE SHOULD NOT EXCEED 78 CHARACTERS.
โข IF THEN ELSE FORMATTING : It's up to the programmer. Different
bracing styles will yield slightly different looks. One common approach is:
if (condition) {
} else if (condition) {
} else {
}
โข SWITCH FORMATTING
โข Falling through a case statement into the next case statement shall be
permitted as long as a comment is included.
โข The default case should always be present and trigger an error if it
should not be reached, yet is reached.
7. DOCUMENTATION
โข COMMENTS : Expect your comments to be extracted by a robot and
formed into a man page. Class comments are one part of the story, method
signature comments are another part of the story, method arguments another
part, and method implementation yet another part.
โข DOCUMENT DECISIONS: Comments should document decisions. At
every point where you had a choice of what to do place a comment
describing which choice you made and why.
โข MAKE GOTCHAS EXPLICIT: Explicitly comment variables changed
out of the normal control flow or other code likely to break during
maintenance. Embedded keywords are used to point out issues and potential
problems.
โข COMMENTING FUNCTION DECLARATIONS: Functions headers
should be in the file where they are declared.
8. LAYERING
โข Layering is the primary technique for reducing complexity in a
system.
โข A system should be divided into layers. Layers should communicate
between adjacent layers using well defined interfaces.
โข When a layer uses a non-adjacent layer then a layering violation has
occurred.
โข A layering violation simply means we have dependency between
layers that is not controlled by a well defined interface.
โข When one of the layers changes code could break. We don't want code
to break so we want layers to work only with other adjacent layers.