CS242N Lecture 3 Adam Mitz mitzcse'wustl'edu Dept' of Computer Science and Engineering Washington Un - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CS242N Lecture 3 Adam Mitz mitzcse'wustl'edu Dept' of Computer Science and Engineering Washington Un

Description:

Tips for Easily Readable and Debugable Code. Style Goals ... Avoid the tendency to use code 'tricks' it will make the code less readable ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 13
Provided by: adam3
Category:

less

Transcript and Presenter's Notes

Title: CS242N Lecture 3 Adam Mitz mitzcse'wustl'edu Dept' of Computer Science and Engineering Washington Un


1
CS242N Lecture 3Adam Mitz (mitz_at_cse.wustl.edu)
Dept. of Computer Science and EngineeringWashingt
on University in St. Louis
  • Style
  • Tips for Easily Readable and Debugable Code

2
Style Goals
  • Optimize for programmer time, not execution time.
  • Easy to read (for you and others)
  • Easy to debug
  • Easy to re-use code in future projects
  • Consistency among programmers
  • Algorithms look like their pseudocode
  • Make it easy to write correct syntax the first
    time through the compiler

3
General Suggestions 1
  • Heed compiler warnings
  • Use encapsulation and good object-oriented design
    (methods do only what their name implies)
  • Verify newly written procedures
  • Step through with debugger, checking sanity of
    variable values and code coverage
  • Write a testing routine
  • Use representation invariants and asserts
  • Avoid globals and nonmember statics

4
General Suggestions 2
  • Comment for readability, for others and yourself
    (you wont remember what you were thinking)
  • Avoid pointer operations and new/delete when
    possible
  • Always check array index bounds in DEBUG code
  • Use each variable for exactly one purpose.
  • Use a consistent layout and whitespace scheme

5
Avoiding Pointers
  • Object oriented programs can be written without
    pointers (see Java)
  • new returns a pointer so you have to use them
    once in a while, but you can convert the result
    from new into a reference with the operator
  • Variables that may be NULL must be pointers
  • Use an array class (template) instead of a native
    dynamic array
  • Pass parameters and return values by reference

6
Style for Memory Management
  • If a function besides a copy constructor or
    assignment operator calls new, clearly indicate
    in comments if the calling function must call
    delete to avoid a leak.
  • Example
  • Matrix mult(Matrix m1, Matrix m2)
  • // mult creates a new matrix, calling function
    must delete

7
Textual Formatting
  • Use white space to aid readability
  • Between expressions and operands
  • Blank lines between statement groups
  • Split up complicated expressions into multiple
    statements (temporary const variable)
  • Use descriptive identifier names for local
    variables, functions, classes, methods, etc

8
Modified Hungarian Notation for Variable Names
  • Scope Prefixes
  • m_ class member
  • g_ global
  • s_ static
  • in_ input to function
  • out_ output parameter
  • inout_ modified parameter
  • (none) local

9
Hungarian Continued
  • Type Prefixes
  • i int, c char, f float, d double
  • p pointer, a array, o other
  • Format
  • ltscopegt_lttypegtlttypegtName
  • Examples
  • int m_aiIndicies10
  • draw(const Point in_ptStart, )
  • void length(double out_dLength)
  • double g_dEpsilon 1e-6

10
Conclusions
  • Avoid the tendency to use code tricks it will
    make the code less readable
  • Non-obvious syntax should be commented
  • Use the debugger and assert
  • Write testing functions that call all members
    especially for templates

11
Course Info
  • Open lab time until 4 PM
  • My office hours 4-530 M, W
  • Email me questions any time
  • Unix command to send in your homework cd
    ltdirectorygt tar czvf 242.tar.gz uuencode
    242.tar.gz 242.tar.gz Mail s 242
    mitz_at_cse.wustl.edu
  • Do so by a week from today
  • Evaluations will be placed in your pendaflexes at
    the end of the semester

12
Credits
  • CS242N was developed by
  • Prof. Bill Smart (wds_at_cse.wustl.edu)
  • Prof. Cindy Grimm (grimm_at_cse.wustl.edu)
Write a Comment
User Comments (0)
About PowerShow.com