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

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Chapter 1: Computer Systems
Coding and Debugging. Requirements and Specification Recall the four steps of problem solving: Orient, Plan, Execute, Test Before you start the implementation.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
COSC 120 Computer Programming
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ Crash Course Class 1 What is programming?. What’s this course about? Goal: Be able to design, write and run simple programs in C++ on a UNIX machine.
Administrative Stuff ECE 297. Administration Milestone 0: –Submit by Friday at 5 pm –Demo in lab this week –Write your name on the board when ready to.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Visual Basic Chapter 1 Mr. Wangler.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Java: Chapter 1 Computer Systems Computer Programming II.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
WESS Module 4 Chopping and Releasing HAZREPs Web Enabled Safety System.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.
COMP 208/214/215/216 Lecture 3 Planning. Planning is the key to a successful project It is doubly important when multiple people are involved Plans are.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
C Hints and Tips The preprocessor and other fun toys.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Blackfin Array Handling Part 1 Making an array of Zeros void MakeZeroASM(int foo[ ], int N);
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CS140 Project 1: Threads Slides by Kiyoshi Shikuma.
1 Project 3 The Kumquat Society Conference. 2 Conference Registration In this project you will write a program to handle a conference registration. The.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
Version Control and SVN ECE 297. Why Do We Need Version Control?
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Administration: Upcoming Due Dates Milestone 1: due Monday, Feb. 1 at 5 pm Code Review on milestone 1: –Coming soon; due Monday, Feb. 8 WD1: graphics proposal.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Debugging, bug finding and bug avoidance Part 2 Alan Dix
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
1 Building a program in C: Preprocessor, Compilation and Linkage.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Computer Programming in C++ 黃鐘揚 教授 Prof. Chung-Yang (Ric) Huang Department of Electrical Engineering National Taiwan University 2007/06/26.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Defining Your Own Classes II
Working with Java.
Chapter 6 CS 3370 – C++ Functions.
Designing For Testability
Milestone 2 Overview.
Testing and Debugging.
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Variables and Arithmetic Operations
Testing (Continued).
Focus of the Course Object-Oriented Software Development
Unit 1 Programming - Assignment 3
Review of Previous Lesson
Milestone 2 Overview.
IS 135 Business Programming
Presentation transcript:

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

Good Coding Style Continued

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

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!

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

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

7. Defensive Coding A.Use assertions to verify assumptions in your code void myFunc (int *ptr) { // Don’t 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

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  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

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

10 Should have used assert or validity checkers!

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 …

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

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

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!

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

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

Intro to Graphics

Graphics APIs myProg.exe x11 API win32 API PostScript Low level APIs Different for different platforms

Graphics APIs myProg.exe x11 API win32 API PostScript Solution: another layer Higher level API Can target multiple low-level APIs

Graphics APIs myProg.exe x11 API win32 API PostScript This course: EasyGL (simple, cross-platform graphics)

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

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

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?

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