1. Developing Standard Subroutines for Re-Use

  • Most programmers create and maintain their own library of source code. 
    • When new projects are being developed much of this code can be reused. 
    • In 8.2.1.2 (Davis, chapter 4), we discussed modularity as a method used to enhance the reusability of code. 
      • If modules are developed as self-contained units, then little or no modification will be required to reuse them in new projects.
  • There are also commercial, open source and public domain libraries where modules can be obtained. 
    • For example, many languages are able to use Active X controls and other .COM components.  
    • These components are available as compiled units (often dynamic linked libraries or DLLs) that provide enhanced capabilities to a variety of programming languages. 
  • Many programmers maintain a library of such components and controls for future use. 
  • There are currently many companies who specialise in the development and distribution of such components.


In this section, 

  • we consider the development of some standard routines suitable for inclusion in a library of code. 
  • We also consider methods of including and using code and modules from different sources within new projects. 
    • This includes adding the code to the project, calling the code and also sharing and passing data to and from different modules.


DEVELOPING STANDARD SUBROUTINES FOR REUSE

When developing code for reuse, it is important to consider a more generic case than the one presented for the current project. Standard routines will be more useful if they solve a more generic case of the problem rather than the precise current problem. 

  • For example, a routine written to sort the data in an array would be more useful if it could sort both numerical and string data in either ascending or descending order. 
  • If a routine can be written that requires no modification then we can thoroughly test the module when first developed and then use it with confidence in future software projects. 
  • More generic solutions are likely to require minimal changes to work within new projects.

CASE STUDY:  Data Validation

Data validation is a process used by most software. Without a robust data validation routine, user input will often be the cause of runtime errors. It makes sense to develop standard routines to perform this process.

  • It is common practice to obtain user input as string data
  • The string is then analysed according to the validation rules appropriate to the particular variable and context. 
  • If the data is appropriate then no visible action takes place. 
  • On the other hand, if the data is out of the required range or is of the wrong type some corrective action is required.

1. Validating Non-Negative Integers

Let us develop a Visual Basic routine for validating non-negative integers. The following general requirements have been formulated:

  • User input is to be in string form presumably via a text box control. This string forms the input to the validation routine.
  • If the user enters any digits these will become the input. For example, a3&9.4 should be returned by the routine as 394.
  • The routine should also ensure the integer is within a specified range. For example, if the input was a mark out of 100 then the range would be from 0 to 100.
  • Any input that is inappropriate should result in a message box being displayed with an appropriate message. In this case the routine should return a default value.

An algorithm is first developed using pseudocode. This algorithm is reproduced below


BEGIN ValidateInteger(InputString,Min,Max,Default)
WHILE more characters in InputString
IF current character is a digit THEN
Add the character to StringInteger
ENDIF
Move to next character in InputString
ENDWHILE
Set ValidInt to value of StringInteger
IF StringInteger is empty OR ValidInt is not within Min to Max THEN
Set ValidInt to Default
Display message box prompt
ENDIF
RETURN ValidInt
END ValidateInteger









def validateInteger(InputString, Min, Max, Default):

    StringInteger = ""

    for char in InputString:

        if char.isdigit:

            StringInteger += char

    ValidInt = int(StringInteger)

    if (len(StringInteger)==0 or ValidInt < Min or ValidInt> Max):

        ValidInt = Default

        print("Invalid Integer, default returned")

    return ValidInt


num = validateInteger(input("Enter Number"),0,10,9)

print(num)








Comments