concept procedural code in category php

appears as: procedural code
PHP in Action: Objects, Design, Agility

This is an excerpt from Manning's book PHP in Action: Objects, Design, Agility.

Features to modify visibility are often absent or inconspicuous in dynamically typed languages; this is logical, since these languages tend to let programmers do whatever seems convenient without too many artificial boundaries. On the other hand, the ability to control the visibility of methods and instance variables can be seen as a natural extension of the ability to restrict the scope of an ordinary variable in procedural code.

All refactoring aims at making code easier to read and understand, but the main tool to improve readability is one of the simplest refactorings: Extract Method. Probably all programmers have done it at some point. Even if you’ve never written a line of object-oriented code, you’ve probably extracted a function from procedural code, and that’s the same thing in principle.

Extracting functions or methods to enhance readability is simple in theory. If we have a few lines of code whose intention can be described in a few words, we make it a separate method or function. What is challenging sometimes is the fact that enclosing code in a function or method makes all the variables local to that function or method. Long stretches of procedural code tend to have lots of temporary variables that might or might not be used before or after the chunk we want to extract. To make it work, we may have to pass one or more variables as arguments and return one or more variables. The idea is to do it if it enhances readability and to ignore temporarily other problems we’re not currently working on; the result doesn’t have to be perfect or “right,” just better.

11.5.1. Getting procedural code under test

In a certain ideal sense, the best way to make procedural code testable is to make it object-oriented first. Or rather, it would be if we weren’t likely to break it on the way from procedural to OO. We really need some way to make it testable without such radical surgery. We want to be able to make procedural code testable without having to make it object-oriented first. There are three ways to do this without changing the code.