Title: Error Prevention: Coding Standards
1Error Prevention Coding Standards
- Why Coding Standards?
- Coding Standard Examples
2Agenda
- Announcements
- CoC Computer Accounts
- Notetaker
- Reminder
- Lab 1 on Friday
- Lab 1 Due Thursday, January 25 _at_ 1100 PM
- Lecture Coding Standards
3What are Coding Standards?
- Rules of the road
- Write for three separate audiences
- Compiler
- Yourself
- Others
- Team members
- Project manager
- Other programmers re-using your code
- Rules of discourse
- Compiler
- Unforgiving about rules
- Doesnt care about readability
- Humans
- Inferences/assumptions allow flexibility with
rules - Readability is critical
4Example
- Pathological Example
- X X - XX
- XXX Aretha SalesTax( Aretha )
- X X LateFee( X1, X ) XXX
- X X Interest( X1, X )
- Correct Code Fragment
- Balance Balance - LastPayment
- MonthlyTotal NewPurchases SalesTax(
NewPurchases ) - Balance Balance LateFee( CustomerID,
Balance ) MonthlyTotal - Balance Balance Interest( CustomerID,
Balance )
5Example
- Pathological Example
- The elements in positions FirstElmt
- through SortBoundary-1 are always sorted.
- for SortBoundary FirstElmt1 do
- LastElmt do begin InsertVal Data
- SortBoundary InsertPos SortBoundary
- while InsertVal lt Data InsertPos-1 do
- begin Data InsertPos Data InsertPos-
- 1 InsertPos InsertPos-1 end Data
- InsertPos InsertVal end
- Corrected Code Fragment
- The elements in positions FirstElmt
- through SortBoundary-1 are always sorted.
- for SortBoundary FirstElmt1 to LastElmt do
- begin
- InsertVal Data SortBoundary
- InsertPos SortBoundary
- while InsertVal lt Data InsertPos-1 do
- begin
- Data InsertPos Data InsertPos-1
- InsertPot InsertPos-1
- end
- Data InsertPos InsertVal
- end
-
6Why Bother?
Who cares what a program looks like if it
works? Doesnt it take too much time to make it
look pretty? Arent the rules arbitrary
anyway? Kernighan Pike
- Easier to read and understand
- Easier to modify
- Almost always has fewer errors
- Sloppy code is bad code
- Likely to be smaller than unpolished code
- Avoid past mistakes
- Learning efforts for new project are reduced
- Good style becomes habit
- Becomes discipline leads to code that is more
likely to be correct - Empirical evidence
- Programmers expect conventions to be followed
- If not followed, productivity drops significantly
- Soloway Ehrlich
7Principles
- Based on common sense guided by experience
- Code should be clear and simple
- Straightforward logic
- Natural expression
- Conventional language use
- Meaningful names
- Neat formatting
- Helpful Comments
- Avoid clever tricks and unusual constructions
- Consistency is important
8So whats a programmer to do?
- Rule 1
- Always write programs so that others can read
them without having to know the excruciating
details of what you did or why you did them. - Rule 2
- Always write programs that are structured.
- Rule 3
- Always write programs that contains cues to
remind you and others about what is going on.
9Guidelines Conventions
- Naming
- Guidelines
- Informative
- Concise
- Pronounceable
- Conventions
- Use descriptive names for globals, short names
for locals - Be consistent accurate
- Use active function names
- Expressions
- Guidelines
- Use natural form for expressions
- Use parentheses to reduce ambiguity
- Break up complex expressions
- Be clear
- Be careful with side effects
10Guidelines Conventions (cont.)
- Layout
- Indent to show structure
- Use consistent indentation brace style
- Use idioms for consistency
- Idiom Examples
-
- i 0
- while (i lt n-1)
- Arrayi 1.0
- for (i 0 iltn)
- arrayi 1.0
- for (i0 iltn i)
- arrayi 1.0
11Comments, Comments, Comments
- Kinds of Comments
- Repeat of the code
- Explanation of the code
- Marker in the code
- Summary of the code
- Description of the codes intent
- Guidelines
- Dont belabor the obvious
- Comments functions and global data
- Dont comment bad code, rewrite it
- Dont contradict the code
12The Last Word...
It is an old observation that the best writers
sometimes disregard the rules of rhetoric. When
they do, however, the reader will usually find in
the sentence some compensating merit, attained at
the cost of the violation. Unless he is certain
of doing as well, he will probably do best to
follow the rules. Strunk White
- Good style is crucial to good programming