Let’s say I am developing an ecommerce application. And I have modules organized in certain way.
- Products
- Coupon
- Tag (can be used as brands and linked data)
- Offer
Now what I want to achieve is user can enable certain of these modules and get the features related to these modules. But how can I truly achieve this without putting all these codes pre-conditions in controller? eg:
// Check if modules are active and preassign variables related to this
if($productModuleIsActive){
$data['product'] = Product::all();
}
if($tagModuleIsActive){
$data['tag'] = Tag::all();
}
if($offerModuleIsActive){
$data['offer'] = Offer::all();
}
return view('certainViewName', [ $datas ]);
Now this brings unwanted overhead to code as you can imagine the views will also be constructed similarly. How can I truly say that my code is modular when in fact it is not. Am I approaching the problem with wrong angle?
Now this becomes complicated when a user wants to checkout from the application.
When checking cart out the simplest codes will be to check for all modules that affect the product using similar approach as above. For each enabled modules(offer, coupon) which if enabled will change the price of the products which in turn affects the cart. Also how would I go about calculating price at the end.
The another problem with modular approach I have found is that it introduces complexity in application. Let’s say the analytics module is active and I want to see all the exceptions that occurs in our code then I would go to each module and put it’s related code there. This means that the analytics will now be related to all modules. Which puts another overhead to the code and will be difficult to manage.
From this way all I see is that each module introduces unwanted complexity in an application. Now is there any way to achieve this in some way? Does the modular pattern really hold the water? When the modules are related with each other. How can this be achieved? The theoretical part seems intriguing and captivating enough to implement this but when in implementation It seems that this introduces unwanted, redundant codes in an application.
From my view the modular approach only hold the water when modules are not related with each other completely. How should the application be structured enough so that the developer working on a module should no longer worry about offer, category or any other modules code?
1
The corollary of Modular Code is that it is well Interfaced Code.
That is that the modules have strong boundaries where their interaction is governed by a communication protocol.
This communication protocol is custom to that particular boundary. It prescribes how the two collaborators are to work together. Who calls what, when, how, with, where, and why. It is usually a set of interfaces that are implemented by one side, the other, or both. Those interfaces have certain requirements on the given function behaviour and property states that the implementor has to meet for everything to just work.
You are correct in that this can introduce unnecessary complexity. Poorly considered boundaries usually do make for poor neighbours. You cannot escape the need for boundaries, even spaghetti code has them every time a jump occurs.
When you find yourself contorting code to use an interface correctly, that is a design smell that the boundary might not be as strong/in the right place as it could be.