+ All Categories
Home > Documents > General Control Issues

General Control Issues

Date post: 09-Apr-2018
Category:
Upload: zabiriphah
View: 218 times
Download: 0 times
Share this document with a friend

of 50

Transcript
  • 8/8/2019 General Control Issues

    1/50

    General control IssuesGeneral control Issues

  • 8/8/2019 General Control Issues

    2/50

    Contents.Contents.

    y Boolean Expressions

    y Compound statements

    y

    Null statementsy Taming Dangerously Deep Nesting

    y A Programming Foundation: StructuredProgramming

    y Control Structures and Complexity

  • 8/8/2019 General Control Issues

    3/50

    Boolean ExpressionsBoolean Expressions

    y Use the identifiers

    True or false

    Dont use flags like 0 or 1.

    x By using flags we may cant understand that which is

    representing true and which one is representingfalse.

  • 8/8/2019 General Control Issues

    4/50

    Visual Basic Examples of Using Ambiguous FlagsVisual Basic Examples of Using Ambiguous Flagsfor BooleanValuesfor BooleanValues

    Dim printerError As Integer

    Dim reportSelected As Integer

    Dim summarySelected As Integer

    ...If printerError = 0 Then InitializePrinter()

    If printerError = 1 Then NotifyUserOfError()

    If reportSelected = 1 Then PrintReport()If summarySelected = 1 Then PrintSummary()

    If printerError = 0 Then CleanupPrinter()

  • 8/8/2019 General Control Issues

    5/50

    Visual Basic Examples of UsingVisual Basic Examples of Using TrueTrue andand FalseFalse for Tests Insteadfor Tests Insteadof NumericValuesof NumericValues

    Dim printerError As Boolean

    Dim reportSelected As ReportType

    Dim summarySelected As Boolean

    ...If ( printerError = False ) Then InitializePrinter()

    If ( printerError = True ) Then NotifyUserOfError()

    If ( reportSelected = ReportType_First ) Then

    PrintReport()If ( summarySelected = True ) Then PrintSummary()

    If ( printerError = False ) Then CleanupPrinter()

  • 8/8/2019 General Control Issues

    6/50

    Making complicated expressionsMaking complicated expressions

    simple.simple.

    y Several steps to simplify complicated

    expressions.

    Break complicated tests into partial tests with

    new boolean variables.

    Move complicated expressions into booleanfunctions.

    x If a test is repeated often.

    x Distracts from the main flow of the program

    x Make function of the code for test purpose.

  • 8/8/2019 General Control Issues

    7/50

    Visual Basic Example of a Complicated TestVisual Basic Example of a Complicated Test

    If ( ( document.AtEndOfStream ) And ( Not inputError ) )

    And _

    ( ( MIN_LINES

  • 8/8/2019 General Control Issues

    8/50

    Example of complicated code with newExample of complicated code with new

    boolean variable to make it easy.boolean variable to make it easy.Function DocumentIsValid( _

    ByRef documentToCheck As Document, _

    lineCount As Integer, _

    inputError As Boolean _

    ) As BooleanDim allDataRead As Boolean

    Dim legalLineCount As Boolean

    allDataRead = ( documentToCheck.AtEndOfStream ) And ( NotinputError )

    legalLineCount = ( MIN_LINES

  • 8/8/2019 General Control Issues

    9/50

    Contd.Contd.

    y In above example.

    Error processing() is a boolean function thatindicates the processing status.

  • 8/8/2019 General Control Issues

    10/50

    Example of the Main Flow of the Code Without theExample of the Main Flow of the Code Without theComplicated TestComplicated Test

    If ( DocumentIsValid( document, lineCount, inputError ) )

    Then

    do something or other

    ...

    End If

  • 8/8/2019 General Control Issues

    11/50

    Forming boolean expressions positivelyForming boolean expressions positively

    y Many people cant understand negative expressions.

    y An example of negative IF ELSE code.

    if ( !statusOK )

    {

    // do something

    ...

    }

    else {

    // do something else

    ...

    }

  • 8/8/2019 General Control Issues

    12/50

    Contd.Contd.

    y The previous code is now changed to

    positive expressions.if ( statusOK ) {

    // do something else...

    }

    else {

    // do something

    ...

    }

  • 8/8/2019 General Control Issues

    13/50

    Apply DeMorgan Law.Apply DeMorgan Law.

    y It converts doubly negated code into one

    negation.

    y E.gif ( !displayOK || !printerOK ) ...

    y After applying DeMorgan law.

    if ( !( displayOK && printerOK ) ) ...

  • 8/8/2019 General Control Issues

    14/50

    Transformation of logical expression underTransformation of logical expression under

    DeMorgans theoremDeMorgans theorem

    Initial Expression Equivalent Expression

    Not A not B Not (A or B)

    Not A and B Not (A or not B)

    A and not B Not (not A or B)

    A and B Not (not a or not B)

    Not A or not B Not (A and B)

    A or not B Not (not A and B)

    A or B Not (not A and not B)

  • 8/8/2019 General Control Issues

    15/50

    Using Parentheses to Clarify BooleanUsing Parentheses to Clarify Boolean

    ExpressionsExpressions

    y When expressions are complex dont rely

    on language evaluation order.

    y User proper parentheses to makereadable code.

    y Example of few parentheses.if ( a < b == c == d ) ...

    y Example of better parentheses.if ( ( a < b ) == ( c == d ) ) ...

  • 8/8/2019 General Control Issues

    16/50

    Writing Numeric Expressions in numberWriting Numeric Expressions in number--

    line orederline oreder

    y Organize numeric tests so that they

    follow the points on a number line.

    y The idea is to order the elements left toright, from smallest to largest.

    Example:MIN_ELEMENTS

  • 8/8/2019 General Control Issues

    17/50

    Contd.Contd.

    y testing i against MIN_ELEMENTS only, position of ivaries

    depending on where i is when the test is successful. If i is

    supposed to be smaller.

    while ( i < MIN_ELEMENTS ) ...

    y But ifi is supposed to be larger, youll have a test like

    while ( MIN_ELEMENTS < i ) ...

    y This approach is clearer than tests like

    ( i > MIN_ELEMENTS ) and ( i < MAX_ELEMENTS )

  • 8/8/2019 General Control Issues

    18/50

    Comparisons to 0.Comparisons to 0.

    y Programming languages use 0 for several

    purposes.

    Null terminator for string.

    Lowest address for a pointer.

    Value of first item in an enumeration.

    False in logical expression.

  • 8/8/2019 General Control Issues

    19/50

    Contd.Contd.

    y Compare numbers to 0

    Example:

    while(balance !=0)

    rather than

    while(balance)

    y Compare characters to null terminator

    explicitly.while ( *charPtr != '\0' ) ...

    rather than

    while ( *charPtr ) ...

  • 8/8/2019 General Control Issues

    20/50

    Contd.Contd.

    y Compare pointers to NULL.

    while ( bufferPtr != NULL ) ...

    rather than

    while ( bufferPtr ) ...

  • 8/8/2019 General Control Issues

    21/50

    Common problems with BooleanCommon problems with Boolean

    expressionsexpressions

    y In C and C++, put constants on the left

    side of comparisons

    C++ poses some special problems with

    boolean expressions. Interchanging bitwise operators with logical

    operators is common problem.

    Example:if ( MIN_ELEMENTS = i ) ... (Error)

    if ( i = MIN_ELEMENTS ) ... (No error)

  • 8/8/2019 General Control Issues

    22/50

    Contd.Contd.

    y In Java, difference between a==b and

    a.equals(b)

    a==b tests for whether a and b refer to the same

    object

    a.equals(b) tests for whether the objects have the

    same logical value

  • 8/8/2019 General Control Issues

    23/50

    2.Compound statements.2.Compound statements.

    y Compound statement or block is a

    collection of statements that are treatedas a single statement

    y Compound statements are created by

    writing { and } in C,C++,C# and java.

  • 8/8/2019 General Control Issues

    24/50

    Guidelines for using compound statements.Guidelines for using compound statements.

    y Write pair of braces together.

    Complaint about to match pairs of braces.

    How to know begin and end braces.

    Unnecessary problem

  • 8/8/2019 General Control Issues

    25/50

    Guideline to solve braces problemGuideline to solve braces problem

    y Write this first: for ( i =0; i < maxLines;i++ )

    y Write this next: for ( i =0; i 0.0 ) {

    // do something

    ...

    }

    else {

    // do something else

    ...

    }

  • 8/8/2019 General Control Issues

    43/50

    Contd.Contd.

    // selection in a case statement

    switch ( commandShortcutLetter ) {

    case 'a':

    PrintAnnualReport();

    break;case 'q':

    PrintQuarterlyReport();

    break;

    case 's':

    PrintSummaryReport();

    break;

    default:

    DisplayInternalError( "Internal Error 905:Call customer support." );

    }

  • 8/8/2019 General Control Issues

    44/50

    Contd.Contd.

    y Iteration:

    Iteration causes to a group of statements tobe executed multiple times.

    Iteration is used as commonly in loop. For, while,

  • 8/8/2019 General Control Issues

    45/50

    Visual basic example of Iteration.Visual basic example of Iteration.

    ' example of iteration using a For loopFor index = firstTo last

    DoSomething( index )

    Next

    ' example of iteration using a while loop

    index = firstWhile ( index

  • 8/8/2019 General Control Issues

    46/50

    Control structure and complexity.Control structure and complexity.

    y Control structure have a big contribution

    in overall program complexity.

    Poor use increases complexity.

    Good use decreases complexity.

  • 8/8/2019 General Control Issues

    47/50

    How to measure complexity.How to measure complexity.

    y Researchers gave many techniques to

    measure complexity.

    y The most influential of the numeric

    techniques is Tom McCabes. Measure complexity by counting number of

    decision points.

  • 8/8/2019 General Control Issues

    48/50

    Contd.Contd.

    y 1. Start with 1 for the straight path through the routine.

    y 2. Add 1 for each of the following keywords, or theirequivalents: if while repeat for and or

    y 3. Add 1 for each case in a case statement.

    Example:

    if ( ( (status = Success) and done ) or

    ( not done and ( numLines >= maxLines ) ) ) then ...

    In this fragment, you count 1 to start; 2 for the if; 3 for theand; 4 for the or; and5 for the and. Thus, this fragment

    contains a total of five decision points.

  • 8/8/2019 General Control Issues

    49/50

    What to do with complexity measurement.What to do with complexity measurement.

    y Number to analyze routines.05 The routine is probably fine.

    610 Start to think about ways to simplify the routine.

    10+ Break part of the routine into a second routineand call it from the first routine.

  • 8/8/2019 General Control Issues

    50/50

    Thank-U


Recommended