![Page 1: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/1.jpg)
CS242
![Page 2: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/2.jpg)
Reduce Complexity Introduce an intermediate, understandable
abstraction Avoid code duplication Support subclassing Hide sequences Hide pointer operations Improve portability Simplify complicated boolean tests Improve performance Insure all routines are small?
![Page 3: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/3.jpg)
Isolate complexity Hide implementation details Limit effects of changes Hide global data Make central points of control Facilitate reusable code Accomplish a specific refactoring
![Page 4: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/4.jpg)
Each routine should do ONE thing and do it well
Results Higher reliability Easier debugging Easier maintainability Easier to understand
![Page 5: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/5.jpg)
Describe everything the routine does Avoid meaningless, vague or wishy-washy verbs
Handle, process, output, “deal with” Don’t differentiate routine names solely by
number – OutputUser1 Make names of routines as long as necessary
(ok, be reasonable) To Name a function – describe return value
sin(), cos(), atan(), etc To Name a procedure – strong verb object
BuildNormalVectorTable(normaltable* normals, raster* r)
![Page 6: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/6.jpg)
Should be impervious or immune to bad input (?)
Avoid catch-all routines Avoid HIDDEN side-effects Stick to a standard parameter ordering Limit the number of parameters Be mindful of the way data is passed to a
routine Avoid speculative generality
Solve today’s problem today
![Page 7: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/7.jpg)
This is true when public Local, helper, private routines may
assume no bad input Use asserts during development
![Page 8: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/8.jpg)
Catch-alls will have characteristics like flags that indicate operations to be performed
Dispatch to smaller, simpler functions if you can’t think of a cleaner design
![Page 9: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/9.jpg)
Do not modify global data Do not
Clear memory Create anything Destroy anything Unless that is the purpose of the routine or it
is only for the duration of the routine
![Page 10: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/10.jpg)
Stick to a standard parameter ordering Example:
Destination first Source next Flags last (any values that modify the destination or source
immediately follow destination or source Decreasing order of importance
CopyValue(destRaster, row, column, sourceRaster, row, column);MapValues(destRaster, WATER, SWAMP);
![Page 11: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/11.jpg)
Limit the number of parameters to about 7
![Page 12: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/12.jpg)
Be mindful of the way data is passed to a routine
Do not pass large structures or strings to be modified, pass a reference instead
![Page 13: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/13.jpg)
Solve today’s problem today Be general only when you KNOW it will be useful
This is a potential time sink
![Page 14: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/14.jpg)
No set rule, except in this class We set that arbitrarily at 25 lines
One screen of code: 50–150 lines Length IS correlated with errors Cost?
![Page 15: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/15.jpg)
Order of parameters: Input Modify Output
Consider your own “in” and “out” keywords Use a consistent ordering scheme Use all the parameters Put status or error variables last Don’t use routine parameters as working variables
Create a local working variable instead Document interface assumptions about parameters Limit the number of a routine’s parameters to about seven Consider an input, modify, output naming convention for
parameters Pass the variables or objects that the routine needs to maintain
its interface Make sure actual parameters match formal parameters
![Page 16: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/16.jpg)
Function vs Procedure Function returns only one value like sin()
Setting function return values Check all possible return paths Don’t return references or pointers to local
data
![Page 17: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/17.jpg)
Fully parenthesize macro expressions Surround multiple-statement macros with
curly braces Name macros like routines so they can be
replaced with routines Limitations
Const Inline Template Enum Typedef
![Page 18: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/18.jpg)
The documentation provided does not exactly match the data
But it is sufficient to complete the assignment
![Page 19: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/19.jpg)
Write routines to accomplish the following in the BMP: Read a single value from the BMP Set a single value in the BMP Fill a row with a value Fill a column with a value Fill part of a row Fill part of a column Draw a line segment Fill a rectangle Fill a non-convex polygon
Polygon fill - http://alienryderflex.com/polygon_fill/
![Page 20: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/20.jpg)
Bresenham’s line algorithm http://en.wikipedia.org/wiki/
Bresenham's_line_algorithm
![Page 21: CS242. Reduce Complexity Introduce an intermediate, understandable abstraction Avoid code duplication Support subclassing Hide sequences Hide](https://reader035.vdocuments.site/reader035/viewer/2022062721/56649f255503460f94c3c5f1/html5/thumbnails/21.jpg)
Cases All four corners in bounds 2 corners in bounds 1 corner in bounds No corners in bounds