posted by Matt Ward on Dec 4, 2010.
In this post, I will be sharing a technique that I started using to help teach other people how to program and code, and which I still use from time to time in my own work. Basically, we will be looking at how to manually resolve a series of nested functions (or chained methods), and some of the benefits of using such a technique.
Over the course of the past decade or so, I have occasionally been responsible for trying to teach a few different people the basics of different programming languages. For the most part, the languages have been from the old, procedural family that includes languages like dBase and Clipper, which are pretty much entirely irrelevant to the web.
- They are instructional. Basically, and program or script is just a series of instructions that tell the computer (or other device) what to do, based on different conditions that may arise, usually based on user input.
- They also use functions, which are generally routines that accept a particular set of arguments, run those arguments through a process and then return a particular value (though, in some contexts, a function–especially a user-defined function–may simply execute and not return anything at all). Methods, of course, are essentially similar to functions, but use a different syntax.
Break It Down
Basically, it all comes down to an exercise of what one might call formulaic resolution. I’m sure there’s some sort of technical term, but basically what I mean by this is the way that a function – and more importantly, a series of nested functions – is executed. If you remember the lessons of middle school and/or high school math classes, it shouldn’t be to difficult to work through this simple formula to derive the answer, provided we have the necessary values.
z = x + (y * 7) – (9 + ( 20 / ( x / 2 ) ) )
It’s just basic algebra, really, and the way I was always resolve this kind of problem was to work through it one step at a time, starting with the innermost values and working your way out. So, assuming that x is equal to 4 and y is equal to 6, in a step by step process, we would solve the above equation as follows:
z = x + ( y * 7 ) – ( 9 + ( 20 / ( x / 2 ) ) ) ⇓ z = 4 + ( 6 * 7 ) – ( 9 + ( 20 / ( 4 / 2 ) ) ) ⇓ z = 4 + ( 42 ) – ( 9 + ( 20 / 2 ) ) ⇓ z = 4 + ( 42 ) – ( 9 + 10 ) ⇓ z = 4 + 42 – 19 ⇓ z = 46 – 19 ⇓ z = 27
Simple as pie really. All we needed to do was start with the innermost values and just do the basic arithmetic, continuously reducing the expression until we come up with our final result.
Doing it in Code
In programming, seemingly complex expressions built on a multitude of nested functions (or methods), work in very much the same way. For example, suppose that we have a name, such as, mine saved to a variable of some sort, and in the format Matthew D. Ward. Now, let’s suppose that we wanted to programatically flip the name so that my last name came first, followed by a comma, a space and then my given names. We’ll also assume that we want to convert the entire string to uppercase. In PHP, we could use functions do it all in a single expression like this:
$name = "Matthew D. Ward"; echo strtoupper(substr($name,strrpos($name," ")) . ", " . substr($name,0,strrpos($name," ")));
Although it looks a bit different than our mathematical example, the exact same principle of reduction can still be applied here to solve the “problem” and determine the output. We’ll go through it, but first (because I don’t know how familiar all readers are with PHP) let’s take a quick look at the three functions that we will be using:
- strrpos( ) – this function accepts two strings and searches for the index (numerical position, starting at 0) of the last instance of the second string within the first.For example: strrpos(“123456789”, “5”) would return a value of 4.
- substr( ) – this function accepts a string and either one or two numeric values. It will than return a section of the string, starting at the position of the first number and continuing for a length equal to the second number (if it is provided). For example: substr(“ABC DEF GHI”, 4 ,3) would return a value of “DEF”
- strtoupper( ) – this function accepts a single string and simply returns it in all uppercase.For example: strtoupper(“jaZZy JaZz”) would return “JAZZY JAZZ”
Now that we’re all on the same page in terms of what our functions will actually do, we can very easily reduce our PHP statement, starting with the innermost functions and resolving in an outwardly fashion.
echo strtoupper(substr($name,strrpos($name," ")) . ", " . substr($name,0,strrpos($name," "))); ⇓ echo strtoupper(substr("Matthew D. Ward",strrpos("Matthew D. Ward"," ")) . ", " . substr("Matthew D. Ward",0,strrpos("Matthew D. Ward"," "))); ⇓ echo strtoupper(substr("Matthew D. Ward",10) . ", " . substr("Matthew D. Ward",0,10)); ⇓ echo strtoupper("Ward" . ", " . "Matthew D."); ⇓ echo strtoupper("Ward, Matthew D.") ⇓ echo "WARD, MATTHEW D."
Obviously, things can get a lot more complicated than this. Specifically, I think of part of the code for my Contained Sticky Scroll jQuery plugin, which I also tried to explain in a recent tutorial. In the part of the code that actually controls the actual scrolling, there is a very complex conditional statement that uses all kinds of different functions and jQuery methods to determine if the particular condition is true or false.
Actually, it was as I was working through that exact bit of code that I decided that this might be an article worth writing, and which some people might find useful
How is it Relevant?
So what’s the point of all this? How is it relevant to our day to day work? Well I think there are a number of circumstances in which it can be truly useful.
- Teaching/Learning. As I mentioned in the opening, reducing complex code by resolving functions in this manner is a technique that I started using to teach others some of the basics of programming. More specifically, it helps show how even the most complex routines are really nothing more than a series of small and simple steps. Sometimes programming can seem vastly more daunting than it really is and this technique can help dispel that aura.
- Dissect another person’s code. There’s quite often more than one way to accomplish a particular goal in code, and different people will develop different coding styles. For instance, I like to do as much as possible in a single line of code, which can involve a lot of nested functions and/or chained methods. Other coders, however, might prefer to break this up over multiple lines. When we inherit code from a programmer with a different style, it can sometime seem foreign and inaccessible. Resolving functions is a great way to make sense of the code and help you understand what you’re actually working with.
- Debugging. Similarly, and for many of the same reasons, this technique can also be useful in debugging. If you can isolate an issue to a line (or even a few lines) of code, then carefully resolving the functions and methods manually can often help you find the problem, and ultimately solve it.
As you can see, then, this sort of function resolution can be useful for coders and programmers of all levels – from the novice just trying to wrap their head around the concept, to the seasoned pro trying to iron out a bug or dig into someone else’s code. I hope you will find it useful too!
What do you think? Have you ever used this kind of technique? Do you think it might be useful to you?Post A Comment
Also from Echo Enduring Media: