Idiotproofing Outline

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.Repetition and Looping.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
Software Lesson #1 CS1313 Spring Software Lesson 1 Outline 1.Software Lesson 1 Outline 2.What is Software? A Program? Data? 3.What are Instructions?
CS 201 Functions Debzani Deb.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.An Idiotproof Website.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
Variables & Constants Lesson 2 Outline
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
Algorithm: procedure in terms of
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Chapter 2 - Introduction to C Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
for Loop 1 Outline for Loop 1 Outline for Loop
CS1061 C Prgramming Lecture 11: Functions
Chapter 2 - Introduction to C Programming
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Character Strings Lesson Outline
Numeric Data Types Outline
Array Lesson 1 Outline Array Lesson 1 Array Lesson 1 Outline
while Loop Outline while Loop Outline while Loop Example #1
Chapter 2 - Introduction to C Programming
User Defined Functions 2 Outline
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 2 - Introduction to C Programming
Structured Program
Chapter 3 - Structured Program Development
Part B – Structured Exception Handling
Nested if Lesson Outline
for Loop Lesson 3 Outline
Assignment Operators Topics Increment and Decrement Operators
Chapter 3 - Structured Program Development
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter 6: Repetition Statements
Chapter 2 - Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
© 2016 Pearson Education, Ltd. All rights reserved.
Arithmetic Expressions Lesson #2 Outline
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 2 - Introduction to C Programming
Variables Lesson Outline
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Arithmetic Expressions Lesson #1 Outline
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
The while Looping Structure
CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Idiotproofing Outline Idiotproofing Quotes An Idiotproof Website Idiotproofing Example #1 Idiotproofing Example #2 Idiotproofing Example #3 Idiotproofing Example #4 Why We Idiotproof The exit Statement #1 The exit Statement #2 The exit Statement #3 The exit Statement #4 exit Example’s Flowchart A New File to #include exit Statement Inside an if Block #1 exit Statement Inside an if Block #2 exit Statement Inside an if Block #3 exit Statement Inside an if Block #4 Idiotproofing Example’s Flowchart Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Idiotproofing means ensuring that a user’s input is valid. Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Quotes “Idiotproofing is difficult because idiots are so clever.” “You can't make anything idiot proof because idiots are so ingenious.” ― Ron Burns “Idiotproofing causes evolutionary selection of more ingenious idiots.” “Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” – Rich Cook “It doesn’t really matter what effort you put into idiot-proofing a product or procedure. They will always build a better idiot.” “Idiot-proofing assumes a finite number of idiots.” “Campaigns to bearproof all garbage containers in wild areas have been difficult because, as one biologist put it, ‘There is a considerable overlap between the intelligence levels of the smartest bears and the dumbest tourists’.” http://www.goodreads.com/quotes/tag/idiots http://scienceblogs.com/goodmath/2008/04/the_real_murphys_law.php http://c2.com/cgi/wiki?IdiotProofProcess Idiotproofing Lesson CS1313 Spring 2018

An Idiotproof Website http://www.idiotproofwebsite.com/ Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Example #1 #include <stdio.h> #include <stdlib.h> int main () { /* main */ const float minimum_area = 0; const int program_success_code = 0; const int program_failure_code = -1; float area_in_square_miles; Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Example #2 printf("What is the area "); printf("in square miles?\n"); scanf("%f", &area_in_square_miles); if (area_in_square_miles < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_square_miles < ...) */ Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Example #3 /* * ASSERT: By the time the program gets to here, * the area must be valid. */ printf("The area is valid.\n"); return program_success_code; } /* main */ Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Example #4 % gcc -o conversions_idiot conversions_idiot.c % conversions_idiot What is the area in square miles? -100 ERROR: you can't have a negative area! 100 The area is valid. Idiotproofing Lesson CS1313 Spring 2018

Why We Idiotproof Idiotproofing ensures that input data are valid, which means that, if our program is otherwise correct, then the output will be valid as well. Idiotproofing allows us to assert certain properties of the data. For example, in the conversions program, properly idiotproofed input data allow us to assert that, in the calculation section, the area in square miles is valid. So, our calculations can assume this fact, which sometimes can be more convenient. Idiotproofing Lesson CS1313 Spring 2018

The exit Statement #1 NOTICE! % cat exitexample.c #include <stdio.h> #include <stdlib.h> int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. The exit statement terminates execution of a given run of a program. NOTICE! Idiotproofing Lesson CS1313 Spring 2018

The exit Statement #2 NOTICE! % cat exitexample.c #include <stdio.h> #include <stdlib.h> int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. The program terminates in a controlled, graceful way – that is, it doesn’t actually crash – without executing the remaining executable statements. NOTICE! Idiotproofing Lesson CS1313 Spring 2018

The exit Statement #3 NOTICE! % cat exitexample.c #include <stdio.h> #include <stdlib.h> int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. Notice that the exit statement takes an int argument. This argument represents the value that will be returned by the program to the operating system (for example, Linux). By convention, returning 0 from a program to the OS means that the program completed successfully, so if the program is exiting prematurely, then you should return a non-zero value. NOTICE! Idiotproofing Lesson CS1313 Spring 2018

The exit Statement #4 NOTICE! % cat exitexample.c #include <stdio.h> #include <stdlib.h> int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. Jargon: In the context of running a program, all of the following terms are generally used to mean the same thing: exit, stop, halt, terminate, abort. NOTICE! Idiotproofing Lesson CS1313 Spring 2018

exit Example’s Flowchart printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); Apparent Flowchart Actual Flowchart Notice that the symbol for an exit is also an oval. Idiotproofing Lesson CS1313 Spring 2018

A New File to #include NOTICE! % cat exitexample.c #include <stdio.h> #include <stdlib.h> int main () { /* main */ const int program_failure_code = -1; printf("This statement will be always be executed.\n"); exit(program_failure_code); printf("This statement will be never be executed.\n"); } /* main */ % gcc -o exitexample exitexample.c % exitexample This statement will be always be executed. To use an exit statement, you MUST include an additional header file, IMMEDIATELY AFTER stdio.h: NOTICE! Idiotproofing Lesson CS1313 Spring 2018

exit Statement Inside an if Block #1 if (area_in_square_miles < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_square_miles < ...) */ When you put an exit statement inside an if block, the exit statement will be executed only in the event that the appropriate clause of the if block is entered, and then only after all prior statements in that clause of the if block have already been executed. Idiotproofing Lesson CS1313 Spring 2018

exit Statement Inside an if Block #2 if (area_in_square_miles < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_square_miles < ...) */ In the above example, the exit statement is executed only in the event that the area is negative, and only after executing the printf statement that precedes it. Idiotproofing Lesson CS1313 Spring 2018

exit Statement Inside an if Block #3 if (area_in_square_miles < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_square_miles < ...) */ Notice that the exit statement DOESN’T have to have a comment after it. Idiotproofing Lesson CS1313 Spring 2018

exit Statement Inside an if Block #4 if (area_in_square_miles < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); /* <--- NOTICE! */ } /* if (area_in_square_miles < ...) */ Notice that the exit statement is inside the if block and therefore is indented MORE than the if statement. Idiotproofing Lesson CS1313 Spring 2018

Idiotproofing Example’s Flowchart area < 0? if (area_in_square_miles < minimum_area) { printf("ERROR: you can’t have a negative"); printf(" area!\n"); exit(program_failure_code); } /* if (area_in_square_miles < ...) */ Idiotproofing Lesson CS1313 Spring 2018