02 Coding Conventions
02 Coding Conventions
02 Coding Conventions
A Local Standard
Motivation
You have a team working on a project that will extend over a moderately long period of time. Over the course of the project, people will come and go. The project needs to progress as fast as possible
You want to implement only what you really need Key: reuse as much legacy code as possible New functionality will be built as the need for it emerges.
3
Style of Working
Teams will be looking at many modules as they craft simple enhancements to make the system do what is needed.
They will read many modules, and edit many. They will factor out duplicate code and move it to a single place in the system. Over time, few modules will be able to be said to have an author.
4
Benefits
Projects benefit from having strong Coding Conventions/Standards because...
People can stop reformatting code and renaming variables and methods whenever working on code written by other people. It's slightly easier to understand code that is consistently formatted and uses a consistent naming standard. It's easier to integrate modules that use a common consistent naming standard -- less need to look up and cross-reference the different names that refer to the same thing.
6
CIS*3430 & 3200 do have team projects Practice adapting to local standards
Coop term, later jobs, mandated by company No place for hotshot sailing in and insisting on doing everything his/her own way better self-employed
8
Organization of Program
Analogy: Organize programs for readability, as you would organize a book:
Title page & Table of contents File header Chapter Module (function or logical group of functions) Paragraph Block of code Index & Glossary can be generated automatically if comments are used wisely (Javadoc, doxygen) Cross references ctags.sourceforge.net free tool
11
Organization of Modules
Apply comp. sci. principle of information hiding
Hide details of of implementation that users dont need to know
File Headers
Tells at a glance what file youre looking at and whats in there. Filename
Suggests the purpose of the files module(s) Could be class name (Java/C++)
Description
This is the main reason to have the header. It clearly tells the purpose of the module(s), their role in the system. If you can't describe the purpose in one or two sentences, youve thrown unrelated modules together.
14
File Headers
Creation date
Provides a creation timestamp for copyright purposes, but it does more than that. It provides a quick clue to the context that existed at the time the module was created. Not as accurate as source control, but quick and maintenance free.
File Headers
E-mail address
If this is publicly released code, then the author should be contactable.
Function Headers
Commenting interfaces is an especially good thing to do.
Function headers describe purpose of function and use of arguments, return value, also pre-/post-conditions.
public interface function prototypes go in .h files internal helper functions in .c files (unless called from multiple .c files)
Meaningful Comments
Comments provide meta information about the program, reasons for choosing this algorithm or implementation, known issues, hints for future readers (including yourself). Meaningful in this context has two parts:
The comment can be understood by readers. The comment says something that is likely not to be understood by the same readers unless it was present. Dont parrot the code: x = 2 + y; // add 2 to y
Psychological Factors
Redundancy
Cxn yxx rxxd thxs sxntxnce? Cn y rd ths sntnce? More effort is required as redundancy is removed. Something to consider when creating variable names and writing comments.
19
Variable Names
Use simple, descriptive variable names. Good names can be created by using one word or putting multiple words together joined by underscores or caps
prefer usual English word order #define MAX_FIELD 127 int numStudents, studentID; char *homeAddr;
20
Variable Names
Be careful of lower-case L (l) or upper-case O in variable or constant names int l, O; l = O + l/0.1; //bad int length; FILE *outfile; //OK #define KG_PER_TON 907.18474; Do not use names of existing library functions or constants multiply-defined externals or worse
do not use the names argc, argv for any other purpose except command line argument manipulation
21
Variable Names
Avoid variable names that differ by only one or two characters. Short names such as x, n, i are acceptable when their meaning is clear and a longer name would not add any more information. Trade off: long, unabbreviated names statements become too long, hard to follow
studentIdentificationNumber, arraySubscript
22
Variable Names
Follow every variable declaration with a comment that defines it. Group similar variables together.
use similar names for variables that perform similar functions
23
Statement Style
Put each statement on a line by itself. Avoid very long statements. Use two shorter statements instead. Keep all lines to 80 characters or less. Group statements in logical chunks separated with white space (blank lines)
Helps eye follow logic without getting overwhelmed
24
Psychology of Chunking
Recognizable pieces of information that can be fitted into one slot of (human) short term memory. Seven plus or minus two.
25
Ugly Code
How about this?
if (WndHt < WIN_MIN) { ResetWin(WPtr ); while( WndHt> WinHt) { WinHt =getWindow( pWin ); if ( WinHt== ( winhite * WND_CORR )) { stepup (wdwhght ); STEPUP( wndwh); } } }
Align Consistently!
if ( WndHt < WIN_MIN ) { ResetWin(WPtr); while ( WndHt > WinHt ) { WinHt = getWindow(pWin); if ( WinHt == (winhite * WND_CORR) ) { stepup (wdwhght); STEPUP(wndwh); } } }
27
28
{{{Nested Blocks}}}
Increases apparent complexity non-linearly. 3 levels is enough for major nesting. Ways to reduce nesting (goto-less jumps):
function return from nested code loops:
continue skips to end-of-loop test break exits off bottom of loop
Be Explicit
SWYM - Say What You Mean if ( WordCount ) vs. if ( WordCount != 0 ) n+3*x-5/y vs. n + ((3*x)-5)/y
30