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

23
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

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

Page 1: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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

Page 2: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Good Coding Style

Continued

Page 3: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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

Page 4: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Thoughts on This Code?

#include <math.h>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!

Page 5: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Better Version?

#include <math.h>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

Page 6: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

6. Don’t Do Too Much in a Statement / Line

#include <string>#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.

Page 7: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

7. Defensive CodingA. Use assertions to verify assumptions in your codevoid myFunc (int *ptr) {

// Don’t ever call myFunc with a NULL ptr! if (*ptr == 1) { …

#include <assert.h>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

Page 8: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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 <assert.h>#include <assert.h>

void myFunc (int *ptr) { ptr = NULL;

assert (ptr != NULL); // Not checked won’t 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

Page 9: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

7. Defensive Coding

B. Set deleted / invalid pointers to NULL

delete (ptr); ptr = NULL; // Now we’ll 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);

Page 10: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

10

Should have used assert or validity checkers!

Page 11: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Find the Bug!const int NUM_WEIGHTS = 20; // 1. Constant variable#define WEIGHT_ZERO 0 // 2. Pre-processor constantenum 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 …

Page 12: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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

Page 13: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

8. No Warnings

Don’t 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

Page 14: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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

Page 15: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Code Reviews

• This course: you will read another team’s 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 team’s 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

Page 16: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Intro to Graphics

Page 17: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Graphics APIs

myProg.exe

x11 APIwin32 API PostScript

Low level APIsDifferent for different platforms

Page 18: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Graphics APIs

myProg.exe

x11 APIwin32 API PostScript

Solution: another layerHigher level API

Can target multiple low-level APIs

Page 19: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Graphics APIs

myProg.exe

x11 APIwin32 API PostScript

This course: EasyGL(simple, cross-platform graphics)

Page 20: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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

Page 21: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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);– ...

Page 22: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

Issue: Interaction?

myProg.exe

x11 API

This course: EasyGL(simple, cross-platform graphics) Graphics

drawing:myProg.exe

calls functions

User resizes window

orUser clicks on a button

Hardware receives the

input and X11 knows there is an

event

How to pass this information to myProg.exe?

Page 23: Administration Upcoming deadlines –Milestone 1 code due Monday Feb. 2 –Graphics proposal document: due Friday, Feb. 13 –Milestone 2 on web: due Monday,

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