Title: Administration
1Administration
- Upcoming deadlines
- Milestone 1 code due Monday Feb. 2
- Graphics proposal document due Friday, Feb. 13
- Milestone 2 on web due Monday, Feb. 23
2Good Coding Style
35. Many Short Functions
- Short functions
- Easier to re-use
- Fix bugs in one place, not many
- Make code easier to read more abstraction
- How long should functions be?
- Should have many 5 to 10 line functions
- Should very rarely have a function gt 100 lines
4Thoughts on This Code?
include ltmath.hgt void myFunc (float a, float
b, int nvals) float absAvg1 0
for (int i 0 i lt nvals i) absAvg1
abs(ai) absAvg1 / nvals float absAvg2
0 for (int i 0 i lt nvals i)
absAvg2 abs(bi) absAvg2 / nvals
...
- There is no honour in copy
- and paste coding!
5Better Version?
include ltmath.hgt float compAbsAvg (float
array, int nvals) float absAvg 0 for
(int i 0 i lt nvals i) absAvg abs
(arrayi) return (absAvg / nvals) void
myFunc (float a, float b, int nvals)
float absAvg1 compAbsAvg (a, nvals) float
absAvg2 compAbsAvg (b, nvals) ...
- 1. Not much shorter, but easier to read
- 2. Less chance of more code copying ? future code
will be shorter
66. Dont Do Too Much in a Statement / Line
include ltstringgt include StreetsDatabaseAPI.h
string name getIntersectionName(getStreetSegmen
tEnds( getIntersectionStreetSegmen
t(id,0)).to) // Hard to read! unsigned
firstSeg getIntersectionStreetSegment
(myInterId,0) unsigned destInterId
getStreetSegmentEnds(firstSeg).to string
destInterName getIntersectionName
(destInterId) // Show your work ? divide into
several steps on several lines // Use good
variable names to show what intermediate
results // are.
77. Defensive Coding
- Use assertions to verify assumptions in your code
- void myFunc (int ptr)
- // Dont ever call myFunc with a NULL ptr!
- if (ptr 1)
-
- include ltassert.hgt
- void myFunc (int ptr)
- assert (ptr ! NULL)
- if (ptr 1)
-
- Exits program if ptr is NULL (condition not true)
- Better than a comment
- Checked at runtime gives useful error message
- gt assert failed on line 208 of file myFunc.cpp
ptr ! NULL
8What If I Need That Last Bit of Speed?
- define NDEBUG // Just turned off assertion
checking - // Make sure this line is in
front of - // include ltassert.hgt
- include ltassert.hgt
- void myFunc (int ptr)
- ptr NULL
- assert (ptr ! NULL) // Not checked ? wont
fire. -
- Can turn off assertion checking in release build
- Avoids any speed overhead
- Leave on for debug build for extra checking
- And maybe leave on in release too if your program
not very time critical
97. Defensive Coding
- B. Set deleted / invalid pointers to NULL
- delete (ptr)
- ptr NULL // Now well crash if we try to
use it ? good! -
- ptr-gtvalue 1 // Will seg fault immediately
- C. Self-checking code (advanced technique)
- program_ok validate_key_data_structure
(my_struct) -
10Should have used assert or validity checkers!
11Find the Bug!
- const int NUM_WEIGHTS 20 // 1. Constant
variable - define WEIGHT_ZERO 0 // 2. Pre-processor
constant - enum WtReturn HAS_NEG -1, HAS_ZERO 0,
ALL_POS 1 - // 3. make an enumeration (list) of int
constants - int checkWeights (int weightsNUM_WEIGHTS)
for (int i 0 i lt NUM_WEIGHTS i) - if (weightsi 0)
- return (HAS_ZERO)
- if (weightsi lt 0)
- return (HAS_NEG)
-
- return (ALL_POS)
Test program, find a failing case
- weights -1, 2, 3, 4, 5, 6, 20
- checkWeights returns ALL_POS
128. Use Compiler Warnings
- gt g -Wall weights.cpp
- gt weights.cpp In function int
checkWeights(int) - gt weights.cpp11 warning suggest parentheses
around assignment used as truth value
int checkWeights (int weightsNUM_WEIGHTS)
for (int i 0 i lt NUM_WEIGHTS i)
if (weightsi 0) return (HAS_ZERO)
if (weightsi lt 0) return
(HAS_NEG) return (ALL_POS)
Line 11
138. No Warnings
- Dont have any warnings in your code
- Warnings flag potentially problematic code
- If you leave some warnings in, hard to see new
ones - Fix right away stay at 0 warnings!
- Tell compiler to generate all useful warnings
(more than default) - Command line g Wall ltgt
- Netbeans
- File Project Properties C Compiler More
Warnings
14Over-Arching Rules
- Code will be read more than written
- By you
- By others
- Make it readable!
- Code will be modified many times
- Make small functions avoid repeated code
- Test as you develop
- Keep the tests with the code
- Documentation should be in the code comments!
15Code Reviews
- If you do one thing for code quality, do code
reviews - Altera 4 reviewers read code written by one team
member - Significant amount 400 1000 lines
- Write down thoughts
- Then meet to discuss ? readable, clear,
efficient? - Google every commit reviewed and approved
16Code Reviews
- This course you will read another teams
milestone1 code submission - Both teams have the same TA
- Write a short (1 page) code review
- Can have an appendix with code examples
- 2 of your final mark
- Does not affect the other teams grade
- TA already reviewed their code for style
- Same TA will read your code review
- Code sent to you week of Feb. 2
- Review due Monday, Feb. 9
17Intro to Graphics
18Graphics APIs
myProg.exe
Low level APIs Different for different platforms
win32 API
PostScript
x11 API
19Graphics APIs
myProg.exe
Solution another layer Higher level API Can
target multiple low-level APIs
win32 API
PostScript
x11 API
20Graphics APIs
myProg.exe
This course EasyGL (simple, cross-platform
graphics)
win32 API
PostScript
x11 API
21EasyGL Overview
- include graphics.h
- In any file where you want to make graphics calls
- Need to include 3 files in your project, and some
libraries in your build step - See EasyGL quick start guide and example
code/makefile - First call set up window
- init_graphics (Some example graphics, WHITE)
- Second call choose your coordinate system
- set_visible_world (xleft, ybottom, xright, ytop)
Background colour
Window title
22EasyGL Overview
- Set drawing attributes
- setcolor (int color_index) // E.g. BLUE ( 9)
- setcolor (t_color (red, green, blue))
- // red, green and blue are 8-bit integers
- // e.g. t_color (0, 0, 255) is also blue
- setlinewidth (3) // 3 pixels wide
- setlinestyle (DASHED)
- sticky affect all subsequent drawing until
changed - Draw primitives
- drawline (x1, y1, x2, y2)
- fillrect (lower_left_pt, upper_right_pt)
- ...
23Issue Interaction?
myProg.exe
How to pass this information to myProg.exe?
This course EasyGL (simple, cross-platform
graphics)
Graphics drawing myProg.exe calls functions
Hardware receives the input and X11 knows there
is an event
x11 API
User resizes window or
User clicks on a button
24Solution Callbacks
EasyGL checks the event queue and calls the
appropriate callback ? now myProg.exe can handle
it.
myProg.exe
This course EasyGL (simple, cross-platform
graphics)
myProg.exe registers callback functions for
various events
Hardware receives the input and X11 inserts event
into event queue
x11 API
Then hands control to EasyGL
User resizes window or clicks on a button