Code Analysis: Methods

May 31, 2017

We’ve already gone through a lot of different programming language constructs, however we haven’t really talked about a very highly used feature.  How do we think about methods using these diagrams.

So there’s a lot we could talk about methods in general, but I want to avoid any specific digression into what object oriented and by extension methods actually mean or could mean.  We’re just going to go with a simplistic explanation and then show how to represent that.  Hopefully it can be adapted and modified to handle any special features that exist in different programming language.

interface Choice {
     void One();
     void Two ();
     void Three (); 
} 
class A : Choice {
     public void One() { }
     public void Two() { }
     public void Three() { }
}
class B : Choice {
     public void One() { }
     public void Two() { }
     public void Three() { } 
}

We can imagine creating two different classes that implement the same interface.  The methods on this class are basically just a function pointer that is attached to some data and then has some sort of dynamic lookup mechanism.  If we call an instance of Choice, then the result is going to be a lookup based off of what class instance we are using.

The diagram here shows us kind of what this is going to look like.  Someplace we have a bunch of different function call trees.  These call trees are somehow related to some sort of data or reference in the main program call tree.  Someplace inside of the main program call tree we may end up using the data or reference such that we call into the associated call tree.  At this point we end up entering some sort of dynamic call tree.

Methods (and also higher order functions) represent a dynamically chosen sub call tree that fills a hole in the main call tree.

This sort of thing is actually very similar to what conditionals do.  Once you have a programming language that has some sort of conditional execution, then you also end up with call trees that abstractly represent all possible call trees that the program can exhibit.  Methods do the same thing, but are like a higher order conditional.  You can pass methods around, modify them, and conditionally decide which method to send to the call point.