110 likes | 229 Views
ICAPRG301A Apply introductory programming techniques. Program Bugs.
E N D
ICAPRG301A Apply introductory programming techniques Program Bugs • US Navy Admiral Grace Hopper is often credited with creating the term computer program bug. When she started there were no computer programming languages so she had to invent her own called ARITH-MATIC, MATH-MATIC and FLOW-MATIC. These later formed the basis for the language COBOL. Hopper introduced the idea that computer programs should be written in an English like languages rather than machine code or mathematical formulas. There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies - C.A.R. Hoare ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques Types of Bugs • Bugs occur when your program does something unexpected. There are three main causes: • Syntax errors – here you made a mistake entering the code, spelled a word wrong, got a space in the wrong place. In Python getting the indentation wrong is a common bug. • Programming errors – here you have used a command the wrong way and it has unintended consequences. Often in loops there are complex patterns and indent errors can easily occur causing a command to be executed at the wrong time • Logic errors – here the program runs and works well on all your test cases but does not always do what you want. Bloody instructions, which, being taught, returnTo plague the inventor … William Shakespeare – Macbeth, Act 1, Scene 7 ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques What to do about bugs • Be perfect, or if you can’t be perfect, be careful. • Syntax bugs • These are fixed by reading the error messages carefully. Your program won’t run with these bugs. Sometimes the IDE will highlight these errors. • Indent errors in Python might still run but your program will produce very strange results. I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone - BjarneStroustrup ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques What to do about bugs • Programming bugs • These often occur during loops when undertaking complex processing of information. It is helpful to add print statements to your code to work out what is going on. IDE’s have a feature where they can step through the code as it runs and watch the variables being changed. • These can be very frustrating bugs to work out. I'm a programmer. People seem to think I can fix their computer problems. I guess they never wonder where those problems came from. ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques What to do about bugs • Logic Bugs • These exist in every program. You can’t control which machines your programs will run on, what the user will do, what other programs are running at the same time etc. Any of these could cause your program to not work properly. Good testing helps to reduce these bugs. • Testing • You must test your program once you have it working. Test should include: • Expected data • Extreme end of the expected range of data • All possible data outside the expected range of data • Anything else you can think of that might possibly happen • Other users (Alpha and Beta releases) …a good testing question doesn't necessarily have a definitive, expected result. Tests that are designed to confirm a prevailing theory tend not to reveal new information ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques What to do about bugs • Some things are beyond your control. For example your program might access a web site and that site might not be working at that point. • There are ways that your program can check for this, and we will be covering them later in this unit. • However you MUST ALWAYS check user input. Never assume that your user will give you what you want them to. It is common practise to replace commands like input with specially designed functions which take the input and provide it back to the program in the form that the program expects. Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it - Brian W. Kernighan ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques Scope and Mutability • Scope is a computer term which is used to define where your variables can be used. • Mutability is a term which is used to determine whether variables can be change (Mutable) or not (Immutable). • As a general rule, in Python variables only exist within the function (or module) where they were declared. This means that if two functions contain the same variable name, Python treats them as two separate variables. If you want a function to use a variable from another function it must come into the function as an input and leave as an output. • There is an exception to this rule, but we won’t be covering that in this course. ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques Mutability Mutability is a term which is used to determine whether variables can be change (Mutable) or not (Immutable). In Python most variables are immutable, if you want to change them you need to reassign them. However Python lists are mutable, they can be changed. Controlling complexity is the essence of computer programming. Brian Kernighan ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques • Look at the following program: • a=5def inc(x): a=a+xinc(10)print (a) • Don’t run this. Think about what is happening and try to predict what Python will do, you have four options. Will it: • A. Print 5 • B. Print 10 • C. Print 15 • D. Throw an error ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques • Now we will change it slightly • a=5def inc(x): a=9 a=a+xinc(10)print (a) • What will this do? Will it: • A. Print 5 • B. Print 9 • C. Print 19 • D. Throw an error ICAPRG301A Week 4 Buggy Programming
ICAPRG301A Apply introductory programming techniques • a=5def inc(a): a=a+9inc(10)print (a) • What will this do? • Will it: • A. Print 5 • B. Print 9 • C. Print 19 • D. Throw an error ICAPRG301A Week 4 Buggy Programming