Implementation Topics • Describe • Understand role of comments • Learn debugging techniques

advertisement
Implementation Topics
• Describe
– Characteristics of good implementations
– Best practices to achieve them
• Understand role of comments
• Learn debugging techniques
• Analyze refactoring
Introduction
• Implementation: transforming detailed
design into valid program
• Detailed design may be done as part of
implementation
– Faster
– Less cohesive and less organized
• Writing code, unit testing, debugging,
configuration management
Good implementations
•
•
•
•
•
•
•
Readability
Maintainability
Performance
Traceability
Correctness
Completeness
Other issues:
– Relative importance ?
– Tradeoffs ?
Coding guidelines
•
•
•
•
Organization-specific
Important for consistency
Programmers can get used to them easily
Usually mandate:
– Indenting, formatting
– Naming conventions (for files, variables etc)
– Language features to use or avoid
Style issues - I
• Be consistent and highlight meaning
• Naming
– Convey meaning
– Be consistent
– Warning: If you can’t think of a good name
chances are you don’t understand or the
design can be improved
– Multicultural issues
Style issues - II
• Separating words, capitalization
– c_uses_this_style
– JavaUsesThisOne
• Indentation and Spacing
• Function/Method size
– When is it too big ? When to break ?
• File naming
• Error prone constructs
Comments
• Types:
– Repeat of the code
– Explanation of the code
– Marker in the code
– Summary of the code
– Description of the code intent
– External references
• Keep up to date !!
Debugging
• Locating and fixing errors in code.
• Errors noticed by testing, inspection, use.
• Four phases
– Stabilization (reproduction)
– Localization
– Correction
– Verification
Debugging II
• Heuristics:
–
–
–
–
Some routines will have many errors
Routines with an error tend to have more
New code tends to have more error
Particular ones: languages, parts, coders
• Tools
–
–
–
–
–
Code comparators
Extended checkers (lint)
Interactive debuggers
Special libraries
Others: Profilers, pre/post conditions, test coverage
Assertions
• Pre-condition: condition your module
requires in order to work
• Post-condition: condition that should be
true if your module worked
• Assertion: Executable statement that
checks a condition and produces an error
if it is not met
• Assertions supported by many languages
Performance optimization
• Performance tradeoffs
– Readability ?
– Maintainability ?
• Correctness is usually more important
• Profiler: runs a program and calculates
how much time it spends on each part
• Cost-benefit analysis
• Measure before ‘optimizing’
Refactoring
• Improving your code style without affecting
its behavior
Bad Smells
• Duplicated code
• Long method
• Large class
• Switch statement
• Feature envy
• Intimacy
Refactorings
• Extract method
• Substitute
algorithm
• Move method
• Extract class
Download