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

1 / 12
About This Presentation
Title:

CS242N Lecture 4 Adam Mitz mitzcse.wustl.edu Dept. of Computer Science and Engineering Washington Un

Description:

Comment for readability, for others and yourself (you won't remember what you were thinking) ... Don't comment obvious ideas that your code already conveys. ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

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


1
CS242N Lecture 4Adam Mitz (mitz_at_cse.wustl.edu)
Dept. of Computer Science and EngineeringWashingt
on University in St. Louis
  • Style
  • Tips for Good Code
  • 242 Conclusions

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
  • You may think you know what the execution time
    will be, but you dont!
  • 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 (esp. for templates)
  • 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 using
    references in many places where you may be
    tempted to use pointers.
  • new returns a pointe, 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
    (with const as appropriate)

6
Style for Memory Management
  • If a function calls new, clearly indicate in
    comments if the client must call delete (or
    some cleanup function that does) 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
  • Comments having too few or too many is bad.
    Dont comment obvious ideas that your code
    already conveys.

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
  • C requires you to think at a high level
    (classes, templates) and at a low level
    (pointers, bit manipulation) at the same time.
  • C requires attention to detail in syntax, and
    thinking about how your program will behave at
    runtime.

11
Course Info
  • Email me questions any time
  • Unix command to send in your homework
  • cd 242 tar czvf - uuencode 242.tgz.uue
    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 (cmg_at_cse.wustl.edu)
Write a Comment
User Comments (0)
About PowerShow.com