Programming Style - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Programming Style

Description:

Programming Style. 27 January 2000. CS 2331 - Programming Practicum I. 3 ... Use consistent indentation & brace style. Use the natural form for expressions ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 15
Provided by: Alli225
Category:

less

Transcript and Presenter's Notes

Title: Programming Style


1
Programming Style
  • Naming Guidelines
  • Indentation Spacing
  • Comments

2
Agenda
  • Administrivia
  • Course Information
  • Workon Woes
  • Lab 1 Turnin
  • Lecture
  • Programming Style

3
Programming 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

4
Why 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

5
Naming Guidelines
  • Purpose of Variable/Function Name
  • Label object
  • Information about its purpose
  • General Guidelines
  • Informative
  • Concise
  • Memorable
  • Pronounceable

6
Naming 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
7
K 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

8
Choosing 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

9
Indentation 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

10
K 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

11
Layout 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
12
Layout 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
13
Comments
  • 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

14
K 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
Write a Comment
User Comments (0)
About PowerShow.com