+ All Categories
Home > Documents > Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN...

Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN...

Date post: 22-Dec-2015
Category:
View: 215 times
Download: 2 times
Share this document with a friend
Popular Tags:
14
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same thing somewhere else in the application Rather than retyping the code twice (or even more than twice….), code can be written in a general (sub/function) procedure and can be called into use from other procedures (therefore promoting reuse)
Transcript
Page 1: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Promoting Code Reuse

Often in programming, multiple procedures will perform the same operation

IN OTHER WORDS – the same piece of code will do the same thing somewhere else in the application

Rather than retyping the code twice (or even more than twice….), code can be written in a general

(sub/function) procedure and can be called into use from other procedures (therefore promoting

reuse)

Page 2: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Promoting Code ReuseClick each command button triggering the click event procedure associated with each one

Page 3: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Promoting Code Reuse

Now when we click on the hello command button we trigger the click event procedure associated with that command button but also call the other procedures which execute in turn producing the same output

Page 4: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Creating a ‘Sub Procedure’

The coding of a new procedure is similar to the other event procedures that we have been coding,

but is not in itself attached to any event

Therefore, this code cannot be executed unless a CALL is specified from another procedure, for the

General Sub Procedure

To call a Sub Procedure, just give the procedure name

Page 5: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Creating a ‘Sub Procedure’

• 2 ways to create a General Sub Procedure

• [1] simply type in the procedure definition in the code window

• [2] use the tools / add procedure menu command

Page 6: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Type sub myprocedure and press return on the keyboard and the procedure is created

Page 7: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Very Important - visibility• Sub procedures are by default Public in all

modules, which means they can be called from anywhere in the application

• So what is the scope of this newly created procedure?

Page 8: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Very Important - visibility

So you should put the PRIVATE keyword in front of the sub keyword to reduce the procedures visibility

Page 9: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

tools / add procedure menu command

Page 10: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Creating a ‘Sub Procedure’

The Sub statement declares the name, arguments, and code that will form the body of a subroutine / sub procedure as follows:

[Private | Public] Sub ProcedureName [(arguments)]

statements

End Sub

Page 11: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Advantages of a ‘Sub Procedure’

Code is easier to write and debug - test individual modules

Code is maintainable

Improves code structure

Decreases the size of individual procedures and often the size of the program itself

Disadvantages of a ‘Sub Procedure’

Requires extra preparatory work

Need to understand logical design of code structure

May require more CPU time and memory

Page 12: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Calling a ‘Sub Procedure’

The use of the keyword Call is optional but required in certain cases:

ProcedureName

Call ProcedureName

Call ProcedureName ( )

An Event Procedure can also be called

Command1_Click

Call Command1_Click

Call Command1_Click ( )

Page 13: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Scope of Variables: General Sub Procedures

Example 1

Page 14: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.

Example 1Scope of Variables:

General Sub Procedures


Recommended