Quick Index
Method summaries may be the most valuable tool in object coding. They allow object coders to determine which methods to send to objects and how to send them. Here is a method summaries listing example from the Java library of the String class. (see section "Method Summary")

Example - The Vanishing Code Blob Magic Trick 💡


Logic Problem: Do the Frames Intersect?
Algorithm Without Using Method Summaries 🐞
Here is a method that has a simple concept: To figure out if two frames (rectangles) intersect, and return true if they do.

Wow! Need we say more. Complexity beyond belief.
intersects1(other) {
	/*
		Method in "Frame" class
		Return true if this (rectangular) frame intersects "other" (also a Frame)
		Both are simple rectangular frames that have (point object) corners "lt", "rt", "rb" and "lb"
		(i.e. left-top, right-top, right-bottom, and left-bottom).
		We define an intersection if any corner of "other" is in "this" frame object.
		If other corner is "on" any of our edges, we'll call that inside (yes an intersection)
		Assume frame y-axis is downward (common in computer graphics)
	*/
	//Algorithm -- no helper (buddy) methods
	let
		lt = new Point(other.getLeft(), other.getTop()),
		rt = new Point(other.getRight(), other.getTop()),
		rb = new Point(other.getRight(), other.getBottom()),
		lb = new Point(other.getLeft(), other.getBottom());
	return ((lt.getX() >= this.getLeft() && (lt.getX() <= this.getRight()))
		&& (lt.getY() >= this.getTop() && (lt.getY() <= this.getBottom())))
		|| ((rt.getX() >= this.getLeft() && (rt.getX() <= this.getRight()))
			&& (rt.getY() >= this.getTop() && (rt.getY() <= this.getBottom())))
		|| ((rb.getX() >= this.getLeft() && (rb.getX() <= this.getRight()))
			&& (rb.getY() >= this.getTop() && (rb.getY() <= this.getBottom())))
		|| ((lb.getX() >= this.getLeft() && (lb.getX() <= this.getRight()))
			&& (lb.getY() >= this.getTop() && (lb.getY() <= this.getBottom())))
}

Class Frame -- Method Summary
  • //...
  • getLT() -- Return left top point
  • getRT() -- Return right top point
  • getRB() -- Return right bottom point
  • getLB() -- Return left bottom point
  • contains(aPoint) -- Return true if this frame contains method param "aPoint"
  • //...
Algorithm Using Method Summaries ✔
Same method as above.

But before writing logic we have looked at method summaries (previous step) found some friendly helper methods.

The big blob of code in the previous step has evaporated.

But the best thing is in regard to testing and bug fixing. Code bugs like to live in large blobs of logic.
intersects2(other) {
	//Method in "Frame" class
	let
		lt = other.getLT(),
		rt = other.getRT(),
		rb = other.getRB(),
		lb = other.getLB();
	return this.contains(lt) || this.contains(rt) || this.contains(rb) || this.contains(lb);
}



Example - Choose to Simplify!


Algorithm Without Using Method Summaries 🐞
A method that simply returns the first element from an array (ivar).
first() {
	//We would also validation here
	let array = this.getArray();
	let elem = array[0];
	return elem;
}
Algorithm Using Method Summaries ✔
Let us say we look in the method summaries (menu) for this object, and we and find "get".

There are many advantages to calling "this.get" (we'll discuss this next).
first() {
	//We would also validation here
	return this.get(0);
}
Advantages
Here are just a couple (big) advantages:

  • Methods (like "get") are individually tested so we can trust them (if we write new code, the testing starts over)
  • Methods (like "get") can be easily debugged, spied on, logged, etc -- generally ivar access cannot be
first() {
	//We would also validation here
	return this.get(0);
}


Advantages


Advantages of using method summaries:


Disadvantages


Disadvantages of using method summaries:


Conclusion


If a method is available for you to use -- go for it (use it). We can assume it is tested (because all code should be).

Simplifying logic should be are number one goal.

Navigation