Why we need to create a routine?
Aside from the computer itself, the routine is the single greatest invention in computer science. The routine makes programs easier to read and easier to understand than any other feature of any programming language.
Today I will show what is a routine and some reasons to create a routine.
I. What is a routine?
A routine is an individual method or procedure invocable for single purpose. Example include a function in C++, PHP, a method in Java, a function or sub procedure in Microsoft Visual Basic.
II. Reasons to create a routine
Here’s a list of valid reasons to create a routine.
- Reduce complexity:
The single most important reason to create a routine is to reduce a program’s complexity. Create a routine to hide information so that you won’t need to think about it.
Sure, you’ll need to think about it when you write the routine. But after it’s written, you should be able to forget the details and use the routine without any knowledge of its internal workings.
Example: In Laravel, you will see something like this
- Introduce an intermediate, understandable abstraction:
Putting a section of code into a well-named routine is one of the best ways to document its purpose. Here is an example
The new routine is so short that nearly all it needs for documentation is a good name. The name introduces a high level of abstraction than the original three lines of code, which makes the code more readable and easier to understand, and it reduces complexity within the routine that originally contained the code.
- Avoid duplicate code:
Undoubtedly the most popular reason for creating a routine is to avoid duplicate code.
With code in one place, you save the space that would have been used by the duplicated code. Modification will be easier because you will need to modify the code in only one location.
The code will be more reliable because you will have to check only one place to ensure that the code is right.
- Hide sequences:
It’s a good idea to hide the order in which events happen to be processed. Example
Using PopStack() routine will hide the assumption about the order in which the two operations must be performed. Hiding that assumption will be better than baking it into code from one end of the system to the other.
- Improve portability:
Use of routines isolates nonportable capabilities, explicitly identifying and isolation future portability work. Nonportable capabilities include nonstandard language features, hardware dependencies, operating-system dependencies, and so on.
- Simplify complicated boolean tests:
Understanding complicated boolean tests in detail are rarely necessary for understanding program flow. Example
Putting such a test into a function makes the code more readable because the details of the test are out of the way and a descriptive function name summarizes the purpose of the test.
Giving the test a function of its own emphasizes its significance. It encourages extra effort to make the details of the test readable inside its function. The result is that both the main flow of the code and the test itself become clearer. Simplifying a boolean test is an example of reducing complexity, which was discussed earlier.
- Improve performance:
You can optimize the code in one place instead of in several places. Having code in one place will make it easier to profile to find inefficiencies. Example
Centralizing code into a routine means that a single optimization benefits all the code that uses that routine, whether it uses it directly or indirectly. Having code in one place makes it practical to recode the routine with a more efficient algorithm or in a faster, more efficient language.
- To ensure all routines are small?:
No. With so many good reasons for putting code into a routine, this one is unnecessary. In fact, some jobs are performed better in a single large routine.
Operations that seem too simple to put into routines
One of the strongest mental blocks to creating effective routines is a reluctance to create a simple routine for a simple purpose. Constructing a whole routine to contain two or three lines of code might seem like overkill, but experience shows how helpful a good small routine can be.
Small routines offer several advantages. One is that they improve readability. Here is an example:
This function DeviceUnitsToPoints is more readable – even approaching self-documenting.
This example hints at another reason to put small operations into functions: small operations tend to turn into large operations. I didn’t know it when I wrote the routine, but under certain conditions and when certain devices were active, DeviceUnitsPerInch() returned 0. That meant I had to account for division by zero, which took four more lines of code:
If that original line of code ($points = $deviceUnits * ( POINTS_PER_INCH / DeviceUnitsPerInch() );) had still been in a dozen places, the test would have been repeated a dozen times, for a total of 48 new lines of code. A simple routine reduced the 48 new lines to 4.
Here’s a summary list of the valid reason for creating a routine:
- Reduce complexity
- Introduce an intermediate, understandable abstraction
- Avoid duplicate code
- Hide sequences
- Improve portability
- Simplify complicated boolean tests
- Improve performance
In addition, many of the reason to create a class are also good reasons to create a routine:
- Isolate complexity
- Hide implementation details
- Limit effects of changes
- Hide global data
- Make central points of control
- Facilitate reusable code
Reference: Code Complete 2