Photo by Luca Bravo on Unsplash

What is Module-Based Development and how it improved my code reviews

Breaking down code repetition and improving code quality through modularization.

Burhanuddin Rangwala
4 min readApr 2, 2023

--

Introduction

Recently while while working with sequelize and postgresSQL I faced the problem of code repetition in my microservices. If you have used sequelize before you know that one needs to create the model schemas to query them. This can be quite cumbersome when you have multiple services using the same database schema.

This is where module based development comes into play.

As developers, we all strive to write clean, maintainable, and reusable code. However, when working on complex projects with multiple microservices, it can be challenging to achieve this goal. In such cases, module-based development can be a useful approach to improve code quality and maintainability.

In this blog, we’ll explore module-based development and discuss how it can help improve the quality of your code.

Code Repetition in Microservices

When working on microservices-based projects, it’s common to encounter code repetition. This can occur when you have to reuse the same code in multiple microservices. Lets take my example and try to solve the above problem, if you’re using Sequelize models in multiple microservices, you might end up duplicating the same code in each microservice. This can lead to several issues, such as:

  • Increased code complexity: As you add more microservices, the complexity of your codebase grows, making it harder to maintain and understand.
  • Higher chance of introducing bugs: Duplicated code is prone to errors and inconsistencies, which can cause bugs to creep into your system.
  • Increased development time: Duplication of code across multiple microservices can result in longer development cycles, as you’ll have to make the same changes in multiple places.

So what do we do to solve this?

By Splitting your app into modules

Let’s consider a scenario where you have multiple microservices that use the same piece of code. If you duplicate the same code in each microservice, it can lead to code repetition and increased complexity.

To avoid this issue, we can isolate the code and its functionality into a separate repository or package. We can then use this package in each microservice that requires this functionality.

By creating a package, we can ensure that the code is consistent across all microservices. Any changes made to the code will only need to be made in one place, rather than in multiple microservices. This approach helped me to split my application into 3 main parts.

  1. The main CRUD
  2. The sequelize models
  3. The serverless function

Creating an NPM Package

One way to avoid code repetition in microservices is to create an NPM package containing the common code. This package can then be shared across multiple microservices, eliminating the need for duplicating code.

In my case, I created an NPM package containing the code for the Sequelize models. This package was then used in all the microservices that required this functionality. This approach has several benefits:

  • Improved code quality: By removing duplicate code, you can improve the overall quality of your codebase, making it easier to maintain and understand.
  • Reduced development time: With the common code packaged in an NPM package, you only need to make changes in one place, reducing development time and improving productivity.
  • Easier testing: By having a single source of truth for the common code, you can more easily test and debug your system.

Best Practices for Module-Based Development

Here are some best practices for module-based development that I came across:

  • Keep modules small and focused on a single responsibility.
  • Ensure that each module has well-defined inputs and outputs.
  • Use dependency injection to manage module dependencies and improve testability.
  • Avoid tight coupling between modules to ensure that changes to one module don’t impact other modules.

By following these best practices, you can create modular, maintainable, and testable code that’s easy to understand.

Drawbacks of using modules

While module-based development can bring many benefits to your codebase, it’s also important to address possible concerns or drawbacks that developers may have. Here are a few examples:

  1. Impact on Performance: One concern that developers may have when using modules is that it can impact the performance of the system. This can be especially true if the modules are not optimized for performance. However, this can be addressed by following best practices for module-based development, such as keeping modules small and focused, ensuring well-defined inputs and outputs, and avoiding tight coupling between modules.
  2. Increased Complexity: Another concern that developers may have is that using modules can increase the complexity of the system. This can be true if there are too many modules or if the modules are not organized in a clear and understandable way. However, this can be addressed by following best practices for module-based development, such as keeping modules small and focused, and ensuring that each module has a clear responsibility.
  3. Workflow Changes: Using modules may require changes to the team’s workflow, which can be challenging for some developers. This can include changes to how code is shared and integrated, and how dependencies are managed. However, these changes can be addressed by establishing clear guidelines and best practices for using modules, and by providing training and support for developers who may be new to this approach.

Conclusion

In this blog, we’ve explored the benefits of module-based development. We’ve seen how it can help avoid code repetition, improve code quality, and reduce development time.

By creating an NPM package that contains common code, we can ensure consistency across multiple microservices and improve the overall quality of our codebase. Additionally, following best practices for module-based development can lead to more modular, maintainable, and testable code.

So next time you’re working on a microservices-based project, consider module-based development as an approach to improve code quality and reduce code repetition. It might just be the key to writing better code.

Thank you for reading this blog post. If you have any questions or feedback, feel free to leave a comment below.

--

--

Burhanuddin Rangwala
Burhanuddin Rangwala

Written by Burhanuddin Rangwala

Software dev helping startups scale to new heights. Follow me if you want to know more about software development, startups, and me.

No responses yet