In software development programming languages Modules are an integral unit of
An enterprise-level software application may contain several different modules, and each module serves unique and separate business operations.Modules make a programmer’s job easy by allowing the programmer to focus on only one area of the functionality of the software application. Modules are typically incorporated into the program through interfaces.
In a previous post I discussed ,
Fibonacci.js module to return a Fibonacci sequence of numbers.
Fibonacci.js is an example of the
Traditional Object Oriented programming languages like Java, C#, C++ , Python etc, make use of the
Class object to create self contained and decoupled units of code, which can be treated as block boxes of functionality which can be enhanced, added and removed according to requirements.
A Module can be considered as similar to a
Singleton Class in an object oriented language.
In a Module, all the variables defined are visible only in the module. Methods in a module have scope and access to the shared private data and private methods. Thus implementing a
closure, as discussed later
- Immediate functions
- Private and public methods
Using the module pattern you can with very little code, effectively namespace related methods and properties, organise and create privacy for entire modules of code and minimise global scope pollution.
closure principle providing the control of privacy in your methods so that third party applications or modules cannot access data in variables or overwrite it.
What is a closure ?
global scope except variables that are declared within a function using the
Variables declared in a function have access to the global scope and every other scope above the function that you are in. In other words an inner function has access to the variables that are within the function that wraps it.
A closure is the combination of a function and the lexical environment within which that function was declared.
What we have done here is declare a function, which essentially calls itself immediately. This creates a new scope in which we can wrap all our function logic in, and we return only that parts we need.
After we have created a new
scope , we can create a namespace for our anonymous Module. In my example I will be creating a Module to return a Fibonacci sequence of numbers, so I will create a Fibonacci namespace.
- Reduces clutter with the global namespace
- suports private data
- Cleaner code for developers
- Enables unit testability of code
- private methods are not accessible outside the module
- Private methods are not extensible
Typically Modules will use
return and return an
object to the Module, which will enable methods to bound to the object and accessible from the modules namespace. Essentially we return an
Object Literal and we can call them exactly the same way.
Locally scoped object literal
When developing a module, you may want some means to identify when you’re calling external public methods and internal private methods. This can be really handy when you’re developing large Modules i.e. Modules that that exceed 100 lines of code.
You may want to identify private method without have to check the return statement. In this instance we can create a locally scoped namespace within our Module.
Stacked locally scope Object Literal
Virtually the same as the previous example, but it uses more of traditional single object literal notation:
Using a technique called theRevealing Module Pattern which is an enhanced design pattern to ensure all methods and variables are kept private until they are explicitly exposed
Revealing Module Pattern
The Revealing Module Pattern is an enhanced design pattern to ensure all methods and variables are kept private until they are explicitly exposed.
The methods are usually exposed through an object literal returned by the closure from which it’s defined.
Advantages of the Revealing Module Pattern
- Cleaner Approach
- Support for private data
- Reduces global noise
- Localization of functions and variables through closures
- concise syntax
- Explicitly defined public methods and variables
A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
Latest posts by Gary Woodfine (see all)
- Making API calls with HttpClientFactory in Console applications - April 16, 2019
- Product Review: System76 Darter Pro - April 12, 2019
- Disrupting the disruptors - April 10, 2019