Java Reference
In-Depth Information
Modules
A module is a self-contained piece of code that provides functions and methods that can
then be used in other files. This helps to keep code organized in separate, reusable files,
which improves code maintainability. The code in a module should have a single purpose
and group together functions with distinct functionality. For example, you might keep any
functions used for Ajax in their own module. This could then be used in any projects where
Ajax was required. Keeping code modular helps to make it more loosely coupled and in-
terchangeable, meaning you can easily swap one module for another without affecting oth-
er parts of a project. Indeed, small single-purpose modules are the exact opposite of large
monolithic frameworks as they enable developers to use only the modules that are needed,
avoiding any wasted code.
Note: Tightly And Loosely Coupled
The coupling of code refers to how dependent cetain elements or modules
of code are on each other. Two pieces of code are said to be tightly coupled
if one relies on the other to run. This often occurs if a piece of code makes
hard coded references to another piece of code, requiring it to be used. This
will often mean that changes to one piece of code will necessitate changes in
the other. On the other hand, two pieces of code are said to loosely coupled if
one piece of code can be easily substituted by another without affecting the
final outcome. This is often achieved by referring to common methods (often
provided in an API) that are used by many different pieces of code. It is con-
sidered good design to keep code as loosely coupled as possible as this allows
for the most flexibilty in developing sytems of code as different modules can
be used independently and in a variety of different applications, rather than
being restricted to a single use-case.
When you start to use more and more modules, you'll find that some modules depend on
other modules to work. These are known as dependencies . Dependency management is the
process of ensuring that all the dependencies a module requires are met. This can be difficult
to do manually once a project becomes large.
JavaScript lacks a built-in way of creating modules, [8] but it is possible to create modules
using the tools that JavaScript provides. A popular way of doing this is using Christian Heil-
mann's Revealing Module pattern . This uses an IIFE to return the module as an object that's
 
Search WWH ::




Custom Search