Applied Programming/Conditions
This lesson introduces conditions, validation, exception handling, and defensive programming.
Objectives and Skills
editObjectives and skills for this lesson include:[1]
- Construct and analyze code segments that use branching statements
- if; elif; else; nested and compound conditional expressions
- Analyze, detect, and fix code segments that have errors
- Syntax errors; logic errors; runtime errors
- Analyze and construct code segments that handle exceptions
- Try; except; else; finally; raise; assert
Readings
editMultimedia
edit- YouTube: Introduction to Structured Programming
- YouTube: The Three Basic Structures—Sequence, Selection, and Loop
- YouTube: Programming For Beginners - Relational Operators
- YouTube: Introduction to Programming - Control Flow
- YouTube: Exception Handling in Java
- YouTube: Defensive Programming
- YouTube: Python 3 Programming Tutorial: If Statement
- YouTube: Python 3 Programming Tutorial - Try and Except error Handling
- YouTube: Assert statements and unit tests (Python)
Examples
editActivities
editModify your program from the previous lesson to add input validation, parameter validation, assertions, and exception handling.
- Review Wikipedia: Data validation. Add input validation to ensure that only valid data may be entered for each input. Validate for both data type and range. Invalid input should terminate the program with an appropriate error message.
- Review Wikipedia: Parameter validation. Add parameter validation to all calculation / conversion functions to ensure that only valid parameters are passed to each function. Validate for both data type and range. Throw or raise appropriate exceptions for invalid parameters.
- Review Wikipedia: Assertion (software development). Add assertions to the output function to assure that only valid parameters are passed. Validate for both data type and range.
- Review Wikipedia: Exception handling. Add exception handling to your main function to catch any errors thrown during processing and terminate the program gracefully.
- Update program and function documentation regarding parameters and exceptions, consistent with the documentation standards for your selected programming language.
Lesson Summary
edit- Assertion is a predicate(a Boolean-valued function over the state space, usually expressed as a logical proposition using the variables of a program) connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects. [2]
- Exception handling is a means to respond to and ideally address run-time errors that occur during the execution of a program—these are errors that either cannot be handled by another mechanism or their handling would make for clumsy and inelegant design.[3]
- There are viable alternatives. Critical functions may return error codes that are subject to explicit checks by the programmer. For example, the new operator in C++ allocates dynamic memory, returning a pointer after doing so. If for some reason, the system cannot perform such a task, the pointer returned may hold the special value NULL, indicating the failure of the desired operation.[3]
- Where possible, you may also use other data validation techniques (making use of conditions and loops) to "preemptively filter exceptional cases."[3]
- Designated handling constructs separate the main logic of the program from the handling of exceptional cases into semantic blocks, resulting in code that is simpler to read and maintain.[3]
- Most implementations of exception handling feature a try-catch or in the specific case of Python, a try-except, statement. Despite syntactical differences between languages, the concept of exception handling is similar across the board.[3]
- The try clause houses code that is to be attempted. If an exception (either thrown or raised by the run-time environment or manually by the programmer) were to occur, further processing halts as the exception are passed onto the corresponding catch or except clause. There may be several of these clauses.[3]
- Generally, the intent of exception handling is to ameliorate the problem with grace. If this is not possible, you can throw or raise the exception again to be caught by a higher-level exception handler.[3]
- In Python, as well as many other languages, you may specify to catch an exception generically or precisely (by denoting the exact name of the exceptional case, either language-specified or user-defined). You can even group exceptional cases to handle by adjoining them into a tuple data structure.[4]
- There are two models to approach code that may result in exceptional cases: EAFP and LBYL.[5]
- The LBYL, "look before you leap," mentality has the programmer write code such that a precondition is tested before accessing the sought after resource.[5]
- This, however, doesn't always work as intended. If the state of an object were changing in real-time, the resource that was once safe can be rendered unsafe between the evaluation of the precondition and the later accessing of that resource. This is a specific bug known as a time of check to time of use (abbreviated TOCTTOU) race condition.[5]
- The EAFP, "it's easier to ask for forgiveness than permission," form is a staple of Python idioms. Abiding by such a philosophy, you are to simply attempt the desired action. If there is a resultant exception, it's to be handled by a try-except block. This sidesteps the potentiality for a change of state and the ensuing "race condition" that it might cause, as described above.[5]
- The LBYL, "look before you leap," mentality has the programmer write code such that a precondition is tested before accessing the sought after resource.[5]
- Structured programming aims to improve clarity, quality, and readiness by taking advantage of subroutines, block codes, loops, and more.[6]
- The discovery of what is now known as the structured program theorem contributed to the acceptance of structured programming by computer scientists.[6]
- The structured program theorem is composed of control structures: Sequence, Selection, Iteration, and Recursion. It proved that goto statements are not necessary to code programs. It led to Spaghetti code which was difficult to read, maintain, and had a complex structure.[6]
- Conditional statements in computer science allow for the selection between alternatives at runtime.[7]
- If-then-else is widely used in many programming languages. An additional else-if can be used multiple times to combine several conditions. Switch statements also offer the same concept.[7]
- Data Validation is the process of ensuring that data is both correct and useful. [8]
- There are 4 general kinds of validation: Data type validation, Range and constraint validation, Code and Cross-reference validation, and Structured validation. [8]
- Data type validation verifies that the individual characters provided through user input are consistent with the expected characters of one or more known primitive data types: integer, float (decimal), or string. [8]
- Simple range and constraint validation examine user input to ensure it falls within the minimum/maximum range or consistency with a test for evaluating a sequence of characters. [8]
- Code and cross-reference validation include tests for data type validation, combined with one or more operations to verify that the user-supplied data is consistent with one or more external rules. These additional validity constraints may involve cross-referencing supplied data with a known look-up table or directory information service. [8]
- Structured validation allows for the combination of any of various basic data type validation steps, along with more complex processing. In other words, when a piece of data is accepted and continued its operation, it does not mean that it is the correct piece of data. This method examines that the piece of data is entered correctly. [8]
- Different methods of validation include but are not limited to: Allowed character checks, Batch totals, Cardinality check, Check digits, Consistency checks, Control totals, Cross-system consistency checks, Data type checks, File existence checks, Format or picture check, Hash totals, Limit check, Logic check, Presence check, Range check, Referential integrity, Spelling and Grammar check, Uniqueness check, and Table Look Up check. [8]
- Post Validation Actions include:
- Enforcement Action - typically rejects the data entry request and requires the input actor to make a change that brings the data into Advisory Action.[8]
- Advisory Actions - typically allow data to be entered unchanged but sends a message to the source actor indicating those validation issues that were encountered. [8]
- Verification Actions - special cases of advisory actions in which the source actor can be asked to verify that this data is what they would really want to enter, in the light of a suggestion to the contrary. [8]
- Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances. [9]
- Defensive programming practices are often used where high availability, safety or security is needed. [9]
- Defensive programming is an approach to improve software and source code, in terms of:
- General quality – reducing the number of software bugs and problems. [9]
- Making the source code comprehensible – the source code should be readable and understandable, so it is approved in a code audit. [9]
- Making the software behave in a predictable manner despite unexpected inputs or user actions.[9]
- Offensive programming is a category of defensive programming, with the added emphasis that certain errors should not be handled defensively. [9]
- Defensive Programming Techniques:
- Intelligent Source Code Reuse - If existing code is tested and known to work, reusing it may reduce the chance of bugs being introduced. However, reusing code is not always a good practice, because it also amplifies the damages of a potential attack on the initial code. [9]
- Canonicalization - libraries that can be employed to avoid bugs due to non-canonical input. [9]
- Low tolerance against "potential" bugs – Assume that code constructs that appear to be problem prone are bugs and potential security flaws. [9]
Key Terms
edit- assertion
- An assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.[2]
- Boolean expression
- An expression in a programming language that produces a Boolean value when evaluated, i.e. one of true or false.[10]
- conditional statements
- Allow for selection between alternatives at runtime.[7]
- data validation
- The process of ensuring that data have undergone data cleansing to ensure they have data quality, that is, that they are both correct and useful.[11]
- defensive programming
- A form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances.[9]
- EAFP (Easier to Ask Forgiveness than Permission)
- Approach that first attempts the desired action then handles any resulting exceptions.[5]
- exception handling
- The process of responding to the occurrence, during computation, of exceptions. Anomalous or exceptional conditions requiring special processing, often changing the normal flow of program execution.[3]
- GoTo statement
- Performs a one-way transfer of control to another line of code; in contrast, a function call normally returns control.[12]
- if statement
- An if statement is a programming conditional statement that, if proved true, performs a function or displays information.[13]
- LBYL (Look Before You Leap)
- Approach which a precondition is tested before accessing the sought-after resource.[5]
- logic error
- Error that makes the program deliver unexpected results without crashing it.[14]
- relational operator
- A programming language construct or operator that tests or defines some kind of relation between two entities, including numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3).[15]
- runtime error
- Error produced by the runtime system if something goes wrong while a syntactically correct program is running.[14]
- software bug
- An error, flaw, failure or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.[16]
- structured programming
- A programming paradigm aimed at improving the clarity, quality, and development time of a computer program.[6]
- syntax
- Syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language.[17]
- syntax error
- Error that indicates something is wrong with program syntax. It's produced by Python during the translation of the source code into byte code.[14]
- truth table
- A mathematical table used in logic, truth tables can be used to show whether a propositional expression is true for all legitimate input values, that is, logically valid.[18]
See Also
edit- Computer Programming/Conditions
- Jeff Knupp: LBYL vs. EAFP
- TutorialsPoint: Conditions Tutorial with Examples
- Open Book Project: Error Handling
- Scott Dorman: What is "Defensive Programming"?
- Python for Beginners: Exception Handling in Python
- Digital Ocean: How To Write Conditional Statements in JavaScript
References
edit- ↑ Microsoft: Exam 98-381 Introduction to Programming Using Python
- ↑ 2.0 2.1 Wikipedia: Assertion (software development)
- ↑ 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 Wikipedia: Exception handling
- ↑ 4.0 4.1 4.2 Python Documentation: Errors and Exceptions
- ↑ 5.0 5.1 5.2 5.3 5.4 5.5 Wikipedia: Python syntax and semantics
- ↑ 6.0 6.1 6.2 6.3 Wikipedia: Structured programming
- ↑ 7.0 7.1 7.2 Wikipedia: Conditional (computer programming)
- ↑ 8.00 8.01 8.02 8.03 8.04 8.05 8.06 8.07 8.08 8.09 Wikipedia: Data Validation
- ↑ 9.00 9.01 9.02 9.03 9.04 9.05 9.06 9.07 9.08 9.09 Wikipedia: Defensive programming
- ↑ Wikipedia: Boolean expression
- ↑ Wikipedia: Data validation
- ↑ Wikipedia: Goto
- ↑ Definition: If statement
- ↑ 14.0 14.1 14.2 How to Think Like a Computer Scientist (Swarthmore Comp Sci Version)ː Debugging
- ↑ Wikipedia: Relational operator
- ↑ Wikipedia: Software bug
- ↑ Wikipedia: Syntax (programming languages)
- ↑ Wikipedia: Truth table