Robust Coding and Debugging

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

CSE 331 SOFTWARE DESIGN & IMPLEMENTATION DEBUGGING Autumn 2011 Bug-Date: Sept 9, 1947.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Debugging: Catching Bugs ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
CODING Research Data Management. Research Data Management Coding When writing software or analytical code it is important that others and your future.
1 Joe Meehean. 2 Testing is the process of executing a program with the intent of finding errors. -Glenford Myers.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Debugging and Interpreting Exceptions UW CSE 190p Summer 2012.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Software Quality Assurance and Testing Fazal Rehman Shamil.
Testing and Debugging. Testing Fundamentals  Test as you develop Easier to find bugs early rather than later Prototyping helps identify problems early.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Eighth Lecture Exception Handling in Java
Content Coverity Static Analysis Use cases of Coverity Examples
Recitation 7 – 3/18/02 Outline fork and scheduling Signals
Testing and Debugging.
C #include <stdio.h> int main() { printf ("hello class\n");
Design Patterns (cont.) and Coding
CSE 403 Software Engineering
Introduction to C CSE 2031 Fall /3/ :33 AM.
Exceptions 10-Nov-18.
YG - CS170.
Software Construction
Preconditions, Postconditions & Assertions
Exceptions & Error Handling
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
CSE 403 Lecture 18 Debugging.
CSE 403 Lecture 17 Coding.
Debugging at Scale.
When your program crashes
Michael Ernst CSE 140 University of Washington
Homework Any Questions?.
Introduction to Static Analyzer
Robust Coding and Debugging
CSC 143 Java Errors and Exceptions.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Exceptions 10-May-19.
Binary multiplication
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Debugging UW CSE 160.
Software Construction
CHAPTER 6 Testing and Debugging.
Defensive Programming
Presentation transcript:

Robust Coding and Debugging CSE 403

Outline Writing solid code Errors, asserts, exceptions Debugging practices

Resources 403 Sp’05 “The Practice of Programming,” by Brian Kernighan and Rob Pike “The Pragmatic Programmer,” by Andrew Hunt and David Thomas

– Sophocles, Ajax "It's a painful thing To look at your own trouble and know That you yourself and no one else has made it." – Sophocles, Ajax

Don’t do this #include <stdio.h> char *T="IeJKLMaYQCE]jbZRskc[SldU^V\\X\\|/_<[<:90!\"$434-./2>]s", K[3][1000],*F,x,A,*M[2],*J,r[4],*g,N,Y,*Q,W,*k,q,D;X(){r [r [r[3]=M[1-(x&1)][*r=W,1],2]=*Q+2,1]=x+1+Y,*g++=((((x& 7) -1)>>1)-1)?*r:r[x>>3],(++x<*r)&&X();}E(){A||X(x=0,g =J ),x=7&(*T>>A*3),J[(x[F]-W-x)^A*7]=Q[x&3]^A*(*M)[2 +( x&1)],g=J+((x[k]-W)^A*7)-A,g[1]=(*M)[*g=M[T+=A ,1 ][x&1],x&1],(A^=1)&&(E(),J+=W);}l(){E(--q&&l () );}B(){*J&&B((D=*J,Q[2]<D&&D<k[1]&&(*g++=1 ), !(D-W&&D-9&&D-10&&D-13)&&(!*r&&(*g++=0) ,* r=1)||64<D&&D<91&&(*r=0,*g++=D-63)||D >= 97&&D<123&&(*r=0,*g++=D-95)||!(D-k[ 3] )&&(*r=0,*g++=12)||D>k[3]&&D<=k[ 1] -1&&(*r=0,*g++=D-47),J++));}j( ){ putchar(A);}b(){(j(A=(*K)[D* W+ r[2]*Y+x]),++x<Y)&&b();}t () {(j((b(D=q[g],x=0),A=W) ), ++q<(*(r+1)<Y?*(r+1): Y) )&&t();}R(){(A=(t( q= 0),'\n'),j(),++r [2 ]<N)&&R();}O() {( j((r[2]=0,R( )) ),r[1]-=q) && O(g-=-q) ;} C(){( J= gets (K [1]))&&C((B(g=K[2]),*r=!(!*r&&(*g++=0)),(*r)[r]=g-K[2],g=K[2 ],r[ 1]&& O()) );;} main (){C ((l( (J=( A=0) [K], A[M] =(F= (k=( M[!A ]=(Q =T+( q=(Y =(W= 32)- (N=4 )))) +N)+ 2)+7 )+7) ),Y= N<<( *r=! -A)) );;}

Writing solid code Shred your garbage Force early failure, increase determinism Why 0xA3? void FreeMemory(void *pv){ Assert(pv != NULL); memset(pv, 0xA3, sizeofBlock(pv); free(pv); }

Coding quiz char tolower(char ch){ } return ch – ’A’ + ’a’;

Handling out of range inputs Ignore Return error code Assert Redefine the function to do something reasonable Write functions that, given valid inputs, cannot fail

Candy machine interfaces Error prone return values or arguments Classic bad example, getchar() returns an int! Alternate approach bool fGetChar(char pch); Many bugs with malloc returning NULL char c; c = getchar(); If (c == EOF) …

Assertions Don’t use assertions to check unusual conditions You need explicit error code for this Only use them to ensure that illegal conditions are avoided

Exceptions Put error handling in a single place Exceptions should be reserved for unexpected events It is exceptional if a file should be there but isn’t It is not exceptional if you have no idea if the file should be exist or not

Debugging What are the key steps in debugging a program?

Step through your code Maguire Knuth Step through new code in the debugger the first time it is used Add code, set break points, run debugger Add code, run tests, if failure, run debugger Knuth Developed tool to print out first two executions of every line of code

Kernigan and Pike's debugging wisdom Look for common patterns Common bugs have distinct signatures int n; scanf("%d", n); Examine most recent change Don't make the same mistake twice Debug it now, not later Get a stack trace Read before typing

K & P, II Explain your code to someone else Make the bug reproducible Divide and conquer Find simplest failing case Display output to localize your search Debugging with printf() Write self checking code Keep records

Don't do this Can cover up very bad things try { doSomething(); } catch (Exception e){ Can cover up very bad things Violates K&P: Debug it now, not later

Should debug code be left in shipped version Pro: Debug code useful for maintenance Removing debug code change behavior Bugs in release but not debug versions Con: Efficiency issues Different behavior for debug vs. release Early fail vs. recover

Apocryphal (but still a good story) A program which fails only in the month of September

Apocryphal (but still a good story) A program which fails only in the month of September char monthName[9]; strcpy(monthName, "September");