Modern programs are based on <modular code, which breaks code up into small self-contained functions. This article discusses modularization in more detail to explain why this is important and useful.
What is Modular Coding?
The concept of modularization can be applied to a single program, or a large application with thousands of lines of code. The purpose and utility of this approach are the same.
There are 3 core concepts behind modular coding:
- Programs are constructed using self-contained "functions", also called "methods"
- A self-contained function does not depend on external data; data is sent to it and received from it, but the internal working of the function is independent of external data and global variables
- Optimally, functions are built to accomplish a single task
Self-contained functions are beneficial because they are easier to maintain. For example, if there is an error inside a function, only the code in the function needs to be examined and fixed.
Such functions can be considered a "black box" to the rest of the program. As far as the program is concerned it does not matter what happens inside the function - only that the result is correct. How the function accomplishes the result is contained only within the function, and not known to the calling program.
For example, assume there is a function to return a list of restaurants based on GIS coordinates. The function accepts latitude and longitude coordinates and returns a list of names and addresses. A phone app could use this function to give a user a list of restaurants based on their current location. But how did the function obtain the list - a file, a database, a call to Yelp? To the phone app, it doesn't matter. All it knows is that it calls the function and gets a result.
(In very real cases, such a function might have multiple ways to obtain its results. For example, if the phone is not currently connected to the Internet, perhaps it has a local file or database it can use for information. And if it is connected, perhaps it also pulls down real-time data. )
There are also cases where the process inside a function may change. Because the coding is modular, this does not affect the calling program.
Python is Modular
You already use self-contained functions for everything in Python. The print() statement, for example, is a function. Do you know what it does internally? Most of us don't know and don't care. We just want it to work.
print() is one of the many functions automatically included in the basic Python library. You can also import
other libraries to get access to special functions that are not part of the basic Python implementation. Examples are NumPy (for complex math),
Pendulum (date-time handling), Requests (HTTP requests), and TKinter (GUI development). More information on these and other libraries can be found
in these articles:
Top 10 Python Packages Every Developer Should Learn
11 Python Libraries and Modules Every Developer Should Know
The diagram below illustrates how a modular program is constructed. It is often compared to use building blocks to create a structure. The main() function is where a Python program starts. It calls other functions to do work and processing. Those functions (methods) may call other functions, and so on. In some cases, programs import external libraries and call their methods, when needed.
Modular code makes maintaining programs easier because each building block is self-contained, and changes to the code only affect that block. Just as important, blocks can be swapped out or modified, as needed, without affecting the rest of the program.
Creating Your Own Python Libraries
A developer can easily create their own library of commonly used functions by placing the code in one or more separate files. These files can then be imported into any program where their methods are useful. For example, many programs need a generic input process to ask the user for a decimal, verify that the entry is valid, and then convert the string input to a decimal? A standard method could be written and placed in an external file (library) and then used whenever such an entry is needed.
Modularization allows developers to build programs by using "building blocks" of self-contained code in functions. Modular programs are easier to test and easier to maintain, making the overall development process more efficient.