Debugging !!!  ► Believe it or not, errors happen ► The most frustrating part of programming ► Hunting down an error can be harder and more time consuming.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

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.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Debugging Introduction to Computing Science and Programming I.
Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
11-Jun-15 Exceptions. 2 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.
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.
Debugging CPSC 315 – Programming Studio Fall 2008.
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.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Programming. Software is made by programmers Computers need all kinds of software, from operating systems to applications People learn how to tell the.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Hello AP Computer Science!. What are some of the things that you have used computers for?
Homework Reading Programming Assignments
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
Lists in Python.
General Programming Introduction to Computing Science and Programming I.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
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.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
Chapter 22 Developer testing Peter J. Lane. Testing can be difficult for developers to follow  Testing’s goal runs counter to the goals of the other.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
By: Maisha Loveday 8C Maths Reflection: Binomial Expansion.
Error messages 25-Apr-17.
Files Tutor: You will need ….
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
HKOI Programming HKOI Training Team (Intermediate) Alan, Tam Siu Lung Unu, Tse Chi Yung.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Outline Announcements: –HW I key online this afternoon –HW II due Friday –Sign up to discuss projects Debugging Testging for correctness.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
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,
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Introduction to Exceptions in Java CS201, SW Development Methods.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
14 Compilers, Interpreters and Debuggers
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Testing and Debugging.
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
PSP Class Practice 1 MARCIA begins work her program [10:00] by reviewing the requirements in the assignment package, including the test requirements, to.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
Debugging at Scale.
Programming.
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Tonga Institute of Higher Education IT 141: Information Systems
An Introduction to Debugging
Tonga Institute of Higher Education IT 141: Information Systems
Running a Java Program using Blue Jay.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Arrays.
Software Development Techniques
Presentation transcript:

Debugging !!!  ► Believe it or not, errors happen ► The most frustrating part of programming ► Hunting down an error can be harder and more time consuming than the solution to the original problem ► Debugging tools and methodologies are still in their infancy ► It’s still an art form: must be done manually ► Can’t tell you how to be Da Vinci, but can certainly give you a brush and some paint

Just use a debugger! ► Unfortunately, we don’t live in that perfect world just yet ► Several problems with debuggers:  May not be available for several reasons  Don’t work for complex systems like distributed computing  It’s just plain confusing sometimes ► Can be thought of more as reverse engineering than a straight forward process

Methodology ► Use your brain… not your fingers ► Keep it simple initially  Look for simple solutions first  The most likely culprit will usually be it ► Move on to more complex solutions ► For the most part, try and assume the computer is doing its job. It’s not wrong ► There are different kinds of bugs to look for

Kinds of Errors ► Compile time errors  Usually pointed out to you, easy to fix  Syntactical errors ► Run time errors  “System Errors”: Out of bounds, class cast exception, null pointer exception  “Logic Errors”: No visible errors, you just get the wrong answer ► Causes of Errors  Flawed Logic (semantics)  Typos (syntactical)

Thought Process ► Its not working… now what? ► Make sure it is actually a bug ► Was your solution model flawed? ► Are you printing what you want to print? ► Did you forget to save / correct version? ► Libraries are up to date? ► Did you forget to end a comment? ► Was there a crucial input missing?

Easy Bugs: Lots of Clues ► How to look for clues? ► Tip: Avoid writing too much before running ► Did a recent change break the code? ► Have you seen this bug before? What did you do in the past to fix it? ► Think first, write down on paper what is going on ► A second set of eyes might be able to see what you are missing. You have been staring at it so long you might miss something simple.

More complex problems ► Might be system specific, check manuals ► Narrow down inputs which reproduce the bug. If you know which inputs make it fail you can pinpoint where the error may be. ► Are you close to data type limits? ► Look for suspicious values: Powers of 2, weird ASCII (more applicable in other languages) ► Are your data structures valid? ► Does taking out bits of code make it work? ► Inserting print statements…

Foreign/Old Code ► Get a copy of the design / problem statement ► Work it out by hand first ► Group their functions into small bits of readable English: “Formats text”, “Convert hex to binary” ► Know what it all does before attempting to fix it ► Sadly: Complete rewrites often faster than debugging other people’s code ► Look for examples of ‘hard coding’: declared strings, integers, counters which are defined instead of read as input

The Art of Print Statements ► System.out.println(“UUUUUUUUUUUUUGH”); ► System.out.println(“YESSSSSSSSSSSSSSSS”); ► Usually best way to find errors in small systems, especially number-based ► Things to print  Counter values  Min/Max values  Array indices  Data structure values ► Where to print  Before / after declarations/initializations  Before / after function calls  Before / after entering loops / recursion  Before “breaking” from a loop

Java Specific ► Always give your objects a.toString() or a.toDebug() method ► Make use of Arrays.toString() etc ► print(Index + “ : “ + value) in loops ► Use iterators where possible ► Instead of setting your own parameters for a prebuilt class, override it ► If building GUI/JS, use System.err.println()

Examples ► NullPointerExceptions  Print statement after initialization or immediately before passing into functions ► ArrayOutOfBounds  Lucky if you are using java that they are thrown  Print index counters, array values ► Infinite Loops  Do searches for index counter names/updates ► “Wrong answers”  Go over it again by hand, your solution probably is not correct in the first place. Put the code on hold

Example: Mathematics ► Sine and Cosine mixed up? ► Negative values? ► Values close to zero / rounding errors? ► Adding small values to large values ► Reversing min / max values? ► Pass by reference / value? ► Loops correct?

Examples (ctd) ► Loops / Conditionals ► Are your counters in the right order? ► ++counter vs. counter++ ► Multi dimensional – do it by hand first ► Make sure bracketing matches with indenting ► Copy / Paste changes

Life Lessons… ► Format of a file is not standard ascii ► Format of a file is little endian instead of big endian ► Naming conventions actually matter in fortran ► Programming in C for the first time – arrays do not initialize

Reverse Engineering ► Start from the error and work backwards ► Make sure all your parts work before testing the system as a whole ► Surround the affected area with output information / line breaks ► Edit lines of ‘fluff’ code out to test the ‘meat’ ► Experiment with different input values ► Fix the code line by line

Debugger ► Built into Eclipse for java ► GUI / Text based, same idea ► Only used as a last resort, very time consuming ► Allow for insertion of break lines to halt code execution at any point ► Allow you to see the contents of any object / array / primitive at any time by query ► Amount of data shown often overwhelming ► Doesn’t provide insight into logical error ► Still a manual process to find errors

How to avoid ► Don’t write 1000 lines before testing ► Variable names can have meaning ► Don’t hard code ANYTHING. If you must, define all of them in the same place where they are easily seen / changed ► Comment everywhere it isn’t obvious ► If its more than a few lines… put it in a function ► Always use { }

Personal Horror Stories ► if (conditional which ran longer than the width of my screen); break; break; ► while (index < end) go from the end of a string to the front… go from the end of a string to the front… index = index -1; index = index -1; ► for (int i=0; i<x; i++) for (int j=0; j<y; i++) ► Forgot about a hard coded n*n matrix read ► Drew background last in graphics class ► Spent 4 hours debugging because C didn’t flush a buffer ► Tried to do anything in fortran or prolog