Lesson 3
Lesson 3
Methods
A module is an independent program or code unit that can be
referenced by certain means. A method (as a programming
unit) is a module, so is a group of methods if a language has
a construct to reference such a collection. A class is a
module, as is a (Java) package containing a group of classes.
In this fashion, a module can also be a combination of
methods and classes. A method, with a name to represent
either a procedure (with no “value” returned) or a function
(to return a “value”), is usually the smallest unit of a module
that can be independently referenced. Thus, design of
methods is essential to module design. A method
encapsulates a computational task using the information
from the caller’s code environment. A method typically has
parameters to deliver the information or data the method
needs to complete the task. Overview
A method can be independent, part of a bigger module,
or associated with an object. When a method is
associated with a class, it is often an instance method.
An instance method is often a void method (like a
classic procedure) with a task resulting in modifications
to an object’s state (consisting of current values of
instance data fields). An independent method, often
with a modifier static, can be a function that returns
computed information through a return statement or
out-mode parameters (if the language supports). An
independent method can also be a void method to
update values of variables in an external scope.
Overview
Essential Characteristics of a
Our ability to decomposeMethod
a complex problem depends on our ability
to abstract away the details that can be considered later. Methods
provide a low-level abstraction to encapsulate process details such
as business rules, proprietary practices, algorithms, details for data
source connections, or basic data structures. A method can also use
other methods to delegate some of its implementation complexity.
Thus, methods can also provide higher level abstractions to work
collaboratively with lower-level counterparts. To appropriately design
a method to balance its role between detail encapsulation and high-
level modular abstraction, we need an understanding about general
module attributes expected and the design goals we intend to
achieve
Procedural Abstraction and
Modularity
We use the term procedural abstraction to
describe the notion of abstracting away variations
of method implementations with appropriately
designed parameters. Thus, an invocation of the
method only represents an execution of the
method with a particular set of parameter values.
Procedural abstraction has the following
characteristics (Liskov & Guttag, 2000):
Procedural Abstraction and
Modularity
Parametric abstraction: Parameters of a method abstract away
the input data the method uses, so that the method invoked repeatedly
with different values of parameters completes different variants of a task