Title: Programming Style
1Programming Style
- Naming Guidelines
- Indentation Spacing
- Comments
2Agenda
- Administrivia
- Course Information
- Workon Woes
- Lab 1 Turnin
- Lecture
- Programming Style
3Programming Style Overview
- What is Style?
- How it looks
- Format
- Readability
- Clarity
- Programming conventions (idioms)
- Naming conventions
- Commenting
- Guidelines for consistency
- Common Guidelines
- Indent to show structure
- Comment, comment, comment
- Capitalize constants
- Meaningful variable names
- Write for clarity
- Avoid clever tricks
4Why Does Style Matter?
- Purpose To make code easy to read
- Benefits
- Consistency, consistency, consistency
- Easier to understand
- Easier to change
- Discipline can reduce errors
- Real World Commentary
5Naming Guidelines
- Purpose of Variable/Function Name
- Label object
- Information about its purpose
- General Guidelines
- Informative
- Concise
- Memorable
- Pronounceable
6Naming Code Example
X X XX XXX Aretha SalesTax(Aretha) X
X LateFee(X1, X) XXX X X
Interest(X1, X)
Balance Balance LastPayment MonthlyTotal
NewPurchases SalesTax(NewPurchases) Balance
Balance LateFee(CustomerID, Balance)
MonthlyTotal Balance Balance
Interest(CustomerID, Balance)
Examples from Code Complete
7K P Guidelines
- Use descriptive names for globals, short names
for locals - Conventions p for pointers, Globals, CONSTANTS,
Hungarian Naming Convention - variableName vs. variable_name
- Be consistent
- Use active names for functions
- Be accurate
8Choosing Good Names
- State in words what variable represents
- Optimum name length (10 16)
- Problem space, not solution space
- What not how
- Use opposites precisely
- First/Last vs. First/End
- Can find better name than temp for temporary
variable - Give booleans names that imply True or False
- Use standard abbreviations, but be sure to be
consistent - Avoid variables with
- Similar meaning names
- Different meanings, but similar names
9Indentation Spacing
- Write for clarity
- Make a statements meaning as transparent as
possible - Write the clearest code
- Use spaces for grouping
- Format for readability
- Be consistent
10K P Guidelines
- Indent to show structure
- Use consistent indentation brace style
- Use the natural form for expressions
- Make use of language idioms
- Parenthesize to remove ambiguity
- Break up complex expressions
- Be clear
- Avoid side effects
11Layout Fundamentals
- Good visual layout shows the logical structure
of a program - Objectives of Good Layout
- Consistently represent the logical structure
- Improve readability
- Withstand modifications
for (i 0 i lt MAX_ELEMTS i) LeftElmt Left
i Lefti Righti Righti LeftElmt
Examples from Code Complete
12Layout Techniques
- White Space
- Grouping
- Blank Lines
- Alignment
- Indentation
- Parentheses
- Use more than you think you need
variable 12 4 3 7 / 8
Examples from Code Complete
13Comments
- Purpose To increase understanding
- Point out important details
- Provide context/overview
- Misuse
- Do not state what the code clearly says
- Do not contradict the code
- Do not distract with elaborate formatting
14K P Guidelines
- Dont belabor the obvious
- zerocount / Increment zero entry counter
/ - Add something not immediately evident
- Collect information in once place
- Comment functions and global data
- Dont comment bad code, rewrite it
- If comment is longer than code
- Dont contradict the code
- Clarify, dont confuse
- Clearer code leads to fewer comments