Methods

Review: Variables

Variables abstract values.

The name of the variable hides the details of what particular value it has at any time.

2 * 5 is concrete. It means double 5.

2 * x is abstract. It means double any value.

Methods

Methods abstract procedures. (“Procedural abstraction”)

public double distance(double a, double b) {
  return Math.abs(a - b);
}

distance is now a black box that we can use:

distance(3, 4) ⟹ 1.0

distance(-13.5, 23.7) ⟹ 37.2

Method signatures

A method is identified by its signature which consists of three parts:

  • The name of the method.

  • The number and types of its parameters.

  • Its return type.

Example

public double distance(double a, double b) {

The name is distance.

It has two parameters, a and b.

It returns a double.

(The public is a bit of Java gorp we’ll explain later in the year.)

Calling methods

The method signature gives us the information we need to call the method.

From its signature, we know to call distance we need two double values to pass as arguments.

And we know it will return a double value that we should probably do something with.

Argument passing

distance(an expression that produces a double, and another one)

Method calls are expressions

If a method has a non-void return type, a call to the method is an expression.

distance(15.0, 20.0) * 2 ⟹ 10

Methods can call methods

public double taxicabDistance(int x1, int y1, int x2, int y2) {
  return distance(x1, x2) + distance(y1, y2);
}

Extremely important!

While we usually describe a method signature with names for the parameters, those names are only meaningful within the body of the method.

double distance(double a, double b)
double distance(double n1, double n2)

These are effectively the same signature.

When calling a method you don’t need to know or care what the method’s parameters are named.

When writing methods

Choose meaningful names for your parameters.

Different methods can have parameters with the same names.

It is often a good idea to use the same names between different methods when the parameters represent the same thing.

For example

public double manhattan(double xDistance, double yDistance) {
  return xDistance + yDistance;
}

And

public double cartesian(double xDistance, double yDistance) {
  return Math.sqrt(
    Math.pow(xDistance, 2) + Math.pow(yDistance, 2)
  );
}

Two kinds of methods

Generally speaking we can divide most methods into one of two categories:

  • Methods that compute values

  • Methods that are called for their side effects.

Some methods do both but they should be rare.

“Pure” methods are easier to understand

Methods that only compute values where the value is only based on the method’s arguments are called “pure”.

Pure methods are much easier to think about because they are completely self contained.

But ultimately you need some impure methods

Ultimately we’re running a program for some reason which will be expressed in some side effect such as printing an answer on the screen.

But the more of your program is made up of pure methods, the easier it will be to understand and test your code.