Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal...

Click here to load reader

  • date post

    13-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Embed Size (px)

Transcript of Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal...

  • Slide 1

Administration Upcoming deadlines Milestone 1 code due Monday Feb. 2 Graphics proposal document: due Friday, Feb. 13 Milestone 2 on web: due Monday, Feb. 23 Slide 2 Good Coding Style Continued Slide 3 5. 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 > 100 lines Slide 4 Thoughts on This Code? #include void myFunc (float a[], float b[], int nvals) { float absAvg1 = 0; for (int i = 0; i < nvals; i++) absAvg1 += abs(a[i]); absAvg1 /= nvals; float absAvg2 = 0; for (int i = 0; i < nvals; i++) absAvg2 += abs(b[i]); absAvg2 /= nvals;... } There is no honour in copy and paste coding! Refactor! Slide 5 Better Version? #include float compAbsAvg (float array[], int nvals) { float absAvg = 0; for (int i = 0; i < nvals; i++) absAvg += abs (array[i]); 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 Slide 6 6. Dont Do Too Much in a Statement / Line #include #include StreetsDatabaseAPI.h string name = getIntersectionName(getStreetSegmentEnds( getIntersectionStreetSegment(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. Slide 7 7. Defensive Coding A.Use assertions to verify assumptions in your code void myFunc (int *ptr) { // Dont ever call myFunc with a NULL ptr! if (*ptr == 1) { #include 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 > assert failed on line 208 of file myFunc.cpp: ptr != NULL Slide 8 What If I Need That Last Bit of Speed? #define NDEBUG // Just turned off assertion checking // Make sure this line is in front of // #include #include 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 Slide 9 7. Defensive Coding B. Set deleted / invalid pointers to NULL delete (ptr); ptr = NULL; // Now well crash if we try to use it good! ptr->value = 1; // Will seg fault immediately C. Self-checking code (advanced technique) program_ok = validate_key_data_structure (my_struct); Slide 10 10 Should have used assert or validity checkers! Slide 11 Find 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 weights[NUM_WEIGHTS]) { for (int i = 0; i < NUM_WEIGHTS; i++) { if (weights[i] = 0) return (HAS_ZERO); if (weights[i] < 0) return (HAS_NEG); } return (ALL_POS); } weights = {-1, 2, 3, 4, 5, 6, 20} checkWeights returns ALL_POS Test program, find a failing case g++ -Wall weights.cpp > weights.cpp: In function int checkWeights(int*): > weights.cpp:11: warning: suggest parentheses a"> 8. Use Compiler Warnings > g++ -Wall weights.cpp > weights.cpp: In function int checkWeights(int*): > weights.cpp:11: warning: suggest parentheses around assignment used as truth value int checkWeights (int weights[NUM_WEIGHTS]) { for (int i = 0; i < NUM_WEIGHTS; i++) { if (weights[i] = 0) return (HAS_ZERO); if (weights[i] < 0) return (HAS_NEG); } return (ALL_POS); } Line 11 Slide 13 8. 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 Netbeans: File | Project Properties | C++ Compiler | More Warnings Slide 14 Over-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! Slide 15 Code 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 Slide 16 Code 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 Slide 17 Intro to Graphics Slide 18 Graphics APIs myProg.exe x11 API win32 API PostScript Low level APIs Different for different platforms Slide 19 Graphics APIs myProg.exe x11 API win32 API PostScript Solution: another layer Higher level API Can target multiple low-level APIs Slide 20 Graphics APIs myProg.exe x11 API win32 API PostScript This course: EasyGL (simple, cross-platform graphics) Slide 21 EasyGL 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); Window title Background colour Slide 22 EasyGL 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); ... Slide 23 Issue: Interaction? myProg.exe x11 API This course: EasyGL (simple, cross-platform graphics) Graphics drawing: myProg.exe calls functions User resizes window or User clicks on a button Hardware receives the input and X11 knows there is an event How to pass this information to myProg.exe? Slide 24 Solution: Callbacks myProg.exe x11 API This course: EasyGL (simple, cross-platform graphics) myProg.exe registers callback functions for various events User resizes window or clicks on a button Hardware receives the input and X11 inserts event into event queue EasyGL checks the event queue and calls the appropriate callback now myProg.exe can handle it. Then hands control to EasyGL